从 24 位 wav pcm 格式转换为浮点的更快方法? [英] Faster way to convert from 24 bit wav pcm format to float?

查看:26
本文介绍了从 24 位 wav pcm 格式转换为浮点的更快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 24 位 pcm 格式的 wav 文件中读取数据,然后转换为浮点数.我使用的是 Python 2.7.2.

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.

wave 包以字符串形式读取数据,所以我尝试的是:

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?

推荐答案

这似乎相当快,它处理 24 位值,并进行规范化:

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

这篇关于从 24 位 wav pcm 格式转换为浮点的更快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