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

Raspotify - not connecting / not playing sound #314

Closed
farhandsome opened this issue Oct 23, 2020 · 89 comments
Closed

Raspotify - not connecting / not playing sound #314

farhandsome opened this issue Oct 23, 2020 · 89 comments

Comments

@farhandsome
Copy link

Hi all, one issue I'm having is Raspotify not playing any sound when it does connect. Here is what I pulled from the log, it looks like this is where the issue arises:

Oct 23 12:01:00 raspberrypi librespot[3057]: [2020-10-23T17:01:00Z INFO librespot_playback::player] Loading with Spotify URI spotify:track:1McVhmCk8l07LIIdlIpqTF
Oct 23 12:01:00 raspberrypi librespot[3057]: [2020-10-23T17:01:00Z INFO librespot_playback::player] (188766 ms) loaded
Oct 23 12:01:00 raspberrypi librespot[3057]: ALSA lib pcm_dmix.c:1108:(snd_pcm_dmix_open) unable to open slave
Oct 23 12:01:00 raspberrypi librespot[3057]: [2020-10-23T17:01:00Z ERROR librespot_playback::audio_backend::alsa] Alsa error PCM open ALSA function 'snd_pcm_open' failed with error 'ENOENT: No such file or directory'
Oct 23 12:01:00 raspberrypi librespot[3057]: [2020-10-23T17:01:00Z ERROR librespot_playback::player] Could not start audio: Alsa error: PCM open failed
Oct 23 12:01:00 raspberrypi librespot[3057]: ALSA lib pcm_dmix.c:1108:(snd_pcm_dmix_open) unable to open slave
Oct 23 12:01:00 raspberrypi librespot[3057]: [2020-10-23T17:01:00Z ERROR librespot_playback::audio_backend::alsa] Alsa error PCM open ALSA function 'snd_pcm_open' failed with error 'ENOENT: No such file or directory'
Oct 23 12:01:00 raspberrypi librespot[3057]: [2020-10-23T17:01:00Z ERROR librespot_playback::player] Could not start audio: Alsa error: PCM open failed
Oct 23 12:01:00 raspberrypi librespot[3057]: ALSA lib pcm_dmix.c:1108:(snd_pcm_dmix_open) unable to open slave
Oct 23 12:01:00 raspberrypi librespot[3057]: [2020-10-23T17:01:00Z ERROR librespot_playback::audio_backend::alsa] Alsa error PCM open ALSA function 'snd_pcm_open' failed with error 'ENOENT: No such file or directory'
Oct 23 12:01:00 raspberrypi librespot[3057]: [2020-10-23T17:01:00Z ERROR librespot_playback::player] Could not start audio: Alsa error: PCM open failed
Oct 23 12:01:00 raspberrypi librespot[3057]: thread '' panicked at 'called Option::unwrap() on a None value', playback/src/audio_backend/alsa.rs:103:19

Anyone have any idea what this could be?

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Oct 23, 2020

A bunch of ALSA errors, that tells me that something is misconfigured or not configured at all.

  1. What version Pi are you running on?

  2. Will raspotify have to coexist with other audio players, will anything else be making noise? If so what?

  3. What device are you trying to send audio to?

  4. What is the output of aplay -l (lowercase L) ?

  5. What does your /etc/default/raspotify and /etc/asound.conf look like? asound.conf does not exist unless it is created.

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Oct 24, 2020

@dtcooper This is were a wiki would come in handy. ALSA configuration can be a bit of dark art.

@farhandsome
Copy link
Author

Hi @JasonLG1979,

Thanks for the reply.

  1. Running on an RPI4 Buster, fully updated.
  2. Not at the moment, it is the only thing playing.
  3. Here’s where the issue probably lies. RPi’s audio config is abysmal. I have a USB speaker and trying to get all of the applications to default to that has been a pain since I’m also running the Pi thru a HDMI monitor. In the end, I created a .asoundrc file, changed the boot/config file (commented out hdmi force hot plug). This seemed to register my USB device finally.
  4. The only playback device (card 0) is now my USB device which is good. I can play music through Bluetooth and Airplay, just not Raspotify.
  5. I’ll check when I’m home, but I don’t have an .asound.conf file, just .asoundrc. Could that be it?

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Oct 24, 2020

