-
Notifications
You must be signed in to change notification settings - Fork 108
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.pyc | ||
|
||
.tox/ |
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) |
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` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a little confusing that there are files called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using |
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.wave', 'w') | ||
arcresu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data = getData() | ||
fout.setnchannels(1) | ||
fout.setframerate(44100) | ||
fout.setsampwidth(2) | ||
fout.writeframes(data.tobytes()) | ||
fout.close() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import os | ||
import unittest | ||
import audioread | ||
from audioread import rawread, macca | ||
|
||
testMaccaFilename = os.path.abspath(os.path.join('test', 'fixtures', 'test.wav')) | ||
|
||
rowLookup = [ | ||
b'\x00\x00', | ||
b'\x9a\x19', | ||
b'\x00@', | ||
b'3s', | ||
] | ||
numSamples = 512 | ||
|
||
class TestAudioread(unittest.TestCase): | ||
|
||
def test_audio_open_as_generator(self): | ||
result = [] | ||
with audioread.audio_open(testMaccaFilename, block_samples=numSamples) as f: | ||
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(testMaccaFilename, 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]) | ||
|
||
|
||
class TestMacca(unittest.TestCase): | ||
|
||
def test_macca_as_generator(self): | ||
result = [] | ||
with macca.ExtAudioFile(testMaccaFilename, block_samples=numSamples) as f: | ||
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_macca_as_forloop(self): | ||
result = [] | ||
with macca.ExtAudioFile(testMaccaFilename, 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]) | ||
|
||
def test_seek(self): | ||
result = [] | ||
with macca.ExtAudioFile(testMaccaFilename, block_samples=numSamples) as input_file: | ||
gen = input_file.read_data() | ||
|
||
# move forward | ||
row = next(gen) | ||
row = next(gen) | ||
row = next(gen) | ||
|
||
# go back | ||
input_file.seek(512) | ||
row = next(gen) | ||
self.assertEqual(bytes(row[0:2]), rowLookup[1]) | ||
row = next(gen) | ||
self.assertEqual(bytes(row[0:2]), rowLookup[2]) | ||
|
||
|
||
testWaveFilename = os.path.abspath(os.path.join('test', 'fixtures', 'wavetest.wave')) | ||
waveRowLookup = [ | ||
b'\x00\x00', | ||
b'f2', | ||
b'\x008', | ||
b'3;', | ||
] | ||
|
||
class TestRawRead(unittest.TestCase): | ||
|
||
def test_open_as_generator(self): | ||
result = [] | ||
with rawread.RawAudioFile(testWaveFilename, block_samples=numSamples) as input_file: | ||
gen = input_file.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]), waveRowLookup[i]) | ||
|
||
|
||
def test_open_as_forloop(self): | ||
result = [] | ||
with audioread.rawread.RawAudioFile(testWaveFilename, block_samples=numSamples) as input_file: | ||
for buf in input_file: | ||
result.append(buf) | ||
|
||
for i, row in enumerate(result): | ||
self.assertEqual(bytes(row[0:2]), waveRowLookup[i]) | ||
|
||
def test_seek(self): | ||
result = [] | ||
with rawread.RawAudioFile(testWaveFilename, block_samples=numSamples) as input_file: | ||
gen = input_file.read_data() | ||
|
||
# move forward | ||
row = next(gen) | ||
row = next(gen) | ||
row = next(gen) | ||
|
||
# go back | ||
input_file.seek(512) | ||
row = next(gen) | ||
self.assertEqual(bytes(row[0:2]), waveRowLookup[1]) | ||
row = next(gen) | ||
self.assertEqual(bytes(row[0:2]), waveRowLookup[2]) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[tox] | ||
envlist = py27,py36 | ||
|
||
[testenv] | ||
commands = | ||
python test.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nitpicking: it's nice to write docstrings (a) in the imperative voice, and (b) as complete sentences, including a period. So I suggest: