Faster way to convert from 24 bit wav pcm format to float?

情到浓时终转凉″ 提交于 2019-12-02 05:39:32

问题


I need to read data in from a wav file in 24 bit pcm format, and convert to float. I'm using Python 2.7.2.

The wave package reads the data in as a string, so what I've tried is:

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?


回答1:


This seems to be quite fast, it handles 24-bit values, and it does the normalization:

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


来源:https://stackoverflow.com/questions/9779416/faster-way-to-convert-from-24-bit-wav-pcm-format-to-float

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!