5. I’ll check when I’m home, but I don’t have an .asound.conf file, just .asoundrc. Could that be it?

Probably. asoundrc is basically a per user asound.conf. If you want things to be systemwide regardless of user you should use asound.conf. A asoundrc in ~/ would have no affect on system services like raspotify.

@JasonLG1979
Copy link
Collaborator

  1. Running on an RPI4 Buster, fully updated.

Headless or with a desktop?

2. Not at the moment, it is the only thing playing.

Basically what I'm asking is if your Pi is going to be a dedicated Spotify connect device? If raspotify/librespot is the absolutely going to be the only thing outputting sound? If so we can optimize the configuration for raspotify/librespot and we don't have to care about sharing the audio output.

4. The only playback device (card 0) is now my USB device which is good. I can play music through Bluetooth and Airplay, just not Raspotify.

Are you testing those services as a user or as system daemons?

@farhandsome
Copy link
Author

@JasonLG1979

  1. Desktop. Currently connected to an HDMI monitor.

  2. Ah, right. No, it won’t. I also have set up the Alexa AVS module. They won’t be running simultaneously, but Spotify isn’t the only sound emitting app on the Pi.

  3. So far, I’ve only tested them as the user pi. Pretty new to all this, could you provide some syntax on how to test it as the system daemons?

@JasonLG1979
Copy link
Collaborator

2. Ah, right. No, it won’t. I also have set up the Alexa AVS module. They won’t be running simultaneously, but Spotify isn’t the only sound emitting app on the Pi.

Will the output just be speech?

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Oct 24, 2020

3. So far, I’ve only tested them as the user pi. Pretty new to all this, could you provide some syntax on how to test it as the system daemons?

That would explain why they work and raspotify doesn't. If you don't plan on using them there's no need to test them. But if you are it's going to be a bit of a dance to configure them so they gracefully yield to each other. Otherwise if you try to use more than one at a time you may get audio glitches caused by CPU spikes. It may not be a problem for a Pi 4 though, I don't know? The only Pi I have to play with at the moment is a Pi Zero but the Pi 4 is exponentially more powerful.

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Oct 24, 2020

could you provide some syntax on how to test it as the system daemons

Untested but I imagine you could just test them as root.

sudo su

Then test whatever:

<random service>

Then exit back to the pi user:

exit

@farhandsome
Copy link
Author

@JasonLG1979 I’ll give it a shot. May be worth a fresh install of Raspotify looks like

@farhandsome
Copy link
Author

@JasonLG1979 the Alexa output will just be speech, correct. I would love to have them both running at the same time since this project is for a Magic Mirror, but I’m not sure how feasible that is with all these alsa issues.

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Oct 24, 2020

@JasonLG1979 the Alexa output will just be speech, correct. I would love to have them both running at the same time since this project is for a Magic Mirror, but I’m not sure how feasible that is with all these alsa issues.

It shouldn't be hard. If the output is just speech then it really doesn't matter much about the sound quality of the speech. Meaning we can optimize the output for raspotify. Speech is pretty narrow bandwidth compared to music. Resampling it to what raspotify puts out (16bit 44.1 k) even if needed isn't going to affect it in an audible way.

If the Alexa bit has hooks we can even make it so raspotify is temporarily muted while Alexa speaks by giving raspotify a separate software volume control. Basically Alexa fires a hook script before it speaks that mutes raspotify and then when it's done speaking it fire another script that unmutes raspotify.

@farhandsome
Copy link
Author

@JasonLG1979

That would be awesome. The Alexa module does have hooks, I know that because I’m trying to use an addressable LED strip to indicate its “listening” and “speaking” states.

