How do I write a 24-bit WAV file in Python?

前端 未结 7 2167
遇见更好的自我
遇见更好的自我 2020-12-11 02:05

I want to generate a 24-bit WAV-format audio file using Python 2.7 from an array of floating point values between -1 and 1. I can\'t use scipy.io.wavfile.write because it on

7条回答
  •  时光说笑
    2020-12-11 02:10

    You should try scikits.audiolab:

    import numpy as np
    from scikits.audiolab import Sndfile, Format
    
    sig = np.array([0, 1, 0, -1, 0], dtype=np.float32)
    f = Sndfile('test_pcm24.wav', 'w', Format('wav', 'pcm24'), 1, 44100)
    f.write_frames(sig)
    f.close()  # use contextlib.closing in real code
    

    And to read it again:

    f = Sndfile('test_pcm24.wav')
    sig = f.read_frames(f.nframes, dtype=np.float32)
    f.close()  # use contextlib.closing in real code
    

    scikits.audiolab uses libsndfile, so in addition to WAV files, you can also use FLAC, OGG and some more file formats.

提交回复
热议问题