Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Stuff to parse Sun and NeXT audio files. |
| 2 | |
Fred Drake | 24c532a | 2000-10-06 20:28:46 +0000 | [diff] [blame] | 3 | An audio file consists of a header followed by the data. The structure |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 4 | of the header is as follows. |
| 5 | |
| 6 | +---------------+ |
| 7 | | magic word | |
| 8 | +---------------+ |
| 9 | | header size | |
| 10 | +---------------+ |
| 11 | | data size | |
| 12 | +---------------+ |
| 13 | | encoding | |
| 14 | +---------------+ |
| 15 | | sample rate | |
| 16 | +---------------+ |
| 17 | | # of channels | |
| 18 | +---------------+ |
| 19 | | info | |
| 20 | | | |
| 21 | +---------------+ |
| 22 | |
| 23 | The magic word consists of the 4 characters '.snd'. Apart from the |
| 24 | info field, all header fields are 4 bytes in size. They are all |
| 25 | 32-bit unsigned integers encoded in big-endian byte order. |
| 26 | |
| 27 | The header size really gives the start of the data. |
| 28 | The data size is the physical size of the data. From the other |
Fred Drake | 24c532a | 2000-10-06 20:28:46 +0000 | [diff] [blame] | 29 | parameters the number of frames can be calculated. |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 30 | The encoding gives the way in which audio samples are encoded. |
| 31 | Possible values are listed below. |
| 32 | The info field currently consists of an ASCII string giving a |
| 33 | human-readable description of the audio file. The info field is |
| 34 | padded with NUL bytes to the header size. |
| 35 | |
| 36 | Usage. |
| 37 | |
| 38 | Reading audio files: |
| 39 | f = sunau.open(file, 'r') |
| 40 | where file is either the name of a file or an open file pointer. |
| 41 | The open file pointer must have methods read(), seek(), and close(). |
| 42 | When the setpos() and rewind() methods are not used, the seek() |
| 43 | method is not necessary. |
| 44 | |
| 45 | This returns an instance of a class with the following public methods: |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 46 | getnchannels() -- returns number of audio channels (1 for |
| 47 | mono, 2 for stereo) |
| 48 | getsampwidth() -- returns sample width in bytes |
| 49 | getframerate() -- returns sampling frequency |
| 50 | getnframes() -- returns number of audio frames |
| 51 | getcomptype() -- returns compression type ('NONE' or 'ULAW') |
| 52 | getcompname() -- returns human-readable version of |
| 53 | compression type ('not compressed' matches 'NONE') |
| 54 | getparams() -- returns a tuple consisting of all of the |
| 55 | above in the above order |
| 56 | getmarkers() -- returns None (for compatibility with the |
| 57 | aifc module) |
| 58 | getmark(id) -- raises an error since the mark does not |
| 59 | exist (for compatibility with the aifc module) |
| 60 | readframes(n) -- returns at most n frames of audio |
| 61 | rewind() -- rewind to the beginning of the audio stream |
| 62 | setpos(pos) -- seek to the specified position |
| 63 | tell() -- return the current position |
| 64 | close() -- close the instance (make it unusable) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 65 | The position returned by tell() and the position given to setpos() |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 66 | are compatible and have nothing to do with the actual position in the |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 67 | file. |
| 68 | The close() method is called automatically when the class instance |
| 69 | is destroyed. |
| 70 | |
| 71 | Writing audio files: |
| 72 | f = sunau.open(file, 'w') |
| 73 | where file is either the name of a file or an open file pointer. |
| 74 | The open file pointer must have methods write(), tell(), seek(), and |
| 75 | close(). |
| 76 | |
| 77 | This returns an instance of a class with the following public methods: |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 78 | setnchannels(n) -- set the number of channels |
| 79 | setsampwidth(n) -- set the sample width |
| 80 | setframerate(n) -- set the frame rate |
| 81 | setnframes(n) -- set the number of frames |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 82 | setcomptype(type, name) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 83 | -- set the compression type and the |
| 84 | human-readable compression type |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 85 | setparams(tuple)-- set all parameters at once |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 86 | tell() -- return current position in output file |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 87 | writeframesraw(data) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 88 | -- write audio frames without pathing up the |
| 89 | file header |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 90 | writeframes(data) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 91 | -- write audio frames and patch up the file header |
| 92 | close() -- patch up the file header and close the |
| 93 | output file |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 94 | You should set the parameters before the first writeframesraw or |
| 95 | writeframes. The total number of frames does not need to be set, |
| 96 | but when it is set to the correct value, the header does not have to |
| 97 | be patched up. |
| 98 | It is best to first set all parameters, perhaps possibly the |
| 99 | compression type, and then write audio frames using writeframesraw. |
| 100 | When all frames have been written, either call writeframes('') or |
| 101 | close() to patch up the sizes in the header. |
| 102 | The close() method is called automatically when the class instance |
| 103 | is destroyed. |
| 104 | """ |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 105 | |
| 106 | # from <multimedia/audio_filehdr.h> |
| 107 | AUDIO_FILE_MAGIC = 0x2e736e64 |
| 108 | AUDIO_FILE_ENCODING_MULAW_8 = 1 |
| 109 | AUDIO_FILE_ENCODING_LINEAR_8 = 2 |
| 110 | AUDIO_FILE_ENCODING_LINEAR_16 = 3 |
| 111 | AUDIO_FILE_ENCODING_LINEAR_24 = 4 |
| 112 | AUDIO_FILE_ENCODING_LINEAR_32 = 5 |
| 113 | AUDIO_FILE_ENCODING_FLOAT = 6 |
| 114 | AUDIO_FILE_ENCODING_DOUBLE = 7 |
| 115 | AUDIO_FILE_ENCODING_ADPCM_G721 = 23 |
| 116 | AUDIO_FILE_ENCODING_ADPCM_G722 = 24 |
| 117 | AUDIO_FILE_ENCODING_ADPCM_G723_3 = 25 |
| 118 | AUDIO_FILE_ENCODING_ADPCM_G723_5 = 26 |
| 119 | AUDIO_FILE_ENCODING_ALAW_8 = 27 |
| 120 | |
| 121 | # from <multimedia/audio_hdr.h> |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 122 | AUDIO_UNKNOWN_SIZE = 0xFFFFFFFF # ((unsigned)(~0)) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 123 | |
| 124 | _simple_encodings = [AUDIO_FILE_ENCODING_MULAW_8, |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 125 | AUDIO_FILE_ENCODING_LINEAR_8, |
| 126 | AUDIO_FILE_ENCODING_LINEAR_16, |
| 127 | AUDIO_FILE_ENCODING_LINEAR_24, |
| 128 | AUDIO_FILE_ENCODING_LINEAR_32, |
| 129 | AUDIO_FILE_ENCODING_ALAW_8] |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 130 | |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 131 | class Error(Exception): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 132 | pass |
Sjoerd Mullender | f33a69f | 1995-08-14 07:49:51 +0000 | [diff] [blame] | 133 | |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 134 | def _read_u32(file): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 135 | x = 0 |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 136 | for i in range(4): |
| 137 | byte = file.read(1) |
Antoine Pitrou | 7a5dc75 | 2008-08-17 00:38:32 +0000 | [diff] [blame] | 138 | if not byte: |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 139 | raise EOFError |
| 140 | x = x*256 + ord(byte) |
| 141 | return x |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 142 | |
| 143 | def _write_u32(file, x): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 144 | data = [] |
| 145 | for i in range(4): |
| 146 | d, m = divmod(x, 256) |
Antoine Pitrou | 7a5dc75 | 2008-08-17 00:38:32 +0000 | [diff] [blame] | 147 | data.insert(0, int(m)) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 148 | x = d |
Antoine Pitrou | 7a5dc75 | 2008-08-17 00:38:32 +0000 | [diff] [blame] | 149 | file.write(bytes(data)) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 150 | |
| 151 | class Au_read: |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 152 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 153 | def __init__(self, f): |
| 154 | if type(f) == type(''): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 155 | import builtins |
| 156 | f = builtins.open(f, 'rb') |
Antoine Pitrou | 4d98489 | 2010-10-31 21:27:04 +0000 | [diff] [blame] | 157 | self._opened = True |
| 158 | else: |
| 159 | self._opened = False |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 160 | self.initfp(f) |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 161 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 162 | def __del__(self): |
| 163 | if self._file: |
| 164 | self.close() |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 165 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 166 | def initfp(self, file): |
| 167 | self._file = file |
| 168 | self._soundpos = 0 |
| 169 | magic = int(_read_u32(file)) |
| 170 | if magic != AUDIO_FILE_MAGIC: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 171 | raise Error('bad magic number') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 172 | self._hdr_size = int(_read_u32(file)) |
| 173 | if self._hdr_size < 24: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 174 | raise Error('header size too small') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 175 | if self._hdr_size > 100: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 176 | raise Error('header size ridiculously large') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 177 | self._data_size = _read_u32(file) |
| 178 | if self._data_size != AUDIO_UNKNOWN_SIZE: |
| 179 | self._data_size = int(self._data_size) |
| 180 | self._encoding = int(_read_u32(file)) |
| 181 | if self._encoding not in _simple_encodings: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 182 | raise Error('encoding not (yet) supported') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 183 | if self._encoding in (AUDIO_FILE_ENCODING_MULAW_8, |
| 184 | AUDIO_FILE_ENCODING_ALAW_8): |
| 185 | self._sampwidth = 2 |
| 186 | self._framesize = 1 |
| 187 | elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_8: |
| 188 | self._framesize = self._sampwidth = 1 |
| 189 | elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_16: |
| 190 | self._framesize = self._sampwidth = 2 |
| 191 | elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_24: |
| 192 | self._framesize = self._sampwidth = 3 |
| 193 | elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_32: |
| 194 | self._framesize = self._sampwidth = 4 |
| 195 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 196 | raise Error('unknown encoding') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 197 | self._framerate = int(_read_u32(file)) |
| 198 | self._nchannels = int(_read_u32(file)) |
| 199 | self._framesize = self._framesize * self._nchannels |
| 200 | if self._hdr_size > 24: |
| 201 | self._info = file.read(self._hdr_size - 24) |
| 202 | for i in range(len(self._info)): |
Antoine Pitrou | 7a5dc75 | 2008-08-17 00:38:32 +0000 | [diff] [blame] | 203 | if self._info[i] == b'\0': |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 204 | self._info = self._info[:i] |
| 205 | break |
| 206 | else: |
| 207 | self._info = '' |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 208 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 209 | def getfp(self): |
| 210 | return self._file |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 211 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 212 | def getnchannels(self): |
| 213 | return self._nchannels |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 214 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 215 | def getsampwidth(self): |
| 216 | return self._sampwidth |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 217 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 218 | def getframerate(self): |
| 219 | return self._framerate |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 220 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 221 | def getnframes(self): |
| 222 | if self._data_size == AUDIO_UNKNOWN_SIZE: |
| 223 | return AUDIO_UNKNOWN_SIZE |
| 224 | if self._encoding in _simple_encodings: |
| 225 | return self._data_size / self._framesize |
| 226 | return 0 # XXX--must do some arithmetic here |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 227 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 228 | def getcomptype(self): |
| 229 | if self._encoding == AUDIO_FILE_ENCODING_MULAW_8: |
| 230 | return 'ULAW' |
| 231 | elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8: |
| 232 | return 'ALAW' |
| 233 | else: |
| 234 | return 'NONE' |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 235 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 236 | def getcompname(self): |
| 237 | if self._encoding == AUDIO_FILE_ENCODING_MULAW_8: |
| 238 | return 'CCITT G.711 u-law' |
| 239 | elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8: |
| 240 | return 'CCITT G.711 A-law' |
| 241 | else: |
| 242 | return 'not compressed' |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 243 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 244 | def getparams(self): |
| 245 | return self.getnchannels(), self.getsampwidth(), \ |
| 246 | self.getframerate(), self.getnframes(), \ |
| 247 | self.getcomptype(), self.getcompname() |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 248 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 249 | def getmarkers(self): |
| 250 | return None |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 251 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 252 | def getmark(self, id): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 253 | raise Error('no marks') |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 254 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 255 | def readframes(self, nframes): |
| 256 | if self._encoding in _simple_encodings: |
| 257 | if nframes == AUDIO_UNKNOWN_SIZE: |
| 258 | data = self._file.read() |
| 259 | else: |
| 260 | data = self._file.read(nframes * self._framesize * self._nchannels) |
| 261 | if self._encoding == AUDIO_FILE_ENCODING_MULAW_8: |
| 262 | import audioop |
| 263 | data = audioop.ulaw2lin(data, self._sampwidth) |
| 264 | return data |
| 265 | return None # XXX--not implemented yet |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 266 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 267 | def rewind(self): |
| 268 | self._soundpos = 0 |
| 269 | self._file.seek(self._hdr_size) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 270 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 271 | def tell(self): |
| 272 | return self._soundpos |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 273 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 274 | def setpos(self, pos): |
| 275 | if pos < 0 or pos > self.getnframes(): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 276 | raise Error('position not in range') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 277 | self._file.seek(pos * self._framesize + self._hdr_size) |
| 278 | self._soundpos = pos |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 279 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 280 | def close(self): |
Antoine Pitrou | 4d98489 | 2010-10-31 21:27:04 +0000 | [diff] [blame] | 281 | if self._opened and self._file: |
| 282 | self._file.close() |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 283 | self._file = None |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 284 | |
| 285 | class Au_write: |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 286 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 287 | def __init__(self, f): |
| 288 | if type(f) == type(''): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 289 | import builtins |
| 290 | f = builtins.open(f, 'wb') |
Antoine Pitrou | 4d98489 | 2010-10-31 21:27:04 +0000 | [diff] [blame] | 291 | self._opened = True |
| 292 | else: |
| 293 | self._opened = False |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 294 | self.initfp(f) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 295 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 296 | def __del__(self): |
| 297 | if self._file: |
| 298 | self.close() |
Antoine Pitrou | 4d98489 | 2010-10-31 21:27:04 +0000 | [diff] [blame] | 299 | self._file = None |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 300 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 301 | def initfp(self, file): |
| 302 | self._file = file |
| 303 | self._framerate = 0 |
| 304 | self._nchannels = 0 |
| 305 | self._sampwidth = 0 |
| 306 | self._framesize = 0 |
| 307 | self._nframes = AUDIO_UNKNOWN_SIZE |
| 308 | self._nframeswritten = 0 |
| 309 | self._datawritten = 0 |
| 310 | self._datalength = 0 |
Victor Stinner | 7f3652e | 2010-06-07 20:14:04 +0000 | [diff] [blame] | 311 | self._info = b'' |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 312 | self._comptype = 'ULAW' # default is U-law |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 313 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 314 | def setnchannels(self, nchannels): |
| 315 | if self._nframeswritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 316 | raise Error('cannot change parameters after starting to write') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 317 | if nchannels not in (1, 2, 4): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 318 | raise Error('only 1, 2, or 4 channels supported') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 319 | self._nchannels = nchannels |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 320 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 321 | def getnchannels(self): |
| 322 | if not self._nchannels: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 323 | raise Error('number of channels not set') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 324 | return self._nchannels |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 325 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 326 | def setsampwidth(self, sampwidth): |
| 327 | if self._nframeswritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 328 | raise Error('cannot change parameters after starting to write') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 329 | if sampwidth not in (1, 2, 4): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 330 | raise Error('bad sample width') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 331 | self._sampwidth = sampwidth |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 332 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 333 | def getsampwidth(self): |
| 334 | if not self._framerate: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 335 | raise Error('sample width not specified') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 336 | return self._sampwidth |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 337 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 338 | def setframerate(self, framerate): |
| 339 | if self._nframeswritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 340 | raise Error('cannot change parameters after starting to write') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 341 | self._framerate = framerate |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 342 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 343 | def getframerate(self): |
| 344 | if not self._framerate: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 345 | raise Error('frame rate not set') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 346 | return self._framerate |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 347 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 348 | def setnframes(self, nframes): |
| 349 | if self._nframeswritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 350 | raise Error('cannot change parameters after starting to write') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 351 | if nframes < 0: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 352 | raise Error('# of frames cannot be negative') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 353 | self._nframes = nframes |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 354 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 355 | def getnframes(self): |
| 356 | return self._nframeswritten |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 357 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 358 | def setcomptype(self, type, name): |
| 359 | if type in ('NONE', 'ULAW'): |
| 360 | self._comptype = type |
| 361 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 362 | raise Error('unknown compression type') |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 363 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 364 | def getcomptype(self): |
| 365 | return self._comptype |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 366 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 367 | def getcompname(self): |
| 368 | if self._comptype == 'ULAW': |
| 369 | return 'CCITT G.711 u-law' |
| 370 | elif self._comptype == 'ALAW': |
| 371 | return 'CCITT G.711 A-law' |
| 372 | else: |
| 373 | return 'not compressed' |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 374 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 375 | def setparams(self, params): |
| 376 | nchannels, sampwidth, framerate, nframes, comptype, compname = params |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 377 | self.setnchannels(nchannels) |
| 378 | self.setsampwidth(sampwidth) |
| 379 | self.setframerate(framerate) |
| 380 | self.setnframes(nframes) |
| 381 | self.setcomptype(comptype, compname) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 382 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 383 | def getparams(self): |
| 384 | return self.getnchannels(), self.getsampwidth(), \ |
| 385 | self.getframerate(), self.getnframes(), \ |
| 386 | self.getcomptype(), self.getcompname() |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 387 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 388 | def tell(self): |
| 389 | return self._nframeswritten |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 390 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 391 | def writeframesraw(self, data): |
| 392 | self._ensure_header_written() |
| 393 | nframes = len(data) / self._framesize |
| 394 | if self._comptype == 'ULAW': |
| 395 | import audioop |
| 396 | data = audioop.lin2ulaw(data, self._sampwidth) |
| 397 | self._file.write(data) |
| 398 | self._nframeswritten = self._nframeswritten + nframes |
| 399 | self._datawritten = self._datawritten + len(data) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 400 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 401 | def writeframes(self, data): |
| 402 | self.writeframesraw(data) |
| 403 | if self._nframeswritten != self._nframes or \ |
| 404 | self._datalength != self._datawritten: |
| 405 | self._patchheader() |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 406 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 407 | def close(self): |
| 408 | self._ensure_header_written() |
| 409 | if self._nframeswritten != self._nframes or \ |
| 410 | self._datalength != self._datawritten: |
| 411 | self._patchheader() |
| 412 | self._file.flush() |
Antoine Pitrou | 4d98489 | 2010-10-31 21:27:04 +0000 | [diff] [blame] | 413 | if self._opened and self._file: |
| 414 | self._file.close() |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 415 | self._file = None |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 416 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 417 | # |
| 418 | # private methods |
| 419 | # |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 420 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 421 | def _ensure_header_written(self): |
| 422 | if not self._nframeswritten: |
| 423 | if not self._nchannels: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 424 | raise Error('# of channels not specified') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 425 | if not self._sampwidth: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 426 | raise Error('sample width not specified') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 427 | if not self._framerate: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 428 | raise Error('frame rate not specified') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 429 | self._write_header() |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 430 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 431 | def _write_header(self): |
| 432 | if self._comptype == 'NONE': |
| 433 | if self._sampwidth == 1: |
| 434 | encoding = AUDIO_FILE_ENCODING_LINEAR_8 |
| 435 | self._framesize = 1 |
| 436 | elif self._sampwidth == 2: |
| 437 | encoding = AUDIO_FILE_ENCODING_LINEAR_16 |
| 438 | self._framesize = 2 |
| 439 | elif self._sampwidth == 4: |
| 440 | encoding = AUDIO_FILE_ENCODING_LINEAR_32 |
| 441 | self._framesize = 4 |
| 442 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 443 | raise Error('internal error') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 444 | elif self._comptype == 'ULAW': |
| 445 | encoding = AUDIO_FILE_ENCODING_MULAW_8 |
| 446 | self._framesize = 1 |
| 447 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 448 | raise Error('internal error') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 449 | self._framesize = self._framesize * self._nchannels |
| 450 | _write_u32(self._file, AUDIO_FILE_MAGIC) |
| 451 | header_size = 25 + len(self._info) |
| 452 | header_size = (header_size + 7) & ~7 |
| 453 | _write_u32(self._file, header_size) |
| 454 | if self._nframes == AUDIO_UNKNOWN_SIZE: |
| 455 | length = AUDIO_UNKNOWN_SIZE |
| 456 | else: |
| 457 | length = self._nframes * self._framesize |
| 458 | _write_u32(self._file, length) |
| 459 | self._datalength = length |
| 460 | _write_u32(self._file, encoding) |
| 461 | _write_u32(self._file, self._framerate) |
| 462 | _write_u32(self._file, self._nchannels) |
| 463 | self._file.write(self._info) |
Antoine Pitrou | 7a5dc75 | 2008-08-17 00:38:32 +0000 | [diff] [blame] | 464 | self._file.write(b'\0'*(header_size - len(self._info) - 24)) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 465 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 466 | def _patchheader(self): |
| 467 | self._file.seek(8) |
| 468 | _write_u32(self._file, self._datawritten) |
| 469 | self._datalength = self._datawritten |
| 470 | self._file.seek(0, 2) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 471 | |
Fred Drake | 4316135 | 1999-06-22 21:23:23 +0000 | [diff] [blame] | 472 | def open(f, mode=None): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 473 | if mode is None: |
| 474 | if hasattr(f, 'mode'): |
| 475 | mode = f.mode |
| 476 | else: |
| 477 | mode = 'rb' |
| 478 | if mode in ('r', 'rb'): |
| 479 | return Au_read(f) |
| 480 | elif mode in ('w', 'wb'): |
| 481 | return Au_write(f) |
| 482 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 483 | raise Error("mode must be 'r', 'rb', 'w', or 'wb'") |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 484 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 485 | openfp = open |