How to use alsa loopback Device with jack audio backend?

#1

Hey!

I am trying to go full jack - but unfortunately Spotify Connect (https://github.com/dtcooper/raspotify) and Airplay (https://github.com/mikebrady/shairport-sync) haven’t been jackified yet.

I found this tutorial how to do it:
https://alsa.opensrc.org/Jack_and_Loopback_device_as_Alsa-to-Jack_bridge

but I haven’t succeeded.

Could anyone make a guide for this forum how to do a proper loopback device with patchbox OS?

0 Likes

#2

Okay, I guess I am asking too much. Maybe help me then figuring it out… so I can write a guide? Jack or linux audio is so badly documented (the guide is not even properly linked in the main Jack Audio website…) … it is needing more quality guides for the rest of us!

0 Likes

#3

Hi, this is as far as I’ve got:

Created /etc/modules-load.d/snd-aloop.conf with this contents:

snd-aloop

Created /etc/modprobe.d/snd-aloop.conf with following contents:

options snd-aloop pcm_substreams=2 # This limits the devices to 2, default is 8

Do sudo reboot to get the virtual loopback devices created.

I got this in my ~/.asoundrc:

# playback PCM device: using loopback subdevice 0,0
pcm.amix {
  type dmix
  ipc_key 219345
  slave {
    pcm "hw:Loopback,0,0"
    rate 48000
    period_size 2048
    buffer_size 32768
    period_time 0
    buffer_time 0
  }
}

# capture PCM device: using loopback subdevice 0,1
pcm.asnoop {
  type dsnoop
  ipc_key 219346
  slave {
    pcm "hw:Loopback,0,1"
    rate 48000
    period_size 2048
    buffer_size 32768
    period_time 0
    buffer_time 0
  }
}

# duplex device combining our PCM devices defined above
pcm.aduplex {
  type asym
  playback.pcm "amix"
  capture.pcm "asnoop"
}

# ------------------------------------------------------
# for jack alsa_in and alsa_out: looped-back signal at other ends
pcm.ploop {
  type plug
  slave {
    pcm "hw:Loopback,1,1"
    rate 48000
    period_size 2048
    buffer_size 32768
    period_time 0
    buffer_time 0
  }
}

pcm.cloop {
  type dsnoop
  ipc_key 219348
  slave {
    pcm "hw:Loopback,1,0"
    rate 48000
    period_size 2048
    buffer_size 32768
    period_time 0
    buffer_time 0
  }
}

# ------------------------------------------------------
# default device

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

Then in one session, run:

alsa_in -j cloop -dcloop

This should create the ALSA Jack bridge client, you should see it in patchage, you should connect it to system playback ports.

Then if you run for example:

mplayer -ao alsa some.mp3

it should play through the loopback device, then go through the ‘cloop’ Jack client, and then to system playback output, if connection was made.

The Pulseaudio backend, used for browsers, seems to interfere a bit with the loopback devices, as it starts forwarding the loopback devices to the system output somehow. You can disable Pulseaudio by creating ~/.config/pulse/client.conf:

autospawn=no

and run pulseaudio -k to stop the Pulse audio backend.

Anyway, even though ALSA gets forwarded to the system playback via Jack, there’s still work to be done probably with .asoundrc, they playback is stuttering and seems to be playing at the wrong rate, so it must have something to do with the buffer sizes and sampling rate.

0 Likes

#4

Hey Giedrius,

Thanks for your help!
I tried a lot again …half of the day… without any success - this is sooo time consuming.

$ alsa_in -j cloop -dcloop
WARNING: buffer size does not match: (requested 2048, got 4096)
WARNING: period size does not match: (requested 1024, got 2048)
selected sample format: 16bit
delay = 3349

I have no clue if that is a bad warning… When I play with mplayer or aplay I get this:

[AO_ALSA] alsa-lib: pcm_dmix.c:1130:(snd_pcm_dmix_open) unable to initialize sum ring buffer
[AO_ALSA] Playback open error: Invalid argument

For the .asoundrc I had to switch the subdevics (made pisound hw:0)

.asoundrc
pcm.amix {
  type dmix
  ipc_key 219345
  slave {
    pcm "hw:Loopback,1,0"
    rate 48000
    period_size 4048
    buffer_size 32768
    period_time 0
    buffer_time 0
  }
}

# capture PCM device: using loopback subdevice 1,1
pcm.asnoop {
  type dsnoop
  ipc_key 219346
  slave {
    pcm "hw:Loopback,1,1"
    rate 48000
    period_size 4048
    buffer_size 32768
    period_time 0
    buffer_time 0
  }
}

# duplex device combining our PCM devices defined above
pcm.aduplex {
  type asym
  playback.pcm "amix"
  capture.pcm "asnoop"
}

# ------------------------------------------------------
# for jack alsa_in and alsa_out: looped-back signal at other ends
pcm.ploop {
  type plug
  slave {
    pcm "hw:Loopback,0,1"
    rate 48000
    period_size 4048
    buffer_size 32768
    period_time 0
    buffer_time 0
  }
}

pcm.cloop {
  type dsnoop
  ipc_key 219348
  slave {
    pcm "hw:Loopback,0,0"
    rate 48000
    period_size 2048
    buffer_size 32768
    period_time 0
    buffer_time 0
  }
}

# ------------------------------------------------------
# default device

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

This is the output of aplay -l

**** List of PLAYBACK Hardware Devices ****
card 0: pisound [pisound], device 0: PS-3ZAP7QN snd-soc-dummy-dai-0 []
  Subdevices: 0/1
  Subdevice #0: subdevice #0
card 1: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
  Subdevices: 2/2
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
card 1: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]
  Subdevices: 2/2
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1

The only success I had (before trying this here) was that I eventually got an output from shairport-sync (Airplay) and Raspotify …but only when jack wasn’t occupying pisound. This linux audio is such a nightmare for me.

0 Likes

#5

Okay, I guess I have to ditch this idea. And unfortunately also patchbox. I am not even getting the sample rate conversion to work properly (nothing is in sync when watching a video with audio being sent to airplay from my Apple TV - I suppose this comes from 44,1kHz being played back at 48kHz) with this simple asoundrc:

pcm_slave.sl2 {
        pcm "hw:0,0"
        rate 48000
}

pcm.!default {
        type rate
        slave sl2
} 
0 Likes