However, the Alexa files are all .cpp and I’m pretty new to this so I don’t know how to integrate python scripts into those files.

I also use an audio-reactive application to sync up the LED strips when music is playing from the Pi.

My ideal state is having the LED visualization while music is playing, but if the wake word model of Alexa is activated, it mutes Spotify and switches to Alexa AVS as well as switches the LED strip to the Alexa visual indicator.

@JasonLG1979
Copy link
Collaborator

That would be awesome. The Alexa module does have hooks, I know that because I’m trying to use an addressable LED strip to indicate its “listening” and “speaking” states.

However, the Alexa files are all .cpp and I’m pretty new to this so I don’t know how to integrate python scripts into those files.

I was hoping to keep it simple with a bash script since all we'd be doing is muting and unmuting volume. But if you can hook a python script you can just use subprocess to fire off the commands.

My ideal state is having the LED visualization while music is playing, but if the wake word model of Alexa is activated, it mutes Spotify and switches to Alexa AVS as well as switches the LED strip to the Alexa visual indicator.

That should be possible. I can help with the ALSA plumbing but I don't use alexa and I don't have any of the hardware, so the rest you may ultimately be on your own, sorry.

@JasonLG1979
Copy link
Collaborator

I would start with a fresh install and then we can step though getting a good ALSA config. Then you can install alexa and raspotify.

@JasonLG1979
Copy link
Collaborator

OK this will get you 2 separate inputs and volumes (one for spotify and one for alexa) and a master volume and default input.

Simplified (not showing all the resampling and software mixing bits) it looks like this:

stuff

Everything is resampled to 16 bit 44100 which is what raspotify/librespot outputs anyway so in the case of raspotify/librespot no resampling will actually be done. I don't know what your input looks like so we may have to tweak it if it's on the same device?

The device names are the same as the control names (spotify and alexa) so any config where you need to specify a device and/or a control they would be named the same. The master volume is named PCM because that's the convention for the name of the master volume control of a device. Most programs if you do not or can not specify a volume control will use the PCM control. Librespot does not keep track of outside changes to the volume if you use ALSA volume control so in your case until they fix that I would use the builtin software volume control and only use the "spotify" ALSA volume control for muting or dropping the volume when alexa speaks.

To get raspotify to use the spotify output add --device spotify to the OPTIONS section of /etc/default/raspotify.

# /etc/asound.conf

# use aplay -l to determine the below.

# Change to the card number or name that you want to be the default control card.
# Default: 0
defaults.ctl.card 0

# Change to the card number or name that you want to be the default playback card.
# It should usually be the same as defaults.ctl.card.
# Default: 0
defaults.pcm.card 0

# Change to the device number that you want to be the default device on the default card.
# 0 or 1 is usually the correct device number.
# Default: 0
defaults.pcm.device 0

# Change to the subdevice number that you want to be the default subdevice on the default device.
# Should rarely need to be changed.
# Default: -1
defaults.pcm.subdevice -1

pcm.!default {
    type plug
    slave {
        channels 2
        rate 44100
        format S16_LE
        pcm {
            type softvol
            slave.pcm {
                type dmix
                ipc_key 1024
                ipc_perm 0660
                ipc_gid audio
                slowptr true
                hw_ptr_alignment roundup
                slave {
                    pcm {
                        type hw
                        card {
                            @func refer
                            name defaults.pcm.card
                        }
                        device {
                            @func refer
                            name defaults.pcm.device
                        }
                        subdevice {
                            @func refer
                            name defaults.pcm.subdevice
                        }
                    }
                    channels 2
                    period_size 0
                    buffer_size 0
                    buffer_time 0
                    period_time 250000
                    periods 4
                    rate 44100
                    format S16_LE
                }
                bindings {
                    0 0
                    1 1
                }
            }
            control {
                name PCM
                card {
                    @func refer
                    name defaults.ctl.card
                }
            }
        }
    }
}

