def test_basic_io(self): """ Check open, close and basic read/write""" # dirty ! ofilename = join(TEST_DATA_DIR, 'test.wav') rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: nbuff = 22050 # Open the test file for reading a = sndfile(ofilename, 'read') nframes = a.get_nframes() # Open the copy file for writing format = audio_format('wav', 'pcm16') b = sndfile(fd, 'write', format, a.get_channels(), a.get_samplerate()) # Copy the data for i in range(nframes / nbuff): tmpa = a.read_frames(nbuff) assert tmpa.dtype == np.float b.write_frames(tmpa, nbuff) nrem = nframes % nbuff tmpa = a.read_frames(nrem) assert tmpa.dtype == np.float b.write_frames(tmpa, nrem) a.close() b.close() finally: close_tmp_file(rfd, cfilename)
def test_int_io(self): # TODO: check if neg or pos value is the highest in abs rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: nb = 2 ** 25 nbuff = 22050 fs = 22050 a = np.random.random_integers(-nb, nb, nbuff) a = a.astype(np.int32) # Open the file for writing format = audio_format('wav', 'pcm32') b = sndfile(fd, 'write', format, 1, fs) b.write_frames(a, nbuff) b.close() b = sndfile(cfilename, 'read') read_a = b.read_frames(nbuff, dtype = np.int32) b.close() assert_array_equal(a, read_a) finally: close_tmp_file(rfd, cfilename)
def test_bad_wavread(self): """ Check wavread on bad file""" # Create a tmp audio file with non wav format, write some random data into it, # and check it can not be opened by wavread rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: nbuff = 22050 noise = 0.1 * N.random.randn(nbuff) # Open the copy file for writing format = audio_format('aiff', 'pcm16') b = Sndfile(cfilename, 'w', format, 1, nbuff) b.write_frames(noise) b.close() b = Sndfile(cfilename, 'r') rcnoise = b.read_frames(nbuff) b.close() try: rnoise = wavread(cfilename)[0] raise Exception( "wavread on non wav file succeded, expected to fail") except ValueError, e: pass #print str(e) + ", as expected" finally: close_tmp_file(rfd, cfilename)
def test_int_io(self): # TODO: check if neg or pos value is the highest in abs rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: nb = 2**25 nbuff = 22050 fs = 22050 a = np.random.random_integers(-nb, nb, nbuff) a = a.astype(np.int32) # Open the file for writing format = audio_format('wav', 'pcm32') b = sndfile(fd, 'write', format, 1, fs) b.write_frames(a, nbuff) b.close() b = sndfile(cfilename, 'read') read_a = b.read_frames(nbuff, dtype=np.int32) b.close() assert_array_equal(a, read_a) finally: close_tmp_file(rfd, cfilename)
def test_bad_wavread(self): """ Check wavread on bad file""" # Create a tmp audio file with non wav format, write some random data into it, # and check it can not be opened by wavread rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: nbuff = 22050 noise = 0.1 * N.random.randn(nbuff) # Open the copy file for writing format = audio_format('aiff', 'pcm16') b = Sndfile(cfilename, 'w', format, 1, nbuff) b.write_frames(noise) b.close() b = Sndfile(cfilename, 'r') rcnoise = b.read_frames(nbuff) b.close() try: rnoise = wavread(cfilename)[0] raise Exception("wavread on non wav file succeded, expected to fail") except ValueError, e: pass #print str(e) + ", as expected" finally: close_tmp_file(rfd, cfilename)
def test_float_frames(self): """ Check nframes can be a float""" rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: # Open the file for writing format = audio_format('wav', 'pcm16') a = sndfile(fd, 'rwrite', format, channels=1, samplerate=22050) tmp = np.random.random_integers(-100, 100, 1000) tmp = tmp.astype(np.short) a.write_frames(tmp, tmp.size) a.seek(0) a.sync() ctmp = a.read_frames(1e2, dtype=np.short) a.close() finally: close_tmp_file(rfd, cfilename)
def test_mismatch(self): # This test open a file for writing, but with bad args (channels and # nframes inverted) rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: # Open the file for writing format = audio_format('wav', 'pcm16') try: b = sndfile(fd, 'write', \ format, channels = 22000, samplerate = 1) raise Exception("Try to open a file with more than 256 "\ "channels, this should not succeed !") except ValueError, e: #print "Gave %d channels, error detected is \"%s\"" % (22000, e) pass finally: close_tmp_file(rfd, cfilename)
def test_float_frames(self): """ Check nframes can be a float""" rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: # Open the file for writing format = audio_format('wav', 'pcm16') a = sndfile(fd, 'rwrite', format, channels = 1, samplerate = 22050) tmp = np.random.random_integers(-100, 100, 1000) tmp = tmp.astype(np.short) a.write_frames(tmp, tmp.size) a.seek(0) a.sync() ctmp = a.read_frames(1e2, dtype = np.short) a.close() finally: close_tmp_file(rfd, cfilename)
def test_float64(self): """Check float64 write/read works""" # dirty ! ofilename = join(TEST_DATA_DIR, 'test.wav') rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: nbuff = 22050 # Open the test file for reading a = sndfile(ofilename, 'read') nframes = a.get_nframes() # Open the copy file for writing format = audio_format('wav', 'float64') b = sndfile(fd, 'write', format, a.get_channels(), a.get_samplerate()) # Copy the data in the wav file for i in range(nframes / nbuff): tmpa = a.read_frames(nbuff, dtype=np.float64) assert tmpa.dtype == np.float64 b.write_frames(tmpa, nbuff) nrem = nframes % nbuff tmpa = a.read_frames(nrem) b.write_frames(tmpa, nrem) a.close() b.close() # Now, reopen both files in for reading, and check data are # the same a = sndfile(ofilename, 'read') b = sndfile(cfilename, 'read') for i in range(nframes / nbuff): tmpa = a.read_frames(nbuff, dtype=np.float64) tmpb = b.read_frames(nbuff, dtype=np.float64) assert_array_equal(tmpa, tmpb) a.close() b.close() finally: close_tmp_file(rfd, cfilename)
def test_float64(self): """Check float64 write/read works""" # dirty ! ofilename = join(TEST_DATA_DIR, 'test.wav') rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: nbuff = 22050 # Open the test file for reading a = sndfile(ofilename, 'read') nframes = a.get_nframes() # Open the copy file for writing format = audio_format('wav', 'float64') b = sndfile(fd, 'write', format, a.get_channels(), a.get_samplerate()) # Copy the data in the wav file for i in range(nframes / nbuff): tmpa = a.read_frames(nbuff, dtype = np.float64) assert tmpa.dtype == np.float64 b.write_frames(tmpa, nbuff) nrem = nframes % nbuff tmpa = a.read_frames(nrem) b.write_frames(tmpa, nrem) a.close() b.close() # Now, reopen both files in for reading, and check data are # the same a = sndfile(ofilename, 'read') b = sndfile(cfilename, 'read') for i in range(nframes / nbuff): tmpa = a.read_frames(nbuff, dtype = np.float64) tmpb = b.read_frames(nbuff, dtype = np.float64) assert_array_equal(tmpa, tmpb) a.close() b.close() finally: close_tmp_file(rfd, cfilename)
def test_wavwrite(self): """ Check wavwrite """ self._test_write(wavwrite, audio_format('wav', 'pcm16', 'file'), 'wav')
def test_auread(self): """ Check auread """ self._test_read(auread, audio_format('au', 'ulaw', 'file'), 'au')
def test_wavread(self): """ Check wavread """ self._test_read(wavread, audio_format('wav', 'pcm16', 'file'), 'wav')
def test_oggwrite(self): """ Check oggwrite """ self._test_write(oggwrite, audio_format('ogg', 'vorbis', 'file'), 'ogg')
def test_flacread(self): """ Check flacread """ self._test_read(flacread, audio_format('flac', 'pcm16', 'file'), 'flac')
def test_aiffwrite(self): """ Check aiffwrite """ self._test_write(aiffwrite, audio_format('aiff', 'pcm16', 'file'), 'aiff')
def test_sdifwrite(self): """ Check wavwrite """ self._test_write(sdifwrite, audio_format('ircam', 'pcm16', 'file'), 'sdif')
def test_aiffread(self): """ Check aiffread """ self._test_read(aiffread, audio_format('aiff', 'pcm16', 'file'), 'aiff')
def test_sdifread(self): """ Check sdifread (ircam format) """ self._test_read(sdifread, audio_format('ircam', 'pcm16', 'file'), 'sdif')
def test_oggread(self): """ Check oggread """ self._test_read(oggread, audio_format('ogg', 'vorbis', 'file'), 'ogg')
def test_flacwrite(self): """ Check flacwrite """ self._test_write(flacwrite, audio_format('flac', 'pcm16', 'file'), 'flac')
def test_auwrite(self): """ Check wavwrite """ self._test_write(auwrite, audio_format('au', 'ulaw', 'file'), 'au')
def test_raw(self): rawname = join(TEST_DATA_DIR, 'test.raw') format = audio_format('raw', 'pcm16', 'little') a = sndfile(rawname, 'read', format, 1, 11025) assert a.get_nframes() == 11290 a.close()