I want to save the sound waveform data held by numpy to a file. However, I want to use only standard modules, not scikits.audiolab.
In Wave_write class documentation The fourth number of frames among the parameters passed by setparams is "The number of frames will change when frames are written later." It says, but setting it to 0 didn't work.
How to make a binary that represents the waveform to pass to writeframes of wave. It didn't work with numpy.tostring (), and after converting it to'h' with array, tostring worked fine.
import numpy, wave, array
filename = "wave_file_name.wav"
# save wav file
buf = <Contains numpy waveform data>
w = wave.Wave_write(filename)
w.setparams((
1, # channel
2, # byte width
16000, # sampling rate
len(buf), # number of frames
"NONE", "not compressed" # no compression
))
w.writeframes(array.array('h', buf).tostring())
w.close()
Recommended Posts