gpt4 book ai didi

Python Wave 字节数据

转载 作者:太空狗 更新时间:2023-10-29 22:02:48 27 4
gpt4 key购买 nike

我正在尝试从 .wav 文件中读取数据。

import wave
wr = wave.open("~/01 Road.wav", 'r')
# sample width is 2 bytes
# number of channels is 2
wave_data = wr.readframes(1)
print(wave_data)

这给出:

b'\x00\x00\x00\x00'

这是歌曲的“第一帧”。这4个字节显然对应每帧的(2 channel *2字节样本宽度)字节,但是每个字节对应什么?

特别是,我正在尝试将其转换为单声道振幅信号。

最佳答案

如果您想了解“框架”是什么,您必须阅读 wave 文件格式的标准。例如:https://web.archive.org/web/20140221054954/http://home.roadrunner.com/~jgglatt/tech/wave.htm

来自该文档:

The sample points that are meant to be "played" ie, sent to a Digital to Analog Converter(DAC) simultaneously are collectively called a sample frame. In the example of our stereo waveform, every two sample points makes up another sample frame. This is illustrated below for that stereo example.

sample       sample              sample
frame 0 frame 1 frame N
_____ _____ _____ _____ _____ _____
| ch1 | ch2 | ch1 | ch2 | . . . | ch1 | ch2 |
|_____|_____|_____|_____| |_____|_____|
_____
| | = one sample point
|_____|

要转换为单声道,您可以这样做,

import wave

def stereo_to_mono(hex1, hex2):
"""average two hex string samples"""
return hex((ord(hex1) + ord(hex2))/2)

wr = wave.open('piano2.wav','r')

nchannels, sampwidth, framerate, nframes, comptype, compname = wr.getparams()

ww = wave.open('piano_mono.wav','wb')
ww.setparams((1,sampwidth,framerate,nframes,comptype,compname))

frames = wr.readframes(wr.getnframes()-1)

new_frames = ''

for (s1, s2) in zip(frames[0::2],frames[1::2]):
new_frames += stereo_to_mono(s1,s2)[2:].zfill(2).decode('hex')

ww.writeframes(new_frames)

从立体声到单声道没有明确的方法。你可以只删除一个 channel 。上面,我正在对 channel 进行平均。这完全取决于您的应用。

关于Python Wave 字节数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20677390/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com