Mopidy + Spotify Connect / Raspotify

Hey there,

I’m wondering what the recent options are of how to get Raspberry Pi working with Mopidy AND Spotify Connect. I love both and don’t wanna miss one.

My setup consists of a Raspberry Pi 3 with the HiFiBerry Amp 2 connected. I’m running Mopidy 2.1 with Mopidy-Spotify and TuneIn backend extensions, which works like a charm. Parallel to this installation I’m running Raspotify which leverages librespot and enables the use of Spotify Connect. Raspotify also works like a charm - if I run it separately…

Once one of both is running and playing music I’m not able to start playing the other one. In order to play music using Spotify Connect I need to manually stop the mopidy server and vice versa.

Does anybody have an idea of how to get both running together seamlessly. Or is there another way of how to integrate Spotify Connect with Mopidy?

Pimusicbox uses the librespot onevent handler to issue a MPD stop command to Mopidy when librespot playback starts. You could also configure an ALSA dmix device to allow both programs to output to the soundcard at the same time (but you’d still need to remember to stop and start them both to avoid a garbled mess).

Hi all, that works perfectly well – is there a chance to display the current track from Spotify connect in Mopidy Front end? If we cannot display the current track in that view, is at least possible to display a spotify connect logo? Thanks MK

I was googling a bit and I figured it could be done some how as described here:


So the song play events can be captured and collected and we could maybe create a plugin to mopidy that just collects these events and displays it on the now playing page …
Isn’t that possible?
Best regards
MK

Very possible to use the Spotify Web API for that. Not entirely sure if the auth token used by Mopidy-Spotify has the necessary permissions (scopes) to access that part of the API. If not, you may be able to ask @adamcik to add what’s needed (if you find out). I’m not personally interested in adding this to my webclient as I think it would just be confusing, but maybe it just needs some effort in the UI department.

The current default scope is set to:

OAUTH_SCOPES = [
    'playlist-modify-private',
    'playlist-read-private',
    'playlist-read-collaborative',           
    'user-follow-read',
    'user-library-read',
    'user-read-recently-played',             
    'user-read-private',
    'user-top-read',
    'streaming',
]

Older tokens might be missing at least streaming.

Hi!
I’m new here, but I’m very interested to this post.

I’m on Pi Zero w and Pimoroni Pirate Audio line out with Mopidy and Iris installed and all work like a charm.
But if I can use the original Spotify App of Android or PC to control the streaming it would be awesome!
I’ve been setup the Raspify addon ad the spotify App on my phone view the device but after a 15 seconds of tryng to connect nothing happens and the command for streaming doesn’t works.

Can some one explain to me how to do that please?

This topic is quite old but the information remains correct. It’s not clear from your post what exactly you tried to do here in regards to playing from both programs at the same time, or if it’s just a fundamental problem getting Raspify to work. If it’s the latter then you are better off posting in their support channels.

Hi,
Sorry if I was not clear to explain the situation.
I would like to use mopidy and raspotify at the same time. Because now I can use only one service a time. There’s a simple way to do that or a little guide to how to do that?
Thanks in advance.

OK, I thought you had followed the suggestion at the top and were still having some problems. And I also see now that you meant Raspotify.

I don’t have a step-by-step guide but you need to:

You can read about dmix here. I don’t know if Raspotify has any special ALSA configuration requirements but assuming not, you need to make/edit the file at /etc/asound.conf so it contains:

pcm.!default {
        type plug
        slave.pcm "dmix"
}

Now multiple programs should be able to open the default audio device at the same time.

Then you need to come up with some way that the two programs do not both try and actually output at the same time and produce a garbled mess. You can either handle this manually by remember to stop/pause the program you are not using. Or, in my above post I said:

Maybe Raspotify exposes this, or something similar. I don’t know.

Thanks for your support @kingosticks, I’ll try to do what you sais in the previus post.

Maybe I’ve found a fresh nice post that’s perfect for what I need.
I’ll give it a try in theese days.
This use Spotify Connect and view the cover album of the song on the pimorony dislpay board.
If some one need this:

Hi all,

So I tried to configure dmix:

