mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Issue #19641: Added the audioop.byteswap() function to convert big-endian
samples to little-endian and vice versa.
This commit is contained in:
parent
2b38fc187c
commit
3062c9a6c8
10 changed files with 80 additions and 78 deletions
51
Lib/wave.py
51
Lib/wave.py
|
@ -82,17 +82,12 @@ WAVE_FORMAT_PCM = 0x0001
|
|||
|
||||
_array_fmts = None, 'b', 'h', None, 'i'
|
||||
|
||||
import audioop
|
||||
import struct
|
||||
import sys
|
||||
from chunk import Chunk
|
||||
from collections import namedtuple
|
||||
|
||||
def _byteswap3(data):
|
||||
ba = bytearray(data)
|
||||
ba[::3] = data[2::3]
|
||||
ba[2::3] = data[::3]
|
||||
return bytes(ba)
|
||||
|
||||
_wave_params = namedtuple('_wave_params',
|
||||
'nchannels sampwidth framerate nframes comptype compname')
|
||||
|
||||
|
@ -243,29 +238,9 @@ class Wave_read:
|
|||
self._data_seek_needed = 0
|
||||
if nframes == 0:
|
||||
return b''
|
||||
if self._sampwidth in (2, 4) and sys.byteorder == 'big':
|
||||
# unfortunately the fromfile() method does not take
|
||||
# something that only looks like a file object, so
|
||||
# we have to reach into the innards of the chunk object
|
||||
import array
|
||||
chunk = self._data_chunk
|
||||
data = array.array(_array_fmts[self._sampwidth])
|
||||
assert data.itemsize == self._sampwidth
|
||||
nitems = nframes * self._nchannels
|
||||
if nitems * self._sampwidth > chunk.chunksize - chunk.size_read:
|
||||
nitems = (chunk.chunksize - chunk.size_read) // self._sampwidth
|
||||
data.fromfile(chunk.file.file, nitems)
|
||||
# "tell" data chunk how much was read
|
||||
chunk.size_read = chunk.size_read + nitems * self._sampwidth
|
||||
# do the same for the outermost chunk
|
||||
chunk = chunk.file
|
||||
chunk.size_read = chunk.size_read + nitems * self._sampwidth
|
||||
data.byteswap()
|
||||
data = data.tobytes()
|
||||
else:
|
||||
data = self._data_chunk.read(nframes * self._framesize)
|
||||
if self._sampwidth == 3 and sys.byteorder == 'big':
|
||||
data = _byteswap3(data)
|
||||
data = self._data_chunk.read(nframes * self._framesize)
|
||||
if self._sampwidth != 1 and sys.byteorder == 'big':
|
||||
data = audioop.byteswap(data, self._sampwidth)
|
||||
if self._convert and data:
|
||||
data = self._convert(data)
|
||||
self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
|
||||
|
@ -441,20 +416,10 @@ class Wave_write:
|
|||
nframes = len(data) // (self._sampwidth * self._nchannels)
|
||||
if self._convert:
|
||||
data = self._convert(data)
|
||||
if self._sampwidth in (2, 4) and sys.byteorder == 'big':
|
||||
import array
|
||||
a = array.array(_array_fmts[self._sampwidth])
|
||||
a.frombytes(data)
|
||||
data = a
|
||||
assert data.itemsize == self._sampwidth
|
||||
data.byteswap()
|
||||
data.tofile(self._file)
|
||||
self._datawritten = self._datawritten + len(data) * self._sampwidth
|
||||
else:
|
||||
if self._sampwidth == 3 and sys.byteorder == 'big':
|
||||
data = _byteswap3(data)
|
||||
self._file.write(data)
|
||||
self._datawritten = self._datawritten + len(data)
|
||||
if self._sampwidth != 1 and sys.byteorder == 'big':
|
||||
data = audioop.byteswap(data, self._sampwidth)
|
||||
self._file.write(data)
|
||||
self._datawritten += len(data)
|
||||
self._nframeswritten = self._nframeswritten + nframes
|
||||
|
||||
def writeframes(self, data):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue