title: Change the volume of a wav file in python
body: <p>I have a 2 seconds 16bit single channel 8khz wav file and I need to change its volume.</p>
  
  <p>It should be quite straightforward, because changing the volume is the same as changing the amplitude of the signal, and I just need to attenuate it, that is to multiply it for a number between 0 and 1. But it doesn't work: the new sound is lower but VERY full of noise. What am I doing wrong?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import wave, numpy, struct
  
  # Open
  w = wave.open("input.wav","rb")
  p = w.getparams()
  f = p[3] # number of frames
  s = w.readframes(f)
  w.close()
  
  # Edit
  s = numpy.fromstring(s, numpy.int16) * 5 / 10  # half amplitude
  s = struct.pack('h'*len(s), *s)
  
  # Save
  w = wave.open("output.wav","wb")
  w.setparams(p)
  w.writeframes(s)
  w.close()
  </code></pre>
  
  <p>Thank you guys!</p>