LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > number22
User Name
Password

Notices


Rate this Entry

ffmpeg alsa recording multiple applications audio

Posted 01-28-2016 at 10:56 PM by number22
Updated 02-07-2016 at 03:30 AM by number22

The trick is using dmix on loopback device:
2 dmixers; one for regualr onboard audio; the other for loopback virtual device, then duplicates streams send to onboard audio card, so you can hear what is being sent to loopback device.

Need to load snd_aloop module first;

Code:
modprobe snd-aloop index=2 id=Loopback pcm_substreams=1 enable=1
My first audio device is onboard soundcard; 2nd is HDMI card, so I put index=2 for Loopback device.

file for .asoundrc

Code:
# .asoundrc
# periods INT # when buffer_size or buffer_time is not specified
#defaults.pcm.rate_converter "speexrate_medium"

pcm.snd_onboard { type hw card 0 device 0 }
pcm.snd_loopout { type hw card 2 device 0 channels 2 }
pcm.snd_loopin { type hw card 2 device 1 channels 2 }

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

pcm.dual {
      type asym 
      playback.pcm "looptoreal"
      capture.pcm "dsnooploop "
}

pcm.looptoreal {
    type plug 
    slave.pcm {
          type multi
          slaves {
                 a { channels 2 pcm "dmix_onboard" }
                 b { channels 2 pcm "dmixloop" }
          }
          bindings {
                 0 { slave a channel 0 } 
                 1 { slave a channel 1 }
                 2 { slave b channel 0 }
                 3 { slave b channel 1 }
         } 
    }
    route_policy "default"  #default; duplicate; and copy
    #Manual setting up ttable if you have problem of out sync audio with video in flash.
      # ttable.0.0  1
      # ttable.1.1  1
      # ttable.0.2  1
      # ttable.1.3  1
#    ttable [ [ 1 0 1 0 ] [ 0 1 0 1 ] ] #same as above


}

pcm.dmix_onboard {
     type dmix
     ipc_key 234567
     ipc_key_add_uid false
     ipc_perm 0660
     slave.pcm "snd_onboard"
     slave {
           rate 48000
           channels 2
           buffer_time 0
           period_size 4096 
           buffer_size 8192 
           format S32_LE
     }
     bindings {
          0   0
          1   1
     }
}

pcm.dmixloop {
     type dmix
     ipc_key 345678 
     ipc_key_add_uid false
     ipc_perm 0660
     slave.pcm "snd_loopout"
     slave {
           rate 48000
           channels 2
           buffer_time 0
           period_size 4096 
           buffer_size 8192 
           format S32_LE
     }
     bindings {
          0  0
          1  1
     }
}

pcm.dsnooploop {
      type dsnoop
      ipc_key 456789
      ipc_key_add_uid false
      ipc_perm 0660
      slave.pcm "snd_loopin"
      slave {
             rate 48000
             channels 2
             buffer_time 0
             period_size 4096
             buffer_size 8192 
             format S32_LE
       }
       bindings {
             0  0
             1  1
       }
}

pcm.duplexloop {
        type plug
        slave.pcm {
              type asym 
              playback.pcm "dmixloop"
              capture.pcm  "dsnooploop"
        }
}

pcm.card0 { type plug pcm "dual" }
clt.snd_onboard { type hw card 0 }
clt.!default { type hw card 2 }
ctl.multiloop { type hw card 2 }
ctl.mixloop { type hw card 2 }
clt.dmixer { type hw card 0 }
ctl.snd_loopout { type hw card 2 }
ctl.snd_loopin { type hw card 2 }
ctl.dsnooploop { type hw card 2 }
ffmpeg command to capture onboard sound card's microphone input and running desktop application's audio:
turn on capture in alsamixer, default is muted.

Code:
ffmpeg -f alsa -acodec pcm_s32le -ac 2 -ar 48000 -i hw:0,2 
       -f alsa -acodec pcm_s32le -ac 2 -ar 48000 -i duplexloop
       -filter_complex amix=inputs=2
       -f x11grab -r 25 -s 1366x768 -i :0.0+0,0
       -vcodec libx264 -preset veryfast -crf 18
       -acodec libmp3lame -ar 48000  -pix_fmt yuv420p
       -y test.mkv


note:

using buffer_time = 0 or period_time = 0 is optional for buggy card; using buffer/period_size or buffer/period_time; but not both. format S32_LE works great for my card.

Changes:
for my card, I have to use period_time and period_size to fine tune:

If you have problem with flash player hogs one cpu core;
1. test sample rate 48000 back to 44100.
or and
2. using export FLASH_ALSA_DEVICE=plug:dmix_onboard

Code:
period_time=50000 #125000usecs. default 
period_size=2048

note: html5 audios problem 
period_time = 15625 #125000 / 8
period_size = 1024
Thanks for hints from

https://unix.stackexchange.com/quest...ugins-together

https://bbs.archlinux.org/viewtopic.php?id=181922

https://youtu.be/mEV2O8C272g
Posted in Uncategorized
Views 3359 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration