snd-aloop, jack, etc

Post fully complete "how to" guides and tutorials here. This is a great place to get feedback on stuff you might put in the wiki.

Moderators: MattKingUSA, khz

User avatar
funkmuscle
Established Member
Posts: 2986
Joined: Mon Jun 02, 2008 2:30 pm
Has thanked: 197 times
Been thanked: 50 times

Re: snd-aloop, jack, etc

Post by funkmuscle »

thorgal wrote:what freezes your system ?

you have to launch lmms first against the ALSA loopback device (while jack is OFF) and play something. If you see the transport shuttle move, then you're in business. You will then launch jack against your real h/w device and alsa_in against the second loopback device and connect its outputs to your jack system playback ports. You should hear something without a glitch if you jack setup is usually flawless (regardless of lmms).

I saw in the track options that you could record audio (but could not really try it as I was using the loopback deivce only for output)
LMMS won't even start now, with or without jack... funny, it worked before I did the alsa loop thing.
It keeps starting with the dummy driver. :(
thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

strange ... just trash the whole loop thing (get back to your previous conf). I'll give you an update when I get some stuff sorted.
User avatar
funkmuscle
Established Member
Posts: 2986
Joined: Mon Jun 02, 2008 2:30 pm
Has thanked: 197 times
Been thanked: 50 times

Re: snd-aloop, jack, etc

Post by funkmuscle »

cool, I did and I'm back to normal... will wait for the update
thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

OK, I got it sorted out (I think). Been reading a bit about asoundrc, etc, and here is the magic combo for .asoundrc:

Code: Select all

pcm.loopmix {
    type dmix
    ipc_key 219345           # any unique number here
    slave.pcm "hw:Loopback,0,0"
}

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

ctl.!default {
  type hw
  card Loopback
}
The main diff (except for buffer size and such (which made lmms lag behind when recording in ardour, fixed now with the default dmix values) is the use of hw:Loopback,0,0 (using only subdevice 0,0). This fixes the one app only issue. I can now play many ALSA apps together.

The alsa_in command is still the same:

Code: Select all

alsa_in -j some_name -dhw:Loopback,1
connect some_name jack ports to the system output ports or an ardour track ports, and record whatever sound is output to the loopback device from an ALSA app :)
But remember: start the ALSA app BEFORE you launch alsa_in.

I tried a demo of some LMMS projects and here is the result recorded in ardour (nothing fancy, just to demonstrate the glitch-free experience):
http://www.mediafire.com/?qnmnynzztry
thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

ha! :D the loopback thingy fixes the skype issue with jack in a rather neat way!

All I had to do was add another part in asoundrc, namely a dsnoop device for capture stream (it helps to RTFM :D )
This is possible because of how the ALSA loopback card is subdivided into 8 substreams for each device (2 devices)

so here is the asoundrc file in all its glory:

Code: Select all

pcm.loopmix {
    type dmix
    ipc_key 219345           # any unique number here
    slave.pcm "hw:Loopback,0,0"
}

pcm.loopsnoop {
    type dsnoop
    ipc_key 219346           # any unique number here
    slave.pcm "hw:Loopback,1,1"
}

pcm.combo {
    type asym
    playback.pcm "loopmix"
    capture.pcm "loopsnoop"
}

# this makes native ALSA apps default to using dmix
pcm.!default {
  type plug
  slave.pcm "combo"
}

ctl.!default {
  type hw
  card Loopback
}
I combined a full duplex device that I called "combo" thanks to the ALSA asym plugin and made it a plug type default. The loopback device works this way:

each substream in device 0, corresponds to a substream in device 1. So if you output to hw:Loopback,0,x you can collect the signal in hw:Loopback,1,x.

By calling alsa_in on hw:Loopback,1,0, you will be able to able to hear any signal that ALSA apps output to hw:Loopback,0,0. And vice-versa, if you call alsa_out on hw:Loopback,0,1, you will be able to capture the stuff sent to hw:Loopback,1,1.

The asoundrc I made only combines hw:Loopback,0,0 and hw:Loopback,1,1 into a full duplex card.

So, fire up skype, and go to the sound options, select everything to default (since we tricked the default ALSA soundcard with our asoundrc to use the correct loopback devices).

Now you call alsa_in and alsa_out on their opposite substreams:

Code: Select all

alsa_in -j cloop -dhw:Loopback,1,0

Code: Select all

alsa_out -j ploop -dhw:Loopback,0,1
Now, connect some jack system capture ports to the "ploop" client (I tried my guitar).
Connect the "cloop" client to your jack system output ports.

Try now the "test phone call" thing. When it asks you to talk, play your guitar. It will play it back once the test call hangs up and you will hear it.

The beauty of it is that cloop and ploop are permanent jack clients :D

OK, maybe your head is spinning by reading this but you've got to try it and find out for yourself :)
thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

OK, I tricked that stuff to initialize nicely so I don't have to do anything by hand.

In my KDE Autostart folder, I added a little script that will call mplayer -ao alsa on the default sound card I created in asoundrc. It will play a silent mp3 file for one 1mn in the processes background just after KDE startup. I then launch qjackctl which will call another home made script at jack server startup, script that I called loop2jack:

Code: Select all

 #!/bin/sh

/usr/bin/alsa_out -j ploop -dhw:Loopback,0,1 -q 1 1>/dev/null 2>&1 &
/usr/bin/alsa_in -j  cloop -dhw:Loopback,1,0 -q 1 1>/dev/null 2>&1 &

sleep 1

jack_connect cloop:capture_1 system:playback_1 1>/dev/null 2>&1
jack_connect cloop:capture_2 system:playback_2 1>/dev/null 2>&1

jack_connect system:capture_3 ploop:playback_1 1>/dev/null 2>&1
jack_connect system:capture_3 ploop:playback_2 1>/dev/null 2>&1

exit 0
This is done while mplayer is already playing the silent file so alsa_in and alsa_out will not block the loopback device because they are started after mplayer has started on the loopback device.

I added automatic connections between the cloop and ploop jack clients to relevant jack system ports, e.g. I have a voice microphone at capture_3 so I can use skype with it by default thank to the connection to the ploop client.

This is working VERY nicely. I can make a skype call and output my bass or an entire ardour session directly to the call while I am talking at the mic :D
This loop stuff is truly great once it is working!

EDIT: actually, I moved the mplayer command to the "on server startup" option in qjackctl so that I can close jack and start it up again without depending on KDE startup. Hehe, now I am in business :)
User avatar
funkmuscle
Established Member
Posts: 2986
Joined: Mon Jun 02, 2008 2:30 pm
Has thanked: 197 times
Been thanked: 50 times

Re: snd-aloop, jack, etc

Post by funkmuscle »

absolutely confused now(confused very easliy :( ) :?
thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

it is confusing of course, because the loopback device makes it so: what goes out comes in ;)
thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

the whole "construction" I tried to describe as clearly as I could is based on the following thing:

the kernel module snd-aloop creates a virtual sound card that consists of 2 devices:
hw:Loopback,0
hw:Loopback,1

("Loopback" being the default name this ALSA kernel module will assign to it)

Once the module is loaded, if you type "aplay -l", you will get

Code: Select all

~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: DSP [Hammerfall DSP], device 0: RME Hammerfall DSP + Multiface [RME Hammerfall DSP + Multiface]
  Subdevices: 0/1
  Subdevice #0: subdevice #0
card 1: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
  Subdevices: 7/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 1: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
(ignore the RME card entry).

The loopback card works like this:

if an APP outputs its audio to one of the subdevices, say hw:Loopback,0,0, the audio will be available as an input in the corresponding subdevice hw:Loopback,1,0 because the whole point for this device is to send the signal back to itself.

Now that you understand that, here is my trick:

I built up an ALSA plug device by combining one capture and one playback subdevices, namely
hw:Loopback,0,0 and hw:Loopback,1,1.
I chose the playback device to be of type dmix (ALSA plug device that allow mixing of multiple streams) and I chose the capture device to be of type dsnoop (the reverse of dmix).

My asoundrc is only that: it creates a new plug device called "combo" (you can choose another name) by combining a dmix based device and a dsnoop based device which talk to the loopback subdevices.

I made this "combo" plug device the default ALSA soundcard :)

The jack trick now is to link the loopback device audio streams to the jack graph. This is where alsa_in and alsa_out are very handy tools.

