1 from test
.test_support
import findfile
, run_unittest
, TESTFN
8 class AIFCTest(unittest
.TestCase
):
11 self
.f
= self
.fout
= None
12 self
.sndfilepath
= findfile('Sine-1000Hz-300ms.aif')
15 if self
.f
is not None:
17 if self
.fout
is not None:
20 except (aifc
.Error
, AttributeError):
27 def test_skipunknown(self
):
29 #This file contains chunk types aifc doesn't recognize.
30 self
.f
= aifc
.open(self
.sndfilepath
)
32 def test_params(self
):
33 f
= self
.f
= aifc
.open(self
.sndfilepath
)
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'))
43 f
= self
.f
= aifc
.open(self
.sndfilepath
)
44 self
.assertEqual(f
.tell(), 0)
45 self
.assertEqual(f
.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
48 self
.assertEqual(pos0
, 0)
49 self
.assertEqual(f
.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
51 self
.assertEqual(pos2
, 2)
52 self
.assertEqual(f
.readframes(2), '\x17t\x17t"\xad"\xad')
54 self
.assertEqual(f
.readframes(2), '\x17t\x17t"\xad"\xad')
56 self
.assertEqual(f
.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
59 f
= self
.f
= aifc
.open(self
.sndfilepath
)
60 fout
= self
.fout
= aifc
.open(TESTFN
, 'wb')
62 fout
.setparams(f
.getparams())
63 for frame
in range(f
.getnframes()):
64 fout
.writeframes(f
.readframes(1))
66 fout
= self
.fout
= aifc
.open(TESTFN
, 'rb')
68 self
.assertEqual(f
.getparams(), fout
.getparams())
69 self
.assertEqual(f
.readframes(5), fout
.readframes(5))
71 def test_compress(self
):
72 f
= self
.f
= aifc
.open(self
.sndfilepath
)
73 fout
= self
.fout
= aifc
.open(TESTFN
, 'wb')
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))
83 os
.stat(TESTFN
).st_size
,
84 os
.stat(self
.sndfilepath
).st_size
*0.75,
86 fout
= self
.fout
= aifc
.open(TESTFN
, 'rb')
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))
95 class Wrapfile(object):
96 def __init__(self
, file):
97 self
.file = open(file, 'rb')
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)
107 self
.assertEqual(testfile
.closed
, True)
111 run_unittest(AIFCTest
)
114 if __name__
== "__main__":