pcm.spotify {
    type plug
    slave {
        channels 2
        rate 44100
        format S16_LE
        pcm {
            type softvol
            slave.pcm default
            control {
                name spotify
                card {
                    @func refer
                    name defaults.ctl.card
                }
            }
        }
    }
}

pcm.alexa {
    type plug
    slave {
        channels 2
        rate 44100
        format S16_LE
        pcm {
            type softvol
            slave.pcm default
            control {
                name alexa
                card {
                    @func refer
                    name defaults.ctl.card
                }
            }
        }
    }
}

@JasonLG1979
Copy link
Collaborator

There will be about 1 sec of audio latency because of the buffer so anything used to control LED's may need to take that into account depending on how it's implemented to keep the audio and LED's in sync.

@JasonLG1979
Copy link
Collaborator

You can test each output with:

speaker-test -Dalexa -c2 -l1

speaker-test -Dspotify -c2 -l1

It will be loud (the default volume I believe is 80% ?) so turn the volume down on whatever your sound card is connected to.

If whatever your sound card is connected to does not have a volume control you can use the -S arg to set the "scale" which is basically the volume. See speaker-test -h for details.

@farhandsome
Copy link
Author

@JasonLG1979 ,

This is awesome work. I've been out of town for the last few days, but I'll be back tomorrow so I'll definitely give this a test. Do I need to create this in a new file? (asound.conf). Because I have created an ".asoundrc" file that has a similar JSON-type look for the audio outputs. It only specifies system audio though, not specific application audios. I'm wondering if I should just delete that and use this?

@farhandsome
Copy link
Author

@JasonLG1979 ,

I also have a USB microphone (card 1, device 1) - is this something I should specify in asound.conf?

@JasonLG1979
Copy link
Collaborator

Do I need to create this in a new file? (asound.conf). Because I have created an ".asoundrc" file that has a similar JSON-type look for the audio outputs. It only specifies system audio though, not specific application audios. I'm wondering if I should just delete that and use this?

Yes create /etc/asound.conf and paste the contents. Delete .asoundrc. asound.conf is system wide.

I also have a USB microphone (card 1, device 1) - is this something I should specify in asound.conf?

If alexa is the only thing that will be using the mic and you can directly specify the mic somewhere in the alexa audio config you shouldn't have to worry about it. Just let alexa have exclusive access to the mic. Otherwise we can make the virtual alexa audio device asymmetrical and full duplex, meaning we point the playback to your sound card and the capture to your usb mic. Think of all this as a virtual mixing board with faders, batch bays, bus mixers, converters, inputs and outputs. If you can imagine a signal path we can make it happen.

@JasonLG1979
Copy link
Collaborator

It only specifies system audio though, not specific application audios

You don't have to route everything though program specific virtual devices. If not specified a program should use default. default is specifically designed to do auto samplerate, format and channel count conversion and to software mix all streams. You could have as many applications playing audio simultaneously with any samplerate, format or channel count. The per app virtual devices just add a per app volume so you can adjust each apps volume independently at the system level. so you could use them to level match for example.

@farhandsome
Copy link
Author

@JasonLG1979

bindings {
0 0
1 1
}

Is this going to be different depending on which card/device the audio outputs are on?

@JasonLG1979
Copy link
Collaborator

Is this going to be different depending on which card/device the audio outputs are on?

That's not even needed most of the time. It's just saying channel 0 from dmix goes to channel 0 on the sound card and channel 1 goes to channel 1 on the sound card. Most stereo cards don't have a problem but some 5.1 and 7.1 drivers screw up channel mapping.

@JasonLG1979
Copy link
Collaborator

You could switch it and the left channel would come out of the right speaker and the right channel would come out of the left speaker.

@JasonLG1979
Copy link
Collaborator

