1. 从 二进制 pcm 文件中读取数据,并转化位想要的矩阵数组

    with open(audioPath, 'rb') as f:
        audioData = np.fromfile(f, dtype = np.uint16)
    audioData.shape = -1, 8

转换的音频数据是 不确定行,8列数组。

2. 把矩阵转置,以单声道数据为行。

    audioData = audioData.T

转换为 8行的二维数组,每一行就是一个声道的数据。

3. 抽取一个通道的数据。

    ch1 = audioData[4]

4. 把这个通道的数据写入二进制文件

    ch1.tofile("./audio/ch1.pcm")

参考: https://blog.51cto.com/feature09/2316652 https://blog.csdn.net/Tourior/article/details/110791039 https://blog.csdn.net/kebu12345678/article/details/54837245 https://numpy.org/doc/stable/reference/generated/numpy.fromfile.html#:~:text=numpy.fromfile.%20%C2%B6.%20numpy.fromfile%28file%2C%20dtype%3Dfloat%2C%20count%3D-1%2C%20sep%3D%27%27%2C%20offset%3D0%29%20%C2%B6.,tofile%20method%20can%20be%20read%20using%20this%20function. https://www.cnblogs.com/peixu/p/7991715.html https://blog.csdn.net/botao_li/article/details/104378469 https://www.cnblogs.com/noluye/p/11224137.html

标签: python

添加新评论