[英]Faster way to convert from 24 bit wav pcm format to float?
I need to read data in from a wav file in 24 bit pcm format, and convert to float. 我需要从24位pcm格式的wav文件中读取数据,然后转换为float。 I'm using Python 2.7.2.
我正在使用Python 2.7.2。
The wave package reads the data in as a string, so what I've tried is: wave包以字符串形式读取数据,因此我尝试了以下操作:
import wave
import numpy as np
import array
import struct
f = wave.open('filename.wav')
# read in entire wav file
wdata = f.readframes(nFrames)
f.close()
# unpack into signed integers and convert to float
data = array.array('f')
for i in range(0,nFrames*3,3):
data.append(float(struct.unpack('<i', '\x00'+ wdata[i:i+3])[0]))
# normalize sample values
data = np.array(data)
data = data / 0x800000
This is quite a bit faster than my earlier approaches, but still quite slow. 这比我以前的方法要快很多,但仍然很慢。 Can anyone suggest a more efficient method?
谁能建议一种更有效的方法?
This seems to be quite fast, it handles 24-bit values, and it does the normalization: 这似乎相当快,它处理24位值,并进行规范化:
from scikits.audiolab import Sndfile
import numpy as np
f = Sndfile(fname, 'r')
data = np.array(f.read_frames(f.nframes), dtype=np.float64)
f.close()
return data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.