Remember : hw:Loopback,0,0 is my ALSA playback device. So any audio output to this deivce will end up being looped back to hw:Loopback,1,0. So I call alsa_in on the latter :)

Same for the capture device: being hw:Loopback,1,1, the signal will be looped back to hw:Loopback,0,1. So I call alsa_out on the latter.

alsa_in and alsa_out only collect the looped back signals from the ALSA loopback devices I chose and redirect these signals to the jack graph. I called the alsa_in client "cloop" as a capture device, and the alsa_out client "ploop" which ends up being a playback device in the jack graph.

Now, the signal is like this:

when an ALSA app outputs stuff to the default ALSA device I have created ("combo"), you can collect it in the jack graph via the "cloop" client and connect the cloop ports to e.g. your system jack ports. Thisis how I get lmms working without a glitch :)

For skype, it is the opposite: since I connected one of my mics (system:capture port) to the "ploop" client, the audio will be received on the mirroring loopback device which will serve as an input to an ALSA app like skype :)

Is it more understandable ? The cool thing about this is that these loop jack clients are permanent and can get connected to whatever you want! And if jack crashes, it has absolutely no influence on the ALSA loopback device I created in my asoundrc because jack in my case runs on the RME card.

All I have to do (with the helper scripts I described above) is to restart jack via qjackctl and the clients and connections to the loopback device are restored :D

========

The ONLY remaining caveat is this:
if I start alsa_in or alsa_out BEFORE an ALSA app is using the loopback device, the ALSA apps won't be able to use it. Looks like the jack tools are exclusive. If I have an ALSA app already playing on the loopback device (prior to alsa_in and alsa_out), then launching alsa_in/out will work. This is why I needed a hack when I start jack. I needed an ALSA app working shortly in the background while I let qjackctl launch alsa_in and alsa_out. I chose mplayer, playing a silent mp3 file for 1mn, which is plenty of time for jack and the post startup scripts to be executed.
thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

I wrote a WIKI page in alsa.opensrc.org

http://alsa.opensrc.org/index.php/Jack_ ... ack_bridge

we can add or link it in the LM WIKI :)
morko
Established Member
Posts: 45
Joined: Thu Mar 25, 2010 8:08 am
Location: Finland

Re: snd-aloop, jack, etc

Post by morko »

This is great. Thank you for sharing. I'm allready smiling for what I could do in skype with this. Too bad I dont really have anyone to call to. Hehehe. Now I just have to take some time to set it up :p
User avatar
autostatic
Established Member
Posts: 1994
Joined: Wed Dec 09, 2009 5:26 pm
Location: Beverwijk, The Netherlands
Has thanked: 32 times
Been thanked: 104 times
Contact:

Re: snd-aloop, jack, etc

Post by autostatic »

Thanks Thorgal! But aaarrrgggghh, I hate compiling separate kernel modules.

Edit:

Code: Select all

