[Python] Python simpleaudio play_buffer() lag

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working with simpleaudio in Python on a Windows 11 machine.

    I'm seeing really long delays for what is supposed to be an asynchronous library.

    Here is the guts of a test script:

    sample_rate = 44100
    duration = 50 / 1000.0
    t = np.linspace(0, duration, int(sample_rate*duration), False)
    tone = np.sin(2 * np.pi * 440 * t)
    audio = (tone * 32767).astype(np.int16)

    while True:
    try:
    time.sleep(.09)
    # TRACKING CODE....
    print(f"Pre-xxx.play: {time.time()}")
    play_obj = sa.play_buffer(audio, 1, 2, sample_rate)
    # TRACKING CODE....
    print(f"Post-xxx.play: {time.time()}\n")


    And here is some sample output:

    PS C:\python> py.exe .\beep_test.py
    Pre-xxx.play: 1748388885.2099779
    Post-xxx.play: 1748388885.4194748

    Pre-xxx.play: 1748388885.5100489
    Post-xxx.play: 1748388885.8608859

    Pre-xxx.play: 1748388885.9511144
    Post-xxx.play: 1748388886.2871153

    Pre-xxx.play: 1748388886.3772535
    Post-xxx.play: 1748388886.7330158

    Pre-xxx.play: 1748388886.8235033
    Post-xxx.play: 1748388887.1881742

    Pre-xxx.play: 1748388887.278986
    Post-xxx.play: 1748388887.602071


    Notice the sizeable delta of 0.209 - 0.365 seconds.

    What gives!?

    Any way to get these down to much smaller numbers? Is there a better Python audio library for this?

    Note: due to a bug/change required in the original simpleaudio library, I'm actually using this version: https://github.com/cexen/py-simple-audio

    Thank you for your help!

    Continue reading...

Compartilhe esta Página