Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stuttering audio and buffer underruns when WiFi is enabled #4

Open
pathammer opened this issue Apr 8, 2017 · 21 comments
Open

Stuttering audio and buffer underruns when WiFi is enabled #4

pathammer opened this issue Apr 8, 2017 · 21 comments

Comments

@pathammer
Copy link

pathammer commented Apr 8, 2017

I tested this with my phone and it worked fine but when I pair my Amazon echo dot, the playback is interrupted once every one to two seconds. Has anyone else experienced this or know a fix?
I know it's not a signal strength issue because the two are right beside each other. Thanks!

Hiding adapter from all devices.
underrun!!! (at least 9.096 ms long)
underrun!!! (at least 0.642 ms long)
underrun!!! (at least 11.836 ms long)
underrun!!! (at least 12.320 ms long)
underrun!!! (at least 25.990 ms long)
underrun!!! (at least 73.289 ms long)
underrun!!! (at least 53.062 ms long)
underrun!!! (at least 19.260 ms long)
underrun!!! (at least 55.009 ms long)
underrun!!! (at least 20.370 ms long)
underrun!!! (at least 62.025 ms long)

@lukasjapan
Copy link
Owner

It looks like the daemon is not sending the audio data fast enough so alsa complains.
If it works with your phone it sounds like some compatibility issue with bluez (the linux bluetooth system).

In general you could try to do the following things in case of underruns:

  1. Check the CPU load
    Maybe the decoding process does not have enough resources.

  2. Check for PulseAudio
    If PulseAudio is installed I would recomnend to uninstall it.
    aplay should send the data straight to the soundcard.

  3. Update bluez
    Try another version or wait for an update of bluez. For this repository, at least bluez5 is needed.

In your case, the best bet would be option 3, I suppose.

@lnorton89
Copy link

I am having what appears to be the same issue; from my Galaxy S4 I can pair and play audio but with severe drop outs / noise / crazy things happening with audio playback speed (double speed!). With a Galaxy S5 I can pair and play audio as well with zero drop outs but with a fair amount of static in the background.

I have an idle load of 25% with bt_speaker.py being the source. I have uninstalled PulseAudio but have not updated bluez (due to inability to / no straightforward instructions to do so).

What can I do to troubleshoot this further / correct the issue? I have Wi-Fi disabled via the taskbar tool to do so.

@lukasjapan
Copy link
Owner

lukasjapan commented Apr 20, 2017

Playback with double speed and such may indicate that format negotiation (sampling rate, channels, etc) failed. But I am not sure about that since the arriving stream is encoded anyway.

If I can get hand on a device that reproduces the error, maybe I will look into this in the future.

@lnorton89
Copy link

After some digging and messing around I was able to test with my S3 and have no issues with that device. After attempting to setup a hotspot via wlan0 when I brought the adapter up the dropouts occurred again. Taking down wlan0 reverses that action and also allows my S4 to perform without issues. I'm guessing when I disabled the wlan0 via the GUI it really wasn't disabled because I'm doing all of this via SSH. Using another Wi-Fi adapter via USB works and does not cause drop outs with either handset.

@photon-schiesser
Copy link

photon-schiesser commented May 7, 2017

@lnorton89 I can confirm that behavior regarding wlan0.

I am using a Raspberry Pi Zero W. I was also seeing overruns in the journal. I saw your comment and brought down wlan0 while playing music. The dropouts immediately stopped. Now it is playing quite smoothly.

Are the WiFi and Bluetooth chips on these boards (RPi 3 & Zero W) sharing a bus or channel? Perhaps that is the cause.

Could we attempt to get around this by using a lower sampling rate on the ALSA side?

A temporary workaround that would work for my use-case is to have a hook when a bluetooth client connects/disconnects that brings up/down the WiFi.

@lukasjapan
Copy link
Owner

lukasjapan commented May 7, 2017

Thanks for sharing.

Looks like a common problem with the Pi and people fix it by investing in an additional WiFi or Bluetooth dongle.

More info:

