blob: 9fec392f84ffb5f53cf8e8e6835f65ab200d31d2 [file] [log] [blame]
R. David Murray0f457e52009-04-29 20:15:18 +00001from test.test_support import findfile, run_unittest, TESTFN
R. David Murray25b4add2009-04-29 13:17:37 +00002import unittest
R. David Murray0f457e52009-04-29 20:15:18 +00003import os
R. David Murray25b4add2009-04-29 13:17:37 +00004
5import aifc
6
7
8class AIFCTest(unittest.TestCase):
9
10 def setUp(self):
R. David Murray0f457e52009-04-29 20:15:18 +000011 self.f = self.fout = None
R. David Murray25b4add2009-04-29 13:17:37 +000012 self.sndfilepath = findfile('Sine-1000Hz-300ms.aif')
13
R. David Murray0f457e52009-04-29 20:15:18 +000014 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 Murray25b4add2009-04-29 13:17:37 +000027 def test_skipunknown(self):
28 #Issue 2245
29 #This file contains chunk types aifc doesn't recognize.
30 f = aifc.open(self.sndfilepath)
R. David Murray25b4add2009-04-29 13:17:37 +000031
R. David Murray971b1b12009-04-29 13:51:44 +000032 def test_params(self):
R. David Murray0f457e52009-04-29 20:15:18 +000033 f = self.f = aifc.open(self.sndfilepath)
R. David Murray971b1b12009-04-29 13:51:44 +000034 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 Murray971b1b12009-04-29 13:51:44 +000041
42 def test_read(self):
R. David Murray0f457e52009-04-29 20:15:18 +000043 f = self.f = aifc.open(self.sndfilepath)
R. David Murray971b1b12009-04-29 13:51:44 +000044 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 Murray971b1b12009-04-29 13:51:44 +000057
R. David Murray0f457e52009-04-29 20:15:18 +000058 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 Murray971b1b12009-04-29 13:51:44 +000093
R. David Murray25b4add2009-04-29 13:17:37 +000094
95def test_main():
96 run_unittest(AIFCTest)
97
98
99if __name__ == "__main__":
100 unittest.main()