• 语音处理:Python实现Wav序列的声道拆分与合并


    语音处理:Python实现Wav序列的声道拆分与合并

    背景


    项目中有时需要将多声道拆分成单声道,再将单声道进行分别处理,然后再合并为多声道。为提升批量处理音频序列的效率,写了以下Python脚本,供参考。

    实现思路


    这里以输入序列为双声道的wav格式文件为例。

    编码思路

    • 单文件处理逻辑
      • 多声道拆分为单声道
        • 读取单文件序列,获取到分离的声道参数信息和解交织的PCM数据
        • 按一定命名方式,对文件名进行修改适配ch0/ch1/ch..
        • 生成单声道文件,分别写入相应参数和数据
      • 单声道合并为多声道
        • 按指定命名方式,读取相应单声道文件
        • 按原始声道数,将PCM数据进行组合
        • 生成多声道文件,分别写入相应参数和数据
      • 自验证
        • 输入双声道序列
        • 调用多声道拆分函数
        • 调用单声道合并函数
        • 对比合并后序列与拆分前原始序列是否比特一致
    • 批处理调度逻辑
      • 对目录内所有多声道文件获取文件绝对路径
      • 循环调用上述单文件处理逻辑

    其中,利用到了Python自带模块scipy.io里的wavfile包来进行wav文件读写处理。

    Python代码


    import sys
    import os
    import numpy as np
    from scipy.io import wavfile
    
    
    ''''     辅助处理函数     '''''
    
    # 获取当前目录下所有文件对应的绝对路径,dir_in下面不能包含子目录
    def get_dir_files_path(dir_in):
        files_path = []
        for (dirpath, dirnames, filenames) in os.walk(dir_in):
            for filename in filenames:
                files_path += [os.path.join(dirpath, filename)]
        files_path.sort()
        return files_path
    
    
    ''''     声道处理函数     '''''
    
    # 读取wav输入数据,并解交织为[声道数,时序样点]
    def read_wav_data(file_in):
        samplerate, data = wavfile.read(file_in) # data: [time, ch]
        data = np.array(data)
        data = data.T  # data: [ch, time]
        return samplerate, data
    
    
    # 拆分多声道wav为单声道wav
    def split_stereo_to_mono(input_path, output_path):
        # default stereo
        samplerate, data = read_wav_data(input_path)
        ch_num = 0
        for ch in data:
            mono = []
            mono.append(ch)
            file_name = input_path.split('\\')[-1]
            file_name = file_name.split('.')[0]
            #outfile_name = file_name + '_mono_ch{0}_170kbps.wav'.format(ch_num)
            outfile_name = file_name + '_mono_ch{0}.wav'.format(ch_num)
            out_path_file = os.path.join(output_path, outfile_name)
            mono_data = np.array(mono).T
            wavfile.write(out_path_file, samplerate, mono_data)
            ch_num += 1
    
    
    # 合并单声道wav为多声道wav
    def merge_mono_to_stereo(input_path, ref_path, output_path):
        file_name = ref_path.split('\\')[-1]
        file_name = file_name.split('.')[0]
        ch_num = 2
        stereo_data = []
        samplerate = -1
        for i in range(ch_num):
            file_in_name = file_name + '_mono_ch{0}_{1}kbps.wav'.format(i, CONST_TEST_BR)
            file_in_path = os.path.join(input_path, file_in_name)
            samplerate, data = read_wav_data(file_in_path)
            stereo_data.append(data)
        outfile_name = file_name + '_{0}kbps_merge.wav'.format(CONST_TEST_BR * ch_num)
        out_path_file = os.path.join(output_path, outfile_name)
        stereo_data = np.array(stereo_data).T
        wavfile.write(out_path_file, samplerate, stereo_data)
    
    
    # 验证拆分、合并前后数据是否正常
    def seq_closed_process():
        input_path = r'E:\seq_test\tmp\test.wav'
        output_path = r'E:\seq_test\tmp'
        split_stereo_to_mono(input_path, output_path)
        merge_mono_to_stereo(input_path, output_path)
        return
    
    
    # 序列前处理,多声道拆分
    def seq_pre_process(dir_in, dir_out):
        input_files_path = get_dir_files_path(dir_in)
        for input_path in input_files_path:
            split_stereo_to_mono(input_path, dir_out)
        return
    
    
    # 序列后处理,单声道合并
    def seq_post_process(dir_in, dir_ref, dir_out):
        input_files_path = get_dir_files_path(dir_ref)
        for input_path in input_files_path:
            merge_mono_to_stereo(dir_in, input_path, dir_out)
        return
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    相关资料


    1. 语音处理:Python实现音频文件声道分离批量处理,link
    2. Python实践:文件读写功能之txt文本,link
  • 相关阅读:
    2流高手速成记(之六):从SpringBoot到SpringCloudAlibaba
    湖南特色农产品销售系统APP /基于android的农产品销售系统/基于android的购物系统
    问题解决——SSH时出现WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!(转)
    Docker 轻量级可视化工具Portainer
    Hugging News #0821: 新的里程碑:一百万个代码仓库!
    .net MVC下鉴权认证(三)
    HCIA -- 动态路由协议之RIP
    通过字符设备驱动的分步实现编写LED驱动,另外实现特备文件和设备的绑定
    操作系统复习(二):内存管理
    java开发工具IntelliJ IDEA使用教程:检查项目状态
  • 原文地址:https://blog.csdn.net/qq_17256689/article/details/133913231