| 1 | n/a | """Stuff to parse AIFF-C and AIFF files. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Unless explicitly stated otherwise, the description below is true |
|---|
| 4 | n/a | both for AIFF-C files and AIFF files. |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | An AIFF-C file has the following structure. |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | +-----------------+ |
|---|
| 9 | n/a | | FORM | |
|---|
| 10 | n/a | +-----------------+ |
|---|
| 11 | n/a | | <size> | |
|---|
| 12 | n/a | +----+------------+ |
|---|
| 13 | n/a | | | AIFC | |
|---|
| 14 | n/a | | +------------+ |
|---|
| 15 | n/a | | | <chunks> | |
|---|
| 16 | n/a | | | . | |
|---|
| 17 | n/a | | | . | |
|---|
| 18 | n/a | | | . | |
|---|
| 19 | n/a | +----+------------+ |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | An AIFF file has the string "AIFF" instead of "AIFC". |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | A chunk consists of an identifier (4 bytes) followed by a size (4 bytes, |
|---|
| 24 | n/a | big endian order), followed by the data. The size field does not include |
|---|
| 25 | n/a | the size of the 8 byte header. |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | The following chunk types are recognized. |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | FVER |
|---|
| 30 | n/a | <version number of AIFF-C defining document> (AIFF-C only). |
|---|
| 31 | n/a | MARK |
|---|
| 32 | n/a | <# of markers> (2 bytes) |
|---|
| 33 | n/a | list of markers: |
|---|
| 34 | n/a | <marker ID> (2 bytes, must be > 0) |
|---|
| 35 | n/a | <position> (4 bytes) |
|---|
| 36 | n/a | <marker name> ("pstring") |
|---|
| 37 | n/a | COMM |
|---|
| 38 | n/a | <# of channels> (2 bytes) |
|---|
| 39 | n/a | <# of sound frames> (4 bytes) |
|---|
| 40 | n/a | <size of the samples> (2 bytes) |
|---|
| 41 | n/a | <sampling frequency> (10 bytes, IEEE 80-bit extended |
|---|
| 42 | n/a | floating point) |
|---|
| 43 | n/a | in AIFF-C files only: |
|---|
| 44 | n/a | <compression type> (4 bytes) |
|---|
| 45 | n/a | <human-readable version of compression type> ("pstring") |
|---|
| 46 | n/a | SSND |
|---|
| 47 | n/a | <offset> (4 bytes, not used by this program) |
|---|
| 48 | n/a | <blocksize> (4 bytes, not used by this program) |
|---|
| 49 | n/a | <sound data> |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | A pstring consists of 1 byte length, a string of characters, and 0 or 1 |
|---|
| 52 | n/a | byte pad to make the total length even. |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | Usage. |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | Reading AIFF files: |
|---|
| 57 | n/a | f = aifc.open(file, 'r') |
|---|
| 58 | n/a | where file is either the name of a file or an open file pointer. |
|---|
| 59 | n/a | The open file pointer must have methods read(), seek(), and close(). |
|---|
| 60 | n/a | In some types of audio files, if the setpos() method is not used, |
|---|
| 61 | n/a | the seek() method is not necessary. |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | This returns an instance of a class with the following public methods: |
|---|
| 64 | n/a | getnchannels() -- returns number of audio channels (1 for |
|---|
| 65 | n/a | mono, 2 for stereo) |
|---|
| 66 | n/a | getsampwidth() -- returns sample width in bytes |
|---|
| 67 | n/a | getframerate() -- returns sampling frequency |
|---|
| 68 | n/a | getnframes() -- returns number of audio frames |
|---|
| 69 | n/a | getcomptype() -- returns compression type ('NONE' for AIFF files) |
|---|
| 70 | n/a | getcompname() -- returns human-readable version of |
|---|
| 71 | n/a | compression type ('not compressed' for AIFF files) |
|---|
| 72 | n/a | getparams() -- returns a namedtuple consisting of all of the |
|---|
| 73 | n/a | above in the above order |
|---|
| 74 | n/a | getmarkers() -- get the list of marks in the audio file or None |
|---|
| 75 | n/a | if there are no marks |
|---|
| 76 | n/a | getmark(id) -- get mark with the specified id (raises an error |
|---|
| 77 | n/a | if the mark does not exist) |
|---|
| 78 | n/a | readframes(n) -- returns at most n frames of audio |
|---|
| 79 | n/a | rewind() -- rewind to the beginning of the audio stream |
|---|
| 80 | n/a | setpos(pos) -- seek to the specified position |
|---|
| 81 | n/a | tell() -- return the current position |
|---|
| 82 | n/a | close() -- close the instance (make it unusable) |
|---|
| 83 | n/a | The position returned by tell(), the position given to setpos() and |
|---|
| 84 | n/a | the position of marks are all compatible and have nothing to do with |
|---|
| 85 | n/a | the actual position in the file. |
|---|
| 86 | n/a | The close() method is called automatically when the class instance |
|---|
| 87 | n/a | is destroyed. |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | Writing AIFF files: |
|---|
| 90 | n/a | f = aifc.open(file, 'w') |
|---|
| 91 | n/a | where file is either the name of a file or an open file pointer. |
|---|
| 92 | n/a | The open file pointer must have methods write(), tell(), seek(), and |
|---|
| 93 | n/a | close(). |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | This returns an instance of a class with the following public methods: |
|---|
| 96 | n/a | aiff() -- create an AIFF file (AIFF-C default) |
|---|
| 97 | n/a | aifc() -- create an AIFF-C file |
|---|
| 98 | n/a | setnchannels(n) -- set the number of channels |
|---|
| 99 | n/a | setsampwidth(n) -- set the sample width |
|---|
| 100 | n/a | setframerate(n) -- set the frame rate |
|---|
| 101 | n/a | setnframes(n) -- set the number of frames |
|---|
| 102 | n/a | setcomptype(type, name) |
|---|
| 103 | n/a | -- set the compression type and the |
|---|
| 104 | n/a | human-readable compression type |
|---|
| 105 | n/a | setparams(tuple) |
|---|
| 106 | n/a | -- set all parameters at once |
|---|
| 107 | n/a | setmark(id, pos, name) |
|---|
| 108 | n/a | -- add specified mark to the list of marks |
|---|
| 109 | n/a | tell() -- return current position in output file (useful |
|---|
| 110 | n/a | in combination with setmark()) |
|---|
| 111 | n/a | writeframesraw(data) |
|---|
| 112 | n/a | -- write audio frames without pathing up the |
|---|
| 113 | n/a | file header |
|---|
| 114 | n/a | writeframes(data) |
|---|
| 115 | n/a | -- write audio frames and patch up the file header |
|---|
| 116 | n/a | close() -- patch up the file header and close the |
|---|
| 117 | n/a | output file |
|---|
| 118 | n/a | You should set the parameters before the first writeframesraw or |
|---|
| 119 | n/a | writeframes. The total number of frames does not need to be set, |
|---|
| 120 | n/a | but when it is set to the correct value, the header does not have to |
|---|
| 121 | n/a | be patched up. |
|---|
| 122 | n/a | It is best to first set all parameters, perhaps possibly the |
|---|
| 123 | n/a | compression type, and then write audio frames using writeframesraw. |
|---|
| 124 | n/a | When all frames have been written, either call writeframes(b'') or |
|---|
| 125 | n/a | close() to patch up the sizes in the header. |
|---|
| 126 | n/a | Marks can be added anytime. If there are any marks, you must call |
|---|
| 127 | n/a | close() after all frames have been written. |
|---|
| 128 | n/a | The close() method is called automatically when the class instance |
|---|
| 129 | n/a | is destroyed. |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | When a file is opened with the extension '.aiff', an AIFF file is |
|---|
| 132 | n/a | written, otherwise an AIFF-C file is written. This default can be |
|---|
| 133 | n/a | changed by calling aiff() or aifc() before the first writeframes or |
|---|
| 134 | n/a | writeframesraw. |
|---|
| 135 | n/a | """ |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | import struct |
|---|
| 138 | n/a | import builtins |
|---|
| 139 | n/a | import warnings |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | __all__ = ["Error", "open", "openfp"] |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | class Error(Exception): |
|---|
| 144 | n/a | pass |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | _AIFC_version = 0xA2805140 # Version 1 of AIFF-C |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | def _read_long(file): |
|---|
| 149 | n/a | try: |
|---|
| 150 | n/a | return struct.unpack('>l', file.read(4))[0] |
|---|
| 151 | n/a | except struct.error: |
|---|
| 152 | n/a | raise EOFError |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | def _read_ulong(file): |
|---|
| 155 | n/a | try: |
|---|
| 156 | n/a | return struct.unpack('>L', file.read(4))[0] |
|---|
| 157 | n/a | except struct.error: |
|---|
| 158 | n/a | raise EOFError |
|---|
| 159 | n/a | |
|---|
| 160 | n/a | def _read_short(file): |
|---|
| 161 | n/a | try: |
|---|
| 162 | n/a | return struct.unpack('>h', file.read(2))[0] |
|---|
| 163 | n/a | except struct.error: |
|---|
| 164 | n/a | raise EOFError |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | def _read_ushort(file): |
|---|
| 167 | n/a | try: |
|---|
| 168 | n/a | return struct.unpack('>H', file.read(2))[0] |
|---|
| 169 | n/a | except struct.error: |
|---|
| 170 | n/a | raise EOFError |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | def _read_string(file): |
|---|
| 173 | n/a | length = ord(file.read(1)) |
|---|
| 174 | n/a | if length == 0: |
|---|
| 175 | n/a | data = b'' |
|---|
| 176 | n/a | else: |
|---|
| 177 | n/a | data = file.read(length) |
|---|
| 178 | n/a | if length & 1 == 0: |
|---|
| 179 | n/a | dummy = file.read(1) |
|---|
| 180 | n/a | return data |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | _HUGE_VAL = 1.79769313486231e+308 # See <limits.h> |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | def _read_float(f): # 10 bytes |
|---|
| 185 | n/a | expon = _read_short(f) # 2 bytes |
|---|
| 186 | n/a | sign = 1 |
|---|
| 187 | n/a | if expon < 0: |
|---|
| 188 | n/a | sign = -1 |
|---|
| 189 | n/a | expon = expon + 0x8000 |
|---|
| 190 | n/a | himant = _read_ulong(f) # 4 bytes |
|---|
| 191 | n/a | lomant = _read_ulong(f) # 4 bytes |
|---|
| 192 | n/a | if expon == himant == lomant == 0: |
|---|
| 193 | n/a | f = 0.0 |
|---|
| 194 | n/a | elif expon == 0x7FFF: |
|---|
| 195 | n/a | f = _HUGE_VAL |
|---|
| 196 | n/a | else: |
|---|
| 197 | n/a | expon = expon - 16383 |
|---|
| 198 | n/a | f = (himant * 0x100000000 + lomant) * pow(2.0, expon - 63) |
|---|
| 199 | n/a | return sign * f |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | def _write_short(f, x): |
|---|
| 202 | n/a | f.write(struct.pack('>h', x)) |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | def _write_ushort(f, x): |
|---|
| 205 | n/a | f.write(struct.pack('>H', x)) |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | def _write_long(f, x): |
|---|
| 208 | n/a | f.write(struct.pack('>l', x)) |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | def _write_ulong(f, x): |
|---|
| 211 | n/a | f.write(struct.pack('>L', x)) |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | def _write_string(f, s): |
|---|
| 214 | n/a | if len(s) > 255: |
|---|
| 215 | n/a | raise ValueError("string exceeds maximum pstring length") |
|---|
| 216 | n/a | f.write(struct.pack('B', len(s))) |
|---|
| 217 | n/a | f.write(s) |
|---|
| 218 | n/a | if len(s) & 1 == 0: |
|---|
| 219 | n/a | f.write(b'\x00') |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | def _write_float(f, x): |
|---|
| 222 | n/a | import math |
|---|
| 223 | n/a | if x < 0: |
|---|
| 224 | n/a | sign = 0x8000 |
|---|
| 225 | n/a | x = x * -1 |
|---|
| 226 | n/a | else: |
|---|
| 227 | n/a | sign = 0 |
|---|
| 228 | n/a | if x == 0: |
|---|
| 229 | n/a | expon = 0 |
|---|
| 230 | n/a | himant = 0 |
|---|
| 231 | n/a | lomant = 0 |
|---|
| 232 | n/a | else: |
|---|
| 233 | n/a | fmant, expon = math.frexp(x) |
|---|
| 234 | n/a | if expon > 16384 or fmant >= 1 or fmant != fmant: # Infinity or NaN |
|---|
| 235 | n/a | expon = sign|0x7FFF |
|---|
| 236 | n/a | himant = 0 |
|---|
| 237 | n/a | lomant = 0 |
|---|
| 238 | n/a | else: # Finite |
|---|
| 239 | n/a | expon = expon + 16382 |
|---|
| 240 | n/a | if expon < 0: # denormalized |
|---|
| 241 | n/a | fmant = math.ldexp(fmant, expon) |
|---|
| 242 | n/a | expon = 0 |
|---|
| 243 | n/a | expon = expon | sign |
|---|
| 244 | n/a | fmant = math.ldexp(fmant, 32) |
|---|
| 245 | n/a | fsmant = math.floor(fmant) |
|---|
| 246 | n/a | himant = int(fsmant) |
|---|
| 247 | n/a | fmant = math.ldexp(fmant - fsmant, 32) |
|---|
| 248 | n/a | fsmant = math.floor(fmant) |
|---|
| 249 | n/a | lomant = int(fsmant) |
|---|
| 250 | n/a | _write_ushort(f, expon) |
|---|
| 251 | n/a | _write_ulong(f, himant) |
|---|
| 252 | n/a | _write_ulong(f, lomant) |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | from chunk import Chunk |
|---|
| 255 | n/a | from collections import namedtuple |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | _aifc_params = namedtuple('_aifc_params', |
|---|
| 258 | n/a | 'nchannels sampwidth framerate nframes comptype compname') |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | _aifc_params.nchannels.__doc__ = 'Number of audio channels (1 for mono, 2 for stereo)' |
|---|
| 261 | n/a | _aifc_params.sampwidth.__doc__ = 'Sample width in bytes' |
|---|
| 262 | n/a | _aifc_params.framerate.__doc__ = 'Sampling frequency' |
|---|
| 263 | n/a | _aifc_params.nframes.__doc__ = 'Number of audio frames' |
|---|
| 264 | n/a | _aifc_params.comptype.__doc__ = 'Compression type ("NONE" for AIFF files)' |
|---|
| 265 | n/a | _aifc_params.compname.__doc__ = ("""\ |
|---|
| 266 | n/a | A human-readable version of the compression type |
|---|
| 267 | n/a | ('not compressed' for AIFF files)""") |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | class Aifc_read: |
|---|
| 271 | n/a | # Variables used in this class: |
|---|
| 272 | n/a | # |
|---|
| 273 | n/a | # These variables are available to the user though appropriate |
|---|
| 274 | n/a | # methods of this class: |
|---|
| 275 | n/a | # _file -- the open file with methods read(), close(), and seek() |
|---|
| 276 | n/a | # set through the __init__() method |
|---|
| 277 | n/a | # _nchannels -- the number of audio channels |
|---|
| 278 | n/a | # available through the getnchannels() method |
|---|
| 279 | n/a | # _nframes -- the number of audio frames |
|---|
| 280 | n/a | # available through the getnframes() method |
|---|
| 281 | n/a | # _sampwidth -- the number of bytes per audio sample |
|---|
| 282 | n/a | # available through the getsampwidth() method |
|---|
| 283 | n/a | # _framerate -- the sampling frequency |
|---|
| 284 | n/a | # available through the getframerate() method |
|---|
| 285 | n/a | # _comptype -- the AIFF-C compression type ('NONE' if AIFF) |
|---|
| 286 | n/a | # available through the getcomptype() method |
|---|
| 287 | n/a | # _compname -- the human-readable AIFF-C compression type |
|---|
| 288 | n/a | # available through the getcomptype() method |
|---|
| 289 | n/a | # _markers -- the marks in the audio file |
|---|
| 290 | n/a | # available through the getmarkers() and getmark() |
|---|
| 291 | n/a | # methods |
|---|
| 292 | n/a | # _soundpos -- the position in the audio stream |
|---|
| 293 | n/a | # available through the tell() method, set through the |
|---|
| 294 | n/a | # setpos() method |
|---|
| 295 | n/a | # |
|---|
| 296 | n/a | # These variables are used internally only: |
|---|
| 297 | n/a | # _version -- the AIFF-C version number |
|---|
| 298 | n/a | # _decomp -- the decompressor from builtin module cl |
|---|
| 299 | n/a | # _comm_chunk_read -- 1 iff the COMM chunk has been read |
|---|
| 300 | n/a | # _aifc -- 1 iff reading an AIFF-C file |
|---|
| 301 | n/a | # _ssnd_seek_needed -- 1 iff positioned correctly in audio |
|---|
| 302 | n/a | # file for readframes() |
|---|
| 303 | n/a | # _ssnd_chunk -- instantiation of a chunk class for the SSND chunk |
|---|
| 304 | n/a | # _framesize -- size of one frame in the file |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | def initfp(self, file): |
|---|
| 307 | n/a | self._version = 0 |
|---|
| 308 | n/a | self._convert = None |
|---|
| 309 | n/a | self._markers = [] |
|---|
| 310 | n/a | self._soundpos = 0 |
|---|
| 311 | n/a | self._file = file |
|---|
| 312 | n/a | chunk = Chunk(file) |
|---|
| 313 | n/a | if chunk.getname() != b'FORM': |
|---|
| 314 | n/a | raise Error('file does not start with FORM id') |
|---|
| 315 | n/a | formdata = chunk.read(4) |
|---|
| 316 | n/a | if formdata == b'AIFF': |
|---|
| 317 | n/a | self._aifc = 0 |
|---|
| 318 | n/a | elif formdata == b'AIFC': |
|---|
| 319 | n/a | self._aifc = 1 |
|---|
| 320 | n/a | else: |
|---|
| 321 | n/a | raise Error('not an AIFF or AIFF-C file') |
|---|
| 322 | n/a | self._comm_chunk_read = 0 |
|---|
| 323 | n/a | while 1: |
|---|
| 324 | n/a | self._ssnd_seek_needed = 1 |
|---|
| 325 | n/a | try: |
|---|
| 326 | n/a | chunk = Chunk(self._file) |
|---|
| 327 | n/a | except EOFError: |
|---|
| 328 | n/a | break |
|---|
| 329 | n/a | chunkname = chunk.getname() |
|---|
| 330 | n/a | if chunkname == b'COMM': |
|---|
| 331 | n/a | self._read_comm_chunk(chunk) |
|---|
| 332 | n/a | self._comm_chunk_read = 1 |
|---|
| 333 | n/a | elif chunkname == b'SSND': |
|---|
| 334 | n/a | self._ssnd_chunk = chunk |
|---|
| 335 | n/a | dummy = chunk.read(8) |
|---|
| 336 | n/a | self._ssnd_seek_needed = 0 |
|---|
| 337 | n/a | elif chunkname == b'FVER': |
|---|
| 338 | n/a | self._version = _read_ulong(chunk) |
|---|
| 339 | n/a | elif chunkname == b'MARK': |
|---|
| 340 | n/a | self._readmark(chunk) |
|---|
| 341 | n/a | chunk.skip() |
|---|
| 342 | n/a | if not self._comm_chunk_read or not self._ssnd_chunk: |
|---|
| 343 | n/a | raise Error('COMM chunk and/or SSND chunk missing') |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | def __init__(self, f): |
|---|
| 346 | n/a | if isinstance(f, str): |
|---|
| 347 | n/a | f = builtins.open(f, 'rb') |
|---|
| 348 | n/a | # else, assume it is an open file object already |
|---|
| 349 | n/a | self.initfp(f) |
|---|
| 350 | n/a | |
|---|
| 351 | n/a | def __enter__(self): |
|---|
| 352 | n/a | return self |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | def __exit__(self, *args): |
|---|
| 355 | n/a | self.close() |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | # |
|---|
| 358 | n/a | # User visible methods. |
|---|
| 359 | n/a | # |
|---|
| 360 | n/a | def getfp(self): |
|---|
| 361 | n/a | return self._file |
|---|
| 362 | n/a | |
|---|
| 363 | n/a | def rewind(self): |
|---|
| 364 | n/a | self._ssnd_seek_needed = 1 |
|---|
| 365 | n/a | self._soundpos = 0 |
|---|
| 366 | n/a | |
|---|
| 367 | n/a | def close(self): |
|---|
| 368 | n/a | file = self._file |
|---|
| 369 | n/a | if file is not None: |
|---|
| 370 | n/a | self._file = None |
|---|
| 371 | n/a | file.close() |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | def tell(self): |
|---|
| 374 | n/a | return self._soundpos |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | def getnchannels(self): |
|---|
| 377 | n/a | return self._nchannels |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | def getnframes(self): |
|---|
| 380 | n/a | return self._nframes |
|---|
| 381 | n/a | |
|---|
| 382 | n/a | def getsampwidth(self): |
|---|
| 383 | n/a | return self._sampwidth |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | def getframerate(self): |
|---|
| 386 | n/a | return self._framerate |
|---|
| 387 | n/a | |
|---|
| 388 | n/a | def getcomptype(self): |
|---|
| 389 | n/a | return self._comptype |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | def getcompname(self): |
|---|
| 392 | n/a | return self._compname |
|---|
| 393 | n/a | |
|---|
| 394 | n/a | ## def getversion(self): |
|---|
| 395 | n/a | ## return self._version |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | def getparams(self): |
|---|
| 398 | n/a | return _aifc_params(self.getnchannels(), self.getsampwidth(), |
|---|
| 399 | n/a | self.getframerate(), self.getnframes(), |
|---|
| 400 | n/a | self.getcomptype(), self.getcompname()) |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | def getmarkers(self): |
|---|
| 403 | n/a | if len(self._markers) == 0: |
|---|
| 404 | n/a | return None |
|---|
| 405 | n/a | return self._markers |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | def getmark(self, id): |
|---|
| 408 | n/a | for marker in self._markers: |
|---|
| 409 | n/a | if id == marker[0]: |
|---|
| 410 | n/a | return marker |
|---|
| 411 | n/a | raise Error('marker {0!r} does not exist'.format(id)) |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | def setpos(self, pos): |
|---|
| 414 | n/a | if pos < 0 or pos > self._nframes: |
|---|
| 415 | n/a | raise Error('position not in range') |
|---|
| 416 | n/a | self._soundpos = pos |
|---|
| 417 | n/a | self._ssnd_seek_needed = 1 |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | def readframes(self, nframes): |
|---|
| 420 | n/a | if self._ssnd_seek_needed: |
|---|
| 421 | n/a | self._ssnd_chunk.seek(0) |
|---|
| 422 | n/a | dummy = self._ssnd_chunk.read(8) |
|---|
| 423 | n/a | pos = self._soundpos * self._framesize |
|---|
| 424 | n/a | if pos: |
|---|
| 425 | n/a | self._ssnd_chunk.seek(pos + 8) |
|---|
| 426 | n/a | self._ssnd_seek_needed = 0 |
|---|
| 427 | n/a | if nframes == 0: |
|---|
| 428 | n/a | return b'' |
|---|
| 429 | n/a | data = self._ssnd_chunk.read(nframes * self._framesize) |
|---|
| 430 | n/a | if self._convert and data: |
|---|
| 431 | n/a | data = self._convert(data) |
|---|
| 432 | n/a | self._soundpos = self._soundpos + len(data) // (self._nchannels |
|---|
| 433 | n/a | * self._sampwidth) |
|---|
| 434 | n/a | return data |
|---|
| 435 | n/a | |
|---|
| 436 | n/a | # |
|---|
| 437 | n/a | # Internal methods. |
|---|
| 438 | n/a | # |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | def _alaw2lin(self, data): |
|---|
| 441 | n/a | import audioop |
|---|
| 442 | n/a | return audioop.alaw2lin(data, 2) |
|---|
| 443 | n/a | |
|---|
| 444 | n/a | def _ulaw2lin(self, data): |
|---|
| 445 | n/a | import audioop |
|---|
| 446 | n/a | return audioop.ulaw2lin(data, 2) |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | def _adpcm2lin(self, data): |
|---|
| 449 | n/a | import audioop |
|---|
| 450 | n/a | if not hasattr(self, '_adpcmstate'): |
|---|
| 451 | n/a | # first time |
|---|
| 452 | n/a | self._adpcmstate = None |
|---|
| 453 | n/a | data, self._adpcmstate = audioop.adpcm2lin(data, 2, self._adpcmstate) |
|---|
| 454 | n/a | return data |
|---|
| 455 | n/a | |
|---|
| 456 | n/a | def _read_comm_chunk(self, chunk): |
|---|
| 457 | n/a | self._nchannels = _read_short(chunk) |
|---|
| 458 | n/a | self._nframes = _read_long(chunk) |
|---|
| 459 | n/a | self._sampwidth = (_read_short(chunk) + 7) // 8 |
|---|
| 460 | n/a | self._framerate = int(_read_float(chunk)) |
|---|
| 461 | n/a | self._framesize = self._nchannels * self._sampwidth |
|---|
| 462 | n/a | if self._aifc: |
|---|
| 463 | n/a | #DEBUG: SGI's soundeditor produces a bad size :-( |
|---|
| 464 | n/a | kludge = 0 |
|---|
| 465 | n/a | if chunk.chunksize == 18: |
|---|
| 466 | n/a | kludge = 1 |
|---|
| 467 | n/a | warnings.warn('Warning: bad COMM chunk size') |
|---|
| 468 | n/a | chunk.chunksize = 23 |
|---|
| 469 | n/a | #DEBUG end |
|---|
| 470 | n/a | self._comptype = chunk.read(4) |
|---|
| 471 | n/a | #DEBUG start |
|---|
| 472 | n/a | if kludge: |
|---|
| 473 | n/a | length = ord(chunk.file.read(1)) |
|---|
| 474 | n/a | if length & 1 == 0: |
|---|
| 475 | n/a | length = length + 1 |
|---|
| 476 | n/a | chunk.chunksize = chunk.chunksize + length |
|---|
| 477 | n/a | chunk.file.seek(-1, 1) |
|---|
| 478 | n/a | #DEBUG end |
|---|
| 479 | n/a | self._compname = _read_string(chunk) |
|---|
| 480 | n/a | if self._comptype != b'NONE': |
|---|
| 481 | n/a | if self._comptype == b'G722': |
|---|
| 482 | n/a | self._convert = self._adpcm2lin |
|---|
| 483 | n/a | elif self._comptype in (b'ulaw', b'ULAW'): |
|---|
| 484 | n/a | self._convert = self._ulaw2lin |
|---|
| 485 | n/a | elif self._comptype in (b'alaw', b'ALAW'): |
|---|
| 486 | n/a | self._convert = self._alaw2lin |
|---|
| 487 | n/a | else: |
|---|
| 488 | n/a | raise Error('unsupported compression type') |
|---|
| 489 | n/a | self._sampwidth = 2 |
|---|
| 490 | n/a | else: |
|---|
| 491 | n/a | self._comptype = b'NONE' |
|---|
| 492 | n/a | self._compname = b'not compressed' |
|---|
| 493 | n/a | |
|---|
| 494 | n/a | def _readmark(self, chunk): |
|---|
| 495 | n/a | nmarkers = _read_short(chunk) |
|---|
| 496 | n/a | # Some files appear to contain invalid counts. |
|---|
| 497 | n/a | # Cope with this by testing for EOF. |
|---|
| 498 | n/a | try: |
|---|
| 499 | n/a | for i in range(nmarkers): |
|---|
| 500 | n/a | id = _read_short(chunk) |
|---|
| 501 | n/a | pos = _read_long(chunk) |
|---|
| 502 | n/a | name = _read_string(chunk) |
|---|
| 503 | n/a | if pos or name: |
|---|
| 504 | n/a | # some files appear to have |
|---|
| 505 | n/a | # dummy markers consisting of |
|---|
| 506 | n/a | # a position 0 and name '' |
|---|
| 507 | n/a | self._markers.append((id, pos, name)) |
|---|
| 508 | n/a | except EOFError: |
|---|
| 509 | n/a | w = ('Warning: MARK chunk contains only %s marker%s instead of %s' % |
|---|
| 510 | n/a | (len(self._markers), '' if len(self._markers) == 1 else 's', |
|---|
| 511 | n/a | nmarkers)) |
|---|
| 512 | n/a | warnings.warn(w) |
|---|
| 513 | n/a | |
|---|
| 514 | n/a | class Aifc_write: |
|---|
| 515 | n/a | # Variables used in this class: |
|---|
| 516 | n/a | # |
|---|
| 517 | n/a | # These variables are user settable through appropriate methods |
|---|
| 518 | n/a | # of this class: |
|---|
| 519 | n/a | # _file -- the open file with methods write(), close(), tell(), seek() |
|---|
| 520 | n/a | # set through the __init__() method |
|---|
| 521 | n/a | # _comptype -- the AIFF-C compression type ('NONE' in AIFF) |
|---|
| 522 | n/a | # set through the setcomptype() or setparams() method |
|---|
| 523 | n/a | # _compname -- the human-readable AIFF-C compression type |
|---|
| 524 | n/a | # set through the setcomptype() or setparams() method |
|---|
| 525 | n/a | # _nchannels -- the number of audio channels |
|---|
| 526 | n/a | # set through the setnchannels() or setparams() method |
|---|
| 527 | n/a | # _sampwidth -- the number of bytes per audio sample |
|---|
| 528 | n/a | # set through the setsampwidth() or setparams() method |
|---|
| 529 | n/a | # _framerate -- the sampling frequency |
|---|
| 530 | n/a | # set through the setframerate() or setparams() method |
|---|
| 531 | n/a | # _nframes -- the number of audio frames written to the header |
|---|
| 532 | n/a | # set through the setnframes() or setparams() method |
|---|
| 533 | n/a | # _aifc -- whether we're writing an AIFF-C file or an AIFF file |
|---|
| 534 | n/a | # set through the aifc() method, reset through the |
|---|
| 535 | n/a | # aiff() method |
|---|
| 536 | n/a | # |
|---|
| 537 | n/a | # These variables are used internally only: |
|---|
| 538 | n/a | # _version -- the AIFF-C version number |
|---|
| 539 | n/a | # _comp -- the compressor from builtin module cl |
|---|
| 540 | n/a | # _nframeswritten -- the number of audio frames actually written |
|---|
| 541 | n/a | # _datalength -- the size of the audio samples written to the header |
|---|
| 542 | n/a | # _datawritten -- the size of the audio samples actually written |
|---|
| 543 | n/a | |
|---|
| 544 | n/a | def __init__(self, f): |
|---|
| 545 | n/a | if isinstance(f, str): |
|---|
| 546 | n/a | filename = f |
|---|
| 547 | n/a | f = builtins.open(f, 'wb') |
|---|
| 548 | n/a | else: |
|---|
| 549 | n/a | # else, assume it is an open file object already |
|---|
| 550 | n/a | filename = '???' |
|---|
| 551 | n/a | self.initfp(f) |
|---|
| 552 | n/a | if filename[-5:] == '.aiff': |
|---|
| 553 | n/a | self._aifc = 0 |
|---|
| 554 | n/a | else: |
|---|
| 555 | n/a | self._aifc = 1 |
|---|
| 556 | n/a | |
|---|
| 557 | n/a | def initfp(self, file): |
|---|
| 558 | n/a | self._file = file |
|---|
| 559 | n/a | self._version = _AIFC_version |
|---|
| 560 | n/a | self._comptype = b'NONE' |
|---|
| 561 | n/a | self._compname = b'not compressed' |
|---|
| 562 | n/a | self._convert = None |
|---|
| 563 | n/a | self._nchannels = 0 |
|---|
| 564 | n/a | self._sampwidth = 0 |
|---|
| 565 | n/a | self._framerate = 0 |
|---|
| 566 | n/a | self._nframes = 0 |
|---|
| 567 | n/a | self._nframeswritten = 0 |
|---|
| 568 | n/a | self._datawritten = 0 |
|---|
| 569 | n/a | self._datalength = 0 |
|---|
| 570 | n/a | self._markers = [] |
|---|
| 571 | n/a | self._marklength = 0 |
|---|
| 572 | n/a | self._aifc = 1 # AIFF-C is default |
|---|
| 573 | n/a | |
|---|
| 574 | n/a | def __del__(self): |
|---|
| 575 | n/a | self.close() |
|---|
| 576 | n/a | |
|---|
| 577 | n/a | def __enter__(self): |
|---|
| 578 | n/a | return self |
|---|
| 579 | n/a | |
|---|
| 580 | n/a | def __exit__(self, *args): |
|---|
| 581 | n/a | self.close() |
|---|
| 582 | n/a | |
|---|
| 583 | n/a | # |
|---|
| 584 | n/a | # User visible methods. |
|---|
| 585 | n/a | # |
|---|
| 586 | n/a | def aiff(self): |
|---|
| 587 | n/a | if self._nframeswritten: |
|---|
| 588 | n/a | raise Error('cannot change parameters after starting to write') |
|---|
| 589 | n/a | self._aifc = 0 |
|---|
| 590 | n/a | |
|---|
| 591 | n/a | def aifc(self): |
|---|
| 592 | n/a | if self._nframeswritten: |
|---|
| 593 | n/a | raise Error('cannot change parameters after starting to write') |
|---|
| 594 | n/a | self._aifc = 1 |
|---|
| 595 | n/a | |
|---|
| 596 | n/a | def setnchannels(self, nchannels): |
|---|
| 597 | n/a | if self._nframeswritten: |
|---|
| 598 | n/a | raise Error('cannot change parameters after starting to write') |
|---|
| 599 | n/a | if nchannels < 1: |
|---|
| 600 | n/a | raise Error('bad # of channels') |
|---|
| 601 | n/a | self._nchannels = nchannels |
|---|
| 602 | n/a | |
|---|
| 603 | n/a | def getnchannels(self): |
|---|
| 604 | n/a | if not self._nchannels: |
|---|
| 605 | n/a | raise Error('number of channels not set') |
|---|
| 606 | n/a | return self._nchannels |
|---|
| 607 | n/a | |
|---|
| 608 | n/a | def setsampwidth(self, sampwidth): |
|---|
| 609 | n/a | if self._nframeswritten: |
|---|
| 610 | n/a | raise Error('cannot change parameters after starting to write') |
|---|
| 611 | n/a | if sampwidth < 1 or sampwidth > 4: |
|---|
| 612 | n/a | raise Error('bad sample width') |
|---|
| 613 | n/a | self._sampwidth = sampwidth |
|---|
| 614 | n/a | |
|---|
| 615 | n/a | def getsampwidth(self): |
|---|
| 616 | n/a | if not self._sampwidth: |
|---|
| 617 | n/a | raise Error('sample width not set') |
|---|
| 618 | n/a | return self._sampwidth |
|---|
| 619 | n/a | |
|---|
| 620 | n/a | def setframerate(self, framerate): |
|---|
| 621 | n/a | if self._nframeswritten: |
|---|
| 622 | n/a | raise Error('cannot change parameters after starting to write') |
|---|
| 623 | n/a | if framerate <= 0: |
|---|
| 624 | n/a | raise Error('bad frame rate') |
|---|
| 625 | n/a | self._framerate = framerate |
|---|
| 626 | n/a | |
|---|
| 627 | n/a | def getframerate(self): |
|---|
| 628 | n/a | if not self._framerate: |
|---|
| 629 | n/a | raise Error('frame rate not set') |
|---|
| 630 | n/a | return self._framerate |
|---|
| 631 | n/a | |
|---|
| 632 | n/a | def setnframes(self, nframes): |
|---|
| 633 | n/a | if self._nframeswritten: |
|---|
| 634 | n/a | raise Error('cannot change parameters after starting to write') |
|---|
| 635 | n/a | self._nframes = nframes |
|---|
| 636 | n/a | |
|---|
| 637 | n/a | def getnframes(self): |
|---|
| 638 | n/a | return self._nframeswritten |
|---|
| 639 | n/a | |
|---|
| 640 | n/a | def setcomptype(self, comptype, compname): |
|---|
| 641 | n/a | if self._nframeswritten: |
|---|
| 642 | n/a | raise Error('cannot change parameters after starting to write') |
|---|
| 643 | n/a | if comptype not in (b'NONE', b'ulaw', b'ULAW', |
|---|
| 644 | n/a | b'alaw', b'ALAW', b'G722'): |
|---|
| 645 | n/a | raise Error('unsupported compression type') |
|---|
| 646 | n/a | self._comptype = comptype |
|---|
| 647 | n/a | self._compname = compname |
|---|
| 648 | n/a | |
|---|
| 649 | n/a | def getcomptype(self): |
|---|
| 650 | n/a | return self._comptype |
|---|
| 651 | n/a | |
|---|
| 652 | n/a | def getcompname(self): |
|---|
| 653 | n/a | return self._compname |
|---|
| 654 | n/a | |
|---|
| 655 | n/a | ## def setversion(self, version): |
|---|
| 656 | n/a | ## if self._nframeswritten: |
|---|
| 657 | n/a | ## raise Error, 'cannot change parameters after starting to write' |
|---|
| 658 | n/a | ## self._version = version |
|---|
| 659 | n/a | |
|---|
| 660 | n/a | def setparams(self, params): |
|---|
| 661 | n/a | nchannels, sampwidth, framerate, nframes, comptype, compname = params |
|---|
| 662 | n/a | if self._nframeswritten: |
|---|
| 663 | n/a | raise Error('cannot change parameters after starting to write') |
|---|
| 664 | n/a | if comptype not in (b'NONE', b'ulaw', b'ULAW', |
|---|
| 665 | n/a | b'alaw', b'ALAW', b'G722'): |
|---|
| 666 | n/a | raise Error('unsupported compression type') |
|---|
| 667 | n/a | self.setnchannels(nchannels) |
|---|
| 668 | n/a | self.setsampwidth(sampwidth) |
|---|
| 669 | n/a | self.setframerate(framerate) |
|---|
| 670 | n/a | self.setnframes(nframes) |
|---|
| 671 | n/a | self.setcomptype(comptype, compname) |
|---|
| 672 | n/a | |
|---|
| 673 | n/a | def getparams(self): |
|---|
| 674 | n/a | if not self._nchannels or not self._sampwidth or not self._framerate: |
|---|
| 675 | n/a | raise Error('not all parameters set') |
|---|
| 676 | n/a | return _aifc_params(self._nchannels, self._sampwidth, self._framerate, |
|---|
| 677 | n/a | self._nframes, self._comptype, self._compname) |
|---|
| 678 | n/a | |
|---|
| 679 | n/a | def setmark(self, id, pos, name): |
|---|
| 680 | n/a | if id <= 0: |
|---|
| 681 | n/a | raise Error('marker ID must be > 0') |
|---|
| 682 | n/a | if pos < 0: |
|---|
| 683 | n/a | raise Error('marker position must be >= 0') |
|---|
| 684 | n/a | if not isinstance(name, bytes): |
|---|
| 685 | n/a | raise Error('marker name must be bytes') |
|---|
| 686 | n/a | for i in range(len(self._markers)): |
|---|
| 687 | n/a | if id == self._markers[i][0]: |
|---|
| 688 | n/a | self._markers[i] = id, pos, name |
|---|
| 689 | n/a | return |
|---|
| 690 | n/a | self._markers.append((id, pos, name)) |
|---|
| 691 | n/a | |
|---|
| 692 | n/a | def getmark(self, id): |
|---|
| 693 | n/a | for marker in self._markers: |
|---|
| 694 | n/a | if id == marker[0]: |
|---|
| 695 | n/a | return marker |
|---|
| 696 | n/a | raise Error('marker {0!r} does not exist'.format(id)) |
|---|
| 697 | n/a | |
|---|
| 698 | n/a | def getmarkers(self): |
|---|
| 699 | n/a | if len(self._markers) == 0: |
|---|
| 700 | n/a | return None |
|---|
| 701 | n/a | return self._markers |
|---|
| 702 | n/a | |
|---|
| 703 | n/a | def tell(self): |
|---|
| 704 | n/a | return self._nframeswritten |
|---|
| 705 | n/a | |
|---|
| 706 | n/a | def writeframesraw(self, data): |
|---|
| 707 | n/a | if not isinstance(data, (bytes, bytearray)): |
|---|
| 708 | n/a | data = memoryview(data).cast('B') |
|---|
| 709 | n/a | self._ensure_header_written(len(data)) |
|---|
| 710 | n/a | nframes = len(data) // (self._sampwidth * self._nchannels) |
|---|
| 711 | n/a | if self._convert: |
|---|
| 712 | n/a | data = self._convert(data) |
|---|
| 713 | n/a | self._file.write(data) |
|---|
| 714 | n/a | self._nframeswritten = self._nframeswritten + nframes |
|---|
| 715 | n/a | self._datawritten = self._datawritten + len(data) |
|---|
| 716 | n/a | |
|---|
| 717 | n/a | def writeframes(self, data): |
|---|
| 718 | n/a | self.writeframesraw(data) |
|---|
| 719 | n/a | if self._nframeswritten != self._nframes or \ |
|---|
| 720 | n/a | self._datalength != self._datawritten: |
|---|
| 721 | n/a | self._patchheader() |
|---|
| 722 | n/a | |
|---|
| 723 | n/a | def close(self): |
|---|
| 724 | n/a | if self._file is None: |
|---|
| 725 | n/a | return |
|---|
| 726 | n/a | try: |
|---|
| 727 | n/a | self._ensure_header_written(0) |
|---|
| 728 | n/a | if self._datawritten & 1: |
|---|
| 729 | n/a | # quick pad to even size |
|---|
| 730 | n/a | self._file.write(b'\x00') |
|---|
| 731 | n/a | self._datawritten = self._datawritten + 1 |
|---|
| 732 | n/a | self._writemarkers() |
|---|
| 733 | n/a | if self._nframeswritten != self._nframes or \ |
|---|
| 734 | n/a | self._datalength != self._datawritten or \ |
|---|
| 735 | n/a | self._marklength: |
|---|
| 736 | n/a | self._patchheader() |
|---|
| 737 | n/a | finally: |
|---|
| 738 | n/a | # Prevent ref cycles |
|---|
| 739 | n/a | self._convert = None |
|---|
| 740 | n/a | f = self._file |
|---|
| 741 | n/a | self._file = None |
|---|
| 742 | n/a | f.close() |
|---|
| 743 | n/a | |
|---|
| 744 | n/a | # |
|---|
| 745 | n/a | # Internal methods. |
|---|
| 746 | n/a | # |
|---|
| 747 | n/a | |
|---|
| 748 | n/a | def _lin2alaw(self, data): |
|---|
| 749 | n/a | import audioop |
|---|
| 750 | n/a | return audioop.lin2alaw(data, 2) |
|---|
| 751 | n/a | |
|---|
| 752 | n/a | def _lin2ulaw(self, data): |
|---|
| 753 | n/a | import audioop |
|---|
| 754 | n/a | return audioop.lin2ulaw(data, 2) |
|---|
| 755 | n/a | |
|---|
| 756 | n/a | def _lin2adpcm(self, data): |
|---|
| 757 | n/a | import audioop |
|---|
| 758 | n/a | if not hasattr(self, '_adpcmstate'): |
|---|
| 759 | n/a | self._adpcmstate = None |
|---|
| 760 | n/a | data, self._adpcmstate = audioop.lin2adpcm(data, 2, self._adpcmstate) |
|---|
| 761 | n/a | return data |
|---|
| 762 | n/a | |
|---|
| 763 | n/a | def _ensure_header_written(self, datasize): |
|---|
| 764 | n/a | if not self._nframeswritten: |
|---|
| 765 | n/a | if self._comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): |
|---|
| 766 | n/a | if not self._sampwidth: |
|---|
| 767 | n/a | self._sampwidth = 2 |
|---|
| 768 | n/a | if self._sampwidth != 2: |
|---|
| 769 | n/a | raise Error('sample width must be 2 when compressing ' |
|---|
| 770 | n/a | 'with ulaw/ULAW, alaw/ALAW or G7.22 (ADPCM)') |
|---|
| 771 | n/a | if not self._nchannels: |
|---|
| 772 | n/a | raise Error('# channels not specified') |
|---|
| 773 | n/a | if not self._sampwidth: |
|---|
| 774 | n/a | raise Error('sample width not specified') |
|---|
| 775 | n/a | if not self._framerate: |
|---|
| 776 | n/a | raise Error('sampling rate not specified') |
|---|
| 777 | n/a | self._write_header(datasize) |
|---|
| 778 | n/a | |
|---|
| 779 | n/a | def _init_compression(self): |
|---|
| 780 | n/a | if self._comptype == b'G722': |
|---|
| 781 | n/a | self._convert = self._lin2adpcm |
|---|
| 782 | n/a | elif self._comptype in (b'ulaw', b'ULAW'): |
|---|
| 783 | n/a | self._convert = self._lin2ulaw |
|---|
| 784 | n/a | elif self._comptype in (b'alaw', b'ALAW'): |
|---|
| 785 | n/a | self._convert = self._lin2alaw |
|---|
| 786 | n/a | |
|---|
| 787 | n/a | def _write_header(self, initlength): |
|---|
| 788 | n/a | if self._aifc and self._comptype != b'NONE': |
|---|
| 789 | n/a | self._init_compression() |
|---|
| 790 | n/a | self._file.write(b'FORM') |
|---|
| 791 | n/a | if not self._nframes: |
|---|
| 792 | n/a | self._nframes = initlength // (self._nchannels * self._sampwidth) |
|---|
| 793 | n/a | self._datalength = self._nframes * self._nchannels * self._sampwidth |
|---|
| 794 | n/a | if self._datalength & 1: |
|---|
| 795 | n/a | self._datalength = self._datalength + 1 |
|---|
| 796 | n/a | if self._aifc: |
|---|
| 797 | n/a | if self._comptype in (b'ulaw', b'ULAW', b'alaw', b'ALAW'): |
|---|
| 798 | n/a | self._datalength = self._datalength // 2 |
|---|
| 799 | n/a | if self._datalength & 1: |
|---|
| 800 | n/a | self._datalength = self._datalength + 1 |
|---|
| 801 | n/a | elif self._comptype == b'G722': |
|---|
| 802 | n/a | self._datalength = (self._datalength + 3) // 4 |
|---|
| 803 | n/a | if self._datalength & 1: |
|---|
| 804 | n/a | self._datalength = self._datalength + 1 |
|---|
| 805 | n/a | try: |
|---|
| 806 | n/a | self._form_length_pos = self._file.tell() |
|---|
| 807 | n/a | except (AttributeError, OSError): |
|---|
| 808 | n/a | self._form_length_pos = None |
|---|
| 809 | n/a | commlength = self._write_form_length(self._datalength) |
|---|
| 810 | n/a | if self._aifc: |
|---|
| 811 | n/a | self._file.write(b'AIFC') |
|---|
| 812 | n/a | self._file.write(b'FVER') |
|---|
| 813 | n/a | _write_ulong(self._file, 4) |
|---|
| 814 | n/a | _write_ulong(self._file, self._version) |
|---|
| 815 | n/a | else: |
|---|
| 816 | n/a | self._file.write(b'AIFF') |
|---|
| 817 | n/a | self._file.write(b'COMM') |
|---|
| 818 | n/a | _write_ulong(self._file, commlength) |
|---|
| 819 | n/a | _write_short(self._file, self._nchannels) |
|---|
| 820 | n/a | if self._form_length_pos is not None: |
|---|
| 821 | n/a | self._nframes_pos = self._file.tell() |
|---|
| 822 | n/a | _write_ulong(self._file, self._nframes) |
|---|
| 823 | n/a | if self._comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): |
|---|
| 824 | n/a | _write_short(self._file, 8) |
|---|
| 825 | n/a | else: |
|---|
| 826 | n/a | _write_short(self._file, self._sampwidth * 8) |
|---|
| 827 | n/a | _write_float(self._file, self._framerate) |
|---|
| 828 | n/a | if self._aifc: |
|---|
| 829 | n/a | self._file.write(self._comptype) |
|---|
| 830 | n/a | _write_string(self._file, self._compname) |
|---|
| 831 | n/a | self._file.write(b'SSND') |
|---|
| 832 | n/a | if self._form_length_pos is not None: |
|---|
| 833 | n/a | self._ssnd_length_pos = self._file.tell() |
|---|
| 834 | n/a | _write_ulong(self._file, self._datalength + 8) |
|---|
| 835 | n/a | _write_ulong(self._file, 0) |
|---|
| 836 | n/a | _write_ulong(self._file, 0) |
|---|
| 837 | n/a | |
|---|
| 838 | n/a | def _write_form_length(self, datalength): |
|---|
| 839 | n/a | if self._aifc: |
|---|
| 840 | n/a | commlength = 18 + 5 + len(self._compname) |
|---|
| 841 | n/a | if commlength & 1: |
|---|
| 842 | n/a | commlength = commlength + 1 |
|---|
| 843 | n/a | verslength = 12 |
|---|
| 844 | n/a | else: |
|---|
| 845 | n/a | commlength = 18 |
|---|
| 846 | n/a | verslength = 0 |
|---|
| 847 | n/a | _write_ulong(self._file, 4 + verslength + self._marklength + \ |
|---|
| 848 | n/a | 8 + commlength + 16 + datalength) |
|---|
| 849 | n/a | return commlength |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | def _patchheader(self): |
|---|
| 852 | n/a | curpos = self._file.tell() |
|---|
| 853 | n/a | if self._datawritten & 1: |
|---|
| 854 | n/a | datalength = self._datawritten + 1 |
|---|
| 855 | n/a | self._file.write(b'\x00') |
|---|
| 856 | n/a | else: |
|---|
| 857 | n/a | datalength = self._datawritten |
|---|
| 858 | n/a | if datalength == self._datalength and \ |
|---|
| 859 | n/a | self._nframes == self._nframeswritten and \ |
|---|
| 860 | n/a | self._marklength == 0: |
|---|
| 861 | n/a | self._file.seek(curpos, 0) |
|---|
| 862 | n/a | return |
|---|
| 863 | n/a | self._file.seek(self._form_length_pos, 0) |
|---|
| 864 | n/a | dummy = self._write_form_length(datalength) |
|---|
| 865 | n/a | self._file.seek(self._nframes_pos, 0) |
|---|
| 866 | n/a | _write_ulong(self._file, self._nframeswritten) |
|---|
| 867 | n/a | self._file.seek(self._ssnd_length_pos, 0) |
|---|
| 868 | n/a | _write_ulong(self._file, datalength + 8) |
|---|
| 869 | n/a | self._file.seek(curpos, 0) |
|---|
| 870 | n/a | self._nframes = self._nframeswritten |
|---|
| 871 | n/a | self._datalength = datalength |
|---|
| 872 | n/a | |
|---|
| 873 | n/a | def _writemarkers(self): |
|---|
| 874 | n/a | if len(self._markers) == 0: |
|---|
| 875 | n/a | return |
|---|
| 876 | n/a | self._file.write(b'MARK') |
|---|
| 877 | n/a | length = 2 |
|---|
| 878 | n/a | for marker in self._markers: |
|---|
| 879 | n/a | id, pos, name = marker |
|---|
| 880 | n/a | length = length + len(name) + 1 + 6 |
|---|
| 881 | n/a | if len(name) & 1 == 0: |
|---|
| 882 | n/a | length = length + 1 |
|---|
| 883 | n/a | _write_ulong(self._file, length) |
|---|
| 884 | n/a | self._marklength = length + 8 |
|---|
| 885 | n/a | _write_short(self._file, len(self._markers)) |
|---|
| 886 | n/a | for marker in self._markers: |
|---|
| 887 | n/a | id, pos, name = marker |
|---|
| 888 | n/a | _write_short(self._file, id) |
|---|
| 889 | n/a | _write_ulong(self._file, pos) |
|---|
| 890 | n/a | _write_string(self._file, name) |
|---|
| 891 | n/a | |
|---|
| 892 | n/a | def open(f, mode=None): |
|---|
| 893 | n/a | if mode is None: |
|---|
| 894 | n/a | if hasattr(f, 'mode'): |
|---|
| 895 | n/a | mode = f.mode |
|---|
| 896 | n/a | else: |
|---|
| 897 | n/a | mode = 'rb' |
|---|
| 898 | n/a | if mode in ('r', 'rb'): |
|---|
| 899 | n/a | return Aifc_read(f) |
|---|
| 900 | n/a | elif mode in ('w', 'wb'): |
|---|
| 901 | n/a | return Aifc_write(f) |
|---|
| 902 | n/a | else: |
|---|
| 903 | n/a | raise Error("mode must be 'r', 'rb', 'w', or 'wb'") |
|---|
| 904 | n/a | |
|---|
| 905 | n/a | openfp = open # B/W compatibility |
|---|
| 906 | n/a | |
|---|
| 907 | n/a | if __name__ == '__main__': |
|---|
| 908 | n/a | import sys |
|---|
| 909 | n/a | if not sys.argv[1:]: |
|---|
| 910 | n/a | sys.argv.append('/usr/demos/data/audio/bach.aiff') |
|---|
| 911 | n/a | fn = sys.argv[1] |
|---|
| 912 | n/a | with open(fn, 'r') as f: |
|---|
| 913 | n/a | print("Reading", fn) |
|---|
| 914 | n/a | print("nchannels =", f.getnchannels()) |
|---|
| 915 | n/a | print("nframes =", f.getnframes()) |
|---|
| 916 | n/a | print("sampwidth =", f.getsampwidth()) |
|---|
| 917 | n/a | print("framerate =", f.getframerate()) |
|---|
| 918 | n/a | print("comptype =", f.getcomptype()) |
|---|
| 919 | n/a | print("compname =", f.getcompname()) |
|---|
| 920 | n/a | if sys.argv[2:]: |
|---|
| 921 | n/a | gn = sys.argv[2] |
|---|
| 922 | n/a | print("Writing", gn) |
|---|
| 923 | n/a | with open(gn, 'w') as g: |
|---|
| 924 | n/a | g.setparams(f.getparams()) |
|---|
| 925 | n/a | while 1: |
|---|
| 926 | n/a | data = f.readframes(1024) |
|---|
| 927 | n/a | if not data: |
|---|
| 928 | n/a | break |
|---|
| 929 | n/a | g.writeframes(data) |
|---|
| 930 | n/a | print("Done.") |
|---|