tags:

views:

98

answers:

1

How to get the contents of the wav file into array so as to cut the required segment and convert it back to wav format using python?? My prob is similar to "ROMANs" prob,i hav seen earlier in the post at this site..

Basically,i want to combine parts of different wav file into one wav file?? if there is ne other apporach thn takin the contents into an array and cuting part and combining and again converting bac? please suggest...

edited:

I prefer unpacking the contents of the wave file into an array and editing by cutting the required segment of sound from the wav file,as i am working on speech processing,and guess this way would be easy to enchance the quality of sound later...

can ne one suggest a way for this?? Plz help..

Thanks in advance.

+1  A: 

There are a few libraries you can use for handling media files in general, eg. pymedia. However if all you need is support for simple WAVs, you could probably just use the built-in wave module.

import wave
win= wave.open('sample.wav', 'rb')
wout= wave.open('segment.wav', 'wb')

t0, t1= 1.0, 2.0 # cut audio between one and two seconds
s0, s1= int(t0*win.getframerate()), int(t1*win.getframerate())
win.readframes(s0) # discard
frames= win.readframes(s1-s0)

wout.setparams(win.getparams())
wout.writeframes(frames)

win.close()
wout.close()
bobince
@bobince : does the func wave.open create a wav file if it does nt exist as the segment wav is to be stored in new file...Is this the complete code..coz this neither showin an err nor givin an o/p..I am using python26,there is no pymedia suitable fr this,i found only fr 2.4..neways i am using only wav file..i am a beginner in python so do i need to instal ne more package to run this sort of code..coz many code i tried fr this site r nt workin
kaushik
`wave.open` creates a new file when used in write mode (`'wb'` here). This code works for me given a suitable `sample.wav` in the current directory and dropping a second's worth of audio into `segment.wav`. Your mileage may very for compressed (non-PCM) WAVs.
bobince
@bobince: Thanks a lot :)..its working...
kaushik
@bobince: the above code was helpful,i want to evn the merge two file i.e add the contents of one wav at the end of another wav file
kaushik
As long as the files are of the same format you should be able to `readframes` the audio data from each file and `writeframes` them out to the same file.
bobince