我正在尝试编写一个显示PCM数据的程序.我一直非常沮丧地试图找到一个具有正确抽象级别的库,但我找到了python wave库并且一直在使用它.但是,我不确定如何解释数据.
wave.getparams函数返回(2个通道,2个字节,44100 Hz,96333帧,无压缩,无压缩).这一切看起来都很愉快,但后来我尝试打印一个帧:'\ xc0\xff\xd0\xff'这是4个字节.我想一个帧可能是2个样本,但模糊不会在那里结束.
96333帧*2个样本/帧*(1/44.1k秒/样本)= 4.3688秒
然而,iTunes报告时间接近2秒,基于文件大小和比特率的计算在2.7秒的范围内.这里发生了什么?
另外,我怎么知道字节是有符号还是无符号?
非常感谢!
Sap*_*Sun 19
谢谢您的帮助!我得到了它的工作,我会在这里发布解决方案供大家使用,以防其他一些可怜的灵魂需要它:
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
Run Code Online (Sandbox Code Playgroud)