$ sudo aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: sndrpihifiberry [snd_rpi_hifiberry_dac], device 0: HifiBerry DAC HiFi pcm5102a-hifi-0 [HifiBerry DAC HiFi pcm5102a-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: vc4hdmi [vc4-hdmi], device 0: MAI PCM i2s-hifi-0 [MAI PCM i2s-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Added following line to /boot/config.txt:

dtoverlay=i2s-mmap

and created /etc/asound.conf with following content:

pcm.hifiberry { 
  type hw card 0 
}

pcm.!default { 
  type plug 
  slave.pcm "dmixer" 
}

pcm.dmixer { 
  type dmix 
  ipc_key 1024 
  slave { 
    pcm "hifiberry" 
    channels 2 
  } 
}

ctl.dmixer { 
  type hw 
  card 0 
}

Changed Mopidy config and restarted:

[audio]
output = alsasink device=plug:dmixer

WORKED! Mopidy is using dmixer now. However, raspotify is not so eager to participate. Changed this in /etc/raspotify/conf:

# Displayed device type. Defaults to speaker.
LIBRESPOT_DEVICE_TYPE="dmix:CARD=sndrpihifiberry,DEV=0"

But once raspotify is started I get:

lip 25 11:53:11 pirateaudio systemd[1]: Started Raspotify (Spotify Connect Client).
lip 25 11:53:11 pirateaudio librespot[23338]: [2023-07-25T09:53:11Z ERROR librespot] Invalid `--device-type` / `-F`: "dmix:CARD=sndrpihifiberry,DEV=0"
lip 25 11:53:11 pirateaudio librespot[23338]: Valid `--device-type` / `-F` values: computer, tablet, smartphone, speaker, tv, avr, stb, audiodongle, gameconsole, castaudio, castvideo, automobile, smartwatch, chromebook, carthing, homething
lip 25 11:53:11 pirateaudio librespot[23338]: Default: Speaker
lip 25 11:53:11 pirateaudio systemd[1]: raspotify.service: Main process exited, code=exited, status=1/FAILURE
lip 25 11:53:11 pirateaudio systemd[1]: raspotify.service: Failed with result 'exit-code'.
lip 25 11:53:11 pirateaudio systemd[1]: raspotify.service: Triggering OnFailure= dependencies.

This is full aplay -L:

$ sudo -u mopidy aplay -L
null
    Discard all samples (playback) or generate zero samples (capture)
hifiberry
default
dmixer
hw:CARD=sndrpihifiberry,DEV=0
    snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
    Direct hardware device without any conversions
plughw:CARD=sndrpihifiberry,DEV=0
    snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
    Hardware device with all software conversions
sysdefault:CARD=sndrpihifiberry
    snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
    Default Audio Device
dmix:CARD=sndrpihifiberry,DEV=0
    snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
    Direct sample mixing device
hw:CARD=vc4hdmi,DEV=0
    vc4-hdmi, MAI PCM i2s-hifi-0
    Direct hardware device without any conversions
plughw:CARD=vc4hdmi,DEV=0
    vc4-hdmi, MAI PCM i2s-hifi-0
    Hardware device with all software conversions
sysdefault:CARD=vc4hdmi
    vc4-hdmi, MAI PCM i2s-hifi-0
    Default Audio Device
hdmi:CARD=vc4hdmi,DEV=0
    vc4-hdmi, MAI PCM i2s-hifi-0
    HDMI Audio Output
dmix:CARD=vc4hdmi,DEV=0
    vc4-hdmi, MAI PCM i2s-hifi-0
    Direct sample mixing device

And this is what librespot sees:

$ sudo librespot --device ?
[2023-07-25T10:01:21Z INFO  librespot] librespot 0.4.2 a6e1258 (Built on 2023-06-21, Build ID: g9ifMDjd, Profile: release)


        Compatible alsa device(s):

        --------------------------------------------------------------------

        Device:

                hw:CARD=sndrpihifiberry,DEV=0

        Description:

                snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
                Direct hardware device without any conversions

        Supported Format & Sample Rate Combinations:

                Format: S16   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: S24   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: S32   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz

        --------------------------------------------------------------------

        Device:

                plughw:CARD=sndrpihifiberry,DEV=0

        Description:

                snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
                Hardware device with all software conversions

        Supported Format & Sample Rate Combinations:

                Format: S16   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: S24   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: S24_3 Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: S32   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: F32   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: F64   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz

        --------------------------------------------------------------------

        Device:

                sysdefault:CARD=sndrpihifiberry

        Description:

                snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
                Default Audio Device

        Supported Format & Sample Rate Combinations:

                Format: S16   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: S24   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: S24_3 Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: S32   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: F32   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz
                Format: F64   Sample Rate(s): 44.1kHz, 48kHz, 88.2kHz, 96kHz

        --------------------------------------------------------------------

        Device:

                dmix:CARD=sndrpihifiberry,DEV=0

        Description:

                snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
                Direct sample mixing device

        Supported Format & Sample Rate Combinations:

                Format: S32   Sample Rate(s): 48kHz

        --------------------------------------------------------------------

ALSA lib pcm_dmix.c:1075:(snd_pcm_dmix_open) unable to open slave

@kingosticks or anybody, can you please help me finding my mistake in configuration?

I think the clue is in the error message:

Valid --device-type / -F values: computer, tablet, smartphone, speaker, tv, avr, stb, audiodongle, gameconsole, castaudio, castvideo, automobile, smartwatch, chromebook, carthing, homething

To me this suggests that LIBRESPOT_DEVICE_TYPE is the wrong variable to change. Maybe it’s LIBRESPOT_DEVICE instead?

You’re absolutely right @kingosticks, thank you so much. It is starting now well, however when I play anything I get this:

lip 25 13:04:02 pirateaudio librespot[177560]: [2023-07-25T11:04:02Z ERROR librespot_connect::spirc] AutoplayError: MercuryError
lip 25 13:04:05 pirateaudio librespot[177560]: [2023-07-25T11:04:05Z ERROR librespot_playback::player] Audio Sink Error Invalid Parameters: <AlsaSink> Device dmix:CARD=sndrpihifiberry,DEV=0 Unsupported Format S16_LE (S16), ALSA function 'snd_pcm_hw_params_set_format' failed with error 'EINVAL: Invalid argument', Supported Format(s): ["S32"]

I will start the troubleshooting now for that. I suppose I will need to add more formats to the dmix, but not sure how to approach this.

You can see on the librespot docs at Options · librespot-org/librespot Wiki · GitHub that the audio format defaults to signed 16-bit (S16). Your dmix device only supports S32. One of them needs to change to accommodate the either. Either configure librespot to output S32 instead. Or configure dmix to accept S16 (example at Alsa Opensrc Org - Independent ALSA and linux audio support site)