title: How to Encode a 16 Bit WAV file in 8 bit in Python?
body: <p>I am trying to play a sound from a sawtooth wave. I created the waveform in Python and was able to save it as a WAV file, but when I try to play it it says the file is unplayable because the file type is unsupported, the file extension is incorrect, or the file is corrupt. I used this individual's tutorial (<a href="https://thehackerdiary.wordpress.com/2017/06/09/it-is-ridiculously-easy-to-generate-any-audio-signal-using-python/" rel="nofollow noreferrer">https://thehackerdiary.wordpress.com/2017/06/09/it-is-ridiculously-easy-to-generate-any-audio-signal-using-python/</a>) and they worked around it by encoding the raw waveform from 16 bit to 8 bit in Audacity. How can this be done using only Python?</p>
  
  <pre><code>import soundfile
  
  data, samplerate = soundfile.read('sawtooth_100_hz.wav')
  soundfile.write('sawtooth_100_hz_8bit.wav', data, samplerate, subtype='PCM_S8')
  </code></pre>
  
  <p>^^ I tried this and got the following error: ValueError: Invalid combination of format, subtype and endian</p>