Burgestrand / spotify

Low-level Ruby bindings for libspotify, the official Spotify C API
https://rubygems.org/gems/spotify
Other
147 stars 17 forks source link

Laggy Audio Stream on RaspberryPi #28

Open ds1982 opened 10 years ago

ds1982 commented 10 years ago

After managing to install the spottily gem (see https://github.com/Burgestrand/spotify/issues/27) i am now able to play Musik with the delivered examples.

Unfortunaly it is very laggy, The Playback stops every few seconds and starts after further 2-3 seconds again.

"top" shows ~90% cpu-usage by ruby-process. Any hints?

Burgestrand commented 10 years ago

Yes. The audio example is too low performance to work on raspberry pi, the processor is simply not fast enough.

I have two experimental branches since checking this problem out a few weeks ago:

Using the gems from the two experimental versions above allowed me to play audio without lag on raspberry pi. You're welcome to try them.

There have not been a release with these branches, because I'm thinking of possibly merging plaything and the spotify gem... maybe. How to handle audio playback in this library is a bit complex. I want to ship a nice default solution, but still allow people to make advanced custom drivers if they ever want to.

ds1982 commented 10 years ago

sorry for double-Posting this issue. I saw that this topic was discussed in some old threads. I will try your branches.

Burgestrand commented 10 years ago

Don't worry. I like to keep this issue open. It reminds me that some change needs to be made for the spotify gem to work well on raspberry pi.

ds1982 commented 10 years ago

Tried to use the raspberry-pi branch now without success.

example-audio_delivery_speed.rb seems to work, but i can't hear any audio. Log:

[INFO @ 22:18:04] Log in requested. Waiting forever until logged in.................................
[INFO @ 22:18:07] Playing track until end. Use ^C to exit.0f of 0f/s (NaNs)
92160f of 44100f/s (2.09s)
137216f of 44100f/s (3.11s)
65536f of 44100f/s (1.49s)
67584f of 44100f/s (1.53s)
65536f of 44100f/s (1.49s)
...

example-audio_stream.rb gives:

/home/pi/spotify/lib/spotify/reaper.rb:73: [BUG] Segmentation fault
ruby 1.9.3p194 (2012-04-20 revision 35410) [arm-linux-eabi]

-- Control frame information -----------------------------------------------
c:0004 p:---- s:0011 b:0011 l:000010 d:000010 CFUNC  :sleep
c:0003 p:0075 s:0007 b:0007 l:0020d0 d:000006 BLOCK  /home/pi/spotify/lib/spotify/reaper.rb:73
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:---- s:0002 b:0002 l:000001 d:000001 TOP   

-- Ruby level backtrace information ----------------------------------------
/home/pi/spotify/lib/spotify/reaper.rb:73:in `block in initialize'
/home/pi/spotify/lib/spotify/reaper.rb:73:in `sleep'

-- C level backtrace information -------------------------------------------

-- Other runtime information -----------------------------------------------

* Loaded script: example-audio_stream.rb

* Loaded features:

    0 enumerator.so
    1 /usr/lib/ruby/1.9.1/arm-linux-eabi/enc/encdb.so
    2 /usr/lib/ruby/1.9.1/arm-linux-eabi/enc/trans/transdb.so
    3 /usr/lib/ruby/1.9.1/rubygems/defaults.rb
...

Any Idea what could be wrong?

ds1982 commented 10 years ago

Could you give me some Information with wich OS/Config you tested these branches, so i could replicate this working system?

Burgestrand commented 10 years ago

Hm. I've seen segfaults on Raspberry PI earlier. Last time I had audio playback work, I was actually surprised that it did not segfault, as I recall an earlier attempt caused random segfaults at just about any time.

I can't remember what setup I used earlier. I am fairly certain it was one of the mentioned OSes for the official downloads: http://www.raspberrypi.org/downloads/. Since the branch includes a special version of spotify to support another libspotify version, it is possible hard float was my previous OS.

As far as config goes, I would assume ruby 2.0 was installed, but nothing else.

ds1982 commented 10 years ago

Since the branch includes a special version of spotify to support another libspotify version, it is possible hard float was my previous OS.

So i can use it with libspotify 12.1.103 beta ?

I tried to install this by hand and installed the spottily gem (raspberry branch) but got this:

/home/pi/spotify/lib/spotify.rb:4:in `require': cannot load such file -- libspotify (LoadError)
    from /home/pi/spotify/lib/spotify.rb:4:in `<top (required)>'
    from /home/pi/spotify/examples/support.rb:2:in `require'
    from /home/pi/spotify/examples/support.rb:2:in `<top (required)>'
    from example-audio_stream.rb:4:in `require_relative'
    from example-audio_stream.rb:4:in `<main>'

Do i have to install any libspotify gem too? i could not find any raspberry branch here?

Burgestrand commented 10 years ago

I took a look at it today, and I believe this was my setup:

So i can use it with libspotify 12.1.103 beta ?

Yes. The raspberry pi branch of spotify have removed the hard coded version check. It will no longer crash on boot with a wrong libspotify version error.

ds1982 commented 10 years ago

Alright. Can run the examples now. Problem was the pure-ruby install of libspotify (have this and hf-version of libspotify now installed by hand). All of this on Hardfloat OS.

Another Problem was line 1 in the examples "support.rb"

require "bundler/setup"

with this line libspotify cannot be found. I commented it out and now libspotify and spotify gem runs until a track is played.

Problem now ist: Track is played (dots are running in example_audio_stream.rb) BUT if have NO Audio. Tried internal Audio and a USB Soundcard.

Log:

[INFO @ 10:23:05] Log in requested. Waiting forever until logged in..
[INFO @ 10:23:05] (session (log message)) 10:23:05.552 I [ap:1752] Connecting to AP ap.spotify.com:4070

[INFO @ 10:23:05] (session (log message)) 10:23:05.710 I [ap:1226] Connected to AP: 78.31.12.22:4070

[INFO @ 10:23:06] (session (log message)) 10:23:06.048 I [offline-mgr:2082] Storage has been cleaned
.......
Spotify track URI: spotify:track:2FltaWSvGlNcVOfzOgFBEg

[INFO @ 10:23:34] Playing track until end. Use ^C to exit...........................
[INFO @ 10:23:35] (session (log message)) 10:23:35.865 I [file_streamer_simple.cpp:769] Request for file fbc9aef693f20babb41cfb82148b7a24028f4a18 complete (code: 0)
..........
[INFO @ 10:23:36] (session (log message)) 10:23:36.423 I [file_streamer_simple.cpp:769] Request for file fbc9aef693f20babb41cfb82148b7a24028f4a18 complete (code: 0)
.........................................................................................^C
[INFO @ 10:23:41] Logging out..........................................
[INFO @ 10:23:43] (session (log message)) 10:23:43.206 I [offline-mgr:2030] 0 files are locked. 0 images are locked

[INFO @ 10:23:43] (session (log message)) 10:23:43.209 I [offline-mgr:2056] 0 files unlocked. 0 images unlocked

Any ideas?

Burgestrand commented 10 years ago

Yeah, I noticed the problem with bundler now too. I just released a new version of libspotify with support for hard float in the gem, so one should not need to install libspotify manually. Unfortunately, there is a bug in rubygems, so it does not install the arm-linux release of libspotify gem, and there is (probably) a bug in bundler, so it ignores the installed libspotify gem with arm-linux platform, and installs pure ruby version. Like you noticed, the fix is manual install of libspotify gem with gem install libspotify --platform arm-linux, and removal of bundler/setup.

Regarding your problem with no audio, I ran into it too and it is solvable. Here are the issues:

  1. You must install plaything from raspberry-pi branch. Normally bundler would do this by changing Gemfile.
  2. We cannot use bundler, because bundler will install and use pure ruby version of libspotify.

I solved it by doing these steps:

git clone https://github.com/Burgestrand/plaything
cd plaything
git checkout raspberry-pi
rake build # build raspberry-pi gem
gem install pkg/plaything-1.1.1.gem # install raspberry-pi-specific gem globally

After that, you should be able to run the examples, assuming that:

  1. You've removed the require 'bundler/setup' line from the examples.
  2. You do not have pure ruby version of libspotify gem, only the arm-linux version. Check with gem list libspotify

After all of these steps, I could play a sound file through HDMI speakers. I had slight a little lag like two times during the entire song, but it was mostly okay.

ds1982 commented 10 years ago

works. For some reason i didn't have the plaything gem raspberry-branch installed (even if bundler said that this branch was used). Installed plaything now by hand works with internal audio plug (not HDMI, can't test it because i don't have any HDMI speakers available).

Thanks for your good support!!!

I will enjoy your gem now!

Burgestrand commented 10 years ago

Nice!

Sorry about the issues. Raspberry PI is a tricky platform, because not many people use it. I want to change audio to be better though, and so that raspberry pi should be able to play audio too without crazy workarounds.

ds1982 commented 10 years ago

would it possible to use a USB Soundcard? Do i have to write my own driver for this?

Burgestrand commented 10 years ago

I don't know. I don't have a USB soundcard to try with. Plaything will use the default sound output device for playback by default.

I think if you are able to change default device globally on your raspberry pi, plaything should use this new device.

Burgestrand commented 10 years ago

Plaything uses alcOpenDevice: https://github.com/Burgestrand/plaything/blob/master/lib/plaything.rb#L36, and passes in NULL. This is the line that needs to change if you want to change the device through code.

ds1982 commented 10 years ago

works too. Changed my USB sound card to be device 0 in /etc/modprobe.d/alsa-base.conf

I think changing the device index in plaything would be possible too, but didn't try.

Reason for using the USB Soundcard is that this prevents me from having this ugly static noise which the internal raspberry sound has.