def test_simple(self): ofilename = join(TEST_DATA_DIR, 'test.wav') # Open the test file for reading a = sndfile(ofilename, 'read') nframes = a.get_nframes() buffsize = 1024 buffsize = min(nframes, buffsize) # First, read some frames, go back, and compare buffers buff = a.read_frames(buffsize) a.seek(0) buff2 = a.read_frames(buffsize) assert_array_equal(buff, buff2) a.close() # Now, read some frames, go back, and compare buffers # (check whence == 1 == SEEK_CUR) a = sndfile(ofilename, 'read') a.read_frames(buffsize) buff = a.read_frames(buffsize) a.seek(-buffsize, 1) buff2 = a.read_frames(buffsize) assert_array_equal(buff, buff2) a.close() # Now, read some frames, go back, and compare buffers # (check whence == 2 == SEEK_END) a = sndfile(ofilename, 'read') buff = a.read_frames(nframes) a.seek(-buffsize, 2) buff2 = a.read_frames(buffsize) assert_array_equal(buff[-buffsize:], buff2)
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_rw(self): """Test read/write pointers for seek.""" ofilename = join(TEST_DATA_DIR, 'test.wav') rfd, fd, cfilename = open_tmp_file('rwseektest.wav') try: ref = sndfile(ofilename, 'read') test = sndfile(fd, 'rwrite', format = ref._format, channels = ref.get_channels(), samplerate = ref.get_samplerate()) n = 1024 rbuff = ref.read_frames(n, dtype = np.int16) test.write_frames(rbuff) tbuff = test.read_frames(n, dtype = np.int16) assert_array_equal(rbuff, tbuff) # Test seeking both read and write pointers test.seek(0, 0) test.write_frames(rbuff) tbuff = test.read_frames(n, dtype = np.int16) assert_array_equal(rbuff, tbuff) # Test seeking only read pointer rbuff1 = rbuff.copy() rbuff2 = rbuff1 * 2 + 1 rbuff2.clip(-30000, 30000) test.seek(0, 0, 'r') test.write_frames(rbuff2) tbuff1 = test.read_frames(n, dtype = np.int16) try: tbuff2 = test.read_frames(n, dtype = np.int16) except IOError, e: msg = "write pointer was updated in read seek !" msg += "\n(msg is %s)" % e raise AssertionError(msg) assert_array_equal(rbuff1, tbuff1) assert_array_equal(rbuff2, tbuff2) if np.all(rbuff2 == tbuff1): raise AssertionError("write pointer was updated"\ " in read seek !") # Test seeking only write pointer rbuff3 = rbuff1 * 2 - 1 rbuff3.clip(-30000, 30000) test.seek(0, 0, 'rw') test.seek(n, 0, 'w') test.write_frames(rbuff3) tbuff1 = test.read_frames(n, np.int16) try: assert_array_equal(tbuff1, rbuff1) except AssertionError: raise AssertionError("read pointer was updated in write seek !") try: tbuff3 = test.read_frames(n, np.int16) except IOError, e: msg = "read pointer was updated in write seek !" msg += "\n(msg is %s)" % e raise AssertionError(msg)
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_nofile(self): """ Check the failure when opening a non existing file.""" try: f = sndfile("floupi.wav", "read") raise AssertionError("call to non existing file should not succeed") except IOError: pass except Exception, e: raise AssertionError("opening non existing file should raise a IOError exception, got %s instead" % e.__class__)
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_nofile(self): """ Check the failure when opening a non existing file.""" try: f = sndfile("floupi.wav", "read") raise AssertionError( "call to non existing file should not succeed") except IOError: pass except Exception, e: raise AssertionError( "opening non existing file should raise a IOError exception, got %s instead" % e.__class__)
def test_bigframes(self): """ Try to seek really far""" rawname = join(TEST_DATA_DIR, 'test.wav') a = sndfile(rawname, 'read') try: try: a.seek(2 ** 60) raise Exception("Seek really succeded ! This should not happen") except PyaudioIOError, e: pass finally: a.close()
def extract_pitch(wavname): verbose('Openning ',wavname) sound=audiolab.sndfile(wavname,'read') audio=sound.read_frames(sound.get_nframes()) ps=[] for frame in FrameGenerator(essentia.array(audio), frameSize = 1024, hopSize = 210): pitch = pitch_o(frame)[0] confidence = pitch_o.get_confidence() if confidence < 0.8: pitch = 0. ps.append(pitch) return ps
def extract_pitch(wavname): verbose('Openning ',wavname) sound=audiolab.sndfile(wavname,'read') audio=sound.read_frames(sound.get_nframes()) ps=[] for frame in FrameGenerator(essentia.array(audio), frameSize = 1024, hopSize = 210): fspectrum=spectrum(w(frame)) p,c=pitch(fspectrum) if c >= 0.1: ps.append(p) else: ps.append(0) return ps
def test_bigframes(self): """ Try to seek really far""" rawname = join(TEST_DATA_DIR, 'test.wav') a = sndfile(rawname, 'read') try: try: a.seek(2**60) raise Exception( "Seek really succeded ! This should not happen") except PyaudioIOError, e: pass finally: a.close()
def extract_pitch(wavname): verbose('Openning ', wavname) sound = audiolab.sndfile(wavname, 'read') audio = sound.read_frames(sound.get_nframes()) ps = [] for frame in FrameGenerator(essentia.array(audio), frameSize=1024, hopSize=210): pitch = pitch_o(frame)[0] confidence = pitch_o.get_confidence() if confidence < 0.8: pitch = 0. ps.append(pitch) return ps
def extract_pitch(wavname): verbose('Openning ', wavname) sound = audiolab.sndfile(wavname, 'read') audio = sound.read_frames(sound.get_nframes()) ps = [] for frame in FrameGenerator(essentia.array(audio), frameSize=1024, hopSize=210): fspectrum = spectrum(w(frame)) p, c = pitch(fspectrum) if c >= 0.1: ps.append(p) else: ps.append(0) return ps
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 extract_feats(xmlname,dirwave): verbose('Openning ',xmlname) with open(xmlname) as xml_: birdinfo=BeautifulSoup(xml_.read()) wavname=os.path.join(dirwave,birdinfo.filename.string) verbose('Openning ',wavname) sound=audiolab.sndfile(wavname,'read') audio=sound.read_frames(sound.get_nframes()) energies=[] for frame in FrameGenerator(essentia.array(audio), frameSize = 1024, hopSize = 210): fspectrum=spectrum(w(frame)) fpeaksF,fpeaksM=peaks(fspectrum) withening(fspectrum,fpeaksF,fpeaksM) band=melbands(fspectrum) energies.append(band) if birdinfo.classid: idd=birdinfo.classid.string else: idd='u' return idd, vstack(energies)
import audiolab as audiolab filename = "test.wav" a = audiolab.sndfile(filename, "read") print a
from tempfile import mkstemp from os import remove import numpy as N from audiolab import formatinfo as format import audiolab as audiolab # Create a temp file in the system temporary dir, and always remove # it at the end cd, filename = mkstemp('tmptest.wav') try: fmt = format('wav', 'pcm24') nchannels = 2 fs = 44100 afile = audiolab.sndfile(filename, 'write', fmt, nchannels, fs) # Create a stereo white noise, with Gaussian distribution tmp = 0.1 * N.random.randn(1000, nchannels) # Write the first 500 frames of the signal # Note that the write_frames method uses tmp's numpy dtype to determine how # to write to the file; sndfile also converts the data on the fly if necessary afile.write_frames(tmp, 500) afile.close() # Let's check that the written file has the expected meta data afile = audiolab.sndfile(filename, 'read') assert(afile.get_samplerate() == fs) assert(afile.get_channels() == nchannels)
import numpy as N import audiolab as audiolab filename = 'test.wav' a = audiolab.sndfile(filename, 'read') tmp = a.read_frames(1e4) float_tmp = a.read_frames(1e4, dtype = N.float32) import pylab as P P.plot(tmp[:])
import audiolab as audiolab filename = 'test.wav' a = audiolab.sndfile(filename, 'read') print a
def test_rw(self): """Test read/write pointers for seek.""" ofilename = join(TEST_DATA_DIR, 'test.wav') rfd, fd, cfilename = open_tmp_file('rwseektest.wav') try: ref = sndfile(ofilename, 'read') test = sndfile(fd, 'rwrite', format=ref._format, channels=ref.get_channels(), samplerate=ref.get_samplerate()) n = 1024 rbuff = ref.read_frames(n, dtype=np.int16) test.write_frames(rbuff) tbuff = test.read_frames(n, dtype=np.int16) assert_array_equal(rbuff, tbuff) # Test seeking both read and write pointers test.seek(0, 0) test.write_frames(rbuff) tbuff = test.read_frames(n, dtype=np.int16) assert_array_equal(rbuff, tbuff) # Test seeking only read pointer rbuff1 = rbuff.copy() rbuff2 = rbuff1 * 2 + 1 rbuff2.clip(-30000, 30000) test.seek(0, 0, 'r') test.write_frames(rbuff2) tbuff1 = test.read_frames(n, dtype=np.int16) try: tbuff2 = test.read_frames(n, dtype=np.int16) except IOError, e: msg = "write pointer was updated in read seek !" msg += "\n(msg is %s)" % e raise AssertionError(msg) assert_array_equal(rbuff1, tbuff1) assert_array_equal(rbuff2, tbuff2) if np.all(rbuff2 == tbuff1): raise AssertionError("write pointer was updated"\ " in read seek !") # Test seeking only write pointer rbuff3 = rbuff1 * 2 - 1 rbuff3.clip(-30000, 30000) test.seek(0, 0, 'rw') test.seek(n, 0, 'w') test.write_frames(rbuff3) tbuff1 = test.read_frames(n, np.int16) try: assert_array_equal(tbuff1, rbuff1) except AssertionError: raise AssertionError( "read pointer was updated in write seek !") try: tbuff3 = test.read_frames(n, np.int16) except IOError, e: msg = "read pointer was updated in write seek !" msg += "\n(msg is %s)" % e raise AssertionError(msg)
def test_basic_io_fd(self): """ Check open from fd works""" ofilename = join(TEST_DATA_DIR, 'test.wav') fd = os.open(ofilename, os.O_RDONLY) hdl = sndfile(fd, 'read') hdl.close()
from tempfile import mkstemp from os import remove import numpy as N from audiolab import formatinfo as format import audiolab as audiolab # Create a temp file in the system temporary dir, and always remove # it at the end cd, filename = mkstemp('tmptest.wav') try: fmt = format('wav', 'pcm24') nchannels = 2 fs = 44100 afile = audiolab.sndfile(filename, 'write', fmt, nchannels, fs) # Create a stereo white noise, with Gaussian distribution tmp = 0.1 * N.random.randn(1000, nchannels) # Write the first 500 frames of the signal # Note that the write_frames method uses tmp's numpy dtype to determine how # to write to the file; sndfile also converts the data on the fly if necessary afile.write_frames(tmp, 500) afile.close() # Let's check that the written file has the expected meta data afile = audiolab.sndfile(filename, 'read') assert (afile.get_samplerate() == fs) assert (afile.get_channels() == nchannels)
import audiolab as audiolab a = audiolab.sndfile('test.wav', 'read') data = a.read_frames(1000) a.close()
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()