gpt4 book ai didi

python - 解释 WAV 数据

转载 作者:太空狗 更新时间:2023-10-29 21:12:32 26 4
gpt4 key购买 nike

我正在尝试编写一个程序来显示 PCM 数据。我一直很沮丧地试图找到一个具有正确抽象级别的库,但我找到了 python wave 库并一直在使用它。但是,我不确定如何解释这些数据。

wave.getparams 函数返回(2 channel ,2 字节,44100 赫兹,96333 帧,无压缩,无压缩)。这一切看起来很愉快,但后来我尝试打印一个帧:'\xc0\xff\xd0\xff',它是 4 个字节。我想一个帧可能是 2 个样本,但歧义并没有就此结束。

96333 帧 * 2 个样本/帧 *(1/44.1k 秒/样本)= 4.3688 秒

但是,iTunes 报告的时间接近 2 秒,根据文件大小和比特率计算得出的时间大约为 2.7 秒。这是怎么回事?

此外,我如何知道字节是有符号的还是无符号的?

非常感谢!

最佳答案

感谢您的帮助!我让它工作了,我会在这里发布解决方案供大家使用,以防其他可怜的人需要它:

import wave
import struct

def pcm_channels(wave_file):
"""Given a file-like object or file path representing a wave file,
decompose it into its constituent PCM data streams.

Input: A file like object or file path
Output: A list of lists of integers representing the PCM coded data stream channels
and the sample rate of the channels (mixed rate channels not supported)
"""
stream = wave.open(wave_file,"rb")

num_channels = stream.getnchannels()
sample_rate = stream.getframerate()
sample_width = stream.getsampwidth()
num_frames = stream.getnframes()

raw_data = stream.readframes( num_frames ) # Returns byte data
stream.close()

total_samples = num_frames * num_channels

if sample_width == 1:
fmt = "%iB" % total_samples # read unsigned chars
elif sample_width == 2:
fmt = "%ih" % total_samples # read signed 2 byte shorts
else:
raise ValueError("Only supports 8 and 16 bit audio formats.")

integer_data = struct.unpack(fmt, raw_data)
del raw_data # Keep memory tidy (who knows how big it might be)

channels = [ [] for time in range(num_channels) ]

for index, value in enumerate(integer_data):
bucket = index % num_channels
channels[bucket].append(value)

return channels, sample_rate

关于python - 解释 WAV 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2226853/

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