def test_read_frames(self): samples = numpy.arange(200).reshape((100, 2)) samples = (samples - 100) / 200.0 sndfile = MockSndfile(samples) frames = sndfile.read_frames(10) assert_array_equal(samples[:10, :], frames) frames = sndfile.read_frames(20) assert_array_equal(samples[10:30, :], frames) frames = sndfile.read_frames(70) assert_array_equal(samples[30:, :], frames)
def test_seek(self): # Based on TestSeek class from audiolab's test_sndfile.py. samples = numpy.arange(10000) sndfile = MockSndfile(samples) nframes = sndfile.nframes bufsize = 1024 buf = sndfile.read_frames(bufsize) sndfile.seek(0) buf2 = sndfile.read_frames(bufsize) assert_array_equal(buf, buf2) # Now, read some frames, go back, and compare buffers # (check whence == 1 == SEEK_CUR) sndfile = MockSndfile(samples) sndfile.read_frames(bufsize) buf = sndfile.read_frames(bufsize) sndfile.seek(-bufsize, 1) buf2 = sndfile.read_frames(bufsize) assert_array_equal(buf, buf2) # Now, read some frames, go back, and compare buffers # (check whence == 2 == SEEK_END) sndfile = MockSndfile(samples) buf = sndfile.read_frames(nframes) sndfile.seek(-bufsize, 2) buf2 = sndfile.read_frames(bufsize) assert_array_equal(buf[-bufsize:], buf2) # Try to seek past the end. self.assertRaises(IOError, sndfile.seek, len(samples) + 1)
def __iter__(self): if isinstance(self.filename, str): f = audiolab.Sndfile(self.filename) elif np.iterable(self.filename): f = MockSndfile(self.filename, samplerate=44100) else: raise ValueError, 'Invalid filename: %s' % self.filename nbuf = self.nbuf end = self.end if not end: end = f.nframes if not nbuf: nbuf = 10*f.samplerate pos = f.seek(self.start) nremaining = end - pos while nremaining > 0: if nremaining < nbuf: nbuf = nremaining try: yield f.read_frames(nbuf) nremaining -= nbuf except RuntimeError: nremaining = 0 f.close()
def test_read_frames_type_conversion(self): samples = numpy.arange(200, dtype=numpy.float32) sndfile = MockSndfile(samples) frames = sndfile.read_frames(len(samples), dtype=numpy.int16) self.assert_(isinstance(frames[0], numpy.int16)) assert_array_equal(numpy.int16(samples), frames)