1from test.support import findfile 2from test.support.os_helper import TESTFN, unlink 3from test.support.warnings_helper import check_no_resource_warning 4import unittest 5from unittest import mock 6from test import audiotests 7from audioop import byteswap 8import io 9import sys 10import struct 11import aifc 12 13class AifcTest(audiotests.AudioWriteTests, 14 audiotests.AudioTestsWithSourceFile): 15 module = aifc 16 close_fd = True 17 test_unseekable_read = None 18 19 20class AifcPCM8Test(AifcTest, unittest.TestCase): 21 sndfilename = 'pluck-pcm8.aiff' 22 sndfilenframes = 3307 23 nchannels = 2 24 sampwidth = 1 25 framerate = 11025 26 nframes = 48 27 comptype = b'NONE' 28 compname = b'not compressed' 29 frames = bytes.fromhex("""\ 30 02FF 4B00 3104 8008 CB06 4803 BF01 03FE B8FA B4F3 29EB 1AE6 \ 31 EDE4 C6E2 0EE0 EFE0 57E2 FBE8 13EF D8F7 97FB F5FC 08FB DFFB \ 32 11FA 3EFB BCFC 66FF CF04 4309 C10E 5112 EE17 8216 7F14 8012 \ 33 490E 520D EF0F CE0F E40C 630A 080A 2B0B 510E 8B11 B60E 440A \ 34 """) 35 36 37class AifcPCM16Test(AifcTest, unittest.TestCase): 38 sndfilename = 'pluck-pcm16.aiff' 39 sndfilenframes = 3307 40 nchannels = 2 41 sampwidth = 2 42 framerate = 11025 43 nframes = 48 44 comptype = b'NONE' 45 compname = b'not compressed' 46 frames = bytes.fromhex("""\ 47 022EFFEA 4B5D00F6 311804EA 80E10840 CBE106B1 48A903F5 BFE601B2 036CFE7B \ 48 B858FA3E B4B1F34F 299AEBCA 1A5DE6DA EDFAE491 C628E275 0E09E0B5 EF2AE029 \ 49 5758E271 FB35E83F 1376EF86 D82BF727 9790FB76 F5FAFC0F 0867FB9C DF30FB43 \ 50 117EFA36 3EE5FB5B BC79FCB1 66D9FF5D CF150412 431D097C C1BA0EC8 512112A1 \ 51 EEE21753 82071665 7FFF1443 8004128F 49A20EAF 52BB0DBA EFB40F60 CE3C0FBF \ 52 E4B30CEC 63430A5C 08C80A20 2BBB0B08 514A0E43 8BCF1139 B6F60EEB 44120A5E \ 53 """) 54 55 56class AifcPCM24Test(AifcTest, unittest.TestCase): 57 sndfilename = 'pluck-pcm24.aiff' 58 sndfilenframes = 3307 59 nchannels = 2 60 sampwidth = 3 61 framerate = 11025 62 nframes = 48 63 comptype = b'NONE' 64 compname = b'not compressed' 65 frames = bytes.fromhex("""\ 66 022D65FFEB9D 4B5A0F00FA54 3113C304EE2B 80DCD6084303 \ 67 CBDEC006B261 48A99803F2F8 BFE82401B07D 036BFBFE7B5D \ 68 B85756FA3EC9 B4B055F3502B 299830EBCB62 1A5CA7E6D99A \ 69 EDFA3EE491BD C625EBE27884 0E05A9E0B6CF EF2929E02922 \ 70 5758D8E27067 FB3557E83E16 1377BFEF8402 D82C5BF7272A \ 71 978F16FB7745 F5F865FC1013 086635FB9C4E DF30FCFB40EE \ 72 117FE0FA3438 3EE6B8FB5AC3 BC77A3FCB2F4 66D6DAFF5F32 \ 73 CF13B9041275 431D69097A8C C1BB600EC74E 5120B912A2BA \ 74 EEDF641754C0 8207001664B7 7FFFFF14453F 8000001294E6 \ 75 499C1B0EB3B2 52B73E0DBCA0 EFB2B20F5FD8 CE3CDB0FBE12 \ 76 E4B49C0CEA2D 6344A80A5A7C 08C8FE0A1FFE 2BB9860B0A0E \ 77 51486F0E44E1 8BCC64113B05 B6F4EC0EEB36 4413170A5B48 \ 78 """) 79 80 81class AifcPCM32Test(AifcTest, unittest.TestCase): 82 sndfilename = 'pluck-pcm32.aiff' 83 sndfilenframes = 3307 84 nchannels = 2 85 sampwidth = 4 86 framerate = 11025 87 nframes = 48 88 comptype = b'NONE' 89 compname = b'not compressed' 90 frames = bytes.fromhex("""\ 91 022D65BCFFEB9D92 4B5A0F8000FA549C 3113C34004EE2BC0 80DCD680084303E0 \ 92 CBDEC0C006B26140 48A9980003F2F8FC BFE8248001B07D92 036BFB60FE7B5D34 \ 93 B8575600FA3EC920 B4B05500F3502BC0 29983000EBCB6240 1A5CA7A0E6D99A60 \ 94 EDFA3E80E491BD40 C625EB80E27884A0 0E05A9A0E0B6CFE0 EF292940E0292280 \ 95 5758D800E2706700 FB3557D8E83E1640 1377BF00EF840280 D82C5B80F7272A80 \ 96 978F1600FB774560 F5F86510FC101364 086635A0FB9C4E20 DF30FC40FB40EE28 \ 97 117FE0A0FA3438B0 3EE6B840FB5AC3F0 BC77A380FCB2F454 66D6DA80FF5F32B4 \ 98 CF13B980041275B0 431D6980097A8C00 C1BB60000EC74E00 5120B98012A2BAA0 \ 99 EEDF64C01754C060 820700001664B780 7FFFFFFF14453F40 800000001294E6E0 \ 100 499C1B000EB3B270 52B73E000DBCA020 EFB2B2E00F5FD880 CE3CDB400FBE1270 \ 101 E4B49CC00CEA2D90 6344A8800A5A7CA0 08C8FE800A1FFEE0 2BB986C00B0A0E00 \ 102 51486F800E44E190 8BCC6480113B0580 B6F4EC000EEB3630 441317800A5B48A0 \ 103 """) 104 105 106class AifcULAWTest(AifcTest, unittest.TestCase): 107 sndfilename = 'pluck-ulaw.aifc' 108 sndfilenframes = 3307 109 nchannels = 2 110 sampwidth = 2 111 framerate = 11025 112 nframes = 48 113 comptype = b'ulaw' 114 compname = b'' 115 frames = bytes.fromhex("""\ 116 022CFFE8 497C0104 307C04DC 8284083C CB84069C 497C03DC BE8401AC 036CFE74 \ 117 B684FA24 B684F344 2A7CEC04 19FCE704 EE04E504 C584E204 0E3CE104 EF04DF84 \ 118 557CE204 FB24E804 12FCEF04 D784F744 9684FB64 F5C4FC24 083CFBA4 DF84FB24 \ 119 11FCFA24 3E7CFB64 BA84FCB4 657CFF5C CF84041C 417C093C C1840EBC 517C12FC \ 120 EF0416FC 828415FC 7D7C13FC 828412FC 497C0EBC 517C0DBC F0040F3C CD840FFC \ 121 E5040CBC 617C0A3C 08BC0A3C 2C7C0B3C 517C0E3C 8A8410FC B6840EBC 457C0A3C \ 122 """) 123 if sys.byteorder != 'big': 124 frames = byteswap(frames, 2) 125 126 127class AifcALAWTest(AifcTest, unittest.TestCase): 128 sndfilename = 'pluck-alaw.aifc' 129 sndfilenframes = 3307 130 nchannels = 2 131 sampwidth = 2 132 framerate = 11025 133 nframes = 48 134 comptype = b'alaw' 135 compname = b'' 136 frames = bytes.fromhex("""\ 137 0230FFE8 4A0000F8 310004E0 82000840 CB0006A0 4A0003F0 BE0001A8 0370FE78 \ 138 BA00FA20 B600F340 2900EB80 1A80E680 ED80E480 C700E280 0E40E080 EF80E080 \ 139 5600E280 FB20E880 1380EF80 D900F740 9600FB60 F5C0FC10 0840FBA0 DF00FB20 \ 140 1180FA20 3F00FB60 BE00FCB0 6600FF58 CF000420 42000940 C1000EC0 52001280 \ 141 EE801780 82001680 7E001480 82001280 4A000EC0 52000DC0 EF800F40 CF000FC0 \ 142 E4800CC0 62000A40 08C00A40 2B000B40 52000E40 8A001180 B6000EC0 46000A40 \ 143 """) 144 if sys.byteorder != 'big': 145 frames = byteswap(frames, 2) 146 147 148class AifcMiscTest(unittest.TestCase): 149 def test_skipunknown(self): 150 #Issue 2245 151 #This file contains chunk types aifc doesn't recognize. 152 f = aifc.open(findfile('Sine-1000Hz-300ms.aif')) 153 f.close() 154 155 def test_close_opened_files_on_error(self): 156 non_aifc_file = findfile('pluck-pcm8.wav', subdir='audiodata') 157 with check_no_resource_warning(self): 158 with self.assertRaises(aifc.Error): 159 # Try opening a non-AIFC file, with the expectation that 160 # `aifc.open` will fail (without raising a ResourceWarning) 161 self.f = aifc.open(non_aifc_file, 'rb') 162 163 # Aifc_write.initfp() won't raise in normal case. But some errors 164 # (e.g. MemoryError, KeyboardInterrupt, etc..) can happen. 165 with mock.patch.object(aifc.Aifc_write, 'initfp', 166 side_effect=RuntimeError): 167 with self.assertRaises(RuntimeError): 168 self.fout = aifc.open(TESTFN, 'wb') 169 170 def test_params_added(self): 171 f = self.f = aifc.open(TESTFN, 'wb') 172 f.aiff() 173 f.setparams((1, 1, 1, 1, b'NONE', b'')) 174 f.close() 175 176 f = aifc.open(TESTFN, 'rb') 177 self.addCleanup(f.close) 178 params = f.getparams() 179 self.assertEqual(params.nchannels, f.getnchannels()) 180 self.assertEqual(params.sampwidth, f.getsampwidth()) 181 self.assertEqual(params.framerate, f.getframerate()) 182 self.assertEqual(params.nframes, f.getnframes()) 183 self.assertEqual(params.comptype, f.getcomptype()) 184 self.assertEqual(params.compname, f.getcompname()) 185 186 def test_write_header_comptype_sampwidth(self): 187 for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): 188 fout = aifc.open(io.BytesIO(), 'wb') 189 fout.setnchannels(1) 190 fout.setframerate(1) 191 fout.setcomptype(comptype, b'') 192 fout.close() 193 self.assertEqual(fout.getsampwidth(), 2) 194 fout.initfp(None) 195 196 def test_write_markers_values(self): 197 fout = aifc.open(io.BytesIO(), 'wb') 198 self.assertEqual(fout.getmarkers(), None) 199 fout.setmark(1, 0, b'foo1') 200 fout.setmark(1, 1, b'foo2') 201 self.assertEqual(fout.getmark(1), (1, 1, b'foo2')) 202 self.assertEqual(fout.getmarkers(), [(1, 1, b'foo2')]) 203 fout.initfp(None) 204 205 def test_read_markers(self): 206 fout = self.fout = aifc.open(TESTFN, 'wb') 207 fout.aiff() 208 fout.setparams((1, 1, 1, 1, b'NONE', b'')) 209 fout.setmark(1, 0, b'odd') 210 fout.setmark(2, 0, b'even') 211 fout.writeframes(b'\x00') 212 fout.close() 213 f = aifc.open(TESTFN, 'rb') 214 self.addCleanup(f.close) 215 self.assertEqual(f.getmarkers(), [(1, 0, b'odd'), (2, 0, b'even')]) 216 self.assertEqual(f.getmark(1), (1, 0, b'odd')) 217 self.assertEqual(f.getmark(2), (2, 0, b'even')) 218 self.assertRaises(aifc.Error, f.getmark, 3) 219 220 221class AIFCLowLevelTest(unittest.TestCase): 222 223 def test_read_written(self): 224 def read_written(self, what): 225 f = io.BytesIO() 226 getattr(aifc, '_write_' + what)(f, x) 227 f.seek(0) 228 return getattr(aifc, '_read_' + what)(f) 229 for x in (-1, 0, 0.1, 1): 230 self.assertEqual(read_written(x, 'float'), x) 231 for x in (float('NaN'), float('Inf')): 232 self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL) 233 for x in (b'', b'foo', b'a' * 255): 234 self.assertEqual(read_written(x, 'string'), x) 235 for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF): 236 self.assertEqual(read_written(x, 'long'), x) 237 for x in (0, 1, 0xFFFFFFFF): 238 self.assertEqual(read_written(x, 'ulong'), x) 239 for x in (-0x7FFF, -1, 0, 1, 0x7FFF): 240 self.assertEqual(read_written(x, 'short'), x) 241 for x in (0, 1, 0xFFFF): 242 self.assertEqual(read_written(x, 'ushort'), x) 243 244 def test_read_raises(self): 245 f = io.BytesIO(b'\x00') 246 self.assertRaises(EOFError, aifc._read_ulong, f) 247 self.assertRaises(EOFError, aifc._read_long, f) 248 self.assertRaises(EOFError, aifc._read_ushort, f) 249 self.assertRaises(EOFError, aifc._read_short, f) 250 251 def test_write_long_string_raises(self): 252 f = io.BytesIO() 253 with self.assertRaises(ValueError): 254 aifc._write_string(f, b'too long' * 255) 255 256 def test_wrong_open_mode(self): 257 with self.assertRaises(aifc.Error): 258 aifc.open(TESTFN, 'wrong_mode') 259 260 def test_read_wrong_form(self): 261 b1 = io.BytesIO(b'WRNG' + struct.pack('>L', 0)) 262 b2 = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'WRNG') 263 self.assertRaises(aifc.Error, aifc.open, b1) 264 self.assertRaises(aifc.Error, aifc.open, b2) 265 266 def test_read_no_comm_chunk(self): 267 b = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'AIFF') 268 self.assertRaises(aifc.Error, aifc.open, b) 269 270 def test_read_no_ssnd_chunk(self): 271 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 272 b += b'COMM' + struct.pack('>LhlhhLL', 38, 1, 0, 8, 273 0x4000 | 12, 11025<<18, 0) 274 b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00' 275 with self.assertRaisesRegex(aifc.Error, 'COMM chunk and/or SSND chunk' 276 ' missing'): 277 aifc.open(io.BytesIO(b)) 278 279 def test_read_wrong_compression_type(self): 280 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 281 b += b'COMM' + struct.pack('>LhlhhLL', 23, 1, 0, 8, 282 0x4000 | 12, 11025<<18, 0) 283 b += b'WRNG' + struct.pack('B', 0) 284 self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b)) 285 286 def test_read_wrong_number_of_channels(self): 287 for nchannels in 0, -1: 288 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 289 b += b'COMM' + struct.pack('>LhlhhLL', 38, nchannels, 0, 8, 290 0x4000 | 12, 11025<<18, 0) 291 b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00' 292 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 293 with self.assertRaisesRegex(aifc.Error, 'bad # of channels'): 294 aifc.open(io.BytesIO(b)) 295 296 def test_read_wrong_sample_width(self): 297 for sampwidth in 0, -1: 298 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 299 b += b'COMM' + struct.pack('>LhlhhLL', 38, 1, 0, sampwidth, 300 0x4000 | 12, 11025<<18, 0) 301 b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00' 302 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 303 with self.assertRaisesRegex(aifc.Error, 'bad sample width'): 304 aifc.open(io.BytesIO(b)) 305 306 def test_read_wrong_marks(self): 307 b = b'FORM' + struct.pack('>L', 4) + b'AIFF' 308 b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8, 309 0x4000 | 12, 11025<<18, 0) 310 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 311 b += b'MARK' + struct.pack('>LhB', 3, 1, 1) 312 with self.assertWarns(UserWarning) as cm: 313 f = aifc.open(io.BytesIO(b)) 314 self.assertEqual(str(cm.warning), 'Warning: MARK chunk contains ' 315 'only 0 markers instead of 1') 316 self.assertEqual(f.getmarkers(), None) 317 318 def test_read_comm_kludge_compname_even(self): 319 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 320 b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8, 321 0x4000 | 12, 11025<<18, 0) 322 b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00' 323 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 324 with self.assertWarns(UserWarning) as cm: 325 f = aifc.open(io.BytesIO(b)) 326 self.assertEqual(str(cm.warning), 'Warning: bad COMM chunk size') 327 self.assertEqual(f.getcompname(), b'even') 328 329 def test_read_comm_kludge_compname_odd(self): 330 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 331 b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8, 332 0x4000 | 12, 11025<<18, 0) 333 b += b'NONE' + struct.pack('B', 3) + b'odd' 334 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 335 with self.assertWarns(UserWarning) as cm: 336 f = aifc.open(io.BytesIO(b)) 337 self.assertEqual(str(cm.warning), 'Warning: bad COMM chunk size') 338 self.assertEqual(f.getcompname(), b'odd') 339 340 def test_write_params_raises(self): 341 fout = aifc.open(io.BytesIO(), 'wb') 342 wrong_params = (0, 0, 0, 0, b'WRNG', '') 343 self.assertRaises(aifc.Error, fout.setparams, wrong_params) 344 self.assertRaises(aifc.Error, fout.getparams) 345 self.assertRaises(aifc.Error, fout.setnchannels, 0) 346 self.assertRaises(aifc.Error, fout.getnchannels) 347 self.assertRaises(aifc.Error, fout.setsampwidth, 0) 348 self.assertRaises(aifc.Error, fout.getsampwidth) 349 self.assertRaises(aifc.Error, fout.setframerate, 0) 350 self.assertRaises(aifc.Error, fout.getframerate) 351 self.assertRaises(aifc.Error, fout.setcomptype, b'WRNG', '') 352 fout.aiff() 353 fout.setnchannels(1) 354 fout.setsampwidth(1) 355 fout.setframerate(1) 356 fout.setnframes(1) 357 fout.writeframes(b'\x00') 358 self.assertRaises(aifc.Error, fout.setparams, (1, 1, 1, 1, 1, 1)) 359 self.assertRaises(aifc.Error, fout.setnchannels, 1) 360 self.assertRaises(aifc.Error, fout.setsampwidth, 1) 361 self.assertRaises(aifc.Error, fout.setframerate, 1) 362 self.assertRaises(aifc.Error, fout.setnframes, 1) 363 self.assertRaises(aifc.Error, fout.setcomptype, b'NONE', '') 364 self.assertRaises(aifc.Error, fout.aiff) 365 self.assertRaises(aifc.Error, fout.aifc) 366 367 def test_write_params_singles(self): 368 fout = aifc.open(io.BytesIO(), 'wb') 369 fout.aifc() 370 fout.setnchannels(1) 371 fout.setsampwidth(2) 372 fout.setframerate(3) 373 fout.setnframes(4) 374 fout.setcomptype(b'NONE', b'name') 375 self.assertEqual(fout.getnchannels(), 1) 376 self.assertEqual(fout.getsampwidth(), 2) 377 self.assertEqual(fout.getframerate(), 3) 378 self.assertEqual(fout.getnframes(), 0) 379 self.assertEqual(fout.tell(), 0) 380 self.assertEqual(fout.getcomptype(), b'NONE') 381 self.assertEqual(fout.getcompname(), b'name') 382 fout.writeframes(b'\x00' * 4 * fout.getsampwidth() * fout.getnchannels()) 383 self.assertEqual(fout.getnframes(), 4) 384 self.assertEqual(fout.tell(), 4) 385 386 def test_write_params_bunch(self): 387 fout = aifc.open(io.BytesIO(), 'wb') 388 fout.aifc() 389 p = (1, 2, 3, 4, b'NONE', b'name') 390 fout.setparams(p) 391 self.assertEqual(fout.getparams(), p) 392 fout.initfp(None) 393 394 def test_write_header_raises(self): 395 fout = aifc.open(io.BytesIO(), 'wb') 396 self.assertRaises(aifc.Error, fout.close) 397 fout = aifc.open(io.BytesIO(), 'wb') 398 fout.setnchannels(1) 399 self.assertRaises(aifc.Error, fout.close) 400 fout = aifc.open(io.BytesIO(), 'wb') 401 fout.setnchannels(1) 402 fout.setsampwidth(1) 403 self.assertRaises(aifc.Error, fout.close) 404 405 def test_write_header_comptype_raises(self): 406 for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): 407 fout = aifc.open(io.BytesIO(), 'wb') 408 fout.setsampwidth(1) 409 fout.setcomptype(comptype, b'') 410 self.assertRaises(aifc.Error, fout.close) 411 fout.initfp(None) 412 413 def test_write_markers_raises(self): 414 fout = aifc.open(io.BytesIO(), 'wb') 415 self.assertRaises(aifc.Error, fout.setmark, 0, 0, b'') 416 self.assertRaises(aifc.Error, fout.setmark, 1, -1, b'') 417 self.assertRaises(aifc.Error, fout.setmark, 1, 0, None) 418 self.assertRaises(aifc.Error, fout.getmark, 1) 419 fout.initfp(None) 420 421 def test_write_aiff_by_extension(self): 422 sampwidth = 2 423 filename = TESTFN + '.aiff' 424 fout = self.fout = aifc.open(filename, 'wb') 425 self.addCleanup(unlink, filename) 426 fout.setparams((1, sampwidth, 1, 1, b'ULAW', b'')) 427 frames = b'\x00' * fout.getnchannels() * sampwidth 428 fout.writeframes(frames) 429 fout.close() 430 f = self.f = aifc.open(filename, 'rb') 431 self.assertEqual(f.getcomptype(), b'NONE') 432 f.close() 433 434 435if __name__ == "__main__": 436 unittest.main() 437