[英]Faster way to convert from 24 bit wav pcm format to float?
我需要從24位pcm格式的wav文件中讀取數據,然后轉換為float。 我正在使用Python 2.7.2。
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
這比我以前的方法要快很多,但仍然很慢。 誰能建議一種更有效的方法?
這似乎相當快,它處理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.