blob: 54694ea2d914e2bd8f6cf34ad7463130f1324b29 [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.
Benjamin Peterson7c7250d2009-04-30 00:06:33 +000030 self.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 Murray8fd522f2009-05-07 16:27:02 +000094 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 Murray25b4add2009-04-29 13:17:37 +0000109
110def test_main():
111 run_unittest(AIFCTest)
112
113
114if __name__ == "__main__":
115 unittest.main()