You want to use ALSA for this, and load the snd_loopback module.
pi@raspberrypi ~ $ sudo modprobe snd-aloop pcm_substreams=1
This will create a loopback device on your system. Next you want to set the default ALSA audio output to this substream by editing /etc/asound.conf.
pi@raspberrypi ~ $ sudo nano /etc/asound.conf
Setup your configuration 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" } pcm.output { type hw card Headset } pcm.loopin { type plug slave.pcm "hw:Loopback,0,0" } pcm.loopout { type plug slave.pcm "hw:Loopback,1,0" }
You need to make sure the output pcm points to the speaker/output device you want the audio to go to. You can use aplay -L to get a list of available output devices.
You can know use avconv or ffmpeg to record audio from any application and also routing the audio to an output device.
Here is an example:
pi@raspberrypi ~ $ avconv -f alsa -ac 2 -ar 44100 -i loopout test.wav
SOURCE:
https://trac.ffmpeg.org/wiki/Capture/ALSA