If you have a 5.1 or 7.1 card we can spread out 2 channels to fill them. Basically left would get duplicated to all of the left channels and right to all of the right channels and then the center and sub channels would be mono (50% right + 50% left). there wouldn't be any surround sound effects but at least you'd be using all your channels. If you wanted to get fancy though you could probably measure the distance from each speaker to your listening position and calculate the needed delay to make sure everything hits you in phase at your listening position. But that's probably overkill.

@farhandsome
Copy link
Author

@JasonLG1979

I tried both audio outputs and got this error message each time:

Playback device is spotify
Stream parameters are 48000Hz, S16_LE, 2 channels
Using 16 octaves of pink noise
ALSA lib pcm_params.c:2226:(snd1_pcm_hw_refine_slave) Slave PCM not usable
ALSA lib pcm_params.c:2226:(snd1_pcm_hw_refine_slave) Slave PCM not usable
Broken configuration for playback: no configurations available: No such file or directory
Setting of hwparams failed: No such file or directory

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Oct 28, 2020

Ok. You said your playback card is 0 so let's try to find the device number, and make sure your card supports 16bit 44.1k.

aplay -Dhw:0,0 --dump-hw-params /usr/share/sounds/alsa/Front_Right.wav

Of if that doesn't work try this:

aplay -Dhw:0,1 --dump-hw-params /usr/share/sounds/alsa/Front_Right.wav

The output I want to see should look something like this:

ACCESS:  MMAP_INTERLEAVED RW_INTERLEAVED
FORMAT:  S16_LE S24_LE S32_LE
SUBFORMAT:  STD
SAMPLE_BITS: [16 32]
FRAME_BITS: [32 64]
CHANNELS: 2
RATE: [8000 192000]
PERIOD_TIME: (166 8192000]
PERIOD_SIZE: [32 65536]
PERIOD_BYTES: [256 524288]
PERIODS: [2 4096]
BUFFER_TIME: (333 16384000]
BUFFER_SIZE: [64 131072]
BUFFER_BYTES: [256 524288]
TICK_TIME: ALL

It will tell us what your sound card is capable of. It will also tell us the device number. (the device number being the one that works)

@JasonLG1979
Copy link
Collaborator

It's most likely one of 3 things:

  1. This section is not configured correctly:
# Change to the card number or name that you want to be the default control card.
# Default: 0
defaults.ctl.card 0

# Change to the card number or name that you want to be the default playback card.
# It should usually be the same as defaults.ctl.card.
# Default: 0
defaults.pcm.card 0

# Change to the device number that you want to be the default device on the default card.
# 0 or 1 is usually the correct device number.
# Default: 0
defaults.pcm.device 0

# Change to the subdevice number that you want to be the default subdevice on the default device.
# Should rarely need to be changed.
# Default: -1
defaults.pcm.subdevice -1
  1. Your card doesn't support the S16_LE format.
  2. Your card doesn't support a sampling rate of 44100.

Changing format and sampling rate isn't a huge deal though. But if you have to resample you'll want to install and enable a decent resampling lib. The default sounds like garbage and resampling is going to use a little more CPU.

@farhandsome
Copy link
Author

Playing WAVE '/usr/share/sounds/alsa/Front_Right.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
HW Params of device "hw:0,0":
ACCESS: MMAP_INTERLEAVED RW_INTERLEAVED
FORMAT: S16_LE
SUBFORMAT: STD
SAMPLE_BITS: 16
FRAME_BITS: 32
CHANNELS: 2
RATE: 48000
PERIOD_TIME: [1000 2730667)
PERIOD_SIZE: [48 131072]
PERIOD_BYTES: [192 524288]
PERIODS: [2 1024]
BUFFER_TIME: [2000 5461334)
BUFFER_SIZE: [96 262144]
BUFFER_BYTES: [384 1048576]
TICK_TIME: ALL
aplay: set_params:1345: Channels count non available

Here's the output of "aplay -Dhw:0,0 --dump-hw-params /usr/share/sounds/alsa/Front_Right.wav"

