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