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: khz, MattKingUSA

thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

snd-aloop, jack, etc

Post by thorgal »

Hi guys,

A little tip / trick that can be useful to some :)
For those who use jack all the time (like me) and do not wish to use pulseaudio or other forms of non jack layers but still would like to redirect non jack apps to jack, I found a trick by using the snd-aloop ALSA module. I was experimenting for something different when it came to me that this could definitely work out, and it did (there are a few caveats though) :

- get the snd-aloop module. It may not be readily available in your kernel install. To make sure, type:

Code: Select all

sudo modinfo snd-aloop
if it is not available, no panic, you can compile it provided that you have the kernel headers installed as well. Grab the alsa-driver source code (same version as your installed ALSA) from http://www.alsa-project.org, uncompress, untar it and cd to the alsa-driver top dir, now you type

Code: Select all

./configure --with-cards=loopback
make
don't make install, once compiled copy the module by hand

Code: Select all

sudo cp ./modules/snd-aloop.ko /lib/modules/`uname -r`/kernel/sound/drivers/
sudo depmod -a
now check again that you have it (cf. first command line above)
----------

OK, snd-aloop can now be loaded

Code: Select all

sudo modprobe snd-aloop
you should have a new sound card called "Loopback" (you can give another name in the modprobe option by adding id="new_name" but I will stick to the default one)
check in /proc/asound/cards that it is listed

The loopback "card" is a couple of device, each device divided in 8 subdevices
So you can refer to hw:Loopback or hw:Loopback,0 (device 0), or hw:Loopback,1,5 (device 1, subdevice 5).

So, once you have that, you can make an .asoundrc file. I used a minimalist dmix based asoundrc:

Code: Select all

pcm.dmix0 {
    type dmix
    ipc_key 219345           # any unique number here
    slave {
            pcm "hw:Loopback,0"
            period_time 0
            buffer_time 0
            period_size 2048    # jm: much smoother than 1024/8192!
            buffer_size 32768
            rate 96000
    }

    bindings {
        0 0   # from 0 => to 0
        1 1   # from 1 => to 1
    }
}

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

ctl.!default {
  type hw
  card Loopback
}
Note that you can change the sample rate if that makes more sense to you.
You see that I chose to make this dmix stuff the default ALSA soundcard. That is completely OK since I use jack specifically on hw:DSP (RME card) and this is completely independent.

Now the jack part. Fire up jack as usual and type this:

alsa_in -j Loop -d hw:Loopback,1

this will create a jack client called "Loop" and collects stuff coming to loopback device 1 (I used 0 for the dmix).

Now is the goodie: since it is a loopback device, the output is redirected to the input :) so all ALSA apps using the default device (dmix based on Loopback) will have their audio collected by the second Loopback device which alsa_in listens to. Now you can plug our Loop client to your usual jack system outputs and voila :)

You can fire up an non jack app like smasher (the beat crusher :) ) and have its audio in the jack graph.


===========


Caveats: some ALSA apps don't like to be started after alsa_in because they try to initialize the Loopback device ('mplayer -ao alsa' is an example, if you start mplayer like this prior to alsa_in, then it will work as soon as you start alsa_in a little after mplayer). An app like skype is even worse, it will sometimes output garbage, sometimes not.

But apps like smasher, mpg123 (not jackified at all), etc, are playing nicely along.

Anyway, not the most useful trick but in my jack environment, it can be useful :)

User avatar
funkmuscle
Established Member
Posts: 2501
Joined: Mon Jun 02, 2008 2:30 pm

Re: snd-aloop, jack, etc

Post by funkmuscle »

dude, try it with LMMS (lmms using the alsa driver) and see if you get xruns still. If it doesn't we have a winner.

I did something like that but with 2 cards. one card uses jack and the non jack apps then to use the other. so I tried LMMS that way and it worked but still got mad xruns.

thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

mmm, actually I misled myself :(
some of it does work, but not as useful as I thought ... there may be some way to make this snd-aloop helpful in a jack context but I fear I was too optimistic.

FYI, smasher was actually orking with oss2jack ... I had it on by default and I forgot to turn it off during experimentation.

User avatar
funkmuscle
Established Member
Posts: 2501
Joined: Mon Jun 02, 2008 2:30 pm

Re: snd-aloop, jack, etc

Post by funkmuscle »

is there a way for alsa2jack? that would work if it's available, wouldn't?

User avatar
Capoeira
Established Member
Posts: 1296
Joined: Tue May 12, 2009 1:01 pm
Location: Brazil

Re: snd-aloop, jack, etc

Post by Capoeira »

funkmuscle wrote:is there a way for alsa2jack? that would work if it's available, wouldn't?
I use alsa2jack long time now....working perfect.....
The plugin is missing in Karmic but there is a PPA

thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

lmms works with the snd-aloop method I described, no xrun at all :)
but alsa_in applied to one of the ALSA loopback devices is sometimes unstable at startup (segmentation violation). Not sure why but one it starts OK, the audio flows flawless :)

