Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebb9c92

Browse files
committed
Two important fixes:
(1) on a little-endian platform, don't byteswap; (2) in _patchheader(), there was a missing self._file argument to a _write_long() call.
1 parent 2aaeb52 commit ebb9c92

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

Lib/wave.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@
7878

7979
_array_fmts = None, 'b', 'h', None, 'l'
8080

81+
# Determine endian-ness
82+
import struct
83+
if struct.pack("h", 1) == "\000\001":
84+
big_endian = 1
85+
else:
86+
big_endian = 0
87+
8188
def _read_long(file):
8289
x = 0L
8390
for i in range(4):
@@ -309,7 +316,8 @@ def readframes(self, nframes):
309316
nitems = (self._data_chunk.chunksize - self._data_chunk.size_read) / self._sampwidth
310317
data.fromfile(self._data_chunk.file, nitems)
311318
self._data_chunk.size_read = self._data_chunk.size_read + nitems * self._sampwidth
312-
data.byteswap()
319+
if big_endian:
320+
data.byteswap()
313321
data = data.tostring()
314322
else:
315323
data = self._data_chunk.read(nframes * self._framesize)
@@ -482,7 +490,8 @@ def writeframesraw(self, data):
482490
if self._sampwidth > 1:
483491
import array
484492
data = array.array(_array_fmts[self._sampwidth], data)
485-
data.byteswap()
493+
if big_endian:
494+
data.byteswap()
486495
data.tofile(self._file)
487496
self._datawritten = self._datawritten + len(data) * self._sampwidth
488497
else:
@@ -542,7 +551,7 @@ def _patchheader(self):
542551
return
543552
curpos = self._file.tell()
544553
self._file.seek(self._form_length_pos, 0)
545-
_write_long(36 + self._datawritten)
554+
_write_long(self._file, 36 + self._datawritten)
546555
self._file.seek(self._data_length_pos, 0)
547556
_write_long(self._file, self._datawritten)
548557
self._file.seek(curpos, 0)

0 commit comments

Comments
 (0)