Linux and python: Combining multiple wave files to one wave file

随声附和 提交于 2019-12-12 05:59:26

问题


I am looking for a way that I can combine multiple wave files into one wave file using python and run it on linux. I don't want to use any add on other than the default shell command line and default python modules. For example, if I have a.wav and b.wav. I want to create a c.wav which start with the content from a.wav then b.wav. I've found wave module, that I can open a wave file and write into a new file. Since i'm really new in this audio world. I still can't figure out how to do it. Below is my code

import struct, wave

waveFileA = wave.open('./a.wav', 'r')
waveFileB = wave.open('./b.wav', 'r')
waveFileC = wave.open('./c.wav', 'w')

lengthA = waveFileA.getnframes()
for i in range(0,lengthA):
    waveFileC.writeframes(waveFileA.readframes(1))

lengthB = waveFileB.getnframes()
for i in range(0,lengthB):
    waveFileC.writeframes(waveFileB.readframes(1))

waveFileA.close()
waveFileB.close()
waveFileC.close()

When i run this code, I got this error:

wave.Error: # channels not specified

Please can any one help me?


回答1:


You need to set the number of channels, sample width, and frame rate:

waveFileC.setnchannels(waveFileA.getnchannels())
waveFileC.setsampwidth(waveFileA.getsampwidth())
waveFileC.setframerate(waveFileA.getframerate())

If you want to handle a.wav and b.wav having different settings, you'll want to use something like pysox to convert them to the same settings, or for nchannels and sampwidth you may be able to tough through it yourself.




回答2:


Looks like you need to call n=waveFileA.getnchannels() to find out how many channels the first input file uses, likewise for waveFileB, then you'll need to use waveFileC.setnchannels(n) to tell it how many channels to put in the outgoing file. I don't know how it will handle input files with different numbers of channels...




回答3:


Here is the answer I am looking for

How to join two wav files using python? (look for a thread by Tom 10)

It's in another thread. some one already solved this problem.



来源:https://stackoverflow.com/questions/12649465/linux-and-python-combining-multiple-wave-files-to-one-wave-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!