Changeset 388 for python/vendor/current/Lib/wave.py
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Lib/wave.py
r2 r388 81 81 WAVE_FORMAT_PCM = 0x0001 82 82 83 _array_fmts = None, 'b', 'h', None, ' l'83 _array_fmts = None, 'b', 'h', None, 'i' 84 84 85 85 # Determine endian-ness … … 239 239 chunk = self._data_chunk 240 240 data = array.array(_array_fmts[self._sampwidth]) 241 assert data.itemsize == self._sampwidth 241 242 nitems = nframes * self._nchannels 242 243 if nitems * self._sampwidth > chunk.chunksize - chunk.size_read: … … 262 263 263 264 def _read_fmt_chunk(self, chunk): 264 wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('< hhllh', chunk.read(14))265 wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('<HHLLH', chunk.read(14)) 265 266 if wFormatTag == WAVE_FORMAT_PCM: 266 sampwidth = struct.unpack('< h', chunk.read(2))[0]267 sampwidth = struct.unpack('<H', chunk.read(2))[0] 267 268 self._sampwidth = (sampwidth + 7) // 8 268 269 else: … … 320 321 self._datawritten = 0 321 322 self._datalength = 0 323 self._headerwritten = False 322 324 323 325 def __del__(self): … … 385 387 return self._compname 386 388 387 def setparams(self, (nchannels, sampwidth, framerate, nframes, comptype, compname)): 389 def setparams(self, params): 390 nchannels, sampwidth, framerate, nframes, comptype, compname = params 388 391 if self._datawritten: 389 392 raise Error, 'cannot change parameters after starting to write' … … 420 423 import array 421 424 data = array.array(_array_fmts[self._sampwidth], data) 425 assert data.itemsize == self._sampwidth 422 426 data.byteswap() 423 427 data.tofile(self._file) … … 435 439 def close(self): 436 440 if self._file: 437 self._ensure_header_written(0) 438 if self._datalength != self._datawritten: 439 self._patchheader() 440 self._file.flush() 441 self._file = None 441 try: 442 self._ensure_header_written(0) 443 if self._datalength != self._datawritten: 444 self._patchheader() 445 self._file.flush() 446 finally: 447 self._file = None 442 448 if self._i_opened_the_file: 443 449 self._i_opened_the_file.close() … … 449 455 450 456 def _ensure_header_written(self, datasize): 451 if not self._ datawritten:457 if not self._headerwritten: 452 458 if not self._nchannels: 453 459 raise Error, '# channels not specified' … … 459 465 460 466 def _write_header(self, initlength): 467 assert not self._headerwritten 461 468 self._file.write('RIFF') 462 469 if not self._nframes: … … 464 471 self._datalength = self._nframes * self._nchannels * self._sampwidth 465 472 self._form_length_pos = self._file.tell() 466 self._file.write(struct.pack('< l4s4slhhllhh4s',473 self._file.write(struct.pack('<L4s4sLHHLLHH4s', 467 474 36 + self._datalength, 'WAVE', 'fmt ', 16, 468 475 WAVE_FORMAT_PCM, self._nchannels, self._framerate, … … 471 478 self._sampwidth * 8, 'data')) 472 479 self._data_length_pos = self._file.tell() 473 self._file.write(struct.pack('<l', self._datalength)) 480 self._file.write(struct.pack('<L', self._datalength)) 481 self._headerwritten = True 474 482 475 483 def _patchheader(self): 484 assert self._headerwritten 476 485 if self._datawritten == self._datalength: 477 486 return 478 487 curpos = self._file.tell() 479 488 self._file.seek(self._form_length_pos, 0) 480 self._file.write(struct.pack('< l', 36 + self._datawritten))489 self._file.write(struct.pack('<L', 36 + self._datawritten)) 481 490 self._file.seek(self._data_length_pos, 0) 482 self._file.write(struct.pack('< l', self._datawritten))491 self._file.write(struct.pack('<L', self._datawritten)) 483 492 self._file.seek(curpos, 0) 484 493 self._datalength = self._datawritten
Note:
See TracChangeset
for help on using the changeset viewer.