First time posting here, lets see how this goes.
I trying to write a script in python which would add a second of silence in the beginning of the wav file, but so far been unsuccessfully in doing so.
What I was trying to do is read in the wav header and then adding a to the beginning using the wave module but that did not work quite well. Here is the code which is based from here http://andrewslotnick.com/posts/audio-delay-with-python.html
import wave
from audioop import add
def input_wave(filename,frames=10000000): #10000000 is an arbitrary large number of frames
wave_file = wave.open(filename,'rb')
params=wave_file.getparams()
audio=wave_file.readframes(frames)
wave_file.close()
return params, audio
#output to file so we can use ipython notebook's Audio widget
def output_wave(audio, params, stem, suffix):
#dynamically format the filename by passing in data
filename=stem.replace('.wav','_{}.wav'.format(suffix))
wave_file = wave.open(filename,'wb')
wave_file.setparams(params)
wave_file.writeframes(audio)
# delay the audio
def delay(audio_bytes,params,offset_ms):
"""version 1: delay after 'offset_ms' milliseconds"""
#calculate the number of bytes which corresponds to the offset in milliseconds
offset= params[0]*offset_ms*int(params[2]/1000)
#create some silence
beginning= b''
#remove space from the end
end= audio_bytes
return add(audio_bytes, beginning+end, params[0])
audio_params, aduio_bytes = input_wave(<audio_file>)
output_wave(delay(aduio_bytes,audio_params,10000), audio_params, <audio_file>, <audio_file_suffics> )
Using the above code I get an error when I try to add the silence as the audio length is not the same as the input.
I am also quite new to audio processing so now I am just trying anything and see what sticks.
Any advice or ideas how to approach would be great :).
Also I am using python 2.7.5
Many Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…