music_led_strip_control icon indicating copy to clipboard operation
music_led_strip_control copied to clipboard

Tutorial for Virtual Audio Input/Loopback

Open TobKra96 opened this issue 3 years ago β€’ 19 comments

Create a Tutorial for a virtual audio input device or a loopback config. Maybe we can use a Spotify connect app like https://github.com/dtcooper/raspotify

TobKra96 avatar Feb 27 '21 19:02 TobKra96

I have done this whit pulseaudio server and spotifyd but you can use any audio source like vlc ... what is playing on the pi

mlsc

then using the pulsemixer MLSC can be hook to source you like to get audio from mlsc-1 mlsc-2

i have use the same method whit the LedFX project but and here is working

spiro-c avatar Apr 29 '21 09:04 spiro-c

Wow, this is really cool. Thank you. I will install it on my system and write a small tutorial πŸ‘

TobKra96 avatar Apr 30 '21 09:04 TobKra96

My first test failed. It seems like portaudio and pulseaudio fight against each other πŸ˜„. Pyaudio returned this:

ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

I have already tried to uninstall Portaudio, but PyAudio stopped working after this. πŸ˜• Don't worry, I will go into the fight for the next round. πŸ˜„

Have a great day TobKra

TobKra96 avatar May 08 '21 15:05 TobKra96

I have problem using this on the dev2.2 branch i never manage to get it running but on the master i dont have problem

spiro-c avatar May 08 '21 16:05 spiro-c

Ok, thanks for the info. I will check it :)

TobKra96 avatar May 08 '21 16:05 TobKra96

I was able to get a loopback method working with Librespot (dependency of rasp-tidy) back in September of last year for my longboard before I broke the damn thing haha. (Here: Longboard Lights) And just recently I attempted a different method using BlueAlsa AD2P. I know the BlueAlsa AD2P one doesn't require PulseAudio or PortAudio, the Librespot might have worked with just ALSA too. (tested with RPi 4)

carlyd95 avatar Jul 13 '21 07:07 carlyd95

Nice, I will give it a try :)

TobKra96 avatar Jul 14 '21 17:07 TobKra96

I got my setup working well and wanted to share... :D


Purpose:

  • Alleviate the need for a microphone
  • Soley sample the streamed audio (not ambient noise)
  • Output audio via USB soundcard

Audio Input Options:


Loopback Tutorial (BlueALSA version):

  1. Install BlueALSA
  • sudo apt install bluealsa
  1. Enabling Audio Profile Sink Role
  2. Open the configuration file for bluealsa service
  • sudo nano /lib/systemd/system/bluealsa.service
  • Change ExecStart=/usr/bin/bluealsa to ExecStart=/usr/bin/bluealsa -p a2dp-sink
  • Save changes
  1. Reboot
  • sudo reboot
  1. Start ALSA loop
  • sudo modprobe snd-aloop
  • (optional) set up a systemd service or add it in /etc/rc.local that runs this at boot
  1. Configure asound.conf
  • sudo nano /etc/asound.conf
  • refer to attached asound.conf.txt file (it sets up a multi named both and plays to loopout for MLSC and hw:1,0, the USB soundcard)
  1. Connect to A2DP
  • sudo apt install expect
  • run attached autobt.sh script file (don't forget file permissions)

OR

  • or use bluetoothctl
  1. Direct Audio Stream to "both" previously setup in asound.conf
  • play audio on the device you used to connect to the RPi A2DP
  • bluealsa-aplay -d both 00:00:00:00:00:00
  • (optional) set up a systemd service that runs this at boot
  1. Start MLSC If Not Running
  • change audio input source to whichever corresponds to loopout (not loopin!)
  • restart MLSC

DONE!


Notes: I have experienced some issues with the sample rate. Example: PyAudio says loopout's sample rate is 44,100 but MLSC's music effects do not function properly with this sample rate, instead I have found a few random ones when input do. I am using 24000.


Sources Used:


Thanks for everyone's work on this project!

If anyone needs help trying this let me know, and if I made any mistakes or something isn't working just let me know :)

carlyd95 avatar Jul 17 '21 17:07 carlyd95

I used this config from https://trac.ffmpeg.org/wiki/Capture/ALSA

Record audio from an application while also routing the audio to an output device

modprobe snd-aloop pcm_substreams=1
# /etc/asound.conf
pcm.multi {
    type route;
    slave.pcm {
        type multi;
        slaves.a.pcm "output";
        slaves.b.pcm "loopin";
        slaves.a.channels 2;
        slaves.b.channels 2;
        bindings.0.slave a;
        bindings.0.channel 0;
        bindings.1.slave a;
        bindings.1.channel 1;
        bindings.2.slave b;
        bindings.2.channel 0;
        bindings.3.slave b;
        bindings.3.channel 1;
    }

    ttable.0.0 1;
    ttable.1.1 1;
    ttable.0.2 1;
    ttable.1.3 1;
}

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

pcm.output {
	type hw
	card 0 # Your Output Device Name
}

pcm.loopin {
	type plug
	slave.pcm "hw:Loopback,0,0"
}

pcm.loopout {
	type plug
	slave.pcm "hw:Loopback,1,0"
}

The mpg123 and shairport-sync are worked. But MLSC just not working, even it found a list of audio devices. It drives me crazy, does anyone have work way?

JohnGuan avatar Aug 24 '21 18:08 JohnGuan

I used this config from https://trac.ffmpeg.org/wiki/Capture/ALSA

Record audio from an application while also routing the audio to an output device

modprobe snd-aloop pcm_substreams=1
# /etc/asound.conf
pcm.multi {
   type route;
   slave.pcm {
       type multi;
       slaves.a.pcm "output";
       slaves.b.pcm "loopin";
       slaves.a.channels 2;
       slaves.b.channels 2;
       bindings.0.slave a;
       bindings.0.channel 0;
       bindings.1.slave a;
       bindings.1.channel 1;
       bindings.2.slave b;
       bindings.2.channel 0;
       bindings.3.slave b;
       bindings.3.channel 1;
   }

   ttable.0.0 1;
   ttable.1.1 1;
   ttable.0.2 1;
   ttable.1.3 1;
}

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

pcm.output {
	type hw
	card 0 # Your Output Device Name
}

pcm.loopin {
	type plug
	slave.pcm "hw:Loopback,0,0"
}

pcm.loopout {
	type plug
	slave.pcm "hw:Loopback,1,0"
}

The mpg123 and shairport-sync are worked. But MLSC just not working, even it found a list of audio devices. It drives me crazy, does anyone have work way?

Does it show the loopout or loopin as one of the sources and you are selecting that source for MLSC to use?

carlyd95 avatar Aug 24 '21 19:08 carlyd95

I used this config from https://trac.ffmpeg.org/wiki/Capture/ALSA Record audio from an application while also routing the audio to an output device

modprobe snd-aloop pcm_substreams=1
# /etc/asound.conf
pcm.multi {
   type route;
   slave.pcm {
       type multi;
       slaves.a.pcm "output";
       slaves.b.pcm "loopin";
       slaves.a.channels 2;
       slaves.b.channels 2;
       bindings.0.slave a;
       bindings.0.channel 0;
       bindings.1.slave a;
       bindings.1.channel 1;
       bindings.2.slave b;
       bindings.2.channel 0;
       bindings.3.slave b;
       bindings.3.channel 1;
   }

   ttable.0.0 1;
   ttable.1.1 1;
   ttable.0.2 1;
   ttable.1.3 1;
}

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

pcm.output {
 type hw
 card 0 # Your Output Device Name
}

pcm.loopin {
 type plug
 slave.pcm "hw:Loopback,0,0"
}

pcm.loopout {
 type plug
 slave.pcm "hw:Loopback,1,0"
}

The mpg123 and shairport-sync are worked. But MLSC just not working, even it found a list of audio devices. It drives me crazy, does anyone have work way?

Does it show the loopout or loopin as one of the sources and you are selecting that source for MLSC to use?

Ummm... yes.. I tried everyone

JohnGuan avatar Aug 24 '21 19:08 JohnGuan

@JohnGuan

in my asound.conf I have my loop configured a lil different, worth a try:

pcm.loopin { type plug slave.pcm "plughw:Loopback,0,0" }

pcm.loopout { type plug slave.pcm "plughw:Loopback,1,0" }

carlyd95 avatar Aug 24 '21 19:08 carlyd95

@carlyd95 Thank you for your generous answer. In fact, I tried yours. It doesn't work.πŸ˜‚ I think I may need to re-flash the system. My current system is Raspberry Pi OS Lite. I'm not quite sure if it has any effect, but the PulseAudio solution written in the previous reply seems to be more dependent on the desktop environment.

JohnGuan avatar Aug 24 '21 19:08 JohnGuan

@JohnGuan weird, mine works just fine using BlueALSA as an A2DP πŸ˜† I’ll have to mess with shairport sometime soon.

carlyd95 avatar Aug 24 '21 19:08 carlyd95

Anyone got any ideas on how turn an MLSC pi into a DLNA speaker?

Marshalldog avatar Jul 02 '22 12:07 Marshalldog

or snapcast snapclient?

Marshalldog avatar Jul 02 '22 12:07 Marshalldog

or snapcast snapclient?

Snapcast worked when I tried it initially. I dont know if it has support for DLNA though.

Also bluealsa method isn’t as easy with raspberry pi now as it got removed from the official OS. I haven’t got it to work since.

carlyd95 avatar Jul 02 '22 13:07 carlyd95

@carlyd95 I've been trying to get snapcast client running but the second I install MLSC, snapcast client stops working.

● snapclient.service - Snapcast client
     Loaded: loaded (/lib/systemd/system/snapclient.service; enabled; vendor pr>
     Active: failed (Result: exit-code) since Thu 2022-07-07 08:12:00 BST; 3min>
       Docs: man:snapclient(1)
    Process: 711 ExecStart=/usr/bin/snapclient --logsink=system $SNAPCLIENT_OPT>
   Main PID: 711 (code=exited, status=1/FAILURE)
        CPU: 190ms

Jul 07 08:12:00 raspberrypi systemd[1]: snapclient.service: Scheduled restart j>
Jul 07 08:12:00 raspberrypi systemd[1]: Stopped Snapcast client.
Jul 07 08:12:00 raspberrypi systemd[1]: snapclient.service: Start request repea>
Jul 07 08:12:00 raspberrypi systemd[1]: snapclient.service: Failed with result >
Jul 07 08:12:00 raspberrypi systemd[1]: Failed to start Snapcast client.

how did you set yours up?

Marshalldog avatar Jul 07 '22 07:07 Marshalldog

what i did

sudo apt-get update
sudo apt-get upgrade
sudo reboot

I upload snapclient_0.26.0-1_armhf.deb via winscp ran the following commands

sudo apt install vim
sudo dpkg -i snapclient_0.26.0-1_armhf.deb
sudo apt-get -f install
cd /etc/default
sudo vim snapclient

inserted the IP address of the host, save and exit

sudo update-rc.d snapclient defaults
sudo service snapclient status

at this point snapcast was working

then I ran

curl -sSL https://raw.githubusercontent.com/TobKra96/music_led_strip_control/master/setup.sh | sudo bash -s -- -b master
sudo reboot
sudo apt-get -f install

then when I checked snapcast via:

sudo service snapclient status

I got:

● snapclient.service - Snapcast client
     Loaded: loaded (/lib/systemd/system/snapclient.service; enabled; vendor pr>
     Active: failed (Result: exit-code) since Thu 2022-07-07 08:12:00 BST; 3min>
       Docs: man:snapclient(1)
    Process: 711 ExecStart=/usr/bin/snapclient --logsink=system $SNAPCLIENT_OPT>
   Main PID: 711 (code=exited, status=1/FAILURE)
        CPU: 190ms

Jul 07 08:12:00 raspberrypi systemd[1]: snapclient.service: Scheduled restart j>
Jul 07 08:12:00 raspberrypi systemd[1]: Stopped Snapcast client.
Jul 07 08:12:00 raspberrypi systemd[1]: snapclient.service: Start request repea>
Jul 07 08:12:00 raspberrypi systemd[1]: snapclient.service: Failed with result >
Jul 07 08:12:00 raspberrypi systemd[1]: Failed to start Snapcast client.

Marshalldog avatar Jul 07 '22 07:07 Marshalldog