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

Add seeking to rawread and macca, and tests #73

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.pyc
.DS_Store

.tox/
12 changes: 6 additions & 6 deletions audioread/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,45 +70,45 @@ def _mad_available():
return True


def audio_open(path):
def audio_open(path, block_samples=4096):
"""Open an audio file using a library that is available on this
system.
"""
# Standard-library WAV and AIFF readers.
from . import rawread
try:
return rawread.RawAudioFile(path)
return rawread.RawAudioFile(path, block_samples=block_samples)
except DecodeError:
pass

# Core Audio.
if _ca_available():
from . import macca
try:
return macca.ExtAudioFile(path)
return macca.ExtAudioFile(path, block_samples=block_samples)
except DecodeError:
pass

# GStreamer.
if _gst_available():
from . import gstdec
try:
return gstdec.GstAudioFile(path)
return gstdec.GstAudioFile(path, block_samples=block_samples)
except DecodeError:
pass

# MAD.
if _mad_available():
from . import maddec
try:
return maddec.MadAudioFile(path)
return maddec.MadAudioFile(path, block_samples=block_samples)
except DecodeError:
pass

# FFmpeg.
from . import ffdec
try:
return ffdec.FFmpegAudioFile(path)
return ffdec.FFmpegAudioFile(path, block_samples=block_samples)
except DecodeError:
pass

Expand Down
6 changes: 3 additions & 3 deletions audioread/ffdec.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ 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, block_samples=4096):
# On Windows, we need to disable the subprocess's crash dialog
# in case it dies. Passing SEM_NOGPFAULTERRORBOX to SetErrorMode
# disables this behavior.
Expand Down Expand Up @@ -143,7 +143,7 @@ def __init__(self, filename, block_size=4096):

# 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)
self.stdout_reader = QueueReaderThread(self.proc.stdout, blocksize=block_samples)
self.stdout_reader.start()

# Read relevant information from stderr.
Expand All @@ -152,7 +152,7 @@ def __init__(self, filename, block_size=4096):
# Start a separate thread to read the rest of the data from
# stderr. This (a) avoids filling up the OS buffer and (b)
# collects the error output for diagnosis.
self.stderr_reader = QueueReaderThread(self.proc.stderr)
self.stderr_reader = QueueReaderThread(self.proc.stderr, blocksize=block_samples)
self.stderr_reader.start()

def read_data(self, timeout=10.0):
Expand Down
12 changes: 10 additions & 2 deletions audioread/macca.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class ExtAudioFile(object):
>>> do_something(block)

