code: def write_wavefile(f, samples, nframes=None, nchannels=2, sampwidth=2, framerate=44100, bufsize=2048):
      "Write samples to a wavefile."
      if nframes is None:
          nframes = 0
  
      w = wave.open(f, 'wb')
      w.setparams((nchannels, sampwidth, framerate, nframes, 'NONE', 'not compressed'))
  
      max_amplitude = float(int((2 ** (sampwidth * 8)) / 2) - 1)
  
      # split the samples into chunks (to reduce memory consumption and improve performance)
      for chunk in grouper(bufsize, samples):
          frames = b''.join(b''.join(struct.pack('h', int(max_amplitude * sample)) for sample in channels) for channels in chunk if channels is not None)
          w.writeframesraw(frames)
  
      w.close(f write_wavefile(f, samples, nframes=None, nchannels=2, sampwidth=2, framerate=44100, bufsize=2048):
      "Write samples to a wavefile."
      if nframes is None:
          nframes = 0
  
      w = wave.open(f, 'wb')
      w.setparams((nchannels, sampwidth, framerate, nframes, 'NONE', 'not compressed'))
  
      max_amplitude = float(int((2 ** (sampwidth * 8)) / 2) - 1)
  
      # split the samples into chunks (to reduce memory consumption and improve performance)
      for chunk in grouper(bufsize, samples):
          frames = b''.join(b''.join(struct.pack('h', int(max_amplitude * sample)) for sample in channels) for channels in chunk if channels is not None)
          w.writeframesraw(frames)
  
      w.close()
docs: Write samples to a wavefile.