R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 1 | from test.test_support import findfile, run_unittest, TESTFN |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 2 | import unittest |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 3 | import os |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 4 | |
| 5 | import aifc |
| 6 | |
| 7 | |
| 8 | class AIFCTest(unittest.TestCase): |
| 9 | |
| 10 | def setUp(self): |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 11 | self.f = self.fout = None |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 12 | self.sndfilepath = findfile('Sine-1000Hz-300ms.aif') |
| 13 | |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 14 | def tearDown(self): |
| 15 | if self.f is not None: |
| 16 | self.f.close() |
| 17 | if self.fout is not None: |
| 18 | try: |
| 19 | self.fout.close() |
| 20 | except (aifc.Error, AttributeError): |
| 21 | pass |
| 22 | try: |
| 23 | os.remove(TESTFN) |
| 24 | except OSError: |
| 25 | pass |
| 26 | |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 27 | def test_skipunknown(self): |
| 28 | #Issue 2245 |
| 29 | #This file contains chunk types aifc doesn't recognize. |
Benjamin Peterson | 7c7250d | 2009-04-30 00:06:33 +0000 | [diff] [blame] | 30 | self.f = aifc.open(self.sndfilepath) |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 31 | |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 32 | def test_params(self): |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 33 | f = self.f = aifc.open(self.sndfilepath) |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 34 | self.assertEqual(f.getnchannels(), 2) |
| 35 | self.assertEqual(f.getsampwidth(), 2) |
| 36 | self.assertEqual(f.getframerate(), 48000) |
| 37 | self.assertEqual(f.getnframes(), 14400) |
| 38 | self.assertEqual(f.getcomptype(), 'NONE') |
| 39 | self.assertEqual(f.getcompname(), 'not compressed') |
| 40 | self.assertEqual(f.getparams(), (2, 2, 48000, 14400, 'NONE', 'not compressed')) |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 41 | |
| 42 | def test_read(self): |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 43 | f = self.f = aifc.open(self.sndfilepath) |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 44 | self.assertEqual(f.tell(), 0) |
| 45 | self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
| 46 | f.rewind() |
| 47 | pos0 = f.tell() |
| 48 | self.assertEqual(pos0, 0) |
| 49 | self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
| 50 | pos2 = f.tell() |
| 51 | self.assertEqual(pos2, 2) |
| 52 | self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad') |
| 53 | f.setpos(pos2) |
| 54 | self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad') |
| 55 | f.setpos(pos0) |
| 56 | self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 57 | |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 58 | def test_write(self): |
| 59 | f = self.f = aifc.open(self.sndfilepath) |
| 60 | fout = self.fout = aifc.open(TESTFN, 'wb') |
| 61 | fout.aifc() |
| 62 | fout.setparams(f.getparams()) |
| 63 | for frame in range(f.getnframes()): |
| 64 | fout.writeframes(f.readframes(1)) |
| 65 | fout.close() |
| 66 | fout = self.fout = aifc.open(TESTFN, 'rb') |
| 67 | f.rewind() |
| 68 | self.assertEqual(f.getparams(), fout.getparams()) |
| 69 | self.assertEqual(f.readframes(5), fout.readframes(5)) |
| 70 | |
| 71 | def test_compress(self): |
| 72 | f = self.f = aifc.open(self.sndfilepath) |
| 73 | fout = self.fout = aifc.open(TESTFN, 'wb') |
| 74 | fout.aifc() |
| 75 | fout.setnchannels(f.getnchannels()) |
| 76 | fout.setsampwidth(f.getsampwidth()) |
| 77 | fout.setframerate(f.getframerate()) |
| 78 | fout.setcomptype('ULAW', 'foo') |
| 79 | for frame in range(f.getnframes()): |
| 80 | fout.writeframes(f.readframes(1)) |
| 81 | fout.close() |
| 82 | self.assertLess( |
| 83 | os.stat(TESTFN).st_size, |
| 84 | os.stat(self.sndfilepath).st_size*0.75, |
| 85 | ) |
| 86 | fout = self.fout = aifc.open(TESTFN, 'rb') |
| 87 | f.rewind() |
| 88 | self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3]) |
| 89 | self.assertEqual(fout.getcomptype(), 'ULAW') |
| 90 | self.assertEqual(fout.getcompname(), 'foo') |
| 91 | # XXX: this test fails, not sure if it should succeed or not |
| 92 | # self.assertEqual(f.readframes(5), fout.readframes(5)) |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 93 | |
R. David Murray | 8fd522f | 2009-05-07 16:27:02 +0000 | [diff] [blame^] | 94 | def test_close(self): |
| 95 | class Wrapfile(object): |
| 96 | def __init__(self, file): |
| 97 | self.file = open(file) |
| 98 | self.closed = False |
| 99 | def close(self): |
| 100 | self.file.close() |
| 101 | self.closed = True |
| 102 | def __getattr__(self, attr): return getattr(self.file, attr) |
| 103 | testfile = Wrapfile(self.sndfilepath) |
| 104 | f = self.f = aifc.open(testfile) |
| 105 | self.assertEqual(testfile.closed, False) |
| 106 | f.close() |
| 107 | self.assertEqual(testfile.closed, True) |
| 108 | |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 109 | |
| 110 | def test_main(): |
| 111 | run_unittest(AIFCTest) |
| 112 | |
| 113 | |
| 114 | if __name__ == "__main__": |
| 115 | unittest.main() |