栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将多通道PyAudio转换为NumPy数组

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将多通道PyAudio转换为NumPy数组

它似乎是逐样本交错的,首先是左声道。通过左声道输入的信号和右声道的静音,我得到:

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

因此,要将其分离为立体声流,可整形为2D数组:

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

现在访问左声道,使用

result[:, 0]
,对于右声道,使用
result[:, 1]

def depre(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 resultdef enpre(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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/641013.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号