如何使用Python正确解码.wav [英] How to properly decode .wav with Python

查看:297
本文介绍了如何使用Python正确解码.wav的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写WAVE音频文件的基本频率分析,但是当涉及从WAVE帧转换为整数时我遇到了麻烦。

I am coding a basic frequency analisys of WAVE audio files, but I have trouble when it comes to convertion from WAVE frames to integer.

这是相关部分我的代码:

Here is the relevant part of my code:

import wave
track = wave.open('/some_path/my_audio.wav', 'r')

byt_depth = track.getsampwidth() #Byte depth of the file in BYTES
frame_rate = track.getframerate()
buf_size = 512

def byt_sum (word):
#convert a string of n bytes into an int in [0;8**n-1]
    return sum( (256**k)*word[k] for k in range(len(word)) )

raw_buf = track.readframes(buf_size)
'''
One frame is a string of n bytes, where n = byt_depth.
For instance, with a 24bits-encoded file, track.readframe(1) could be:
b'\xff\xfe\xfe'.
raw_buf[n] returns an int in [0;255]
'''

sample_buf = [byt_sum(raw_buf[byt_depth*k:byt_depth*(k+1)])
              - 2**(8*byt_depth-1) for k in range(buf_size)]

问题是:当我为单个正弦信号绘制 sample_buf 时,我得到
一种替代的,破坏的正弦信号
我无法弄清楚为什么信号与udpside-down重叠。

Problem is: when I plot sample_buf for a single sine signal, I get an alternative, wrecked sine signal. I can't figure out why the signal overlaps udpside-down.

任何想法?

PS:因为我是法国人,我的英语非常犹豫。如果有丑陋的错误,请随时编辑。

P.S.: Since I'm French, my English is quite hesitating. Feel free to edit if there are ugly mistakes.

推荐答案

这可能是因为您需要使用无符号值来表示16位样本。请参阅 https://en.wikipedia.org/wiki/Pulse-code_modulation

It might be because you need to use an unsigned value for representing the 16bit samples. See https://en.wikipedia.org/wiki/Pulse-code_modulation

尝试为每个样本添加32767。

Try to add 32767 to each sample.

此外,您应该使用python struct module 解码缓冲区。

Also you should use the python struct module to decode the buffer.

import struct
buff_size = 512
# 'H' is for unsigned 16 bit integer, try 'h' also
sample_buff = struct.unpack('H'*buf_size, raw_buf)

这篇关于如何使用Python正确解码.wav的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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