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_write(self, func, format, filext): """ Check *write functions from matpi """ rfd1, fd1, cfilename1 = open_tmp_file('matapi_test.' + filext) rfd2, fd2, cfilename2 = open_tmp_file('matapi_test.' + filext) try: nbuff = 22050 fs = nbuff noise = 0.1 * N.random.randn(nbuff) # Open the first file for writing with Sndfile b = Sndfile(cfilename1, 'w', format, 1, fs) b.write_frames(noise) b.close() # Write same data with wavwrite func(noise, cfilename2, fs) # Compare if both files have both same audio data and same # meta-data f1 = Sndfile(cfilename1) f2 = Sndfile(cfilename2) assert_array_equal(f1.read_frames(f1.nframes), f2.read_frames(f2.nframes)) assert_equal(f1.format, f2.format) assert_equal(f1.samplerate, f2.samplerate) assert_equal(f1.channels, f2.channels) f1.close() f2.close() finally: close_tmp_file(rfd1, cfilename1) close_tmp_file(rfd2, cfilename2)
def _test_read(self, func, format, filext): # Create a tmp audio file, write some random data into it, and check it # is the expected data when read from a function from the matapi. rfd, fd, cfilename = open_tmp_file('pySndfiletest.' + filext) try: nbuff = 22050 noise = 0.1 * N.random.randn(nbuff) # Open the copy file for writing b = Sndfile(cfilename, 'w', format, 1, nbuff) b.write_frames(noise) b.close() # Reread the data b = Sndfile(cfilename, 'r') rcnoise = b.read_frames(nbuff) b.close() rnoise = func(cfilename)[0] assert_array_equal(rnoise, rcnoise) finally: close_tmp_file(rfd, cfilename)
from audiolab import Format, Sndfile # Use 24 bits encoding, big endian format = Format('wav', 'pcm24', 'big') f = Sndfile('foo.wav', 'w', format, 2, 48000)
import numpy as np from audiolab import Sndfile f = Sndfile('test.wav', 'r') # Sndfile instances can be queried for the audio file meta-data fs = f.samplerate nc = f.channels enc = f.encoding # Reading is straightfoward data = f.read_frames(1000) # This reads the next 1000 frames, e.g. from 1000 to 2000, but as single precision data_float = f.read_frames(1000, dtype=np.float32)
import numpy as np from audiolab import Format, Sndfile filename = 'foo.wav' # Create some data to save as audio data: one second of stereo white noise data = np.random.randn(48000, 2) # Create a Sndfile instance for writing wav files @ 48000 Hz format = Format('wav') f = Sndfile(filename, 'w', format, 2, 48000) # 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 f.write_frames(data[:500]) f.close()