@JasonLG1979
Copy link
Collaborator

Old reliable Logitech C270 lol.

Mine's some Chinese knock off,lol!!!

@farhandsome
Copy link
Author

@JasonLG1979 how can I see what apps are using the mic? I've turned off the audio reactive python code, but it still seems to be hogging the mic for some reason.

@JasonLG1979
Copy link
Collaborator

With a single mic though you're going to have trouble with Alexa hearing you when you speak when there's music playing or any other background noise. You might want to look into an array mic.

how can I see what apps are using the mic? I've turned off the audio reactive python code, but it still seems to be hogging the mic for some reason.

sudo apt install lsof
sudo lsof /dev/snd/pcm*

@farhandsome
Copy link
Author

@JasonLG1979, yeah I've realized that limitation.. I'll probably upgrade eventually to a good mic. Here's what lsof tells me:

lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF  NODE NAME
python3 1006 root  mem    CHR 116,56          11155 /dev/snd/pcmC1D0c
python3 1006 root    5u   CHR 116,56      0t0 11155 /dev/snd/pcmC1D0c

Not sure what these are...

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Nov 1, 2020

Well it's a python3 program? Not very helpful,lol!!! It's run as root so maybe a system service of some kind?

You can kill it and see what happens:

sudo kill -KILL 1006

@JasonLG1979
Copy link
Collaborator

I personally would have not started with the desktop version I would have started headless and then configured the audio and then installed and configured raspotify and whatever other audio bits you wanted and then installed alexa and then what ever UI you needed for the magic mirror stuff. You're going to be fighting with the desktop environment for control of the audio devices the whole way otherwise.

@JasonLG1979
Copy link
Collaborator

It also makes it a lot easier to troubleshoot if you work from the bottom up instead of the top down.

@farhandsome
Copy link
Author

farhandsome commented Nov 1, 2020 via email

@JasonLG1979
Copy link
Collaborator

Thanks again for walking me through all this!

No problem, this stuff is my jam,lol!!! I'm learning stuff along the way too!!!

@JasonLG1979
Copy link
Collaborator

In the mean time I have an extra SD card so I can work though everything up to installing and configuring raspotify and bluetooth if you want and work on a new asound.conf. That way we'll have a cheat sheet. From there we can document the rest of the process so if you ever have to do it again it won't take as long and you can share it with other people so they can do it to.

@farhandsome
Copy link
Author

Wow that would be extremely helpful. I would appreciate it very much, but don’t feel obligated to of course.

The three audio applications I’m trying to configure together are Raspotify, the Alexa AVS service, and the DancyPi-Audio-Reactive LEDs.

The latter two use both the mic and the speaker while Raspotify only uses the speaker.

That should be it in terms of overall summary of the configuration. The speaker is default card 0 and the mic is card 1.

Thanks again man. Looking forward to working on it!!

@farhandsome
Copy link
Author

I’ll make a diagram for it when I’m home.

@JasonLG1979
Copy link
Collaborator

I'm guessing DancyPi-Audio-Reactive LEDs is what was hogging your mic then. Looking at the source code it just wants to use the "default" device so we can just do what I said earlier. The install script looks like it will just clobber an existing asound.conf so after you install DancyPi-Audio-Reactive LEDs we'll have to check that or it will break everything else.

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Nov 1, 2020

I'd say install DancyPi-Audio-Reactive LEDs 1st thing that way if doesn't screw up our asound.conf later. Because the one they'd clobber our fancy custom asound.conf with is this:

pcm.!default {
    type hw
    card 1
}
ctl.!default {
    type hw
    card 1
}

That totally will not due at all...

@JasonLG1979
Copy link
Collaborator

In your case LED controls that worked of the output would be better then working off the mic. Something like a copy of the output to a pipe file, and then have your python script watch the file and process data as it comes?

@farhandsome
Copy link
Author

