blob: 085b9499967e52472bf2ca8bad2ef45d51b8eb1b [file] [log] [blame]
R. David Murrayeb01a6c2009-04-29 20:40:42 +00001from test.support import findfile, run_unittest, TESTFN
R. David Murrayb507d2e2009-04-29 15:34:32 +00002import unittest
R. David Murrayeb01a6c2009-04-29 20:40:42 +00003import os
Sandro Tosibdd53542012-01-01 18:04:37 +01004import io
R. David Murrayb507d2e2009-04-29 15:34:32 +00005
6import aifc
7
8
9class AIFCTest(unittest.TestCase):
10
11 def setUp(self):
R. David Murrayeb01a6c2009-04-29 20:40:42 +000012 self.f = self.fout = None
R. David Murrayb507d2e2009-04-29 15:34:32 +000013 self.sndfilepath = findfile('Sine-1000Hz-300ms.aif')
14
R. David Murrayeb01a6c2009-04-29 20:40:42 +000015 def tearDown(self):
16 if self.f is not None:
17 self.f.close()
18 if self.fout is not None:
19 try:
20 self.fout.close()
21 except (aifc.Error, AttributeError):
22 pass
23 try:
24 os.remove(TESTFN)
25 except OSError:
26 pass
27
R. David Murrayb507d2e2009-04-29 15:34:32 +000028 def test_skipunknown(self):
29 #Issue 2245
30 #This file contains chunk types aifc doesn't recognize.
Benjamin Peterson25c95f12009-05-08 20:42:26 +000031 self.f = aifc.open(self.sndfilepath)
R. David Murrayb507d2e2009-04-29 15:34:32 +000032
33 def test_params(self):
R. David Murrayeb01a6c2009-04-29 20:40:42 +000034 f = self.f = aifc.open(self.sndfilepath)
R. David Murrayb507d2e2009-04-29 15:34:32 +000035 self.assertEqual(f.getnchannels(), 2)
36 self.assertEqual(f.getsampwidth(), 2)
37 self.assertEqual(f.getframerate(), 48000)
38 self.assertEqual(f.getnframes(), 14400)
R. David Murrayb507d2e2009-04-29 15:34:32 +000039 self.assertEqual(f.getcomptype(), b'NONE')
40 self.assertEqual(f.getcompname(), b'not compressed')
41 self.assertEqual(
42 f.getparams(),
43 (2, 2, 48000, 14400, b'NONE', b'not compressed'),
44 )
R. David Murrayb507d2e2009-04-29 15:34:32 +000045
46 def test_read(self):
R. David Murrayeb01a6c2009-04-29 20:40:42 +000047 f = self.f = aifc.open(self.sndfilepath)
R. David Murrayb507d2e2009-04-29 15:34:32 +000048 self.assertEqual(f.tell(), 0)
49 self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
50 f.rewind()
51 pos0 = f.tell()
52 self.assertEqual(pos0, 0)
53 self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
54 pos2 = f.tell()
55 self.assertEqual(pos2, 2)
56 self.assertEqual(f.readframes(2), b'\x17t\x17t"\xad"\xad')
57 f.setpos(pos2)
58 self.assertEqual(f.readframes(2), b'\x17t\x17t"\xad"\xad')
59 f.setpos(pos0)
60 self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
R. David Murrayb507d2e2009-04-29 15:34:32 +000061
R. David Murrayeb01a6c2009-04-29 20:40:42 +000062 def test_write(self):
63 f = self.f = aifc.open(self.sndfilepath)
64 fout = self.fout = aifc.open(TESTFN, 'wb')
65 fout.aifc()
66 fout.setparams(f.getparams())
67 for frame in range(f.getnframes()):
68 fout.writeframes(f.readframes(1))
69 fout.close()
70 fout = self.fout = aifc.open(TESTFN, 'rb')
71 f.rewind()
72 self.assertEqual(f.getparams(), fout.getparams())
73 self.assertEqual(f.readframes(5), fout.readframes(5))
74
75 def test_compress(self):
76 f = self.f = aifc.open(self.sndfilepath)
77 fout = self.fout = aifc.open(TESTFN, 'wb')
78 fout.aifc()
79 fout.setnchannels(f.getnchannels())
80 fout.setsampwidth(f.getsampwidth())
81 fout.setframerate(f.getframerate())
82 fout.setcomptype(b'ULAW', b'foo')
83 for frame in range(f.getnframes()):
84 fout.writeframes(f.readframes(1))
85 fout.close()
86 self.assertLess(
87 os.stat(TESTFN).st_size,
88 os.stat(self.sndfilepath).st_size*0.75,
89 )
90 fout = self.fout = aifc.open(TESTFN, 'rb')
91 f.rewind()
92 self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3])
93 self.assertEqual(fout.getcomptype(), b'ULAW')
94 self.assertEqual(fout.getcompname(), b'foo')
95 # XXX: this test fails, not sure if it should succeed or not
96 # self.assertEqual(f.readframes(5), fout.readframes(5))
R. David Murrayb507d2e2009-04-29 15:34:32 +000097
R. David Murray99352742009-05-07 18:24:38 +000098 def test_close(self):
99 class Wrapfile(object):
100 def __init__(self, file):
101 self.file = open(file, 'rb')
102 self.closed = False
103 def close(self):
104 self.file.close()
105 self.closed = True
106 def __getattr__(self, attr): return getattr(self.file, attr)
107 testfile = Wrapfile(self.sndfilepath)
108 f = self.f = aifc.open(testfile)
109 self.assertEqual(testfile.closed, False)
110 f.close()
111 self.assertEqual(testfile.closed, True)
112
Sandro Tosibdd53542012-01-01 18:04:37 +0100113 def test_write_header_comptype_sampwidth(self):
114 for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
115 fout = self.fout = aifc.open(io.BytesIO(), 'wb')
116 fout.setnchannels(1)
117 fout.setframerate(1)
118 fout.setcomptype(comptype, b'')
119 fout.close()
120 self.assertEqual(fout.getsampwidth(), 2)
121 fout.initfp(None)
122
Sandro Tosi70efbef2012-01-01 22:53:08 +0100123 def test_write_markers_values(self):
124 fout = self.fout = aifc.open(io.BytesIO(), 'wb')
125 self.assertEqual(fout.getmarkers(), None)
126 fout.setmark(1, 0, b'foo1')
127 fout.setmark(1, 1, b'foo2')
128 self.assertEqual(fout.getmark(1), (1, 1, b'foo2'))
129 self.assertEqual(fout.getmarkers(), [(1, 1, b'foo2')])
130 fout.initfp(None)
131
132 def test_read_markers(self):
133 fout = self.fout = aifc.open(TESTFN, 'wb')
134 fout.aiff()
135 fout.setparams((1, 1, 1, 1, b'NONE', b''))
136 fout.setmark(1, 0, b'odd')
137 fout.setmark(2, 0, b'even')
138 fout.writeframes(b'\x00')
139 fout.close()
140 f = self.f = aifc.open(TESTFN, 'rb')
141 self.assertEqual(f.getmarkers(), [(1, 0, b'odd'), (2, 0, b'even')])
142 self.assertEqual(f.getmark(1), (1, 0, b'odd'))
143 self.assertEqual(f.getmark(2), (2, 0, b'even'))
144 self.assertRaises(aifc.Error, f.getmark, 3)
145
R. David Murrayb507d2e2009-04-29 15:34:32 +0000146
147def test_main():
148 run_unittest(AIFCTest)
149
150
151if __name__ == "__main__":
152 unittest.main()