@lukasjapan lukasjapan changed the title Amazon Echo does not stream smoothly Stuttering audio and buffer underruns when WiFi is enabled May 8, 2017
@pathammer
Copy link
Author

I can confirm that using an external wifi adapter resolves the original issue I reported. I'll let @lukasjapan decide if he wants to close the issue.

@lukasjapan
Copy link
Owner

lukasjapan commented May 14, 2017

I'll leave it open for people to know. Using WiFi and Bluetooth at the same time is a very likely use case.

@matisflores
Copy link

Hi @lukasjapan, great project.
You consider add 'ifconfig wlan0 (up|down)' on connect/disconnect events?
I add 'subprocess.Popen('ifconfig wlan0 down', shell=True)' but nothing happen.

@pathammer
Copy link
Author

pathammer commented May 21, 2017 via email

@jdubbandroid
Copy link

I'm experiencing the same problem, only with an Anker Bluetooth keyboard, not a speaker. The keyboard works fine until I enable Bluetooth on my brand new Pi 3 model B. Then it stops working.

@ritiek
Copy link

ritiek commented Jul 11, 2018

I am facing a similar problem on my Pi 2. I use an external wifi adapter but disabling it doesn't fix the problem.

@ritiek
Copy link

ritiek commented Jul 11, 2018

I am tinkering more into this and I found something weird.

I ran the command $ aplay - and KeyboardInterrupt (Control-C) it after a few seconds and then restarted the service $ sudo systemctl restart bt_speaker. I was able to connect back to my Pi and stream music without any issues! I am able to reproduce this behavior consistently and it works for me every time. Also, I am using an Android device (OnePlus Two) to stream music so this might also help with #12 (comment).

I have no idea what's happening at the moment. I'll see more into this if I can find anything useful.

Can you guys test this out?

@ritiek
Copy link

ritiek commented Jul 12, 2018

Another thing I noticed that if I remove bt-speaker service and run the python file directly without root, the sound works fine as well. It causes problems only when running as root. Although, stuff like volume control, etc. is not available without root. Going to take a better look.

@ritiek
Copy link

ritiek commented Jul 20, 2018

As an update, I tried to follow this gist and now everything works perfect for me, including volume control.

@ElShapovalov
Copy link

I have same problem with sound (very fast with pauses) (Samsung A5 2017).
I am a noob in python programming, but i change line 182 in file codecs.py (https://github.com/lukasjapan/bt-speaker/blob/master/bt_manager/codecs.py#L182), i set max_len=10240 and the problem is gone, even with working wi-fi.

@photon-schiesser
Copy link

photon-schiesser commented Feb 24, 2019

@ElShapovalov Thank you! I tried your suggestion, but I found that changing it to 32768 (2^15, seemed like a good number?) did the trick. I was still getting underruns with 10240. WiFi still on as well! Thank you!

Edit: Of course, as I send this message, I started getting underruns again... :( Strangely, after about 2 minutes of perfect playback. I was streaming, but now I am playing a downloaded song via YouTube Music app on Android 5.1.1. Will report back.

@insoPL
Copy link

insoPL commented Apr 23, 2019

I had same problems (Stuttering audio and buffer underruns) when connecting from sm-t530. I checked 2 different android phones and everything worked smoothly. I can confirm that changing max_len to 10240 did the trick. I'm still getting 1/2 second lag but i guess with raspberry pi zero running bt and wifi it will just be like that.

@roflmaostc
Copy link

For me I had to adapt /etc/bt_speaker/config.ini:
play_command = aplay -Dplug:default -f cd -

Now it works flawlessly with a external USB card.

@pktmterenceg
Copy link

Confirming that @ScheissSchiesser 's suggestion above to increase the buffer to 32768 works with the wifi interface off (ifconfig lwan0 down).

I found that playing music from iTunes (12.8) from a Mac (Mac OS 10.13) produced the stuttering effect/underrun situation described by others here.

@Josonosimiti
Copy link

Hello, how exactly can i edit codecs.py? I have the same issue with audio playing too fast and stopping every 2 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests