从Python中24位数据的立体声波形文件wave中读取单个通道的数据
Read the data of a single channel from a stereo wave file wave with 24-bit data in 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()
参数
(2, 3, 48000, 2883584, 'NONE', 'not compressed')
现在我想用24位数据的wave文件来分离左右声道
参数告诉您有 2 个数据通道,每个样本 3 个字节,频率为 48kHz。因此,当您说 readframes(48000)
时,您会得到一秒钟的帧,您可能应该将其读入稍微不同的数据结构:
da = np.fromstring(origAudio.readframes(48000), dtype=np.uint8)
现在你应该有 48000 * 2 * 3 个字节,即 len(da)
。要只使用第一个频道,您可以这样做:
chan1 = np.zeros(48000, np.uint32)
chan1bytes = chan1.view(np.uint8)
chan1bytes[0::4] = da[0::6]
chan1bytes[1::4] = da[1::6]
chan1bytes[2::4] = da[2::6]
也就是说,您制作了一个整数数组,每个样本一个,然后从源数据复制适当的字节(您可以尝试直接从 readframes()
的结果复制并跳过创建 da
).
您可以使用wavio
,这是我编写的一个使用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]
wavio
is on PyPi, and the source is on github at https://github.com/WarrenWeckesser/wavio.
我想看左右声道
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()
参数
(2, 3, 48000, 2883584, 'NONE', 'not compressed')
现在我想用24位数据的wave文件来分离左右声道
参数告诉您有 2 个数据通道,每个样本 3 个字节,频率为 48kHz。因此,当您说 readframes(48000)
时,您会得到一秒钟的帧,您可能应该将其读入稍微不同的数据结构:
da = np.fromstring(origAudio.readframes(48000), dtype=np.uint8)
现在你应该有 48000 * 2 * 3 个字节,即 len(da)
。要只使用第一个频道,您可以这样做:
chan1 = np.zeros(48000, np.uint32)
chan1bytes = chan1.view(np.uint8)
chan1bytes[0::4] = da[0::6]
chan1bytes[1::4] = da[1::6]
chan1bytes[2::4] = da[2::6]
也就是说,您制作了一个整数数组,每个样本一个,然后从源数据复制适当的字节(您可以尝试直接从 readframes()
的结果复制并跳过创建 da
).
您可以使用wavio
,这是我编写的一个使用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]
wavio
is on PyPi, and the source is on github at https://github.com/WarrenWeckesser/wavio.