1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
from test.support import findfile, run_unittest, TESTFN
import unittest
import os
import io
import aifc
class AIFCTest(unittest.TestCase):
def setUp(self):
self.f = self.fout = None
self.sndfilepath = findfile('Sine-1000Hz-300ms.aif')
def tearDown(self):
if self.f is not None:
self.f.close()
if self.fout is not None:
try:
self.fout.close()
except (aifc.Error, AttributeError):
pass
try:
os.remove(TESTFN)
except OSError:
pass
def test_skipunknown(self):
#Issue 2245
#This file contains chunk types aifc doesn't recognize.
self.f = aifc.open(self.sndfilepath)
def test_params(self):
f = self.f = aifc.open(self.sndfilepath)
self.assertEqual(f.getnchannels(), 2)
self.assertEqual(f.getsampwidth(), 2)
self.assertEqual(f.getframerate(), 48000)
self.assertEqual(f.getnframes(), 14400)
self.assertEqual(f.getcomptype(), b'NONE')
self.assertEqual(f.getcompname(), b'not compressed')
self.assertEqual(
f.getparams(),
(2, 2, 48000, 14400, b'NONE', b'not compressed'),
)
def test_read(self):
f = self.f = aifc.open(self.sndfilepath)
self.assertEqual(f.tell(), 0)
self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
f.rewind()
pos0 = f.tell()
self.assertEqual(pos0, 0)
self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
pos2 = f.tell()
self.assertEqual(pos2, 2)
self.assertEqual(f.readframes(2), b'\x17t\x17t"\xad"\xad')
f.setpos(pos2)
self.assertEqual(f.readframes(2), b'\x17t\x17t"\xad"\xad')
f.setpos(pos0)
self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
def test_write(self):
f = self.f = aifc.open(self.sndfilepath)
fout = self.fout = aifc.open(TESTFN, 'wb')
fout.aifc()
fout.setparams(f.getparams())
for frame in range(f.getnframes()):
fout.writeframes(f.readframes(1))
fout.close()
fout = self.fout = aifc.open(TESTFN, 'rb')
f.rewind()
self.assertEqual(f.getparams(), fout.getparams())
self.assertEqual(f.readframes(5), fout.readframes(5))
def test_compress(self):
f = self.f = aifc.open(self.sndfilepath)
fout = self.fout = aifc.open(TESTFN, 'wb')
fout.aifc()
fout.setnchannels(f.getnchannels())
fout.setsampwidth(f.getsampwidth())
fout.setframerate(f.getframerate())
fout.setcomptype(b'ULAW', b'foo')
for frame in range(f.getnframes()):
fout.writeframes(f.readframes(1))
fout.close()
self.assertLess(
os.stat(TESTFN).st_size,
os.stat(self.sndfilepath).st_size*0.75,
)
fout = self.fout = aifc.open(TESTFN, 'rb')
f.rewind()
self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3])
self.assertEqual(fout.getcomptype(), b'ULAW')
self.assertEqual(fout.getcompname(), b'foo')
# XXX: this test fails, not sure if it should succeed or not
# self.assertEqual(f.readframes(5), fout.readframes(5))
def test_close(self):
class Wrapfile(object):
def __init__(self, file):
self.file = open(file, 'rb')
self.closed = False
def close(self):
self.file.close()
self.closed = True
def __getattr__(self, attr): return getattr(self.file, attr)
testfile = Wrapfile(self.sndfilepath)
f = self.f = aifc.open(testfile)
self.assertEqual(testfile.closed, False)
f.close()
self.assertEqual(testfile.closed, True)
def test_write_header_comptype_sampwidth(self):
for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
fout = self.fout = aifc.open(io.BytesIO(), 'wb')
fout.setnchannels(1)
fout.setframerate(1)
fout.setcomptype(comptype, b'')
fout.close()
self.assertEqual(fout.getsampwidth(), 2)
fout.initfp(None)
def test_write_markers_values(self):
fout = self.fout = aifc.open(io.BytesIO(), 'wb')
self.assertEqual(fout.getmarkers(), None)
fout.setmark(1, 0, b'foo1')
fout.setmark(1, 1, b'foo2')
self.assertEqual(fout.getmark(1), (1, 1, b'foo2'))
self.assertEqual(fout.getmarkers(), [(1, 1, b'foo2')])
fout.initfp(None)
def test_read_markers(self):
fout = self.fout = aifc.open(TESTFN, 'wb')
fout.aiff()
fout.setparams((1, 1, 1, 1, b'NONE', b''))
fout.setmark(1, 0, b'odd')
fout.setmark(2, 0, b'even')
fout.writeframes(b'\x00')
fout.close()
f = self.f = aifc.open(TESTFN, 'rb')
self.assertEqual(f.getmarkers(), [(1, 0, b'odd'), (2, 0, b'even')])
self.assertEqual(f.getmark(1), (1, 0, b'odd'))
self.assertEqual(f.getmark(2), (2, 0, b'even'))
self.assertRaises(aifc.Error, f.getmark, 3)
class AIFCLowLevelTest(unittest.TestCase):
def test_read_written(self):
def read_written(self, what):
f = io.BytesIO()
getattr(aifc, '_write_' + what)(f, x)
f.seek(0)
return getattr(aifc, '_read_' + what)(f)
for x in (-1, 0, 0.1, 1):
self.assertEqual(read_written(x, 'float'), x)
for x in (float('NaN'), float('Inf')):
self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL)
for x in (b'', b'foo', b'a' * 255):
self.assertEqual(read_written(x, 'string'), x)
for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF):
self.assertEqual(read_written(x, 'long'), x)
for x in (0, 1, 0xFFFFFFFF):
self.assertEqual(read_written(x, 'ulong'), x)
for x in (-0x7FFF, -1, 0, 1, 0x7FFF):
self.assertEqual(read_written(x, 'short'), x)
for x in (0, 1, 0xFFFF):
self.assertEqual(read_written(x, 'ushort'), x)
def test_read_raises(self):
f = io.BytesIO(b'\x00')
self.assertRaises(EOFError, aifc._read_ulong, f)
self.assertRaises(EOFError, aifc._read_long, f)
self.assertRaises(EOFError, aifc._read_ushort, f)
self.assertRaises(EOFError, aifc._read_short, f)
def test_write_long_string_raises(self):
f = io.BytesIO()
with self.assertRaises(ValueError):
aifc._write_string(f, b'too long' * 255)
def test_main():
run_unittest(AIFCTest)
run_unittest(AIFCLowLevelTest)
if __name__ == "__main__":
unittest.main()
|