Building & installing the ALSA loopback device under Ubuntu

The ALSA Loopback sound card is a virtual soundcard that is created once the ALSA kernel module snd-aloop is loaded. This virtual soundcard device, as its name indicates, sends back the output signal of applications using it back to itself, so one has a chance to e.g. record this signal from the same device. Simply imagine that you have a physical link between one OUT and one IN of the same device. By default, the Loopback sound card consists of 2 devices, each composed of 8 subdevices. 

I required the ALSA Loopback device to re-route sound from one application to another when streaming Airplay to a Logitech Squeezebox see here. Read on to see how I installed the ALSA loopback on Ubuntu 10.04...


Once the kernel module snd-aloop is loaded, you can verify that the sound card has been created by using aplay -l

Which once the loopback device installed will return something akin to:


**** List of PLAYBACK Hardware Devices ****
card 0: NVidia [HDA NVidia], device 0: ALC887 Analog [ALC887 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: NVidia [HDA NVidia], device 1: ALC887 Digital [ALC887 Digital]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: 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 2: 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

Install the ALSA loopback device from source code

1. Check for you version of ALSA as the source code needs to match this version number
sudo  cat /proc/asound/version

2. Check if you already have the loopback device, if you get and error or no output it's safe to say you haven't.
sudo modinfo snd-aloop

3. Backup your current ALSA module to somewhere safe (home directory?)
cd ~
mkdir alsa-backup
cd alsa-backup
cp -a /lib/modules/`uname -r`/kernel/sound  .

4. As a prerequisite you need a compiler and other tools. In debian based distros, you can install a package called build-essential to provide a compiler
sudo apt-get install build-essential

5. Download the ALSA source code for your current version of ALSA, where XX is the version number
cd /usr/src/modules
sudo wget ftp://ftp.alsa-project.org/pub/driver/alsa-driver-1.0.XX.tar.bz2

6. Extract ALSA
sudo tar xjvf alsa-driver-1.0.XX.tar.bz2
cd alsa-driver-1.0.XX

7. Check which hardware drivers you already have installed
sudo cat /proc/asound/modules

For me this returned:

0 snd_hda_intel

8. Configure the new ALSA modules to be built with support with the hardware detected above

sudo ./configure --with-cards=hda_intel,loopback,hrtimer --with-sequencer=yes
sudo make 
sudo make install

9. Unload the old ALSA drivers / modules
sudo alsa force-unload

10. Detect your new ALSA loopback device
sudo modprobe snd-aloop

11. Check the loopback device has installed ok
lsmod | grep aloop

My successful install returned

snd_aloop               5913  4 
snd_pcm                70694  5 snd_aloop,snd_hda_intel,snd_hda_codec
snd                    54244  17 snd_aloop,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer

Alternatively use aplay -l

12. Create the asound config file this verbatim from the ALSA JACK guide

vi ~/.asoundrc


The goal is to create a default ALSA plug device out of the Loopback card. For a complete software solution, we need one PCM playback, so ALSA apps can send audio to it, one PCM capture, so ALSA apps can get audio from it, and combine these 2 PCMs into a nice full duplex "plug" device.



# playback PCM device: using loopback subdevice 0,0
pcm.amix {
  type dmix
  ipc_key 219345
  slave.pcm "hw:Loopback,0,0"
}

# capture PCM device: using loopback subdevice 0,1
pcm.asnoop {
  type dsnoop
  ipc_key 219346
  slave.pcm "hw:Loopback,0,1"
}

# 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"
}

pcm.cloop {
  type dsnoop
  ipc_key 219348
  slave.pcm "hw:Loopback,1,0"
}

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

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


13. Load the alsa config

sudo killall pulseaudio
sudo alsa force-reload

14. In case anything went wrong and you wish to go back to your previous ALSA installation, no problem:

sudo rm /lib/modules/`uname -r`/kernel/sound

sudo cp -a ~/alsa-backup/sound /lib/modules/`uname -r`/kernel/
sudo alsa force-reload

Great source material that guided me through this experience:

1 comment:

  1. hi , I want redirect this raw data to file,
    so what should i do next...

    ReplyDelete