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