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') |
Serhiy Storchaka | e06a896 | 2013-09-04 00:43:03 +0300 | [diff] [blame] | 54 | getparams() -- returns a namedtuple consisting of all of the |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 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. |
Serhiy Storchaka | e0fd7ef | 2015-07-10 22:13:40 +0300 | [diff] [blame] | 100 | When all frames have been written, either call writeframes(b'') or |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 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 | |
Serhiy Storchaka | e06a896 | 2013-09-04 00:43:03 +0300 | [diff] [blame] | 106 | from collections import namedtuple |
Brian Curtin | 9f914a0 | 2017-11-10 11:38:25 -0500 | [diff] [blame] | 107 | import warnings |
Serhiy Storchaka | e06a896 | 2013-09-04 00:43:03 +0300 | [diff] [blame] | 108 | |
| 109 | _sunau_params = namedtuple('_sunau_params', |
| 110 | 'nchannels sampwidth framerate nframes comptype compname') |
| 111 | |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 112 | # from <multimedia/audio_filehdr.h> |
| 113 | AUDIO_FILE_MAGIC = 0x2e736e64 |
| 114 | AUDIO_FILE_ENCODING_MULAW_8 = 1 |
| 115 | AUDIO_FILE_ENCODING_LINEAR_8 = 2 |
| 116 | AUDIO_FILE_ENCODING_LINEAR_16 = 3 |
| 117 | AUDIO_FILE_ENCODING_LINEAR_24 = 4 |
| 118 | AUDIO_FILE_ENCODING_LINEAR_32 = 5 |
| 119 | AUDIO_FILE_ENCODING_FLOAT = 6 |
| 120 | AUDIO_FILE_ENCODING_DOUBLE = 7 |
| 121 | AUDIO_FILE_ENCODING_ADPCM_G721 = 23 |
| 122 | AUDIO_FILE_ENCODING_ADPCM_G722 = 24 |
| 123 | AUDIO_FILE_ENCODING_ADPCM_G723_3 = 25 |
| 124 | AUDIO_FILE_ENCODING_ADPCM_G723_5 = 26 |
| 125 | AUDIO_FILE_ENCODING_ALAW_8 = 27 |
| 126 | |
| 127 | # from <multimedia/audio_hdr.h> |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 128 | AUDIO_UNKNOWN_SIZE = 0xFFFFFFFF # ((unsigned)(~0)) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 129 | |
| 130 | _simple_encodings = [AUDIO_FILE_ENCODING_MULAW_8, |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 131 | AUDIO_FILE_ENCODING_LINEAR_8, |
| 132 | AUDIO_FILE_ENCODING_LINEAR_16, |
| 133 | AUDIO_FILE_ENCODING_LINEAR_24, |
| 134 | AUDIO_FILE_ENCODING_LINEAR_32, |
| 135 | AUDIO_FILE_ENCODING_ALAW_8] |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 136 | |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 137 | class Error(Exception): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 138 | pass |
Sjoerd Mullender | f33a69f | 1995-08-14 07:49:51 +0000 | [diff] [blame] | 139 | |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 140 | def _read_u32(file): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 141 | x = 0 |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 142 | for i in range(4): |
| 143 | byte = file.read(1) |
Antoine Pitrou | 7a5dc75 | 2008-08-17 00:38:32 +0000 | [diff] [blame] | 144 | if not byte: |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 145 | raise EOFError |
| 146 | x = x*256 + ord(byte) |
| 147 | return x |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 148 | |
| 149 | def _write_u32(file, x): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 150 | data = [] |
| 151 | for i in range(4): |
| 152 | d, m = divmod(x, 256) |
Antoine Pitrou | 7a5dc75 | 2008-08-17 00:38:32 +0000 | [diff] [blame] | 153 | data.insert(0, int(m)) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 154 | x = d |
Antoine Pitrou | 7a5dc75 | 2008-08-17 00:38:32 +0000 | [diff] [blame] | 155 | file.write(bytes(data)) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 156 | |
| 157 | class Au_read: |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 158 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 159 | def __init__(self, f): |
| 160 | if type(f) == type(''): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 161 | import builtins |
| 162 | f = builtins.open(f, 'rb') |
Antoine Pitrou | 4d98489 | 2010-10-31 21:27:04 +0000 | [diff] [blame] | 163 | self._opened = True |
| 164 | else: |
| 165 | self._opened = False |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 166 | self.initfp(f) |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 167 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 168 | def __del__(self): |
| 169 | if self._file: |
| 170 | self.close() |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 171 | |
Serhiy Storchaka | 34d2013 | 2013-09-05 17:01:53 +0300 | [diff] [blame] | 172 | def __enter__(self): |
| 173 | return self |
| 174 | |
| 175 | def __exit__(self, *args): |
| 176 | self.close() |
| 177 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 178 | def initfp(self, file): |
| 179 | self._file = file |
| 180 | self._soundpos = 0 |
| 181 | magic = int(_read_u32(file)) |
| 182 | if magic != AUDIO_FILE_MAGIC: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 183 | raise Error('bad magic number') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 184 | self._hdr_size = int(_read_u32(file)) |
| 185 | if self._hdr_size < 24: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 186 | raise Error('header size too small') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 187 | if self._hdr_size > 100: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 188 | raise Error('header size ridiculously large') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 189 | self._data_size = _read_u32(file) |
| 190 | if self._data_size != AUDIO_UNKNOWN_SIZE: |
| 191 | self._data_size = int(self._data_size) |
| 192 | self._encoding = int(_read_u32(file)) |
| 193 | if self._encoding not in _simple_encodings: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 194 | raise Error('encoding not (yet) supported') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 195 | if self._encoding in (AUDIO_FILE_ENCODING_MULAW_8, |
| 196 | AUDIO_FILE_ENCODING_ALAW_8): |
| 197 | self._sampwidth = 2 |
| 198 | self._framesize = 1 |
| 199 | elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_8: |
| 200 | self._framesize = self._sampwidth = 1 |
| 201 | elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_16: |
| 202 | self._framesize = self._sampwidth = 2 |
| 203 | elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_24: |
| 204 | self._framesize = self._sampwidth = 3 |
| 205 | elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_32: |
| 206 | self._framesize = self._sampwidth = 4 |
| 207 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 208 | raise Error('unknown encoding') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 209 | self._framerate = int(_read_u32(file)) |
| 210 | self._nchannels = int(_read_u32(file)) |
Serhiy Storchaka | 134cb01 | 2018-03-18 09:55:53 +0200 | [diff] [blame] | 211 | if not self._nchannels: |
| 212 | raise Error('bad # of channels') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 213 | self._framesize = self._framesize * self._nchannels |
| 214 | if self._hdr_size > 24: |
| 215 | self._info = file.read(self._hdr_size - 24) |
Serhiy Storchaka | 74a49ac | 2015-03-20 16:46:19 +0200 | [diff] [blame] | 216 | self._info, _, _ = self._info.partition(b'\0') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 217 | else: |
Serhiy Storchaka | 74a49ac | 2015-03-20 16:46:19 +0200 | [diff] [blame] | 218 | self._info = b'' |
Serhiy Storchaka | 0300a8d | 2013-09-28 21:21:39 +0300 | [diff] [blame] | 219 | try: |
| 220 | self._data_pos = file.tell() |
| 221 | except (AttributeError, OSError): |
| 222 | self._data_pos = None |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 223 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 224 | def getfp(self): |
| 225 | return self._file |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 226 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 227 | def getnchannels(self): |
| 228 | return self._nchannels |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 229 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 230 | def getsampwidth(self): |
| 231 | return self._sampwidth |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 232 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 233 | def getframerate(self): |
| 234 | return self._framerate |
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 getnframes(self): |
| 237 | if self._data_size == AUDIO_UNKNOWN_SIZE: |
| 238 | return AUDIO_UNKNOWN_SIZE |
| 239 | if self._encoding in _simple_encodings: |
Serhiy Storchaka | 0300a8d | 2013-09-28 21:21:39 +0300 | [diff] [blame] | 240 | return self._data_size // self._framesize |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 241 | return 0 # XXX--must do some arithmetic here |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 242 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 243 | def getcomptype(self): |
| 244 | if self._encoding == AUDIO_FILE_ENCODING_MULAW_8: |
| 245 | return 'ULAW' |
| 246 | elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8: |
| 247 | return 'ALAW' |
| 248 | else: |
| 249 | return 'NONE' |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 250 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 251 | def getcompname(self): |
| 252 | if self._encoding == AUDIO_FILE_ENCODING_MULAW_8: |
| 253 | return 'CCITT G.711 u-law' |
| 254 | elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8: |
| 255 | return 'CCITT G.711 A-law' |
| 256 | else: |
| 257 | return 'not compressed' |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 258 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 259 | def getparams(self): |
Serhiy Storchaka | e06a896 | 2013-09-04 00:43:03 +0300 | [diff] [blame] | 260 | return _sunau_params(self.getnchannels(), self.getsampwidth(), |
| 261 | self.getframerate(), self.getnframes(), |
| 262 | self.getcomptype(), self.getcompname()) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 263 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 264 | def getmarkers(self): |
| 265 | return None |
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 getmark(self, id): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 268 | raise Error('no marks') |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 269 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 270 | def readframes(self, nframes): |
| 271 | if self._encoding in _simple_encodings: |
| 272 | if nframes == AUDIO_UNKNOWN_SIZE: |
| 273 | data = self._file.read() |
| 274 | else: |
Serhiy Storchaka | 0300a8d | 2013-09-28 21:21:39 +0300 | [diff] [blame] | 275 | data = self._file.read(nframes * self._framesize) |
| 276 | self._soundpos += len(data) // self._framesize |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 277 | if self._encoding == AUDIO_FILE_ENCODING_MULAW_8: |
| 278 | import audioop |
| 279 | data = audioop.ulaw2lin(data, self._sampwidth) |
| 280 | return data |
| 281 | return None # XXX--not implemented yet |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 282 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 283 | def rewind(self): |
Serhiy Storchaka | 0300a8d | 2013-09-28 21:21:39 +0300 | [diff] [blame] | 284 | if self._data_pos is None: |
| 285 | raise OSError('cannot seek') |
| 286 | self._file.seek(self._data_pos) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 287 | self._soundpos = 0 |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 288 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 289 | def tell(self): |
| 290 | return self._soundpos |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 291 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 292 | def setpos(self, pos): |
| 293 | if pos < 0 or pos > self.getnframes(): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 294 | raise Error('position not in range') |
Serhiy Storchaka | 0300a8d | 2013-09-28 21:21:39 +0300 | [diff] [blame] | 295 | if self._data_pos is None: |
| 296 | raise OSError('cannot seek') |
| 297 | self._file.seek(self._data_pos + pos * self._framesize) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 298 | self._soundpos = pos |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 299 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 300 | def close(self): |
Serhiy Storchaka | 7e7a3db | 2015-04-10 13:24:41 +0300 | [diff] [blame] | 301 | file = self._file |
| 302 | if file: |
| 303 | self._file = None |
| 304 | if self._opened: |
| 305 | file.close() |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 306 | |
| 307 | class Au_write: |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 308 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 309 | def __init__(self, f): |
| 310 | if type(f) == type(''): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 311 | import builtins |
| 312 | f = builtins.open(f, 'wb') |
Antoine Pitrou | 4d98489 | 2010-10-31 21:27:04 +0000 | [diff] [blame] | 313 | self._opened = True |
| 314 | else: |
| 315 | self._opened = False |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 316 | self.initfp(f) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 317 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 318 | def __del__(self): |
| 319 | if self._file: |
| 320 | self.close() |
Antoine Pitrou | 4d98489 | 2010-10-31 21:27:04 +0000 | [diff] [blame] | 321 | self._file = None |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 322 | |
Serhiy Storchaka | 34d2013 | 2013-09-05 17:01:53 +0300 | [diff] [blame] | 323 | def __enter__(self): |
| 324 | return self |
| 325 | |
| 326 | def __exit__(self, *args): |
| 327 | self.close() |
| 328 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 329 | def initfp(self, file): |
| 330 | self._file = file |
| 331 | self._framerate = 0 |
| 332 | self._nchannels = 0 |
| 333 | self._sampwidth = 0 |
| 334 | self._framesize = 0 |
| 335 | self._nframes = AUDIO_UNKNOWN_SIZE |
| 336 | self._nframeswritten = 0 |
| 337 | self._datawritten = 0 |
| 338 | self._datalength = 0 |
Victor Stinner | 7f3652e | 2010-06-07 20:14:04 +0000 | [diff] [blame] | 339 | self._info = b'' |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 340 | self._comptype = 'ULAW' # default is U-law |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 341 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 342 | def setnchannels(self, nchannels): |
| 343 | if self._nframeswritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 344 | raise Error('cannot change parameters after starting to write') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 345 | if nchannels not in (1, 2, 4): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 346 | raise Error('only 1, 2, or 4 channels supported') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 347 | self._nchannels = nchannels |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 348 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 349 | def getnchannels(self): |
| 350 | if not self._nchannels: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 351 | raise Error('number of channels not set') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 352 | return self._nchannels |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 353 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 354 | def setsampwidth(self, sampwidth): |
| 355 | if self._nframeswritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 356 | raise Error('cannot change parameters after starting to write') |
Serhiy Storchaka | 81895f8 | 2013-11-10 21:02:53 +0200 | [diff] [blame] | 357 | if sampwidth not in (1, 2, 3, 4): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 358 | raise Error('bad sample width') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 359 | self._sampwidth = sampwidth |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 360 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 361 | def getsampwidth(self): |
| 362 | if not self._framerate: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 363 | raise Error('sample width not specified') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 364 | return self._sampwidth |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 365 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 366 | def setframerate(self, framerate): |
| 367 | if self._nframeswritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 368 | raise Error('cannot change parameters after starting to write') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 369 | self._framerate = framerate |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 370 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 371 | def getframerate(self): |
| 372 | if not self._framerate: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 373 | raise Error('frame rate not set') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 374 | return self._framerate |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 375 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 376 | def setnframes(self, nframes): |
| 377 | if self._nframeswritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 378 | raise Error('cannot change parameters after starting to write') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 379 | if nframes < 0: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 380 | raise Error('# of frames cannot be negative') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 381 | self._nframes = nframes |
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 getnframes(self): |
| 384 | return self._nframeswritten |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 385 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 386 | def setcomptype(self, type, name): |
| 387 | if type in ('NONE', 'ULAW'): |
| 388 | self._comptype = type |
| 389 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 390 | raise Error('unknown compression type') |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 391 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 392 | def getcomptype(self): |
| 393 | return self._comptype |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 394 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 395 | def getcompname(self): |
| 396 | if self._comptype == 'ULAW': |
| 397 | return 'CCITT G.711 u-law' |
| 398 | elif self._comptype == 'ALAW': |
| 399 | return 'CCITT G.711 A-law' |
| 400 | else: |
| 401 | return 'not compressed' |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 402 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 403 | def setparams(self, params): |
| 404 | nchannels, sampwidth, framerate, nframes, comptype, compname = params |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 405 | self.setnchannels(nchannels) |
| 406 | self.setsampwidth(sampwidth) |
| 407 | self.setframerate(framerate) |
| 408 | self.setnframes(nframes) |
| 409 | self.setcomptype(comptype, compname) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 410 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 411 | def getparams(self): |
Serhiy Storchaka | af722bf | 2013-09-04 14:30:16 +0300 | [diff] [blame] | 412 | return _sunau_params(self.getnchannels(), self.getsampwidth(), |
Serhiy Storchaka | e06a896 | 2013-09-04 00:43:03 +0300 | [diff] [blame] | 413 | self.getframerate(), self.getnframes(), |
| 414 | self.getcomptype(), self.getcompname()) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 415 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 416 | def tell(self): |
| 417 | return self._nframeswritten |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 418 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 419 | def writeframesraw(self, data): |
Serhiy Storchaka | 452bab4 | 2013-11-16 14:01:31 +0200 | [diff] [blame] | 420 | if not isinstance(data, (bytes, bytearray)): |
| 421 | data = memoryview(data).cast('B') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 422 | self._ensure_header_written() |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 423 | if self._comptype == 'ULAW': |
| 424 | import audioop |
| 425 | data = audioop.lin2ulaw(data, self._sampwidth) |
Serhiy Storchaka | 0300a8d | 2013-09-28 21:21:39 +0300 | [diff] [blame] | 426 | nframes = len(data) // self._framesize |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 427 | self._file.write(data) |
| 428 | self._nframeswritten = self._nframeswritten + nframes |
| 429 | self._datawritten = self._datawritten + len(data) |
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 writeframes(self, data): |
| 432 | self.writeframesraw(data) |
| 433 | if self._nframeswritten != self._nframes or \ |
| 434 | self._datalength != self._datawritten: |
| 435 | self._patchheader() |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 436 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 437 | def close(self): |
Serhiy Storchaka | 34d2013 | 2013-09-05 17:01:53 +0300 | [diff] [blame] | 438 | if self._file: |
| 439 | try: |
| 440 | self._ensure_header_written() |
| 441 | if self._nframeswritten != self._nframes or \ |
| 442 | self._datalength != self._datawritten: |
| 443 | self._patchheader() |
| 444 | self._file.flush() |
| 445 | finally: |
Serhiy Storchaka | 7e7a3db | 2015-04-10 13:24:41 +0300 | [diff] [blame] | 446 | file = self._file |
Serhiy Storchaka | 34d2013 | 2013-09-05 17:01:53 +0300 | [diff] [blame] | 447 | self._file = None |
Serhiy Storchaka | 7e7a3db | 2015-04-10 13:24:41 +0300 | [diff] [blame] | 448 | if self._opened: |
| 449 | file.close() |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 450 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 451 | # |
| 452 | # private methods |
| 453 | # |
Sjoerd Mullender | 2a45141 | 1993-12-20 09:36:01 +0000 | [diff] [blame] | 454 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 455 | def _ensure_header_written(self): |
| 456 | if not self._nframeswritten: |
| 457 | if not self._nchannels: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 458 | raise Error('# of channels not specified') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 459 | if not self._sampwidth: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 460 | raise Error('sample width not specified') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 461 | if not self._framerate: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 462 | raise Error('frame rate not specified') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 463 | self._write_header() |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 464 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 465 | def _write_header(self): |
| 466 | if self._comptype == 'NONE': |
| 467 | if self._sampwidth == 1: |
| 468 | encoding = AUDIO_FILE_ENCODING_LINEAR_8 |
| 469 | self._framesize = 1 |
| 470 | elif self._sampwidth == 2: |
| 471 | encoding = AUDIO_FILE_ENCODING_LINEAR_16 |
| 472 | self._framesize = 2 |
Serhiy Storchaka | 81895f8 | 2013-11-10 21:02:53 +0200 | [diff] [blame] | 473 | elif self._sampwidth == 3: |
| 474 | encoding = AUDIO_FILE_ENCODING_LINEAR_24 |
| 475 | self._framesize = 3 |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 476 | elif self._sampwidth == 4: |
| 477 | encoding = AUDIO_FILE_ENCODING_LINEAR_32 |
| 478 | self._framesize = 4 |
| 479 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 480 | raise Error('internal error') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 481 | elif self._comptype == 'ULAW': |
| 482 | encoding = AUDIO_FILE_ENCODING_MULAW_8 |
| 483 | self._framesize = 1 |
| 484 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 485 | raise Error('internal error') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 486 | self._framesize = self._framesize * self._nchannels |
| 487 | _write_u32(self._file, AUDIO_FILE_MAGIC) |
| 488 | header_size = 25 + len(self._info) |
| 489 | header_size = (header_size + 7) & ~7 |
| 490 | _write_u32(self._file, header_size) |
| 491 | if self._nframes == AUDIO_UNKNOWN_SIZE: |
| 492 | length = AUDIO_UNKNOWN_SIZE |
| 493 | else: |
| 494 | length = self._nframes * self._framesize |
Serhiy Storchaka | 0300a8d | 2013-09-28 21:21:39 +0300 | [diff] [blame] | 495 | try: |
| 496 | self._form_length_pos = self._file.tell() |
| 497 | except (AttributeError, OSError): |
| 498 | self._form_length_pos = None |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 499 | _write_u32(self._file, length) |
| 500 | self._datalength = length |
| 501 | _write_u32(self._file, encoding) |
| 502 | _write_u32(self._file, self._framerate) |
| 503 | _write_u32(self._file, self._nchannels) |
| 504 | self._file.write(self._info) |
Antoine Pitrou | 7a5dc75 | 2008-08-17 00:38:32 +0000 | [diff] [blame] | 505 | self._file.write(b'\0'*(header_size - len(self._info) - 24)) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 506 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 507 | def _patchheader(self): |
Serhiy Storchaka | 0300a8d | 2013-09-28 21:21:39 +0300 | [diff] [blame] | 508 | if self._form_length_pos is None: |
| 509 | raise OSError('cannot seek') |
| 510 | self._file.seek(self._form_length_pos) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 511 | _write_u32(self._file, self._datawritten) |
| 512 | self._datalength = self._datawritten |
| 513 | self._file.seek(0, 2) |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 514 | |
Fred Drake | 4316135 | 1999-06-22 21:23:23 +0000 | [diff] [blame] | 515 | def open(f, mode=None): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 516 | if mode is None: |
| 517 | if hasattr(f, 'mode'): |
| 518 | mode = f.mode |
| 519 | else: |
| 520 | mode = 'rb' |
| 521 | if mode in ('r', 'rb'): |
| 522 | return Au_read(f) |
| 523 | elif mode in ('w', 'wb'): |
| 524 | return Au_write(f) |
| 525 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 526 | raise Error("mode must be 'r', 'rb', 'w', or 'wb'") |
Sjoerd Mullender | 43bf0bc | 1993-12-13 11:42:39 +0000 | [diff] [blame] | 527 | |
Brian Curtin | 9f914a0 | 2017-11-10 11:38:25 -0500 | [diff] [blame] | 528 | def openfp(f, mode=None): |
| 529 | warnings.warn("sunau.openfp is deprecated since Python 3.7. " |
| 530 | "Use sunau.open instead.", DeprecationWarning, stacklevel=2) |
| 531 | return open(f, mode=mode) |