使用Python音频双通道分离

某些音频是双方对话,有可能需要对音频作通道的分离。

示例代码如下:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 """
 4 音频双通道分离
 5 """
 6 import sys
 7 import numpy as np
 8 from scipy.io import wavfile
 9 from converter import mp3_to_wav
10 
11 
12 def split_channel(wav_path, left_wav_path, right_wav_path):
13     """
14     通道分离
15     :param wav_path: wav音频的路径
16     :param left_wav_path: 左声道的wav音频路径
17     :param right_wav_path: 右声道的wav音频路径
18     :return None:
19     """
20     try:
21         sample_rate, wav_data = wavfile.read(wav_path)
22         left = []
23         right = []
24         for item in wav_data:
25             left.append(item[0])
26             right.append(item[1])
27         wavfile.write(left_wav_path, sample_rate, np.array(left))
28         wavfile.write(right_wav_path, sample_rate, np.array(right))
29     except IOError as e:
30         print('error is %s' % str(e))
31     except:
32         print('other error', sys.exc_info())
33 
34 
35 if __name__ == '__main__':
36     mp3_to_wav('input/test.mp3', 'tmp/tmp.wav')
37     split_channel('tmp/tmp.wav', 'output/left.wav', 'output/right.wav')

其中调用了一个自定义的库,converter.py 加入了代码:

 1 from pydub import AudioSegment
 2 
 3 
 4 def mp3_to_wav(source, destin):
 5     """
 6     mp3 转 wav
 7     :param source:
 8     :param destin:
 9     :return None:
10     """
11     data = AudioSegment.from_mp3(source)
12     data.export(destin, format='wav')

本代码示例可以生成两个通道分离后的 wav 文件。

注意:如果源文件的格式是 wav 文件,可以省掉转换格式的过程。

 

转载于:https://www.cnblogs.com/noluye/p/11224137.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ReadMe Release Version beta_1.0 index.py imageMatlab.py This is more or less a wrapper for Matplotlib imaging functions such that their behavior is equivalent, in terms of colormap, aspect and so forth, to the expected behavior of Matlab's functions. sepVocal.py This script can be used to execute the desired separation. See below for an example of use of this file. SIMM.py This script implements the actual algorithm for parameter estimation. It is mainly used by sepVocal.py. tracking.py The Viterbi decoding algorithm is implemented in this script. Requirements: These scripts have been tested with Python 2.7, The packages that are required to run the scripts are pydub,ffmepg, Numpy, Spicy, Matplotlib. One can respectively find the latest versions at the following addresses: http://pydub.com/ https://ffmpeg.org http://numpy.org/ http://scipy.org/ http://matplotlib.sourceforge.net/ Notes: Prefer recent versions of the above packages, in order to avoid compatibility issues, notably for Matplotlib. Note that this latter package is not necessary for the program to run, although you might want to watch a bit what is happening! Spicy should be version 0.8+, since we use its io.wavefile module to read the wave files. We once used the audio lab module, but it would seem that it is a bit more complicated to install (with the benefit that many more file formats are allowed). Usage: The easy way to use these scripts is to run the exec package of our release version: http://www.github.com/beata_1.0 for more develop: you can run the index.py on pycharm directly. note: the output files will create under you source wav file. ContactMe Email:xlzhang14@fudan.edu.cn

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值