Ok, got it. It's actually pretty simple. Tried this in a fresh new Ubuntu 16.04.1 VM on VirtualBox. The VM does not have sound hardware (disabled in VirtualBox).
I had some problems with the recorded file, there was just white noise. Turned out something went wrong when I tried to record the output using arecord and pipe that to lame to get a MP3. Switched to WAV instead.
Preparations:
sudo modprobe snd-aloop
sudo apt-get install alsa
Check devices:
tw@ubuntu-snddummy:~$ sudo aplay -l
**** List of PLAYBACK Hardware Devices ****
Home directory not accessible: Permission denied
card 0: 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 0: 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
tw@ubuntu-snddummy:~$
Play something
sudo aplay -D hw:0,0,0 sample.wav
Playing WAVE 'sample.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
Record (different shell)
sudo arecord -D hw:0,1,0 -f cd record.wav
PS: Did everything as root to avoid any permission issues.
Source:
http://www.alsa-project.org/main/index.php/Matrix:Module-aloop