【问题标题】:Read the data of a single channel from a stereo wave file wave with 24-bit data in Python在 Python 中用 24 位数据从立体声 wav 文件 wav 中读取单个通道的数据
【发布时间】:2016-11-09 12:34:17
【问题描述】:

我想读左声道和右声道。

 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 位数据的波形文件分离左右声道

【问题讨论】:

    标签: python numpy scipy wave


    【解决方案1】:

    您可以使用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,源代码在 github 上https://github.com/WarrenWeckesser/wavio

    【讨论】:

    • 感谢它正在工作,但它可以在 16 位中修改 24 位波签名
    【解决方案2】:

    参数告诉您,您有 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)。

    【讨论】:

    • 谢谢@John Zwing 我要取左右声道数据
    • @mouridetouba 好的,然后尝试上述解决方案。如果它适用于左声道,你可以对右声道做基本相同的操作,但使用 da[3::6]da[4::6]da[5::6]
    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    相关资源
    最近更新 更多