[Python] How to reduce delay between measured start and end time of a sound signal played...

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have a simple sine signal of duration 100 ms that I play on my speakers using the sounddevice library. I am trying to measure the exact time point a sound signal started and ended being played. However, when doing it in the most simple way, the time passing between the measure points is more than 3 times as long as the duration of the signal. How can I measure the time the signal is played more precisely?

    import sounddevice as sd
    import numpy as np
    import time

    #parameters
    duration = 0.1
    frequency = 440.0
    sample_rate = 44100

    t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
    sine_wave = 0.5 * np.sin(2 * np.pi * frequency * t)

    start_time = time.time()
    sd.play(sine_wave, samplerate=sample_rate)
    sd.wait()
    end_time = time.time()

    time_taken = end_time - start_time
    print(f"Time taken to play the sound: {time_taken:.6f} seconds") #discrepancy is 300ms for 100ms signal

    Continue reading...

Compartilhe esta Página