[  529.160038] snd_aloop: disagrees about version of symbol snd_pcm_new
[  529.160041] snd_aloop: Unknown symbol snd_pcm_new
[  529.160261] snd_aloop: disagrees about version of symbol snd_pcm_lib_preallocate_pages_for_all
[  529.160264] snd_aloop: Unknown symbol snd_pcm_lib_preallocate_pages_for_all
[  529.160350] snd_aloop: disagrees about version of symbol snd_pcm_lib_malloc_pages
[  529.160352] snd_aloop: Unknown symbol snd_pcm_lib_malloc_pages
[  529.160417] snd_aloop: disagrees about version of symbol snd_pcm_lib_ioctl
[  529.160419] snd_aloop: Unknown symbol snd_pcm_lib_ioctl
[  529.160594] snd_aloop: disagrees about version of symbol snd_pcm_lib_free_pages
[  529.160597] snd_aloop: Unknown symbol snd_pcm_lib_free_pages
[  529.160665] snd_aloop: disagrees about version of symbol snd_pcm_set_ops
[  529.160667] snd_aloop: Unknown symbol snd_pcm_set_ops
[  529.160742] snd_aloop: disagrees about version of symbol snd_pcm_suspend_all
[  529.160744] snd_aloop: Unknown symbol snd_pcm_suspend_all
[  529.160809] snd_aloop: disagrees about version of symbol snd_pcm_hw_constraint_integer
[  529.160812] snd_aloop: Unknown symbol snd_pcm_hw_constraint_integer
[  529.161038] snd_aloop: disagrees about version of symbol snd_pcm_period_elapsed
[  529.161040] snd_aloop: Unknown symbol snd_pcm_period_elapsed
[ 1205.873174] snd_aloop: disagrees about version of symbol snd_pcm_new
[ 1205.873178] snd_aloop: Unknown symbol snd_pcm_new
[ 1205.873380] snd_aloop: disagrees about version of symbol snd_pcm_lib_preallocate_pages_for_all
[ 1205.873383] snd_aloop: Unknown symbol snd_pcm_lib_preallocate_pages_for_all
[ 1205.873468] snd_aloop: disagrees about version of symbol snd_pcm_lib_malloc_pages
[ 1205.873470] snd_aloop: Unknown symbol snd_pcm_lib_malloc_pages
[ 1205.873534] snd_aloop: disagrees about version of symbol snd_pcm_lib_ioctl
[ 1205.873536] snd_aloop: Unknown symbol snd_pcm_lib_ioctl
[ 1205.873713] snd_aloop: disagrees about version of symbol snd_pcm_lib_free_pages
[ 1205.873715] snd_aloop: Unknown symbol snd_pcm_lib_free_pages
[ 1205.873784] snd_aloop: disagrees about version of symbol snd_pcm_set_ops
[ 1205.873786] snd_aloop: Unknown symbol snd_pcm_set_ops
[ 1205.873863] snd_aloop: disagrees about version of symbol snd_pcm_suspend_all
[ 1205.873864] snd_aloop: Unknown symbol snd_pcm_suspend_all
[ 1205.873931] snd_aloop: disagrees about version of symbol snd_pcm_hw_constraint_integer
[ 1205.873933] snd_aloop: Unknown symbol snd_pcm_hw_constraint_integer
[ 1205.874173] snd_aloop: disagrees about version of symbol snd_pcm_period_elapsed
So unfortunately, compiling a simple kernel module doesn' t work for Ubuntu 9.10 kernel 2.6.31 RT :(
Last edited by autostatic on Mon May 03, 2010 8:37 pm, edited 1 time in total.
Pablo
Established Member
Posts: 1274
Joined: Thu Apr 17, 2008 9:57 pm
Been thanked: 3 times

Re: snd-aloop, jack, etc

Post by Pablo »

Thanks thorgal!

I have an error when trying to modprobe the module. First, what i have done:

$dpkg -l | grep alsa
ii alsa-base 1.0.22.1+dfsg-0ubuntu3
etc...
$wget ftp://ftp.alsa-project.org/pub/driver/a ... .1.tar.bz2
tar and cd
$./configure --with-cards=loopback
$make
$sudo cp ./modules/snd-aloop.ko /lib/modules/2.6.31-10-rt/kernel/sound/drivers/
$sudo depmod -a
$sudo modinfo snd-aloop
filename: /lib/modules/2.6.31-10-rt/kernel/sound/drivers/snd-aloop.ko
license: GPL
description: A loopback soundcard
author: Jaroslav Kysela <perex@perex.cz>
srcversion: B85A5847D027749DCF96195
depends: snd-pcm,snd
vermagic: 2.6.31-10-rt SMP preempt mod_unload modversions 586
parm: index:Index value for loopback soundcard. (array of int)
parm: id:ID string for loopback soundcard. (array of charp)
parm: enable:Enable this loopback soundcard. (array of bool)
parm: pcm_substreams:PCM substreams # (1-8) for loopback driver. (array of int)

so far so good!

aplay -l does not list the new "card" yet.

I think you have missed a step in your excellent alsa wiki page:

$sudo modprobe snd-aloop

But in my case I have this error!

FATAL: Error inserting snd_aloop (/lib/modules/2.6.31-10-rt/kernel/sound/drivers/snd-aloop.ko): Unknown symbol in module, or unknown parameter (see dmesg)
pablo@desktop:/lib/modules/2.6.31-10-rt/kernel/sound/drivers$ sudo modprobe snd-aloop
FATAL: Error inserting snd_aloop (/lib/modules/2.6.31-10-rt/kernel/sound/drivers/snd-aloop.ko): Unknown symbol in module, or unknown parameter (see dmesg)

