python - 将多声道 PyAudio 转换为 NumPy 数组

标签 python numpy pyaudio

我能找到的所有示例都是单声道,CHANNELS = 1。如何使用 PyAudio 中的回调方法读取立体声或多声道输入并将其转换为二维 NumPy 数组或多个一维数组?

对于单声道输入,像这样的东西可以工作:

def callback(in_data, frame_count, time_info, status):
    global result
    global result_waiting

    if in_data:
        result = np.fromstring(in_data, dtype=np.float32)
        result_waiting = True
    else:
        print('no input')

    return None, pyaudio.paContinue

stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=fs,
                output=False,
                input=True,
                frames_per_buffer=fs,
                stream_callback=callback)

但不适用于立体声输入,result 数组的长度是原来的两倍,所以我假设 channel 是交错的,但我找不到这方面的文档。

最佳答案

它似乎是一个样本一个样本地交错,左声道在前。在左声道输入信号和右声道静音的情况下,我得到:

result = [0.2776, -0.0002,  0.2732, -0.0002,  0.2688, -0.0001,  0.2643, -0.0003,  0.2599, ...

因此,要将其分离为立体声流,请 reshape 为二维数组:

result = np.fromstring(in_data, dtype=np.float32)
result = np.reshape(result, (frames_per_buffer, 2))

现在访问左声道,使用result[:, 0],对于右声道,使用result[:, 1]

def decode(in_data, channels):
    """
    Convert a byte stream into a 2D numpy array with 
    shape (chunk_size, channels)

    Samples are interleaved, so for a stereo stream with left channel 
    of [L0, L1, L2, ...] and right channel of [R0, R1, R2, ...], the output 
    is ordered as [L0, R0, L1, R1, ...]
    """
    # TODO: handle data type as parameter, convert between pyaudio/numpy types
    result = np.fromstring(in_data, dtype=np.float32)

    chunk_length = len(result) / channels
    assert chunk_length == int(chunk_length)

    result = np.reshape(result, (chunk_length, channels))
    return result


def encode(signal):
    """
    Convert a 2D numpy array into a byte stream for PyAudio

    Signal should be a numpy array with shape (chunk_size, channels)
    """
    interleaved = signal.flatten()

    # TODO: handle data type as parameter, convert between pyaudio/numpy types
    out_data = interleaved.astype(np.float32).tostring()
    return out_data

关于python - 将多声道 PyAudio 转换为 NumPy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22636499/

相关文章:

Python 使用正则表达式提取 twitter 文本数据中的@user 和 url 链接

python - 使用 NumPy 高效返回带有小数分量的插入点索引

python - 如何根据序列替换 Numpy Ndarrays 中的值

python - 我用 pyAudio 测量什么?

python - 如何分别返回完整的数组列表?

python - 类似 PEP 354 的枚举实现

python - 如何在 PyAudio 上实际播放歌曲?

python - PyAudio:如何以回调/非阻塞模式访问 stream.read() 数据

python - _csv.错误: field larger than field limit (131072)

python - 使用 numpy.savetxt 导出数组时在每行前添加注释