3

I saw a post that joined two .wav files together, but I was wondering how can I join multiple .wav files using python? I am using python 3.6.0. If anyone has a way to do this please teach me. I saw that the other post ask for joining 2 .wav files and with that I used this code form the comments there:

import wave
infiles = ["sound_1.wav", "sound_2.wav"]
outfile = "sounds.wav"

data= []
for infile in infiles:
    w = wave.open(infile, '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 want to read my .wav files from a path then join it as one. I was thinking that after I add the first 2 .wav files I would delete them and just keep joining and deleting until only one .wav file remains like this:

file:
sound1.wav
sound2.wav
sound3.wav

code:
sound1.wav + sound2.wav = sound4.wav

file:
sound3.wav
sound4.wav

code:
sound4.wav + sound3.wav = sound5.wav

file:
sound5.wav

I just don't know how to code this. I am new to coding with python and I am not that good with coding programming languages in general. Thanks in advances.

Haperious
  • 43
  • 1
  • 6
  • https://stackoverflow.com/questions/2890703/how-to-join-two-wav-files-using-python – Boran Oct 08 '18 at 08:59
  • What is joining multiple files if not joining two files repeatedly? – Mr. T Oct 08 '18 at 09:24
  • Yes, that is the post I saw joining two .wav files. I just don't know how to add more onto the two .wav files. Sorry for the trouble I am new and not that good at programming with python. – Haperious Oct 08 '18 at 11:51
  • `output.writeframes(data[0][1]) output.writeframes(data[1][1])` this is the part of the code which hardcodes joining only two files together. Simply put all your filenames into the `infiles` list and replace the previous code with: `for i in range(len(data)): output.writeframes(data[i][1])`. This will join arbitrarily many files. – Ardweaden May 30 '19 at 10:45

1 Answers1

3

Use module like pydub

#!/usr/bin/env python
from pydub import AudioSegment
sound1 = AudioSegment.from_wav("filename01.wav")
sound2 = AudioSegment.from_wav("filename02.wav")
sound3 = AudioSegment.from_wav("filename03.wav")
combined_sounds = sound1 + sound2 + sound3 
combined_sounds.export("joinedFile.wav", format="wav")
Deepak Garud
  • 977
  • 10
  • 18