Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join two wav files using python?

Tags:

python

audio

wav

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...

Thanks in advance

like image 226
kaushik Avatar asked May 23 '10 04:05

kaushik


People also ask

How do I merge MP3 files in python?

Use binary read mode in python to edit the MP3 file. s = open(file_name, 'rb'). read() will put the whole file into a string object representing the raw bytes in your file (e.g. \xeb\xfe\x80 ). You can then search and edit the string, addressing the byte offsets with indeces using brackets: s[n] .

What is Pydub in python?

pydub is a Python library to work with only . wav files. By using this library we can play, split, merge, edit our . wav audio files.


2 Answers

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]) for i in range(len(data)):     output.writeframes(data[i][1]) output.close() 
like image 169
tom10 Avatar answered Sep 22 '22 04:09

tom10


I'm the maintainer of pydub, which is designed to make this sort of thing easy.

from pydub import AudioSegment  sound1 = AudioSegment.from_wav("/path/to/file1.wav") sound2 = AudioSegment.from_wav("/path/to/file2.wav")  combined_sounds = sound1 + sound2 combined_sounds.export("/output/path.wav", format="wav") 

note: pydub is a light wrapper around audioop. So behind the scenes, it's doing essentially what Tom10 mentioned

like image 37
Jiaaro Avatar answered Sep 20 '22 04:09

Jiaaro