1

I'm trying to setup a loopback device so that I can record the audio as it is being outputted through the audio jack. I've got this working on my desktop PC, running Ubuntu 14.04, with the following .asoundrc file, cobbled together from different sources:

pcm.!default {
  type asym
  playback.pcm "CardAndLoop"
  capture.pcm "hw:Loopback,1"
}

pcm.CardAndLoop {
  type plug
  slave.pcm MultiCh
  route_policy "duplicate"
}

pcm.MultiCh {
  type multi
  slaves.a.pcm pcm.MixCard
  #slaves.a.pcm "hw:ALSA,0"
  slaves.a.channels 2
  slaves.b.pcm pcm.MixLoopback
  #slaves.b.pcm "hw:Loopback,0"
  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
}

pcm.MixCard {
  type dmix
  ipc_key 1024
  slave {
    pcm "hw:PCH,0"
    rate 44100
    periods 128
    period_time 0
    period_size 1024 # must be power of 2
    buffer_size 8192
  }
}

pcm.MixLoopback {
  type dmix
  ipc_key 1025
  slave {
    pcm "hw:Loopback,0"
    rate 44100
    periods 128
    period_time 0
    period_size 1024 # must be power of 2
    buffer_size 8192
  }
}

Now, using this config file on my raspberry pi 3 running the latest Raspbian Jessie does not work (I made sure to change the audio card from PCH to ALSA). I've tracked the issue down to the dmix plug in. Anytime there is a dmix in the pipeline, playing audio does not work; it "hangs".

For instance, running

aplay -vvv /usr/share/sounds/alsa/Front_Center.wav`

hangs and shows the following log:

Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
Plug PCM: Route conversion PCM (sformat=S16_LE)
  Transformation table:
    0 - 0
    1 - 0
    2 - 0
    3 - 0
Its setup is:
  stream       : PLAYBACK
  access       : RW_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 1
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 16128
  period_size  : 4032
  period_time  : 84000
  tstamp_mode  : NONE
  period_step  : 1
  avail_min    : 4032
  period_event : 0
  start_threshold  : 16128
  stop_threshold   : 16128
  silence_threshold: 0
  silence_size : 0
  boundary     : 2113929216
Slave: Multi PCM
  Channel bindings:
    0: slave 0, channel 0
    1: slave 0, channel 1
    2: slave 1, channel 0
    3: slave 1, channel 1
Its setup is:
  stream       : PLAYBACK
  access       : MMAP_NONINTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 4
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 16128
  period_size  : 4032
  period_time  : 84000
  tstamp_mode  : NONE
  period_step  : 1
  avail_min    : 4032
  period_event : 0
  start_threshold  : 16128
  stop_threshold   : 16128
  silence_threshold: 0
  silence_size : 0
  boundary     : 2113929216
Slave #0: Direct Stream Mixing PCM
Its setup is:
  stream       : PLAYBACK
  access       : MMAP_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 2
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 16128
  period_size  : 4032
  period_time  : 84000
  tstamp_mode  : NONE
  period_step  : 1
  avail_min    : 4032
  period_event : 0
  start_threshold  : 16128
  stop_threshold   : 16128
  silence_threshold: 0
  silence_size : 0
  boundary     : 2113929216
Hardware PCM card 3 'bcm2835 ALSA' device 0 subdevice 0
Its setup is:
  stream       : PLAYBACK
  access       : MMAP_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 2
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 16384
  period_size  : 4032
  period_time  : 84000
  tstamp_mode  : ENABLE
  period_step  : 1
  avail_min    : 4032
  period_event : 0
  start_threshold  : 1
  stop_threshold   : 1073741824
  silence_threshold: 0
  silence_size : 1073741824
  boundary     : 1073741824
  appl_ptr     : 0
  hw_ptr       : 0
Slave #1: Direct Stream Mixing PCM
Its setup is:
  stream       : PLAYBACK
  access       : MMAP_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 2
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 16128
  period_size  : 4032
  period_time  : 84000
  tstamp_mode  : NONE
  period_step  : 1
  avail_min    : 4032
  period_event : 0
  start_threshold  : 16128
  stop_threshold   : 16128
  silence_threshold: 0
  silence_size : 0
  boundary     : 2113929216
Hardware PCM card 0 'Loopback' device 0 subdevice 0
Its setup is:
  stream       : PLAYBACK
  access       : MMAP_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 2
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 24192
  period_size  : 4032
  period_time  : 84000
  tstamp_mode  : ENABLE
  period_step  : 1
  avail_min    : 4032
  period_event : 0
  start_threshold  : 1
  stop_threshold   : 1585446912
  silence_threshold: 0
  silence_size : 1585446912
  boundary     : 1585446912
  appl_ptr     : 0
  hw_ptr       : 0
Max peak (4032 samples): 0x0000020a #                    1%
Max peak (4032 samples): 0x00003b8d ##########           46%
Max peak (4032 samples): 0x00001d9b #####                23%
Max peak (4032 samples): 0x00001a94 #####                20%

After this it hangs and I have to kill it.

I think my issue is the same as reported in this post: https://www.raspberrypi.org/forums/viewtopic.php?t=64936&p=481557

Does anyone have experience with dmix/alsa and can point me in the right direction to solve this?

Thanks!

  • The same issue exists on arch. With lucab solution, you won't be able to play several sounds at same time. I got same problem, first I thought it was an alsa issue, but now I think it is a raspberry issue, here are posts about it: 1, 2, and 3. So far, no "alsa" solution has been found (as a workaround pulse could be used). – rem Feb 14 '17 at 14:31
1

I was not able to get dmix to work on my raspberry pi. Instead, I avoided it. Here's my alsa config file:

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

ctl.!default {
    type hw
    card ALSA
} 

pcm.output {
    type hw
    card ALSA
}

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

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

To record the output sound, I use the loopout device.

Hope this helps...

1

Another method to make this happen is to use pavucontrol. The general approach is as follows :

  • If pulseaudio isn't already running, install pulse.
  • Install / start pavucontrol
  • Get audio playing as you normally would.
  • Get audio recording, as you normally would.
  • In pavucontrol, go to the recording tab and when showing "Applications" Select the audio device to record from against the application which is recording. The volume bar will indicate audio activity. You should now be recording loopback audio.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.