简体   繁体   中英

Concatenate wav files using wave in python

I am trying to concatenate a list of wav files into a single continuous wav file. I have used the following snippet but the output is not correct, sounds almost like the files are on top of each other. audio_files is a list with .wav filenames that are playing as expected.

This is my current code:

outfile = "sounds.wav"

    data= []
    for wav_file in audio_files:
        w = wave.open(wav_file, 'rb')
        data.append( [w.getparams(), w.readframes(w.getnframes())] )
        w.close()
    output = wave.open(outfile, 'wb')
    output.setparams(data[0][0])
    output.writeframes(data[0][1])
    output.writeframes(data[1][1])
    output.close()

I am assuming that you were using this question as your reference. However, the code that you have taken only takes into account 2 wav files because that is what the original question had asked.

Although I am not if this will fix the problem of the sound being on top of each other, you should iterate through each item in your data list.

output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
for params,frames in data:
    output.writeframes(frames)
output.close()

to ensure you are putting in the frames from each file you have.

One thing to keep in mind is that the params for your new wav files might be specific to each file and you might want to check that getparams() is returning similar results for each file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM