Reputation: 266
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?
Upvotes: 0
Views: 2951
Reputation: 266
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.
Upvotes: 0
Reputation: 11214
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.
Upvotes: 3
Reputation: 2517
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...
Upvotes: 2