Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Stuff to parse WAVE files. |
| 2 | |
| 3 | Usage. |
| 4 | |
| 5 | Reading WAVE files: |
| 6 | f = wave.open(file, 'r') |
| 7 | where file is either the name of a file or an open file pointer. |
| 8 | The open file pointer must have methods read(), seek(), and close(). |
| 9 | When the setpos() and rewind() methods are not used, the seek() |
| 10 | method is not necessary. |
| 11 | |
| 12 | This returns an instance of a class with the following public methods: |
| 13 | getnchannels() -- returns number of audio channels (1 for |
| 14 | mono, 2 for stereo) |
| 15 | getsampwidth() -- returns sample width in bytes |
| 16 | getframerate() -- returns sampling frequency |
| 17 | getnframes() -- returns number of audio frames |
| 18 | getcomptype() -- returns compression type ('NONE' for linear samples) |
| 19 | getcompname() -- returns human-readable version of |
| 20 | compression type ('not compressed' linear samples) |
R David Murray | 671cd32 | 2013-04-10 12:31:43 -0400 | [diff] [blame] | 21 | getparams() -- returns a namedtuple consisting of all of the |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 22 | above in the above order |
| 23 | getmarkers() -- returns None (for compatibility with the |
| 24 | aifc module) |
| 25 | getmark(id) -- raises an error since the mark does not |
| 26 | exist (for compatibility with the aifc module) |
| 27 | readframes(n) -- returns at most n frames of audio |
| 28 | rewind() -- rewind to the beginning of the audio stream |
| 29 | setpos(pos) -- seek to the specified position |
| 30 | tell() -- return the current position |
| 31 | close() -- close the instance (make it unusable) |
| 32 | The position returned by tell() and the position given to setpos() |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 33 | 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] | 34 | file. |
| 35 | The close() method is called automatically when the class instance |
| 36 | is destroyed. |
| 37 | |
| 38 | Writing WAVE files: |
| 39 | f = wave.open(file, 'w') |
| 40 | where file is either the name of a file or an open file pointer. |
| 41 | The open file pointer must have methods write(), tell(), seek(), and |
| 42 | close(). |
| 43 | |
| 44 | This returns an instance of a class with the following public methods: |
| 45 | setnchannels(n) -- set the number of channels |
| 46 | setsampwidth(n) -- set the sample width |
| 47 | setframerate(n) -- set the frame rate |
| 48 | setnframes(n) -- set the number of frames |
| 49 | setcomptype(type, name) |
| 50 | -- set the compression type and the |
| 51 | human-readable compression type |
| 52 | setparams(tuple) |
| 53 | -- set all parameters at once |
| 54 | tell() -- return current position in output file |
| 55 | writeframesraw(data) |
| 56 | -- write audio frames without pathing up the |
| 57 | file header |
| 58 | writeframes(data) |
| 59 | -- write audio frames and patch up the file header |
| 60 | close() -- patch up the file header and close the |
| 61 | output file |
| 62 | You should set the parameters before the first writeframesraw or |
| 63 | writeframes. The total number of frames does not need to be set, |
| 64 | but when it is set to the correct value, the header does not have to |
| 65 | be patched up. |
| 66 | It is best to first set all parameters, perhaps possibly the |
| 67 | compression type, and then write audio frames using writeframesraw. |
| 68 | When all frames have been written, either call writeframes('') or |
| 69 | close() to patch up the sizes in the header. |
| 70 | The close() method is called automatically when the class instance |
| 71 | is destroyed. |
| 72 | """ |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 73 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 74 | import builtins |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 75 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 76 | __all__ = ["open", "openfp", "Error"] |
| 77 | |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 78 | class Error(Exception): |
| 79 | pass |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 80 | |
| 81 | WAVE_FORMAT_PCM = 0x0001 |
| 82 | |
Serhiy Storchaka | d3b7505 | 2013-10-17 23:04:04 +0300 | [diff] [blame] | 83 | _array_fmts = None, 'b', 'h', None, 'i' |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | ebb9c92 | 1999-02-05 22:28:17 +0000 | [diff] [blame] | 85 | import struct |
Serhiy Storchaka | d739bda | 2013-05-29 23:38:00 +0300 | [diff] [blame] | 86 | import sys |
Guido van Rossum | 3601e88 | 1999-08-26 15:50:43 +0000 | [diff] [blame] | 87 | from chunk import Chunk |
R David Murray | 671cd32 | 2013-04-10 12:31:43 -0400 | [diff] [blame] | 88 | from collections import namedtuple |
| 89 | |
Serhiy Storchaka | a44372f | 2013-11-09 23:12:06 +0200 | [diff] [blame] | 90 | def _byteswap3(data): |
| 91 | ba = bytearray(data) |
| 92 | ba[::3] = data[2::3] |
| 93 | ba[2::3] = data[::3] |
| 94 | return bytes(ba) |
| 95 | |
Serhiy Storchaka | 4c6a020 | 2013-09-04 00:28:43 +0300 | [diff] [blame] | 96 | _wave_params = namedtuple('_wave_params', |
R David Murray | 671cd32 | 2013-04-10 12:31:43 -0400 | [diff] [blame] | 97 | 'nchannels sampwidth framerate nframes comptype compname') |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 98 | |
| 99 | class Wave_read: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 100 | """Variables used in this class: |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 102 | These variables are available to the user though appropriate |
| 103 | methods of this class: |
| 104 | _file -- the open file with methods read(), close(), and seek() |
| 105 | set through the __init__() method |
| 106 | _nchannels -- the number of audio channels |
| 107 | available through the getnchannels() method |
| 108 | _nframes -- the number of audio frames |
| 109 | available through the getnframes() method |
| 110 | _sampwidth -- the number of bytes per audio sample |
| 111 | available through the getsampwidth() method |
| 112 | _framerate -- the sampling frequency |
| 113 | available through the getframerate() method |
| 114 | _comptype -- the AIFF-C compression type ('NONE' if AIFF) |
| 115 | available through the getcomptype() method |
| 116 | _compname -- the human-readable AIFF-C compression type |
| 117 | available through the getcomptype() method |
| 118 | _soundpos -- the position in the audio stream |
| 119 | available through the tell() method, set through the |
| 120 | setpos() method |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 122 | These variables are used internally only: |
| 123 | _fmt_chunk_read -- 1 iff the FMT chunk has been read |
| 124 | _data_seek_needed -- 1 iff positioned correctly in audio |
| 125 | file for readframes() |
| 126 | _data_chunk -- instantiation of a chunk class for the DATA chunk |
| 127 | _framesize -- size of one frame in the file |
| 128 | """ |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 130 | def initfp(self, file): |
| 131 | self._convert = None |
| 132 | self._soundpos = 0 |
| 133 | self._file = Chunk(file, bigendian = 0) |
Guido van Rossum | 51a883b | 2007-07-23 21:28:30 +0000 | [diff] [blame] | 134 | if self._file.getname() != b'RIFF': |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 135 | raise Error('file does not start with RIFF id') |
Guido van Rossum | 51a883b | 2007-07-23 21:28:30 +0000 | [diff] [blame] | 136 | if self._file.read(4) != b'WAVE': |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 137 | raise Error('not a WAVE file') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 138 | self._fmt_chunk_read = 0 |
| 139 | self._data_chunk = None |
| 140 | while 1: |
| 141 | self._data_seek_needed = 1 |
| 142 | try: |
| 143 | chunk = Chunk(self._file, bigendian = 0) |
| 144 | except EOFError: |
| 145 | break |
| 146 | chunkname = chunk.getname() |
Guido van Rossum | 51a883b | 2007-07-23 21:28:30 +0000 | [diff] [blame] | 147 | if chunkname == b'fmt ': |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 148 | self._read_fmt_chunk(chunk) |
| 149 | self._fmt_chunk_read = 1 |
Guido van Rossum | 51a883b | 2007-07-23 21:28:30 +0000 | [diff] [blame] | 150 | elif chunkname == b'data': |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 151 | if not self._fmt_chunk_read: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 152 | raise Error('data chunk before fmt chunk') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 153 | self._data_chunk = chunk |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 154 | self._nframes = chunk.chunksize // self._framesize |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 155 | self._data_seek_needed = 0 |
| 156 | break |
| 157 | chunk.skip() |
| 158 | if not self._fmt_chunk_read or not self._data_chunk: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 159 | raise Error('fmt chunk and/or data chunk missing') |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 161 | def __init__(self, f): |
Tim Peters | cfc4178 | 2000-10-09 23:43:55 +0000 | [diff] [blame] | 162 | self._i_opened_the_file = None |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 163 | if isinstance(f, str): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 164 | f = builtins.open(f, 'rb') |
Tim Peters | cfc4178 | 2000-10-09 23:43:55 +0000 | [diff] [blame] | 165 | self._i_opened_the_file = f |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 166 | # else, assume it is an open file object already |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 167 | try: |
| 168 | self.initfp(f) |
| 169 | except: |
| 170 | if self._i_opened_the_file: |
| 171 | f.close() |
| 172 | raise |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 173 | |
Tim Peters | cfc4178 | 2000-10-09 23:43:55 +0000 | [diff] [blame] | 174 | def __del__(self): |
| 175 | self.close() |
R David Murray | c91d5ee | 2013-07-31 13:46:08 -0400 | [diff] [blame] | 176 | |
| 177 | def __enter__(self): |
| 178 | return self |
| 179 | |
| 180 | def __exit__(self, *args): |
| 181 | self.close() |
| 182 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 183 | # |
| 184 | # User visible methods. |
| 185 | # |
| 186 | def getfp(self): |
| 187 | return self._file |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 189 | def rewind(self): |
| 190 | self._data_seek_needed = 1 |
| 191 | self._soundpos = 0 |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 193 | def close(self): |
Tim Peters | cfc4178 | 2000-10-09 23:43:55 +0000 | [diff] [blame] | 194 | if self._i_opened_the_file: |
| 195 | self._i_opened_the_file.close() |
| 196 | self._i_opened_the_file = None |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 197 | self._file = None |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 199 | def tell(self): |
| 200 | return self._soundpos |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 202 | def getnchannels(self): |
| 203 | return self._nchannels |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 205 | def getnframes(self): |
| 206 | return self._nframes |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 208 | def getsampwidth(self): |
| 209 | return self._sampwidth |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 210 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 211 | def getframerate(self): |
| 212 | return self._framerate |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 214 | def getcomptype(self): |
| 215 | return self._comptype |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 217 | def getcompname(self): |
| 218 | return self._compname |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 220 | def getparams(self): |
Serhiy Storchaka | 4c6a020 | 2013-09-04 00:28:43 +0300 | [diff] [blame] | 221 | return _wave_params(self.getnchannels(), self.getsampwidth(), |
R David Murray | 671cd32 | 2013-04-10 12:31:43 -0400 | [diff] [blame] | 222 | self.getframerate(), self.getnframes(), |
| 223 | self.getcomptype(), self.getcompname()) |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 225 | def getmarkers(self): |
| 226 | return None |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 228 | def getmark(self, id): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 229 | raise Error('no marks') |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 231 | def setpos(self, pos): |
| 232 | if pos < 0 or pos > self._nframes: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 233 | raise Error('position not in range') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 234 | self._soundpos = pos |
| 235 | self._data_seek_needed = 1 |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 237 | def readframes(self, nframes): |
| 238 | if self._data_seek_needed: |
| 239 | self._data_chunk.seek(0, 0) |
| 240 | pos = self._soundpos * self._framesize |
| 241 | if pos: |
| 242 | self._data_chunk.seek(pos, 0) |
| 243 | self._data_seek_needed = 0 |
| 244 | if nframes == 0: |
Guido van Rossum | 51a883b | 2007-07-23 21:28:30 +0000 | [diff] [blame] | 245 | return b'' |
Serhiy Storchaka | a44372f | 2013-11-09 23:12:06 +0200 | [diff] [blame] | 246 | if self._sampwidth in (2, 4) and sys.byteorder == 'big': |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 247 | # unfortunately the fromfile() method does not take |
| 248 | # something that only looks like a file object, so |
| 249 | # we have to reach into the innards of the chunk object |
| 250 | import array |
| 251 | chunk = self._data_chunk |
| 252 | data = array.array(_array_fmts[self._sampwidth]) |
Serhiy Storchaka | d3b7505 | 2013-10-17 23:04:04 +0300 | [diff] [blame] | 253 | assert data.itemsize == self._sampwidth |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 254 | nitems = nframes * self._nchannels |
| 255 | if nitems * self._sampwidth > chunk.chunksize - chunk.size_read: |
Benjamin Peterson | 5efea04 | 2010-01-13 03:49:50 +0000 | [diff] [blame] | 256 | nitems = (chunk.chunksize - chunk.size_read) // self._sampwidth |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 257 | data.fromfile(chunk.file.file, nitems) |
| 258 | # "tell" data chunk how much was read |
| 259 | chunk.size_read = chunk.size_read + nitems * self._sampwidth |
| 260 | # do the same for the outermost chunk |
| 261 | chunk = chunk.file |
| 262 | chunk.size_read = chunk.size_read + nitems * self._sampwidth |
| 263 | data.byteswap() |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 264 | data = data.tobytes() |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 265 | else: |
| 266 | data = self._data_chunk.read(nframes * self._framesize) |
Serhiy Storchaka | a44372f | 2013-11-09 23:12:06 +0200 | [diff] [blame] | 267 | if self._sampwidth == 3 and sys.byteorder == 'big': |
| 268 | data = _byteswap3(data) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 269 | if self._convert and data: |
| 270 | data = self._convert(data) |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 271 | self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 272 | return data |
| 273 | |
| 274 | # |
| 275 | # Internal methods. |
| 276 | # |
| 277 | |
| 278 | def _read_fmt_chunk(self, chunk): |
Jesus Cea | e4b86398 | 2012-11-17 03:41:54 +0100 | [diff] [blame] | 279 | wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14)) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 280 | if wFormatTag == WAVE_FORMAT_PCM: |
Jesus Cea | e4b86398 | 2012-11-17 03:41:54 +0100 | [diff] [blame] | 281 | sampwidth = struct.unpack_from('<H', chunk.read(2))[0] |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 282 | self._sampwidth = (sampwidth + 7) // 8 |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 283 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 284 | raise Error('unknown format: %r' % (wFormatTag,)) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 285 | self._framesize = self._nchannels * self._sampwidth |
| 286 | self._comptype = 'NONE' |
| 287 | self._compname = 'not compressed' |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 288 | |
| 289 | class Wave_write: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 290 | """Variables used in this class: |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 291 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 292 | These variables are user settable through appropriate methods |
| 293 | of this class: |
| 294 | _file -- the open file with methods write(), close(), tell(), seek() |
| 295 | set through the __init__() method |
| 296 | _comptype -- the AIFF-C compression type ('NONE' in AIFF) |
| 297 | set through the setcomptype() or setparams() method |
| 298 | _compname -- the human-readable AIFF-C compression type |
| 299 | set through the setcomptype() or setparams() method |
| 300 | _nchannels -- the number of audio channels |
| 301 | set through the setnchannels() or setparams() method |
| 302 | _sampwidth -- the number of bytes per audio sample |
| 303 | set through the setsampwidth() or setparams() method |
| 304 | _framerate -- the sampling frequency |
| 305 | set through the setframerate() or setparams() method |
| 306 | _nframes -- the number of audio frames written to the header |
| 307 | set through the setnframes() or setparams() method |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 308 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 309 | These variables are used internally only: |
| 310 | _datalength -- the size of the audio samples written to the header |
| 311 | _nframeswritten -- the number of frames actually written |
| 312 | _datawritten -- the size of the audio samples actually written |
| 313 | """ |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 314 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 315 | def __init__(self, f): |
Tim Peters | cfc4178 | 2000-10-09 23:43:55 +0000 | [diff] [blame] | 316 | self._i_opened_the_file = None |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 317 | if isinstance(f, str): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 318 | f = builtins.open(f, 'wb') |
Tim Peters | cfc4178 | 2000-10-09 23:43:55 +0000 | [diff] [blame] | 319 | self._i_opened_the_file = f |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 320 | try: |
| 321 | self.initfp(f) |
| 322 | except: |
| 323 | if self._i_opened_the_file: |
| 324 | f.close() |
| 325 | raise |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 326 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 327 | def initfp(self, file): |
| 328 | self._file = file |
| 329 | self._convert = None |
| 330 | self._nchannels = 0 |
| 331 | self._sampwidth = 0 |
| 332 | self._framerate = 0 |
| 333 | self._nframes = 0 |
| 334 | self._nframeswritten = 0 |
| 335 | self._datawritten = 0 |
| 336 | self._datalength = 0 |
Georg Brandl | cc2adbc | 2010-10-30 08:29:28 +0000 | [diff] [blame] | 337 | self._headerwritten = False |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 339 | def __del__(self): |
Tim Peters | cfc4178 | 2000-10-09 23:43:55 +0000 | [diff] [blame] | 340 | self.close() |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 341 | |
R David Murray | c91d5ee | 2013-07-31 13:46:08 -0400 | [diff] [blame] | 342 | def __enter__(self): |
| 343 | return self |
| 344 | |
| 345 | def __exit__(self, *args): |
| 346 | self.close() |
| 347 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 348 | # |
| 349 | # User visible methods. |
| 350 | # |
| 351 | def setnchannels(self, nchannels): |
| 352 | if self._datawritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 353 | raise Error('cannot change parameters after starting to write') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 354 | if nchannels < 1: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 355 | raise Error('bad # of channels') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 356 | self._nchannels = nchannels |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 357 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 358 | def getnchannels(self): |
| 359 | if not self._nchannels: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 360 | raise Error('number of channels not set') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 361 | return self._nchannels |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 362 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 363 | def setsampwidth(self, sampwidth): |
| 364 | if self._datawritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 365 | raise Error('cannot change parameters after starting to write') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 366 | if sampwidth < 1 or sampwidth > 4: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 367 | raise Error('bad sample width') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 368 | self._sampwidth = sampwidth |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 369 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 370 | def getsampwidth(self): |
| 371 | if not self._sampwidth: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 372 | raise Error('sample width not set') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 373 | return self._sampwidth |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 374 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 375 | def setframerate(self, framerate): |
| 376 | if self._datawritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 377 | raise Error('cannot change parameters after starting to write') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 378 | if framerate <= 0: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 379 | raise Error('bad frame rate') |
Mark Dickinson | 64a38c0 | 2010-08-28 17:22:16 +0000 | [diff] [blame] | 380 | self._framerate = int(round(framerate)) |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 382 | def getframerate(self): |
| 383 | if not self._framerate: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 384 | raise Error('frame rate not set') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 385 | return self._framerate |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 386 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 387 | def setnframes(self, nframes): |
| 388 | if self._datawritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 389 | raise Error('cannot change parameters after starting to write') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 390 | self._nframes = nframes |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 392 | def getnframes(self): |
| 393 | return self._nframeswritten |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 394 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 395 | def setcomptype(self, comptype, compname): |
| 396 | if self._datawritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 397 | raise Error('cannot change parameters after starting to write') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 398 | if comptype not in ('NONE',): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 399 | raise Error('unsupported compression type') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 400 | self._comptype = comptype |
| 401 | self._compname = compname |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 402 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 403 | def getcomptype(self): |
| 404 | return self._comptype |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 406 | def getcompname(self): |
| 407 | return self._compname |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 408 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 409 | def setparams(self, params): |
| 410 | nchannels, sampwidth, framerate, nframes, comptype, compname = params |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 411 | if self._datawritten: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 412 | raise Error('cannot change parameters after starting to write') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 413 | self.setnchannels(nchannels) |
| 414 | self.setsampwidth(sampwidth) |
| 415 | self.setframerate(framerate) |
| 416 | self.setnframes(nframes) |
| 417 | self.setcomptype(comptype, compname) |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 418 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 419 | def getparams(self): |
| 420 | if not self._nchannels or not self._sampwidth or not self._framerate: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 421 | raise Error('not all parameters set') |
Serhiy Storchaka | 4c6a020 | 2013-09-04 00:28:43 +0300 | [diff] [blame] | 422 | return _wave_params(self._nchannels, self._sampwidth, self._framerate, |
R David Murray | 671cd32 | 2013-04-10 12:31:43 -0400 | [diff] [blame] | 423 | self._nframes, self._comptype, self._compname) |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 425 | def setmark(self, id, pos, name): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 426 | raise Error('setmark() not supported') |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 427 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 428 | def getmark(self, id): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 429 | raise Error('no marks') |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 430 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 431 | def getmarkers(self): |
| 432 | return None |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 433 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 434 | def tell(self): |
| 435 | return self._nframeswritten |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 436 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 437 | def writeframesraw(self, data): |
| 438 | self._ensure_header_written(len(data)) |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 439 | nframes = len(data) // (self._sampwidth * self._nchannels) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 440 | if self._convert: |
| 441 | data = self._convert(data) |
Serhiy Storchaka | a44372f | 2013-11-09 23:12:06 +0200 | [diff] [blame] | 442 | if self._sampwidth in (2, 4) and sys.byteorder == 'big': |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 443 | import array |
| 444 | data = array.array(_array_fmts[self._sampwidth], data) |
Serhiy Storchaka | d3b7505 | 2013-10-17 23:04:04 +0300 | [diff] [blame] | 445 | assert data.itemsize == self._sampwidth |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 446 | data.byteswap() |
| 447 | data.tofile(self._file) |
| 448 | self._datawritten = self._datawritten + len(data) * self._sampwidth |
| 449 | else: |
Serhiy Storchaka | a44372f | 2013-11-09 23:12:06 +0200 | [diff] [blame] | 450 | if self._sampwidth == 3 and sys.byteorder == 'big': |
| 451 | data = _byteswap3(data) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 452 | self._file.write(data) |
| 453 | self._datawritten = self._datawritten + len(data) |
| 454 | self._nframeswritten = self._nframeswritten + nframes |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 455 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 456 | def writeframes(self, data): |
| 457 | self.writeframesraw(data) |
| 458 | if self._datalength != self._datawritten: |
| 459 | self._patchheader() |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 460 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 461 | def close(self): |
Tim Peters | cfc4178 | 2000-10-09 23:43:55 +0000 | [diff] [blame] | 462 | if self._file: |
R David Murray | 536ffe1 | 2013-07-31 20:48:26 -0400 | [diff] [blame] | 463 | try: |
| 464 | self._ensure_header_written(0) |
| 465 | if self._datalength != self._datawritten: |
| 466 | self._patchheader() |
| 467 | self._file.flush() |
| 468 | finally: |
| 469 | self._file = None |
Tim Peters | cfc4178 | 2000-10-09 23:43:55 +0000 | [diff] [blame] | 470 | if self._i_opened_the_file: |
| 471 | self._i_opened_the_file.close() |
| 472 | self._i_opened_the_file = None |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 473 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 474 | # |
| 475 | # Internal methods. |
| 476 | # |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 477 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 478 | def _ensure_header_written(self, datasize): |
Georg Brandl | cc2adbc | 2010-10-30 08:29:28 +0000 | [diff] [blame] | 479 | if not self._headerwritten: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 480 | if not self._nchannels: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 481 | raise Error('# channels not specified') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 482 | if not self._sampwidth: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 483 | raise Error('sample width not specified') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 484 | if not self._framerate: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 485 | raise Error('sampling rate not specified') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 486 | self._write_header(datasize) |
| 487 | |
| 488 | def _write_header(self, initlength): |
Georg Brandl | cc2adbc | 2010-10-30 08:29:28 +0000 | [diff] [blame] | 489 | assert not self._headerwritten |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 490 | self._file.write(b'RIFF') |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 491 | if not self._nframes: |
Benjamin Peterson | 5efea04 | 2010-01-13 03:49:50 +0000 | [diff] [blame] | 492 | self._nframes = initlength // (self._nchannels * self._sampwidth) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 493 | self._datalength = self._nframes * self._nchannels * self._sampwidth |
Serhiy Storchaka | 7714ebb | 2013-11-16 13:04:00 +0200 | [diff] [blame^] | 494 | try: |
| 495 | self._form_length_pos = self._file.tell() |
| 496 | except (AttributeError, OSError): |
| 497 | self._form_length_pos = None |
Jesus Cea | e4b86398 | 2012-11-17 03:41:54 +0100 | [diff] [blame] | 498 | self._file.write(struct.pack('<L4s4sLHHLLHH4s', |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 499 | 36 + self._datalength, b'WAVE', b'fmt ', 16, |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 500 | WAVE_FORMAT_PCM, self._nchannels, self._framerate, |
| 501 | self._nchannels * self._framerate * self._sampwidth, |
| 502 | self._nchannels * self._sampwidth, |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 503 | self._sampwidth * 8, b'data')) |
Serhiy Storchaka | 7714ebb | 2013-11-16 13:04:00 +0200 | [diff] [blame^] | 504 | if self._form_length_pos is not None: |
| 505 | self._data_length_pos = self._file.tell() |
Jesus Cea | e4b86398 | 2012-11-17 03:41:54 +0100 | [diff] [blame] | 506 | self._file.write(struct.pack('<L', self._datalength)) |
Georg Brandl | cc2adbc | 2010-10-30 08:29:28 +0000 | [diff] [blame] | 507 | self._headerwritten = True |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 508 | |
| 509 | def _patchheader(self): |
Georg Brandl | cc2adbc | 2010-10-30 08:29:28 +0000 | [diff] [blame] | 510 | assert self._headerwritten |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 511 | if self._datawritten == self._datalength: |
| 512 | return |
| 513 | curpos = self._file.tell() |
| 514 | self._file.seek(self._form_length_pos, 0) |
Jesus Cea | e4b86398 | 2012-11-17 03:41:54 +0100 | [diff] [blame] | 515 | self._file.write(struct.pack('<L', 36 + self._datawritten)) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 516 | self._file.seek(self._data_length_pos, 0) |
Jesus Cea | e4b86398 | 2012-11-17 03:41:54 +0100 | [diff] [blame] | 517 | self._file.write(struct.pack('<L', self._datawritten)) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 518 | self._file.seek(curpos, 0) |
| 519 | self._datalength = self._datawritten |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 520 | |
Fred Drake | f960782 | 1999-06-17 15:18:47 +0000 | [diff] [blame] | 521 | def open(f, mode=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 522 | if mode is None: |
| 523 | if hasattr(f, 'mode'): |
| 524 | mode = f.mode |
| 525 | else: |
| 526 | mode = 'rb' |
| 527 | if mode in ('r', 'rb'): |
| 528 | return Wave_read(f) |
| 529 | elif mode in ('w', 'wb'): |
| 530 | return Wave_write(f) |
| 531 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 532 | raise Error("mode must be 'r', 'rb', 'w', or 'wb'") |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 533 | |
| 534 | openfp = open # B/W compatibility |