Anthony Zhang | 03f68b6 | 2017-02-22 02:23:30 -0500 | [diff] [blame] | 1 | from test.support import check_no_resource_warning, findfile, TESTFN, unlink |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 2 | import unittest |
INADA Naoki | 5dc33ee | 2017-02-26 21:11:58 +0900 | [diff] [blame] | 3 | from unittest import mock |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 4 | from test import audiotests |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 5 | from audioop import byteswap |
Sandro Tosi | bdd5354 | 2012-01-01 18:04:37 +0100 | [diff] [blame] | 6 | import io |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 7 | import sys |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 8 | import struct |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 9 | import aifc |
Brian Curtin | 9f914a0 | 2017-11-10 11:38:25 -0500 | [diff] [blame] | 10 | import warnings |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 11 | |
| 12 | |
Serhiy Storchaka | 7714ebb | 2013-11-16 13:04:00 +0200 | [diff] [blame] | 13 | class AifcTest(audiotests.AudioWriteTests, |
| 14 | audiotests.AudioTestsWithSourceFile): |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 15 | module = aifc |
Serhiy Storchaka | 7714ebb | 2013-11-16 13:04:00 +0200 | [diff] [blame] | 16 | close_fd = True |
| 17 | test_unseekable_read = None |
Serhiy Storchaka | 7714ebb | 2013-11-16 13:04:00 +0200 | [diff] [blame] | 18 | |
| 19 | |
| 20 | class AifcPCM8Test(AifcTest, unittest.TestCase): |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 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 | """) |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 35 | |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 36 | |
Serhiy Storchaka | 7714ebb | 2013-11-16 13:04:00 +0200 | [diff] [blame] | 37 | class AifcPCM16Test(AifcTest, unittest.TestCase): |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 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 | """) |
R. David Murray | eb01a6c | 2009-04-29 20:40:42 +0000 | [diff] [blame] | 54 | |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 55 | |
Serhiy Storchaka | 7714ebb | 2013-11-16 13:04:00 +0200 | [diff] [blame] | 56 | class AifcPCM24Test(AifcTest, unittest.TestCase): |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 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 | """) |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 79 | |
| 80 | |
Serhiy Storchaka | 7714ebb | 2013-11-16 13:04:00 +0200 | [diff] [blame] | 81 | class AifcPCM32Test(AifcTest, unittest.TestCase): |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 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 | """) |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 104 | |
| 105 | |
Serhiy Storchaka | 7714ebb | 2013-11-16 13:04:00 +0200 | [diff] [blame] | 106 | class AifcULAWTest(AifcTest, unittest.TestCase): |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 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': |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 124 | frames = byteswap(frames, 2) |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 125 | |
| 126 | |
Serhiy Storchaka | 7714ebb | 2013-11-16 13:04:00 +0200 | [diff] [blame] | 127 | class AifcALAWTest(AifcTest, unittest.TestCase): |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 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': |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 145 | frames = byteswap(frames, 2) |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 146 | |
| 147 | |
Brian Curtin | 9f914a0 | 2017-11-10 11:38:25 -0500 | [diff] [blame] | 148 | class AifcMiscTest(audiotests.AudioMiscTests, unittest.TestCase): |
| 149 | module = aifc |
| 150 | |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 151 | def test_skipunknown(self): |
| 152 | #Issue 2245 |
| 153 | #This file contains chunk types aifc doesn't recognize. |
Serhiy Storchaka | 1b80e63 | 2013-10-13 17:55:07 +0300 | [diff] [blame] | 154 | self.f = aifc.open(findfile('Sine-1000Hz-300ms.aif')) |
R David Murray | 4d35e75 | 2013-07-25 16:12:01 -0400 | [diff] [blame] | 155 | |
Anthony Zhang | 03f68b6 | 2017-02-22 02:23:30 -0500 | [diff] [blame] | 156 | def test_close_opened_files_on_error(self): |
| 157 | non_aifc_file = findfile('pluck-pcm8.wav', subdir='audiodata') |
| 158 | with check_no_resource_warning(self): |
| 159 | with self.assertRaises(aifc.Error): |
| 160 | # Try opening a non-AIFC file, with the expectation that |
| 161 | # `aifc.open` will fail (without raising a ResourceWarning) |
INADA Naoki | 5dc33ee | 2017-02-26 21:11:58 +0900 | [diff] [blame] | 162 | self.f = aifc.open(non_aifc_file, 'rb') |
| 163 | |
| 164 | # Aifc_write.initfp() won't raise in normal case. But some errors |
| 165 | # (e.g. MemoryError, KeyboardInterrupt, etc..) can happen. |
| 166 | with mock.patch.object(aifc.Aifc_write, 'initfp', |
| 167 | side_effect=RuntimeError): |
| 168 | with self.assertRaises(RuntimeError): |
| 169 | self.fout = aifc.open(TESTFN, 'wb') |
Anthony Zhang | 03f68b6 | 2017-02-22 02:23:30 -0500 | [diff] [blame] | 170 | |
R David Murray | 4d35e75 | 2013-07-25 16:12:01 -0400 | [diff] [blame] | 171 | def test_params_added(self): |
| 172 | f = self.f = aifc.open(TESTFN, 'wb') |
| 173 | f.aiff() |
| 174 | f.setparams((1, 1, 1, 1, b'NONE', b'')) |
| 175 | f.close() |
| 176 | |
| 177 | f = self.f = aifc.open(TESTFN, 'rb') |
| 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 | |
Sandro Tosi | bdd5354 | 2012-01-01 18:04:37 +0100 | [diff] [blame] | 186 | def test_write_header_comptype_sampwidth(self): |
| 187 | for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 188 | fout = aifc.open(io.BytesIO(), 'wb') |
Sandro Tosi | bdd5354 | 2012-01-01 18:04:37 +0100 | [diff] [blame] | 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 | |
Sandro Tosi | 70efbef | 2012-01-01 22:53:08 +0100 | [diff] [blame] | 196 | def test_write_markers_values(self): |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 197 | fout = aifc.open(io.BytesIO(), 'wb') |
Sandro Tosi | 70efbef | 2012-01-01 22:53:08 +0100 | [diff] [blame] | 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 = self.f = aifc.open(TESTFN, 'rb') |
| 214 | self.assertEqual(f.getmarkers(), [(1, 0, b'odd'), (2, 0, b'even')]) |
| 215 | self.assertEqual(f.getmark(1), (1, 0, b'odd')) |
| 216 | self.assertEqual(f.getmark(2), (2, 0, b'even')) |
| 217 | self.assertRaises(aifc.Error, f.getmark, 3) |
| 218 | |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | 03757ec | 2012-01-17 17:13:04 +0100 | [diff] [blame] | 220 | class AIFCLowLevelTest(unittest.TestCase): |
| 221 | |
| 222 | def test_read_written(self): |
| 223 | def read_written(self, what): |
| 224 | f = io.BytesIO() |
| 225 | getattr(aifc, '_write_' + what)(f, x) |
| 226 | f.seek(0) |
| 227 | return getattr(aifc, '_read_' + what)(f) |
| 228 | for x in (-1, 0, 0.1, 1): |
| 229 | self.assertEqual(read_written(x, 'float'), x) |
| 230 | for x in (float('NaN'), float('Inf')): |
| 231 | self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL) |
| 232 | for x in (b'', b'foo', b'a' * 255): |
| 233 | self.assertEqual(read_written(x, 'string'), x) |
| 234 | for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF): |
| 235 | self.assertEqual(read_written(x, 'long'), x) |
| 236 | for x in (0, 1, 0xFFFFFFFF): |
| 237 | self.assertEqual(read_written(x, 'ulong'), x) |
| 238 | for x in (-0x7FFF, -1, 0, 1, 0x7FFF): |
| 239 | self.assertEqual(read_written(x, 'short'), x) |
| 240 | for x in (0, 1, 0xFFFF): |
| 241 | self.assertEqual(read_written(x, 'ushort'), x) |
| 242 | |
| 243 | def test_read_raises(self): |
| 244 | f = io.BytesIO(b'\x00') |
| 245 | self.assertRaises(EOFError, aifc._read_ulong, f) |
| 246 | self.assertRaises(EOFError, aifc._read_long, f) |
| 247 | self.assertRaises(EOFError, aifc._read_ushort, f) |
| 248 | self.assertRaises(EOFError, aifc._read_short, f) |
| 249 | |
| 250 | def test_write_long_string_raises(self): |
| 251 | f = io.BytesIO() |
| 252 | with self.assertRaises(ValueError): |
| 253 | aifc._write_string(f, b'too long' * 255) |
| 254 | |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 255 | def test_wrong_open_mode(self): |
| 256 | with self.assertRaises(aifc.Error): |
| 257 | aifc.open(TESTFN, 'wrong_mode') |
| 258 | |
| 259 | def test_read_wrong_form(self): |
| 260 | b1 = io.BytesIO(b'WRNG' + struct.pack('>L', 0)) |
| 261 | b2 = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'WRNG') |
| 262 | self.assertRaises(aifc.Error, aifc.open, b1) |
| 263 | self.assertRaises(aifc.Error, aifc.open, b2) |
| 264 | |
| 265 | def test_read_no_comm_chunk(self): |
| 266 | b = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'AIFF') |
| 267 | self.assertRaises(aifc.Error, aifc.open, b) |
| 268 | |
Zackery Spytz | 80d20b9 | 2018-02-20 14:06:11 -0700 | [diff] [blame] | 269 | def test_read_no_ssnd_chunk(self): |
| 270 | b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 271 | b += b'COMM' + struct.pack('>LhlhhLL', 38, 0, 0, 0, 0, 0, 0) |
| 272 | b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00' |
| 273 | with self.assertRaisesRegex(aifc.Error, 'COMM chunk and/or SSND chunk' |
| 274 | ' missing'): |
| 275 | aifc.open(io.BytesIO(b)) |
| 276 | |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 277 | def test_read_wrong_compression_type(self): |
| 278 | b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 279 | b += b'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0) |
| 280 | b += b'WRNG' + struct.pack('B', 0) |
| 281 | self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b)) |
| 282 | |
| 283 | def test_read_wrong_marks(self): |
| 284 | b = b'FORM' + struct.pack('>L', 4) + b'AIFF' |
| 285 | b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) |
| 286 | b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
| 287 | b += b'MARK' + struct.pack('>LhB', 3, 1, 1) |
Serhiy Storchaka | 4606d36 | 2013-10-14 20:05:33 +0300 | [diff] [blame] | 288 | with self.assertWarns(UserWarning) as cm: |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 289 | f = aifc.open(io.BytesIO(b)) |
Serhiy Storchaka | 4606d36 | 2013-10-14 20:05:33 +0300 | [diff] [blame] | 290 | self.assertEqual(str(cm.warning), 'Warning: MARK chunk contains ' |
| 291 | 'only 0 markers instead of 1') |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 292 | self.assertEqual(f.getmarkers(), None) |
| 293 | |
| 294 | def test_read_comm_kludge_compname_even(self): |
| 295 | b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 296 | b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) |
| 297 | b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00' |
| 298 | b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
Serhiy Storchaka | 4606d36 | 2013-10-14 20:05:33 +0300 | [diff] [blame] | 299 | with self.assertWarns(UserWarning) as cm: |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 300 | f = aifc.open(io.BytesIO(b)) |
Serhiy Storchaka | 4606d36 | 2013-10-14 20:05:33 +0300 | [diff] [blame] | 301 | self.assertEqual(str(cm.warning), 'Warning: bad COMM chunk size') |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 302 | self.assertEqual(f.getcompname(), b'even') |
| 303 | |
| 304 | def test_read_comm_kludge_compname_odd(self): |
| 305 | b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 306 | b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) |
| 307 | b += b'NONE' + struct.pack('B', 3) + b'odd' |
| 308 | b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
Serhiy Storchaka | 4606d36 | 2013-10-14 20:05:33 +0300 | [diff] [blame] | 309 | with self.assertWarns(UserWarning) as cm: |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 310 | f = aifc.open(io.BytesIO(b)) |
Serhiy Storchaka | 4606d36 | 2013-10-14 20:05:33 +0300 | [diff] [blame] | 311 | self.assertEqual(str(cm.warning), 'Warning: bad COMM chunk size') |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 312 | self.assertEqual(f.getcompname(), b'odd') |
| 313 | |
| 314 | def test_write_params_raises(self): |
| 315 | fout = aifc.open(io.BytesIO(), 'wb') |
| 316 | wrong_params = (0, 0, 0, 0, b'WRNG', '') |
| 317 | self.assertRaises(aifc.Error, fout.setparams, wrong_params) |
| 318 | self.assertRaises(aifc.Error, fout.getparams) |
| 319 | self.assertRaises(aifc.Error, fout.setnchannels, 0) |
| 320 | self.assertRaises(aifc.Error, fout.getnchannels) |
| 321 | self.assertRaises(aifc.Error, fout.setsampwidth, 0) |
| 322 | self.assertRaises(aifc.Error, fout.getsampwidth) |
| 323 | self.assertRaises(aifc.Error, fout.setframerate, 0) |
| 324 | self.assertRaises(aifc.Error, fout.getframerate) |
| 325 | self.assertRaises(aifc.Error, fout.setcomptype, b'WRNG', '') |
| 326 | fout.aiff() |
| 327 | fout.setnchannels(1) |
| 328 | fout.setsampwidth(1) |
| 329 | fout.setframerate(1) |
| 330 | fout.setnframes(1) |
| 331 | fout.writeframes(b'\x00') |
| 332 | self.assertRaises(aifc.Error, fout.setparams, (1, 1, 1, 1, 1, 1)) |
| 333 | self.assertRaises(aifc.Error, fout.setnchannels, 1) |
| 334 | self.assertRaises(aifc.Error, fout.setsampwidth, 1) |
| 335 | self.assertRaises(aifc.Error, fout.setframerate, 1) |
| 336 | self.assertRaises(aifc.Error, fout.setnframes, 1) |
| 337 | self.assertRaises(aifc.Error, fout.setcomptype, b'NONE', '') |
| 338 | self.assertRaises(aifc.Error, fout.aiff) |
| 339 | self.assertRaises(aifc.Error, fout.aifc) |
| 340 | |
| 341 | def test_write_params_singles(self): |
| 342 | fout = aifc.open(io.BytesIO(), 'wb') |
| 343 | fout.aifc() |
| 344 | fout.setnchannels(1) |
| 345 | fout.setsampwidth(2) |
| 346 | fout.setframerate(3) |
| 347 | fout.setnframes(4) |
| 348 | fout.setcomptype(b'NONE', b'name') |
| 349 | self.assertEqual(fout.getnchannels(), 1) |
| 350 | self.assertEqual(fout.getsampwidth(), 2) |
| 351 | self.assertEqual(fout.getframerate(), 3) |
| 352 | self.assertEqual(fout.getnframes(), 0) |
| 353 | self.assertEqual(fout.tell(), 0) |
| 354 | self.assertEqual(fout.getcomptype(), b'NONE') |
| 355 | self.assertEqual(fout.getcompname(), b'name') |
| 356 | fout.writeframes(b'\x00' * 4 * fout.getsampwidth() * fout.getnchannels()) |
| 357 | self.assertEqual(fout.getnframes(), 4) |
| 358 | self.assertEqual(fout.tell(), 4) |
| 359 | |
| 360 | def test_write_params_bunch(self): |
| 361 | fout = aifc.open(io.BytesIO(), 'wb') |
| 362 | fout.aifc() |
| 363 | p = (1, 2, 3, 4, b'NONE', b'name') |
| 364 | fout.setparams(p) |
| 365 | self.assertEqual(fout.getparams(), p) |
| 366 | fout.initfp(None) |
| 367 | |
| 368 | def test_write_header_raises(self): |
| 369 | fout = aifc.open(io.BytesIO(), 'wb') |
| 370 | self.assertRaises(aifc.Error, fout.close) |
Serhiy Storchaka | 051722d | 2012-12-29 22:30:56 +0200 | [diff] [blame] | 371 | fout = aifc.open(io.BytesIO(), 'wb') |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 372 | fout.setnchannels(1) |
| 373 | self.assertRaises(aifc.Error, fout.close) |
Serhiy Storchaka | 051722d | 2012-12-29 22:30:56 +0200 | [diff] [blame] | 374 | fout = aifc.open(io.BytesIO(), 'wb') |
| 375 | fout.setnchannels(1) |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 376 | fout.setsampwidth(1) |
| 377 | self.assertRaises(aifc.Error, fout.close) |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 378 | |
| 379 | def test_write_header_comptype_raises(self): |
| 380 | for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): |
| 381 | fout = aifc.open(io.BytesIO(), 'wb') |
| 382 | fout.setsampwidth(1) |
| 383 | fout.setcomptype(comptype, b'') |
| 384 | self.assertRaises(aifc.Error, fout.close) |
| 385 | fout.initfp(None) |
| 386 | |
| 387 | def test_write_markers_raises(self): |
| 388 | fout = aifc.open(io.BytesIO(), 'wb') |
| 389 | self.assertRaises(aifc.Error, fout.setmark, 0, 0, b'') |
| 390 | self.assertRaises(aifc.Error, fout.setmark, 1, -1, b'') |
| 391 | self.assertRaises(aifc.Error, fout.setmark, 1, 0, None) |
| 392 | self.assertRaises(aifc.Error, fout.getmark, 1) |
| 393 | fout.initfp(None) |
| 394 | |
| 395 | def test_write_aiff_by_extension(self): |
| 396 | sampwidth = 2 |
Ezio Melotti | 4b843a7 | 2013-03-01 21:10:26 +0200 | [diff] [blame] | 397 | filename = TESTFN + '.aiff' |
| 398 | fout = self.fout = aifc.open(filename, 'wb') |
| 399 | self.addCleanup(unlink, filename) |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 400 | fout.setparams((1, sampwidth, 1, 1, b'ULAW', b'')) |
| 401 | frames = b'\x00' * fout.getnchannels() * sampwidth |
| 402 | fout.writeframes(frames) |
| 403 | fout.close() |
Ezio Melotti | 4b843a7 | 2013-03-01 21:10:26 +0200 | [diff] [blame] | 404 | f = self.f = aifc.open(filename, 'rb') |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 405 | self.assertEqual(f.getcomptype(), b'NONE') |
| 406 | f.close() |
| 407 | |
Antoine Pitrou | 03757ec | 2012-01-17 17:13:04 +0100 | [diff] [blame] | 408 | |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 409 | if __name__ == "__main__": |
| 410 | unittest.main() |