That would be great as it would free up the mic to only be used for Alexa. No idea how to implement that though. Maybe there’s another repository somewhere that uses output rather than mic feedback.

@farhandsome
Copy link
Author

farhandsome commented Nov 1, 2020

Found this thread on how to record audio output: https://superuser.com/questions/1194750/how-to-record-the-audio-output-on-raspbian

Perhaps, this can be used with the RPI-WS281X library (Python scripts to control LED strip) to configure based on amplitude and frequency?

@JasonLG1979
Copy link
Collaborator

Perhaps, this can be used with the RPI-WS281X library (Python scripts to control LED strip) to configure based on amplitude and frequency?

If you can figure out how to get DancyPi-Audio-Reactive LEDs to listen to another input besides default you could just copy the output from default with the copy plugin and then loop it though the input that DancyPi-Audio-Reactive LEDs is listening to. You could also downsample by a factor of 2 (22050) in ALSA as not much happens in music and nothing happens in speech above 10k. In theory you'd think downsampling before you get to the python code would be better because it would be done in C?

I can only offer guesses as I don't have the hardware to test with.

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Nov 1, 2020

Another approach could be to split the output so it goes to your USB DAC and the analog out. AFAIK the analog out is a PWM maybe even the same one that DancyPi-Audio-Reactive LEDs already drives?

@farhandsome
Copy link
Author

@JasonLG1979

Yeah, let me reach out on their issues log and see if there's any way to not use the mic output. Analog out? How does that work?

@farhandsome
Copy link
Author

arecord -c1 -d60 -fS16_LE -r16000 -twav -Dhw:1,0 test3.wav

Also this worked like a charm. Tested it from 15-20 feet away and it was good.

@JasonLG1979
Copy link
Collaborator

Analog out? How does that work?

Got me,lol??? I'm not very versed in the raspberry pi audio hardware. I never use it. It sounds like garbage,lol!!!

Anyway, this will get you a duplex'd default device with an output volume control named PCM and a mic volume with up to 20dB of boost named Capture, a spotify output device, bluetooth output device, and a duplex'd alexa device all with their own output volume.

All outputs (playback) will accept whatever as far as bit depth and sampling rate but anything not S16_LE 48k will be resampled.
All inputs (capture) will output whatever as far as bit depth and sampling rate but anything not S16_LE 16k will be resampled.


# /etc/asound.conf

# use aplay -l to determine the below.

# Change to the card number or name that you want to be the default control card.
# Default: 0
defaults.ctl.card 0

# Change to the card number or name that you want to be the default playback card.
# It should usually be the same as defaults.ctl.card.
# Default: 0
defaults.pcm.card 0

# Change to the device number that you want to be the default device on the default card.
# 0 or 1 is usually the correct device number.
# Default: 0
defaults.pcm.device 0

# Change to the subdevice number that you want to be the default subdevice on the default device.
# Should rarely need to be changed.
# Default: -1
defaults.pcm.subdevice -1

# To install high quality samplerate converters on Debian based systems:
# sudo apt install -y --no-install-recommends libasound2-plugins 

# To list available rate converters:
# echo "$(ls /usr/lib/*/alsa-lib | grep "libasound_module_rate_")" | sed -e "s/^libasound_module_rate_//" -e "s/.so$//"

# Uncomment and replace speexrate_medium with the rate_converter of your choice. (speexrate and speexrate_medium offer the best "bang for your buck")
# defaults.pcm.rate_converter speexrate_medium

