problems to write a wav file

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu May 24 17:09:04 EDT 2007


En Thu, 24 May 2007 10:55:42 -0300, Gautier Krings  
<gautier.krings at gmail.com> escribió:

> I have a small problem for writing a .wav file. I can't find in which  
> format
> I have to pass my data in the writeframes function of the wave module.
>
> here's my code, I am just trying to open a wav file, extracting the data
> from it, and writing it in another wav file.

You are also reducing each frame to a single number, its rms.

> The problem is that the sound I get in the second wav file isn't the  
> same as
> from the firs file (in fact it is horrible).

(I bet it is!)
Even if you manage to write the file correctly, replacing each frame with  
its rms value won't give an "audible" wav - by example, a constant volume  
section would give a flat wave.

> new_signal = wave.open('C:\demo2.wav', 'wb')

Please use either r'C:\demo2.wav' or 'C:\\demo2.wav'

> new_signal.setsampwidth(width)

This is the sample width in bytes - you must write an integer of this  
size, for each sample.

> new_signal.setframerate(frameRate)
> new_signal.setnchannels(signal.getnchannels())

As you only have a single value (rms), this should be always 1.

> for j in range(len(power)):
>      data = hex(power[j])    ---> I guess that my problem is located at  
> this
> line

One of the problems, yes. For a 16bit sample, you could use:  
data=struct.pack("h",power[j])

>      new_signal.writeframes(data)
> new_signal.close()

Another option would be to use the array module:  
http://docs.python.org/lib/module-array.html

-- 
Gabriel Genellina




More information about the Python-list mailing list