from test.test_support import TESTFN, run_unittest from md5 import md5 import os import wave import wave_futz import unittest class TestWave(unittest.TestCase): def setUp(self): self.f = None def tearDown(self): if self.f is not None: self.f.close() try: os.remove(TESTFN) except OSError: pass def test_it(self): nchannels = 2 sampwidth = 2 framerate = 8000 nframes = 100 self.f = wave.open(TESTFN, 'wb') self.f.setnchannels(nchannels) self.f.setsampwidth(sampwidth) self.f.setframerate(framerate) self.f.setnframes(nframes) output = '\0' * nframes * nchannels * sampwidth self.f.writeframes(output) self.f.close() self.f = wave.open(TESTFN, 'rb') self.assertEqual(nchannels, self.f.getnchannels()) self.assertEqual(sampwidth, self.f.getsampwidth()) self.assertEqual(framerate, self.f.getframerate()) self.assertEqual(nframes, self.f.getnframes()) self.assertEqual(self.f.readframes(nframes), output) # # Test that we can read as PCM samples the raw frames we just wrote. # self.f = wave.open(TESTFN, 'rb') self.assertEqual(nchannels, self.f.getnchannels()) self.assertEqual(sampwidth, self.f.getsampwidth()) self.assertEqual(framerate, self.f.getframerate()) self.assertEqual(nframes, self.f.getnframes()) rsamps = self.f.readpcmsamples(nframes) self.assertEqual(nframes, len(rsamps[0])) self.assertEqual(nframes, len(rsamps[1])) self.assertEqual( [ 0 for i in range(nframes) ], rsamps[0] ) self.assertEqual( [ 0 for i in range(nframes) ], rsamps[1] ) # # Global variable to get us to read in odd-ball ways. # self.nreads = 1 def _test_pcm_in_same_as_out(wsamps, md5sum): """Test that the samples read are the same as those written.""" self.f = wave.open(TESTFN, 'wb') self.f.setnchannels(nchannels) self.f.setsampwidth(sampwidth) self.f.setframerate(framerate) self.f.writepcmsamples(wsamps) self.f.close() # # Note: whack the expected md5 in the call to this routine # and uncomment this write-file to see the wave file. # # open("x.wav", "wb").write(open(TESTFN, 'rb').read()) self.assertEqual(md5(open(TESTFN, 'rb').read()).hexdigest(), md5sum) self.f = wave.open(TESTFN, 'rb') self.assertEqual(nchannels, self.f.getnchannels()) self.assertEqual(sampwidth, self.f.getsampwidth()) self.assertEqual(framerate, self.f.getframerate()) self.assertEqual(nframes, self.f.getnframes()) fc = [ nframes / self.nreads for ri in range(self.nreads) ] fc[-1] += nframes - sum(fc) rsamps = [ [] for i in range(nchannels) ] for ri in range(self.nreads): wavs = self.f.readpcmsamples(fc[ri]) for c in range(nchannels): rsamps[c] += wavs[c] # print "adding", len(wavs[c]), "samples", nchannels, self.nreads, fc pass self.f.close() self.assertEqual(rsamps, wsamps) self.nreads += 1 # next time, make 1 more read to get the samples # # Construct a 1 cycle sawtooth, # right channel inverted and reversed from left channel. # Note that the middle sample will be shifted by 1 # between the two sides. # This silly razimataz is to find glitches in the logic. # left_samps = [ i for i in range(-10000, 10000, 100) ] left_samps += [ i for i in range( 10000, -10000, -100) ] rite_samps = [ -s for s in left_samps ] rite_samps.reverse() wsamps = [ left_samps, rite_samps ] sampwidth = 2 framerate = 8000 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, '8a721669fd8144d18bfcd17d03a5836e') wsamps = [ left_samps ] sampwidth = 2 framerate = 44100 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, '3f431ef58b5090554b93e6bcfafcdb90') wsamps = [ [ (s / 256) + 128 for s in sa ] for sa in [ left_samps, rite_samps ] ] sampwidth = 1 framerate = 44100 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, '780ef667cc36eacbadc603dc4cfb6027') wsamps = [ [ (s / 256) + 128 for s in sa ] for sa in [ rite_samps ] ] sampwidth = 1 framerate = 11000 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, '3aefa56debd44454e7d081cc1472dd61') # Note: The 0x1000 multiply, not 0x10000, makes hex file dump easier to eyeball. wsamps = [ [ (s * 0x1000) for s in sa ] for sa in [ left_samps, rite_samps ] ] sampwidth = 4 framerate = 12345 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'aff3e1ee0211879c4e059570744a9324') wsamps = [ [ (s * 0x1000) for s in sa ] for sa in [ left_samps ] ] sampwidth = 4 framerate = 54321 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'dcf55e3ddf4c13e2fe5fac44e63f01ec') # # Test zero-length sample arrays # wsamps = [ [ ] ] sampwidth = 4 framerate = 1234 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'c4147c568b57ce9425c9396c71654ed9') wsamps = [ [ ], [ ] ] sampwidth = 4 framerate = 4321 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'f8887641b828a254d403207f29c0d241') wsamps = [ [ ], [ ] ] sampwidth = 2 framerate = 9000 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, '1724288d989a47695adcf10d0943db60') wsamps = [ [ ] ] sampwidth = 2 framerate = 9000 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, '9cbc97f4e0bda4a02806d631c5415e16') wsamps = [ [ ], [ ] ] sampwidth = 1 framerate = 22500 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, '3ed3c58968a1de71a6698b9f8c7f0a88') wsamps = [ [ ] ] sampwidth = 1 framerate = 44100 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, '04e3090d8dedf12cf89a5a005a4a94e7') # # Test one-length sample arrays # wsamps = [ [ 1 ] ] sampwidth = 4 framerate = 1234 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'c8310f7d473f8f303cceb46eb63494d9') wsamps = [ [ -0x80000000 ], [ 0x7fffFFFF ] ] sampwidth = 4 framerate = 4321 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, '7e59965c090564e2485a9b33fcc887fd') wsamps = [ [ 12345 ], [ 32767 ] ] sampwidth = 2 framerate = 9000 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'a6bc521e3a4163ed64fea6035cb654b0') wsamps = [ [ -32768 ] ] sampwidth = 2 framerate = 9000 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'f627f41d05e8cdc2f25452836f6f7f14') wsamps = [ [ 123 ], [ 255 ] ] sampwidth = 1 framerate = 22500 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'd5d5967b4dd399e26b32f6ebe8817f24') wsamps = [ [ 0 ] ] sampwidth = 1 framerate = 44100 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'a0420481b3b2cf11934461f5d91385f7') # # Test out-of-range samples. # wsamps = [ [ 256 ] ] sampwidth = 1 framerate = 44100 nchannels = len(wsamps) nframes = len(wsamps[0]) self.assertRaises(OverflowError, _test_pcm_in_same_as_out, wsamps, '') wsamps = [ [ -1 ] ] self.assertRaises(OverflowError, _test_pcm_in_same_as_out, wsamps, '') sampwidth = 2 wsamps = [ [ 32768 ] ] self.assertRaises(OverflowError, _test_pcm_in_same_as_out, wsamps, '') wsamps = [ [ -32769 ] ] self.assertRaises(OverflowError, _test_pcm_in_same_as_out, wsamps, '') sampwidth = 4 wsamps = [ [ 0x80000000 ] ] self.assertRaises(OverflowError, _test_pcm_in_same_as_out, wsamps, '') wsamps = [ [ -0x80000001 ] ] self.assertRaises(OverflowError, _test_pcm_in_same_as_out, wsamps, '') # # Test wrong number of channels and mismatched sample array lengths. # wsamps = [ [ ] ] nchannels = 2 self.assertRaises(wave.Error, _test_pcm_in_same_as_out, wsamps, '') wsamps = [ [ 1 ], [ ] ] nchannels = len(wsamps) self.assertRaises(wave.Error, _test_pcm_in_same_as_out, wsamps, '') # # Test some odd-ball numbers of samples. # self.nreads = 1 wsamps = [ left_samps[0:13], rite_samps[0:13] ] sampwidth = 2 framerate = 44100 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'ecb56cdc03d94fae2a8bbf51db2303af') self.nreads = 1 wsamps = [ left_samps[0:32], rite_samps[0:32] ] sampwidth = 2 framerate = 44100 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, '3b84ef4defc2e08ebd6e3169df0bce9e') self.nreads = 1 wsamps = [ left_samps[0:63], rite_samps[0:63] ] sampwidth = 2 framerate = 44100 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'e9141db80701f4394573e71baa0db638') self.nreads = 1 wsamps = [ left_samps[0:65], rite_samps[0:65] ] sampwidth = 2 framerate = 44100 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'b8ae007dbc16ad48c132a24dcb31921a') # # Test a big file (over 16 meg in length) # self.nreads = 1 left_samps = [] for i in range(11000): left_samps += [ i for i in range(-10000, 10000, 100) ] left_samps += [ i for i in range( 10000, -10000, -100) ] rite_samps = [ -s for s in left_samps ] rite_samps.reverse() wsamps = [ left_samps, rite_samps ] sampwidth = 2 framerate = 44100 nchannels = len(wsamps) nframes = len(wsamps[0]) _test_pcm_in_same_as_out(wsamps, 'd8bbfdf6510adb68508b9687f1478a61') def test_main(): run_unittest(TestWave) if __name__ == '__main__': test_main()