Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compressed audio (continuation of #36) #37

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions audioread/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,18 @@ def audio_open(path):

# All backends failed!
raise NoBackendError()


def decode(audio):
"""Given a file-like object containing encoded audio data, create an
audio file object that produces its *raw* data.
"""
# FFmpeg.
from . import ffdec
try:
return ffdec.FFmpegAudioFile(audio=audio)
except DecodeError:
pass

# All backends failed!
raise NoBackendError()
52 changes: 43 additions & 9 deletions audioread/ffdec.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class NotInstalledError(FFmpegError):
class ReadTimeoutError(FFmpegError):
"""Reading from the ffmpeg command-line tool timed out."""

class NoInputError(FFmpegError):
"""Reading from the ffmpeg command-line tool timed out."""

class QueueReaderThread(threading.Thread):
"""A thread that consumes data from a filehandle and sends the data
Expand All @@ -73,6 +75,18 @@ def run(self):
# Stream closed (EOF).
break

class WriterThread(threading.Thread):
"""A thread that writes data to a filehandle
"""
def __init__(self, fh, audio=None):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter names here could be a little more descriptive. Both parameters are actually file-like objects—so maybe we should just call them outfile and infile or something?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps writefile and readfile?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call; that's better!

super(WriterThread, self).__init__()
self.fh = fh
self.audio = audio
self.daemon = True

def run(self):
self.fh.write(self.audio.read())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently reads the entire input stream into memory and then writes into FFmpeg. We should do this in a more streaming way by reading a block from self.audio and writing it to self.fh, one block at a time, in a loop.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add block size as an argument similar to QueueReaderThread and read a block at a time. Perhaps in a while True: loop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. The loop should probably exit when the file has no more data to read (i.e., when read returns an empty string).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added these changes to the most recent commit.

self.fh.close()

def popen_multiple(commands, command_args, *args, **kwargs):
"""Like `subprocess.Popen`, but can try multiple commands in case
Expand Down Expand Up @@ -100,7 +114,10 @@ def popen_multiple(commands, command_args, *args, **kwargs):

class FFmpegAudioFile(object):
"""An audio file decoded by the ffmpeg command-line utility."""
def __init__(self, filename, block_size=4096):
def __init__(self, filename=None, audio=None, block_size=4096):
self.openFile = True if filename is not None else False
self.readAudio = True if audio is not None else False

# On Windows, we need to disable the subprocess's crash dialog
# in case it dies. Passing SEM_NOGPFAULTERRORBOX to SetErrorMode
# disables this behavior.
Expand All @@ -118,14 +135,26 @@ def __init__(self, filename, block_size=4096):
)

try:
self.devnull = open(os.devnull)
self.proc = popen_multiple(
COMMANDS,
['-i', filename, '-f', 's16le', '-'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=self.devnull,
)
if self.openFile:
self.devnull = open(os.devnull)
self.proc = popen_multiple(
COMMANDS,
['-i', filename, '-f', 's16le', '-'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=self.devnull,
)
elif self.readAudio:
self.devnull = open(os.devnull)
self.proc = popen_multiple(
COMMANDS,
['-i', '-', '-f', 's16le', '-'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
)
else:
raise NoInputError()

except OSError:
raise NotInstalledError()
Expand All @@ -141,6 +170,11 @@ def __init__(self, filename, block_size=4096):
finally:
windows_error_mode_lock.release()

# Start a thread to write the compressed audio to Popen.stdin
if self.readAudio:
self.stdin_writer = WriterThread(self.proc.stdin,audio)
self.stdin_writer.start()

# Start another thread to consume the standard output of the
# process, which contains raw audio data.
self.stdout_reader = QueueReaderThread(self.proc.stdout, block_size)
Expand Down
29 changes: 21 additions & 8 deletions decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,31 @@
import contextlib


def decode(filename):
filename = os.path.abspath(os.path.expanduser(filename))
if not os.path.exists(filename):
print("File not found.", file=sys.stderr)
sys.exit(1)
def decode(filename=None):
"""Decode audio from a file on disk or, if no file is specified,
from the standard input.
"""
if filename:
filename = os.path.abspath(os.path.expanduser(filename))
if not os.path.exists(filename):
print("File not found.", file=sys.stderr)
sys.exit(1)

try:
with audioread.audio_open(filename) as f:
if filename:
f = audioread.audio_open(filename)
else:
f = audioread.decode(sys.stdin)

with f:
print('Input file: %i channels at %i Hz; %.1f seconds.' %
(f.channels, f.samplerate, f.duration),
file=sys.stderr)
print('Backend:', str(type(f).__module__).split('.')[1],
file=sys.stderr)

with contextlib.closing(wave.open(filename + '.wav', 'w')) as of:
outname = filename or 'out'
with contextlib.closing(wave.open(outname + '.wav', 'w')) as of:
of.setnchannels(f.channels)
of.setframerate(f.samplerate)
of.setsampwidth(2)
Expand All @@ -49,4 +59,7 @@ def decode(filename):


if __name__ == '__main__':
decode(sys.argv[1])
if sys.argv[1:]:
decode(sys.argv[1])
else:
decode()