question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

playing sine wave on raspberry pi has 25+ seconds delay before sound starts to play

See original GitHub issue

I have been testing sounddevice to play a sine wave (based on the code example in the docs). Everything works - but after I start the output stream, it takes about 25-30 seconds before the sine wave sound starts to play. On a Raspberry pi 3B - I did a clean latest rasbian buster light install - running headless (SSH from mac), and only installed what was necessary for sounddevice and soundfile. Installed python 3.9.4 - but I had the same problem when I was running 3.7 that came with the OS (playing the wave file example). I can use aplay on the RPi to play a sound wave no problem (no delay)

I initially used the code from sine wave example with outputstream(...) - and but then changed to creating the stream first and start it separately: same result. I inserted a bunch of calls to time.time() - I can see the stream starts right away but the first call to the callback takes 25-30 seconds.

My Code:

import sounddevice as sd
import numpy as np 

start_idx = 0
cnt=1
sd.default.device=0


try:
    samplerate = 44100

    def callback(outdata, frames, time, status):
        if status:
            print(status, file=sys.stderr)
        global start_idx
        global cnt
        t = (start_idx + np.arange(frames)) / samplerate
        t = t.reshape(-1, 1)
        outdata[:] = 1 * np.sin(2 * np.pi * 440 * t) 
        start_idx += frames
        cnt+=1
        if cnt>20: 
            print('count =',cnt)
            raise sd.CallbackStop

    myStream=sd.OutputStream(device=0, channels=1, callback=callback,
                         samplerate=samplerate)
    myStream.start()
    print('stream is active: ',myStream.active).   #This shows True
    print('cpu load = ',myStream.cpu_load).        #This shows 0.0
    input()  # (I wait until I see count=21 printed in terminal and sound stops in headphones, then hit return )
    myStream.stop()
    myStream.close()

For info - sd.query_devices() yields:

* 0 bcm2835 Headphones: - (hw:0,0), ALSA (0 in, 8 out)
  1 Logitech USB Microphone: Audio (hw:1,0), ALSA (1 in, 0 out)
  2 sysdefault, ALSA (0 in, 128 out)
  3 output, ALSA (0 in, 8 out)
  4 dmix, ALSA (0 in, 2 out)
  5 default, ALSA (0 in, 128 out)

any ideas why this happens? could it be a PortAudio issue on the pi - nothing to do with sounddevice?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
HaHehocommented, Jun 8, 2022

I believe this can be closed then.

1reaction
josephernestcommented, Nov 19, 2021

Good news!

apt update
apt install raspberrypi-kernel

solves it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

50s delay issue with audio play - Raspberry Pi Forums
I'm trying to play wave file with pyaudio found on recent version OS there 50s delay for sound actually come out.
Read more >
Audio Delay Module : 12 Steps (with Pictures) - Instructables
1. The block diagram shows how the circuit works. First, the input signal is amplified as needed. Next, the amplified signal is sampled and...
Read more >
Arbitrary Wave Generator With the Raspberry Pi Pico
1. The pico itself cannot produce analog signals, it does not have a digital-to-analog converter (DAC). But they are simple to build from...
Read more >
50 result(s) matching "delay-audio-signal", (0.07 seconds)
I'm using pure data on a raspberry pi 3+ for a project. I have bluetooth configured properly on it. I can send regular...
Read more >
R-Pi Troubleshooting - eLinux.org
Start off by reading the "Is your Pi not booting? (The Boot Problems Sticky)" in the troubleshooting section of the Official Raspberry PI...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found