"""
def __init__(self, filename):
def __init__(self, filename, block_samples=4096):
url = CFURL(filename)
try:
self._obj = self._open_url(url)
Expand All @@ -204,6 +204,7 @@ def __init__(self, filename):
raise
del url

self.block_samples = block_samples
self.closed = False
self._file_fmt = None
self._client_fmt = None
Expand Down Expand Up @@ -295,9 +296,11 @@ def setup(self, bitdepth=16):
newfmt.mBytesPerFrame = newfmt.mBytesPerPacket
self.set_client_format(newfmt)

def read_data(self, blocksize=4096):
def read_data(self, blocksize=None):
"""Generates byte strings reflecting the audio data in the file.
"""
blocksize = blocksize or self.block_samples * self._client_fmt.mBytesPerFrame

frames = ctypes.c_uint(blocksize // self._client_fmt.mBytesPerFrame)
buf = ctypes.create_string_buffer(blocksize)

Expand All @@ -323,6 +326,11 @@ def read_data(self, blocksize=4096):
blob = data[:size]
yield blob

def seek(self, pos):
"""Seek to a frame position in the file."""
check(_coreaudio.ExtAudioFileSeek(self._obj, pos))


def close(self):
"""Close the audio file and free associated memory."""
if not self.closed:
Expand Down
8 changes: 5 additions & 3 deletions audioread/maddec.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class UnsupportedError(DecodeError):

class MadAudioFile(object):
"""MPEG audio file decoder using the MAD library."""
def __init__(self, filename):
def __init__(self, filename, block_samples=4096):
self.block_samples = block_samples
self.fp = open(filename, 'rb')
self.mf = mad.MadFile(self.fp)
if not self.mf.total_time(): # Indicates a failed open.
Expand All @@ -36,11 +37,12 @@ def close(self):
if hasattr(self, 'mf'):
del self.mf

def read_blocks(self, block_size=4096):
def read_blocks(self, block_size=None):
"""Generates buffers containing PCM data for the audio file.
"""
block_samples = block_size or self.block_samples
while True:
out = self.mf.read(block_size)
out = self.mf.read(block_samples)
if not out:
break
yield out
Expand Down
18 changes: 15 additions & 3 deletions audioread/rawread.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ class RawAudioFile(object):
"""An AIFF, WAV, or Au file that can be read by the Python standard
library modules ``wave``, ``aifc``, and ``sunau``.
"""
def __init__(self, filename):
def __init__(self, filename, block_samples=1024):
self._fh = open(filename, 'rb')
self.block_samples = block_samples

try:
self._file = aifc.open(self._fh)
Expand All @@ -71,7 +72,7 @@ def __init__(self, filename):
return

try:
self._file = wave.open(self._fh)
self._file = wave.open(self._fh, 'r')
except wave.Error:
self._fh.seek(0)
pass
Expand Down Expand Up @@ -107,6 +108,11 @@ def close(self):
self._file.close()
self._fh.close()

def seek(self, pos):
"""Seek to a frame position in the file."""
# All three libraries have the same method for seeking
self._file.setpos(pos)

@property
def channels(self):
"""Number of audio channels."""
Expand All @@ -122,8 +128,14 @@ def duration(self):
"""Length of the audio in seconds (a float)."""
return float(self._file.getnframes()) / self.samplerate

def read_data(self, block_samples=1024):
@property
def nframes(self):
"""Gets the number of frames in the source file."""
return self._file.getnframes()

def read_data(self, block_samples=None):
"""Generates blocks of PCM data found in the file."""
block_samples = block_samples or self.block_samples
old_width = self._file.getsampwidth()

while True:
Expand Down
9 changes: 9 additions & 0 deletions derp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import mad
import os
tf = os.path.abspath(os.path.join('test', 'fixtures', 'wavetest.wav'))

fp = open(tf, 'rb')
mf = mad.MadFile(fp)

print('mf.total_time', mf.total_time())
print(mf.read())
10 changes: 10 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import unittest
import sys

if __name__ == '__main__':
loader = unittest.TestLoader()
tests = loader.discover('test')
testRunner = unittest.runner.TextTestRunner()
result = testRunner.run(tests)
if not result.wasSuccessful():
sys.exit(1)
23 changes: 23 additions & 0 deletions test/fixtures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Audio file fixtures for the tests.

#### test.wav
Test.wav was produced by doing:

```py
import numpy as np
from scipy.io import wavfile

if __name__ == '__main__':
size = 512
a = np.full((size, ), 0.)
b = np.full((size, ), 0.2)
c = np.full((size, ), 0.5)
d = np.full((size, ), 0.9)
t = np.concatenate((a, b, c, d))

wavfile.write('test.wav', 44100, t)
```

#### wavetest.wav

Produced with `make_test_wave.py`
Copy link
Member

Choose a reason for hiding this comment

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

It's a little confusing that there are files called test.wav and wavtest.wav. Maybe it would be useful to describe what each one is for, and how they're different?

Copy link
Author

Choose a reason for hiding this comment

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

I'm using test.wav for the macca backend--- I'll find some better names for it. We also might not need both

22 changes: 22 additions & 0 deletions test/fixtures/make_test_wave.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import numpy as np
import wave
import struct

def getData():
size = 512

a = np.full((size, ), 0., dtype=np.float16)
b = np.full((size, ), 0.2, dtype=np.float16)
c = np.full((size, ), 0.5, dtype=np.float16)
d = np.full((size, ), 0.9, dtype=np.float16)
return np.concatenate((a, b, c, d))


if __name__ == '__main__':
fout = wave.open('test/fixtures/wavetest.wav', 'w')
data = getData()
fout.setnchannels(1)
fout.setframerate(44100)
fout.setsampwidth(2)
fout.writeframes(data.tobytes())
fout.close()
Binary file added test/fixtures/mp3test.mp3
Binary file not shown.
Binary file added test/fixtures/sample.mp3
Binary file not shown.
Binary file added test/fixtures/test.wav
Binary file not shown.
Binary file added test/fixtures/wavetest.wav
Binary file not shown.
93 changes: 93 additions & 0 deletions test/test_audioread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import os
import unittest
import audioread

numSamples = 512

testFilename = os.path.abspath(os.path.join('test', 'fixtures', 'wavetest.wav'))
rowLookup = [
b'\x00\x00',
b'f2',
b'\x008',
b'3;',
]

class TestAudioreadWav(unittest.TestCase):

def test_audio_open_as_generator(self):
result = []
with audioread.audio_open(testFilename, block_samples=numSamples) as f:
print('wav decode class', f.__class__)
gen = f.read_data()
try:
while True:
data = next(gen)
result.append(data)
except StopIteration:
pass

self.assertEqual(len(bytes(result[0])), numSamples*2)
self.assertEqual(len(rowLookup), len(result))
for i, row in enumerate(result):
self.assertEqual(bytes(row[0:2]), rowLookup[i])


def test_audio_open_as_forloop(self):
result = []
with audioread.audio_open(testFilename, block_samples=numSamples) as f:
self.assertEqual(f.nframes, 2048)
for buf in f:
result.append(buf)

self.assertEqual(len(bytes(result[0])), numSamples*2)
self.assertEqual(len(rowLookup), len(result))
for i, row in enumerate(result):
self.assertEqual(bytes(row[0:2]), rowLookup[i])


mp3TestFilename = os.path.abspath(os.path.join('test', 'fixtures', 'sample.mp3'))
mp3RowLookup = [
b'\x00\x00',
b'\x00\x00',
b'N\xff',
b'\xe8/',
b'.5',
b'\x089',
b'\x00\x00',
]

class TestAudioreadMp3(unittest.TestCase):

def test_audio_open_as_generator(self):
result = []
with audioread.audio_open(mp3TestFilename, block_samples=numSamples) as f:
print('Mp3 decode class', f.__class__)
gen = f.read_data()
try:
while True:
data = next(gen)
result.append(data)
except StopIteration:
pass

self.assertEqual(len(bytes(result[0])), numSamples*2)
self.assertEqual(len(mp3RowLookup), len(result))
for i, row in enumerate(result):
self.assertEqual(bytes(row[0:2]), mp3RowLookup[i])


def test_audio_open_as_forloop(self):
result = []
with audioread.audio_open(mp3TestFilename, block_samples=numSamples) as f:
# self.assertEqual(f.nframes, 4)
for buf in f:
result.append(buf)

self.assertEqual(len(bytes(result[0])), numSamples*2)
self.assertEqual(len(mp3RowLookup), len(result))
for i, row in enumerate(result):
self.assertEqual(bytes(row[0:2]), mp3RowLookup[i])


if __name__ == '__main__':
unittest.main()
Loading