pcm.dmixer {
    type plug
    slave {
        channels 2
        rate 48000
        format S16_LE
        pcm {
            type softvol
            control {
                name PCM
                card {
                    @func refer
                    name defaults.ctl.card
                }
            }
            slave.pcm {
                type dmix
                ipc_key {
                    @func refer
                    name defaults.pcm.ipc_key
                }
                ipc_gid {
                    @func refer
                    name defaults.pcm.ipc_gid
                }
                ipc_perm {
                    @func refer
                    name defaults.pcm.ipc_perm
                }
                slave {
                    pcm {
                        type hw
                        card {
                            @func refer
                            name defaults.pcm.card
                        }
                        device {
                            @func refer
                            name defaults.pcm.device
                        }
                        subdevice {
                            @func refer
                            name defaults.pcm.subdevice
                        }
                    }
                    channels 2
                    period_size 0
                    buffer_size 0
                    buffer_time 0
                    period_time 250000
                    periods 4
                    rate 48000
                    format S16_LE
                }
                bindings {
                    0 0
                    1 1
                }
            }
        }
    }
}

pcm.dsnooper {
    type plug
    slave {
        channels 1
        rate 16000
        format S16_LE
        pcm {
            type softvol
            # up to 20dB of mic boost.
            max_dB 20.0
            control {
                name Capture
                card {
                    @func refer
                    name defaults.ctl.card
                }
            }
            slave.pcm {
                type dsnoop
                ipc_key {
                    @func refer
                    name defaults.pcm.ipc_key
                }
                ipc_gid {
                    @func refer
                    name defaults.pcm.ipc_gid
                }
                ipc_perm {
                    @func refer
                    name defaults.pcm.ipc_perm
                }
                slave {
                    pcm {
                        type hw
                        card 1
                        device 0
                    }
                    channels 1
                    period_size 0
                    buffer_size 0
                    buffer_time 0
                    period_time 250000
                    periods 4
                    rate 16000
                    format S16_LE
                }
            }
        }
    }
}

# Default input and output.
pcm.!default {
    type asym
    playback.pcm {
        type plug
        slave.pcm dmixer
    }
    capture.pcm {
        type plug
        slave.pcm dsnooper
    }
}

ctl.!default {
    type hw
    card {
        @func refer
        name defaults.ctl.card
    }
}

# Output only.
pcm.bluetooth {
    type plug
    slave {
        channels 2
        rate 48000
        format S16_LE
        pcm {
            type softvol
            slave.pcm dmixer
            control {
                name bluetooth
                card {
                    @func refer
                    name defaults.ctl.card
                }
            }
        }
    }
}

# Output only.
pcm.spotify {
    type plug
    slave {
        channels 2
        rate 48000
        format S16_LE
        pcm {
            type softvol
            slave.pcm dmixer
            control {
                name spotify
                card {
                    @func refer
                    name defaults.ctl.card
                }
            }
        }
    }
}

# Output and input.
pcm.alexa {
    type asym
    capture.pcm {
        type plug
        slave.pcm dsnooper
    }
    playback.pcm {
        type plug
        slave {
            channels 2
            rate 48000
            format S16_LE
            pcm {
                type softvol
                slave.pcm default
                control {
                    name alexa
                    card {
                        @func refer
                        name defaults.ctl.card
                    }
                }
            }
        }
    }
}

All the controls are done in software so you'll have to speaker-test/arecord all of them before they show up in alsamixer but after you'll have something that looks like this:

Screenshot from 2020-11-01 22-30-13

@JasonLG1979
Copy link
Collaborator

In my mind ideally the mythical LED controls would listen to the default output. I'm not sure how you can do that?

@farhandsome
Copy link
Author

@JasonLG1979 Awesome!! I'll try this out in a bit. Currently trying to see how to integrate Python scripts into the .cpp files for Alexa.

Yes, agreed. That'll have to be something I research a little bit more to see if anyone was able to do that.

@JasonLG1979
Copy link
Collaborator

.ccp is C++

@JasonLG1979
Copy link
Collaborator

JasonLG1979 commented Nov 2, 2020

I haven't read the docs but in my mind if you wanted to use python I'd start by using flask to create a REST api to control your device and then hook the skills into that.

@farhandsome
Copy link
Author

@JasonLG1979 ahh gotcha. I was thinking to write the Python scripts in some shell scripts and then just run those using system() command in C++.

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

No branches or pull requests

2 participants