在Python中用24位数据从立体声波形文件wave中读取单个通道的数据

2024-09-30 22:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想读左边和右边的频道。在

 import wave
 origAudio = wave.open("6980.wav","r")
 frameRate = origAudio.getframerate()
 nChannels = origAudio.getnchannels()
 sampWidth = origAudio.getsampwidth()
 nbframe=origAudio.getnframes()
 da = np.fromstring(origAudio.readframes(48000), dtype=np.int16)
 origAudio.getparams()

参数

^{pr2}$

现在我想用24位数据中的wave文件分离左右声道


Tags: importnpopenwave频道wavnchannelsframerate
2条回答

您可以使用^{},这是一个小模块,我用numpy数组读写WAV文件。在您的情况下:

import wavio

wav = wavio.read("6980.wav")

# wav.data is the numpy array of samples.
# wav.rate is the sampling rate.
# wav.sampwidth is the sample width, in bytes.  For a 24 bit file,
# wav.sampwdith is 3.

left_channel = wav.data[:, 0]
right_channel = wav.data[:, 1]

^{} is on PyPi,源代码在github上的https://github.com/WarrenWeckesser/wavio。在

参数告诉你,你有2个通道的数据,每个采样3字节,在48kHz。因此,当你说readframes(48000)时,你会得到一秒钟的帧,你应该读入一个稍微不同的数据结构:

da = np.fromstring(origAudio.readframes(48000), dtype=np.uint8)

现在应该有48000*2*3字节,即len(da)。要只接收第一个频道,请执行以下操作:

^{pr2}$

也就是说,创建一个整数数组,每个样本一个,然后从源数据复制适当的字节(可以尝试直接从readframes()的结果复制,而跳过创建da)。在

相关问题 更多 >