Permalink
Browse files

Whole-audio interface fixed. Fixes #8

  • Loading branch information...
1 parent 07338b4 commit 82621ddb617f9965605c5a22fec063a90521e811 @vokimon committed Jun 29, 2014
Showing with 38 additions and 5 deletions.
  1. +34 −0 test/wavefileTest.py
  2. +4 −5 wavefile/wavefile.py
View
@@ -446,6 +446,40 @@ def test_seek_toResetFileReading(self) :
firstSamples.append(firstSample)
self.assertEqual(list(range(0,100,10))*2, firstSamples)
+ def test_load(self) :
+ data = self.fourSinusoids(samples=400)
+ self.writeWav("file.wav", data)
+ readsamplerate, readdata = wavefile.load("file.wav")
+ np_assert_almost_equal(readdata, data, decimal=7)
+ self.assertEqual(readsamplerate, 44100)
+
+ def test_save(self) :
+ samplerate = 44100
+ data = self.fourSinusoids(samples=400)
+ wavefile.save("file.wav", data, samplerate=samplerate)
+ readsamplerate, readdata = wavefile.load("file.wav")
+ np_assert_almost_equal(readdata, data, decimal=7)
+ self.assertEqual(readsamplerate, samplerate)
+
+ def test_save_slice(self) :
+ samplerate = 44100
+ data = self.fourSinusoids(samples=400)
+ data = np.ascontiguousarray(data)
+ wavefile.save("file.wav", data[::2], samplerate=samplerate)
+ readsamplerate, readdata = wavefile.load("file.wav")
+ np_assert_almost_equal(readdata, data[::2], decimal=7)
+ self.assertEqual(readsamplerate, samplerate)
+
+ def test_save_asCOrder(self) :
+ samplerate = 44100
+ data = self.fourSinusoids(samples=400)
+ data = np.ascontiguousarray(data)
+ wavefile.save("file.wav", data, samplerate=samplerate)
+ readsamplerate, readdata = wavefile.load("file.wav")
+ np_assert_almost_equal(readdata, data, decimal=7)
+ self.assertEqual(readsamplerate, samplerate)
+
+
if __name__ == '__main__' :
import sys
sys.exit(unittest.main())
View
@@ -295,25 +295,24 @@ def seek(self, frames, whence=Seek.SET) :
def loadWave(filename) :
with WaveReader(filename) as r :
blockSize = 512
- data = np.empty((r.frames, r.channels), dtype=np.float32)
+ data = r.buffer(r.frames)
fullblocks = r.frames // blockSize
lastBlockSize = r.frames % blockSize
for i in range(fullblocks) :
- readframes = r.read(data[i*blockSize:(i+1)*blockSize,:])
+ readframes = r.read(data[:,i*blockSize:(i+1)*blockSize])
assert readframes == blockSize
if lastBlockSize :
- readframes = r.read(data[fullblocks*blockSize:,:])
+ readframes = r.read(data[:,fullblocks*blockSize:])
assert readframes == lastBlockSize
return r.samplerate, data
def saveWave(filename, data, samplerate, verbose=False) :
if verbose: print("Saving wave file:",filename)
blockSize = 512
- frames, channels = data.shape
+ channels, frames = data.shape
fullblocks = frames // blockSize
lastBlockSize = frames % blockSize
with WaveWriter(filename, channels=channels, samplerate=samplerate) as w :
- assert data.dtype == np.float32
for i in range(fullblocks) :
w.write(data[blockSize*i:blockSize*(i+1)])
if lastBlockSize :

0 comments on commit 82621dd

Please sign in to comment.