title: Adding silent frame to wav file using python
body: <p>First time posting here, lets see how this goes.</p>
  
  <p>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.</p>
  
  <p>What I was trying to do is read in the wav header and then adding a \0 to the beginning using the wave module but that did not work quite well. Here is the code which is based from here <a href="http://andrewslotnick.com/posts/audio-delay-with-python.html" rel="nofollow noreferrer">http://andrewslotnick.com/posts/audio-delay-with-python.html</a></p>
  
  <pre><code>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'\0'
      #remove space from the end
      end= audio_bytes        
      return add(audio_bytes, beginning+end, params[0])
  
  audio_params, aduio_bytes = input_wave(&lt;audio_file&gt;)
  output_wave(delay(aduio_bytes,audio_params,10000), audio_params, &lt;audio_file&gt;, &lt;audio_file_suffics&gt; )
  </code></pre>
  
  <p>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.</p>
  
  <p>I am also quite new to audio processing so now I am just trying anything and see what sticks.</p>
  
  <p>Any advice or ideas how to approach would be great :).</p>
  
  <p>Also I am using python 2.7.5</p>
  
  <p>Many Thanks.</p>