Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

I am using python programming language,I want to join to wav file one at the end of other wav file? I have a question in the forum which suggest how to merge two wav file i.e add the contents of one wav file at certain offset, but I want to join two wav file at the end of each other...

And also i had a prob playing the my own wav file,using winsound module..I was able to play the sound but using the time.sleep for certain time before playin any windows sound,disadvantage wit this is if i wanted to play a sound longer thn time.sleep(N),N sec also,the windows sound wil jst overlap after N sec play the winsound nd stop..

Can anyone help??please kindly suggest to how to solve these prob...

1 Answer

0 votes
by (16.8k points)

Python ships with the wave module that will do what you need. The example below works when the details of the files (mono or stereo, frame rates, etc) are the same:

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()

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...