It actually works rally good. But I think the .asoundrc does not really do what I wanted (I am not good at this ALSA wizard language).
Last edited by thorgal on Fri Apr 30, 2010 8:55 pm, edited 1 time in total.

thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

the problem with the ALSA jack plugin is that it does not work as a permanent client, and does not do resampling, forcing me down to 48k.

This loopback trick allowed me for the first time to hear lmms without a glitch on my RME based system!

User avatar
autostatic
Established Member
Posts: 1728
Joined: Wed Dec 09, 2009 5:26 pm
Location: Beverwijk, The Netherlands
Contact:

Re: snd-aloop, jack, etc

Post by autostatic »

Oh yeah, the snd-aloop module! I just wrote a blog about jack2alsa and using Smasher (that uses gstreamer as output) with JACK but I totally forgot about snd-aloop. Gonna check it out, because LMMS with jack2alsa still doesn't run happily with JACK.
Blog's in Dutch so not really useful posting it here ;)

thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

that gstreamer stuff is actually quite good :)

User avatar
autostatic
Established Member
Posts: 1728
Joined: Wed Dec 09, 2009 5:26 pm
Location: Beverwijk, The Netherlands
Contact:

Re: snd-aloop, jack, etc

Post by autostatic »

The key is the gstreamer-properties app. But you probably figured that out already. You can configure GStreamer to output to JACK with gstreamer-properties. Then even the prelistening feature of Nautilus works with JACK ;)

thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

EDIT: my babbling below is wrong, I found out how to use the whole thing in a clean way, see last post in this thread.

I was surprised by the number of ports it created for smasher (4 ports).
But, correct me if I am wrong, it only creates the jack client (which was nicely called the app name, "smasher" in this case) when the app is trying to access the audio device.

The loopback method makes a permanent client from the start but there definitely are some issues with it:

- you have to start your ALSA app before alsa_in (turns that there is no exception)
- you can only use one ALSA app at a time (which I thought the dmix trick would remedy but nope)
- there isn't much usefulness except for outputting ALSA apps to the jack playback ports (meaning that doing the opposite: alsa_out on loopback1, and ALSA apps getting sound from loopback0 is not much of a use case).
- once alsa_in has a grip on loopbackX, you cannot use it for other purposes (I think).

But if one wants to use e.g. lmms in jack without glitch, and is not planning on recording audio inside lmms, then it's a nice way to have lmms in the jack graph at least for the output part. I haven't seen any of the dreaded lmms-xrun with the loopback method.
Last edited by thorgal on Sun May 02, 2010 7:04 pm, edited 1 time in total.

User avatar
funkmuscle
Established Member
Posts: 2501
Joined: Mon Jun 02, 2008 2:30 pm

Re: snd-aloop, jack, etc

Post by funkmuscle »

this freezes when I try lmms.. my system froze.
I first got xruns then I launched rtirq and no xruns but lmms launches half way then freezes my system..

this gst-streamer stuff, you guys are techie talking :D , can you guys explain a bit for me? :shock:

thorgal, record with lmms??? is that possible???

thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

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)

User avatar
autostatic
Established Member
Posts: 1728
Joined: Wed Dec 09, 2009 5:26 pm
Location: Beverwijk, The Netherlands
Contact:

Re: snd-aloop, jack, etc

Post by autostatic »

Sounds promising thorgal. Haven't touched LMMS for a while because it just won't play nicely with JACK. I'll compile and play around with the snd-aloop module tonight when I get back from LAC2010.

thorgal
Established Member
Posts: 739
Joined: Mon Apr 07, 2008 6:04 pm

Re: snd-aloop, jack, etc

Post by thorgal »

as I said before, it is a bit too optimistic, e.g. lmms sounds nice while playing and connected directly to the jack system output ports, but as soon as I fire up ardour and try to record the loop client's outputs, I get some glitches (not xruns though, just audible glitches). Same thing with just jack_capture. I suspect that as soon as a disk thread from another jack client is involved, glitches occur in the stream ...

but if I use another ALSA client, say "mplayer -ao alsa some_audio_file" and record the loop client's output to ardour, I have no glitches at all. This points to some internal inefficiencies in lmms as it does not play nice with other apps running concurrently.

Post Reply