Generating sine wave sound in Python

后端 未结 6 1173
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 05:31

I need to generate a sine wave sound in Python, and I need to be able to control frequency, duration, and relative volume. By \'generate\' I mean that I want it to play thou

6条回答
  •  情书的邮戳
    2020-12-01 05:59

    The script from ivan_onys produces a signal that is four times shorter than intended. If a TypeError is returned when volume is a float, try adding .tobytes() to the following line instead.

    stream.write((volume*samples).tobytes())
    

    @mm_ float32 = 32 bits, and 8 bits = 1 byte, so float32 = 4 bytes. When samples are passed to stream.write as float32, byte count (duration) is divided by 4. Writing samples back .tobytes() corrects for quartering the sample count when writing to float32.

提交回复
热议问题