通过一些额外的转换,您可以将24位WAV文件与标准库中的wave模块一起使用.
import wave
import numpy as np
from contextlib import closing
def pcm24to32(data, nchannels=1):
temp = np.zeros((len(data) / 3, 4), dtype='b')
temp[:, 1:] = np.frombuffer(data, dtype='b').reshape(-1, 3)
return temp.view('
def pcm2float(sig, dtype=np.float64):
sig = np.asarray(sig) # make sure it's a NumPy array
assert sig.dtype.kind == 'i', "'sig' must be an array of signed integers!"
dtype = np.dtype(dtype) # allow string input (e.g. 'f')
# Note that 'min' has a greater (by 1) absolute value than 'max'!
# Therefore, we use 'min' here to avoid clipping.
return sig.astype(dtype) / dtype.type(-np.iinfo(sig.dtype).min)
with closing(wave.open('my_24bit_input.wav')) as w:
framerate = w.getframerate()
nframes = w.getnframes()
nchannels = w.getnchannels()
width = w.getsampwidth()
data = w.readframes(nframes)
assert width == 3
pcm = pcm24to32(data, nchannels)
# You can also use np.float64, if you prefer:
normalized = pcm2float(pcm, np.float32)
当然,您也可以使用scikits.audiolab,但请注意当使用除np.float64以外的类型时,目前(版本0.11.0)存在错误(https://github.com/cournape/audiolab/issues/3)!