I see this in dmesg:
$dmesg
[25662.383910] snd_aloop: disagrees about version of symbol snd_pcm_new
[25662.383917] snd_aloop: Unknown symbol snd_pcm_new
[25662.384307] snd_aloop: disagrees about version of symbol snd_pcm_lib_preallocate_pages_for_all
[25662.384311] snd_aloop: Unknown symbol snd_pcm_lib_preallocate_pages_for_all
[25662.384450] snd_aloop: disagrees about version of symbol snd_pcm_lib_malloc_pages
[25662.384453] snd_aloop: Unknown symbol snd_pcm_lib_malloc_pages
[25662.384556] snd_aloop: disagrees about version of symbol snd_pcm_lib_ioctl
[25662.384559] snd_aloop: Unknown symbol snd_pcm_lib_ioctl
[25662.384824] snd_aloop: disagrees about version of symbol snd_pcm_lib_free_pages
[25662.384827] snd_aloop: Unknown symbol snd_pcm_lib_free_pages
[25662.384935] snd_aloop: disagrees about version of symbol snd_pcm_set_ops
[25662.384937] snd_aloop: Unknown symbol snd_pcm_set_ops
[25662.385058] snd_aloop: disagrees about version of symbol snd_pcm_suspend_all
[25662.385061] snd_aloop: Unknown symbol snd_pcm_suspend_all
[25662.385290] snd_aloop: disagrees about version of symbol snd_pcm_hw_constraint_integer
[25662.385294] snd_aloop: Unknown symbol snd_pcm_hw_constraint_integer
[25662.385702] snd_aloop: disagrees about version of symbol snd_pcm_period_elapsed
[25662.385706] snd_aloop: Unknown symbol snd_pcm_period_elapsed

I thought about a version mismatch, as you say,
Now grab the alsa-driver source code (same version as your installed ALSA)...
I am not sure about it. dpkg -l gives 1.0.22.1 but:

$cat /proc/asound/version
Advanced Linux Sound Architecture Driver Version 1.0.20.

So, i download the alsa-drivers 1.0.20 and repeat the process... the same error.

I am running ubuntu lucid with the linux-rt kernel,

$ uname -r
2.6.31-10-rt

I might try to copy the new snd.ko and snd-pcm.ko to /lib/modules/2.6.31-10-rt/kernel/sound/core/
I am not sure.

$ /lib/modules/2.6.31-10-rt/kernel/sound/drivers$ ls
mpu401 opl4 snd-aloop.ko snd-mtpav.ko snd-portman2x4.ko snd-virmidi.ko
opl3 pcsp snd-dummy.ko snd-mts64.ko snd-serial-u16550.ko vx

$ /lib/modules/2.6.31-10-rt/kernel/sound/core$ ls
oss snd-hrtimer.ko snd.ko snd-pcm.ko snd-timer.ko
seq snd-hwdep.ko snd-page-alloc.ko snd-rawmidi.ko

Sorry for the long post.
Cheers! Pablo
User avatar
autostatic
Established Member
Posts: 1994
Joined: Wed Dec 09, 2009 5:26 pm
Location: Beverwijk, The Netherlands
Has thanked: 32 times
Been thanked: 104 times
Contact:

Re: snd-aloop, jack, etc

Post by autostatic »

Ah, so Lucid has the same problem.
thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

ouch! you got distro kernel packaging issues ... hurts a bit ...

My kernel is vanilla 2.6.32 that I compiled myself so it was easier (I am no longer using RT patches, they perform worse than vanilla for me!!!)

Pablo, thanks for mentioning the modprobe command that I missed, I corrected the HOWTO.
I also corrected a typo involving stderr and stdout redirection (an extra '&' that should not have been there). I wrote this from my work office, from a ... windows PC ... so I could not verify all commands (but I copied parts of what I had written here already).

I hope you fix your issues with kernel module version , ALSA version, etc. This issue can be a bit of a bitch ...

oh yes, one app that I do use with ALSA is mythtv!!! the jack option really sucks in mythtv (but I am still using mythtv@0.21 ...). Mythtv works a treat with jack and the loopback bridge :) Just need a volume control (the loopback soundcard does not have one). I think I will try to fiddle around with the "softvol" asoundrc thingy ... will let you know.
Post Reply