Reputation: 73
Knowing some things like : Encoding : 16bit pcm, Byte order Little Endian , Channels : 1 Mono, Start offset : 0 bytes , Amount to import : 100% and Sample rate 16000 Hz.
That Characteristics were given by Audacity, in that way I can hear it in this program.
But I want to do it in Python code.
Upvotes: 7
Views: 12431
Reputation: 1903
Read bytes (rb
) from the raw file and write bytes (wb
) to WAV after setting the header. See wave module
import wave
with open("sound.raw", "rb") as inp_f:
data = inp_f.read()
with wave.open("sound.wav", "wb") as out_f:
out_f.setnchannels(1)
out_f.setsampwidth(2) # number of bytes
out_f.setframerate(44100)
out_f.writeframesraw(data)
Check the header of the WAV file:
with wave.open("sound.wav", "rb") as in_f:
print(repr(in_f.getparams()))
It should print something like _wave_params(nchannels=1, sampwidth=2, framerate=44100, nframes=220500, comptype='NONE', compname='not compressed')
Upvotes: 6
Reputation: 1150
Yes, can do this using the wave library or wavio api
https://docs.python.org/2/library/wave.html
Open The wave file in write binary mode.
wave.open(file[, mode]) If file is a string, open the file by that name, otherwise treat it as a seekable file-like object. mode can be any of
'w', 'wb' Write only mode.
Set all the parameter of the wav file as you have shared w.r.t the audacity
Wave_write.setparams(tuple) The tuple should be (nchannels, sampwidth, framerate, nframes, comptype, compname), with values valid for the set*() methods. Sets all parameters.
Open the raw file in binary mode and pass the data to these function.
Wave_write.writeframesraw(data)¶ Write audio frames, without correcting nframes.
Wave_write.writeframes(data) Write audio frames and make sure nframes is correct.
once done close the file handle.
Wave_write.close() Make sure nframes is correct, and close the file if it was opened by wave. This method is called upon object collection.
I hope you can implement the python code based on these description.
Upvotes: 5