Search code examples
pythonsignalswavepyaudio

converter wave 24 bit to 16 bit


i wish to convert some wavs that are 48kHz 24 bit file.wav to 48kHz 16 bit file-2.wav

import wave 
origAudio = wave.open("Sample_5073.wav","r")
frameRate = origAudio.getframerate()
nChannels = origAudio.getnchannels()
sampWidth = origAudio.getsampwidth()
nbframe=origAudio.getnframes()
da = np.fromstring(origAudio.readframes(48000), dtype=np.int16)
left, right = da[0::2], da[1::2]

Thanks


Solution

  • If converting files from 24 to 16 bit is the only thing you want to do, you could use SoX, it doesn't get much easier than that:

    sox file.wav -b 16 file-2.wav
    

    SoX can also do many more things, just have a look at its man page.

    If you want to use Python, I recommend the soundfile module:

    import soundfile as sf
    
    data, samplerate = sf.read('file.wav')
    sf.write('file-2.wav', data, samplerate, subtype='PCM_16')
    

    Specifying subtype='PCM_16' isn't even strictly necessary, since it's the default anyway.

    If you really want to do it with the built-in ẁave module, have a look at my tutorial about the wave module.