def test_headerReadAndWrite_settingAUformat(self) : wavoutput = sndfile.sndfile("test.au","w", format= sndfile.AU | sndfile.FLOAT) wavoutput.close() wavinput = sndfile.sndfile("test.au","r") self.assertEqual(sndfile.AU | sndfile.FLOAT, wavinput.getFormat()) self.assertEqual(1, wavinput.getChannels()) self.assertEqual(44100, wavinput.getSampleRate())
def test_headerReadAndWrite_byDefaultChangingExtensionToAU(self) : wavoutput = sndfile.sndfile("test.au","w") wavoutput.close() wavinput = sndfile.sndfile("test.au","r") self.assertEqual(sndfile.AU | sndfile.PCM16, wavinput.getFormat()) self.assertEqual(1, wavinput.getChannels()) self.assertEqual(44100, wavinput.getSampleRate())
def test_headerReadAndWrite_settingEverything(self) : wavoutput = sndfile.sndfile("test.wav","w", format= sndfile.WAV | sndfile.PCM24, channels = 2, samplerate = 22050) wavoutput.close() wavinput = sndfile.sndfile("test.wav","r") self.assertEqual(sndfile.WAV | sndfile.PCM24, wavinput.getFormat()) self.assertEqual(2, wavinput.getChannels()) self.assertEqual(22050, wavinput.getSampleRate())
def test_headerReadAndWrite_settingAIFFformat(self) : wavoutput = sndfile.sndfile("test.aiff","w", format= sndfile.AIFF | sndfile.PCM32) wavoutput.close() wavinput = sndfile.sndfile("test.aiff","r") self.assertEqual(sndfile.AIFF | sndfile.PCM32, wavinput.getFormat()) self.assertEqual(1, wavinput.getChannels()) self.assertEqual(44100, wavinput.getSampleRate())
def test_headerReadAndWrite_settingFormat(self) : wavoutput = sndfile.sndfile("test.wav","w", format= sndfile.WAV | sndfile.PCM24) wavoutput.close() wavinput = sndfile.sndfile("test.wav","r") self.assertEqual(sndfile.WAV | sndfile.PCM24, wavinput.getFormat()) self.assertEqual(1, wavinput.getChannels()) self.assertEqual(44100, wavinput.getSampleRate())
def test_readFile_whenMalformed(self) : waveoutput = open("test.wav", "w") print >> waveoutput, "This is not a wav" waveoutput.close() try : sndfile.sndfile("test.wav","r") self.fail("Should have thrown an exception") except sndfile.IOException, e : self.assertEqual(("File contains data in an unknown format.",), e.args)
def test_readWriteToOneChannel(self): wavoutput = sndfile.sndfile("test.wav", "w") seconds = 10 wav = numpy.ones((1, 44100 * seconds)) wav[0,:]*=.6 wavoutput.write(wav) wavoutput.close() wavinput = sndfile.sndfile("test.wav", "r") result = numpy.zeros((1, 44100 * seconds)) self.assertEqual(1 * 44100 * seconds , wavinput.read(result)) #Number of items: CHANNELS * SAMPLERATE * SECONDS self.assertEqual(wav.all() , result.all())
def test_writeReadGetFrames(self): wavoutput = sndfile.sndfile("test.wav", "w", channels=2) seconds = 60 frames = wavoutput.getSampleRate() * seconds wav = numpy.ones((wavoutput.getChannels(), frames)) wav[0,:]*=.6 wav[1,:]*=.3 wavoutput.write(wav) wavoutput.close() wavinput = sndfile.sndfile("test.wav", "r") self.assertEqual(frames , wavinput.getFrames())
def test_readWriteTwoChannels(self): wavoutput = sndfile.sndfile("test.wav", "w", channels=2) seconds = 60 wav = numpy.ones((2, 44100 * seconds)) wav[0,:]*=.6 wav[1,:]*=.3 wavoutput.write(wav) wavoutput.close() wavinput = sndfile.sndfile("test.wav", "r") result = numpy.zeros((2, 44100 * seconds)) self.assertEqual(2 * 44100 * seconds , wavinput.read(result)) self.assertEqual(wav.all() , result.all())
def test_writeReadFourChannelsCreatingBufferWithFileInfo(self): channels = 4 wavoutput = sndfile.sndfile("test.wav", "w", channels=channels) seconds = 60 frames = wavoutput.getSampleRate() * seconds wav = numpy.ones((channels, frames)) wav[0,:]*=.8 wav[1,:]*=.6 wav[1,:]*=.4 wav[1,:]*=.2 self.assertEqual(channels * frames , wavoutput.write(wav)) wavoutput.close() wavinput = sndfile.sndfile("test.wav", "r") result = numpy.zeros((wavinput.getChannels(), wavinput.getFrames())) #With this, we make sure that the buffer will hold all the data self.assertEqual(channels * frames, wavinput.read(result)) self.assertEqual(wav.all() , result.all())
def test_Metadata_settingTagNotSupported(self): wavoutput = sndfile.sndfile("test.wav","w") try : wavoutput.setString("TagNotSupported", "Trying to throw an exception") self.fail("Should have thrown an exception") except sndfile.TagNotSupported, e : self.assertEqual(("setString error : Tag not supported by sndfile.",), e.args) wavoutput.close()
def _test_writeFile_withBadParameters(self) : try : wavoutput = sndfile.sndfile("test.wav","w", format=sndfile.WAV|sndfile.VORBIS ) self.fail("Should have thrown an exception") except sndfile.UnsupportedFormatException, e : self.assertEqual("TODO: Lo que devuelva sf_strerror", e.args)
def test_writeFile_closing(self) : wavoutput = sndfile.sndfile("test.wav","w") wavoutput.close() self.assertEqual(True, os.access("test.wav", os.R_OK))
def test_Metadata_settingComment(self): wavoutput = sndfile.sndfile("test.wav","w") wavoutput.setString("comment", "Adding a comment") self.assertEqual("Adding a comment", wavoutput.getString("comment")) wavoutput.close()
def test_readFile_whenDoesNotExist(self) : try : sndfile.sndfile("notexisting.wav","r") self.fail("Should have thrown an exception") except sndfile.IOException, e : self.assertEqual(("System error : No such file or directory.",), e.args)
def test_Metadata_settingDate(self): wavoutput = sndfile.sndfile("test.wav","w") wavoutput.setString("date", "15-04-2010") self.assertEqual("15-04-2010", wavoutput.getString("date")) wavoutput.close()
def test_Metadata_settingCopyright(self): wavoutput = sndfile.sndfile("test.wav","w") wavoutput.setString("copyright", "Testing the copyright") self.assertEqual("Testing the copyright", wavoutput.getString("copyright")) wavoutput.close()
def test_Metadata_settingSoftware(self): wavoutput = sndfile.sndfile("test.wav","w") wavoutput.setString("software", "Python/C API") self.assertEqual("Python/C API (libsndfile-1.0.20)", wavoutput.getString("software")) wavoutput.close()
def test_Metadata_settingArtist(self): wavoutput = sndfile.sndfile("test.wav","w") wavoutput.setString("artist", "Testing the Artist") self.assertEqual("Testing the Artist", wavoutput.getString("artist")) wavoutput.close()
def test_Metadata_settingTitle(self): wavoutput = sndfile.sndfile("test.wav","w") wavoutput.setString("title", "Testing the Title") self.assertEqual("Testing the Title", wavoutput.getString("title")) wavoutput.close()