HOWTO: Record sound made by some application on Linux (ALSA)
Posted on November 09, 2008 at 07:41 PM
From time to time, I want to record the sound some application makes under Linux. Sadly, my sound card does not provide a “Stereo Mix” device which lets you record all the sound something makes, so I made myself this perl script which records ALSA output of an application and prints it to stdout as PCM.
Update: Added a second of sleep before making sound.
#!/usr/bin/perl
use strict;
my @cards = `cat /etc/asound.names |
grep -C 1 -i loop |
grep -C 1 -i PCM |
grep name`;
my $recordcard = @cards[ 0 ];
my ($recordcard) = ($recordcard =~ /'([^']*)'/);
my ($card, $device) = ($recordcard =~ /:([0-9]*),([0-9]*)$/);
if( @ARGV < 1 ) {
print STDERR
"USAGE: ./record_loopback.pl some_command_that_makes_sound\n";
exit;
}
my $has_aloop = 0;
my @modules = `lsmod`;
foreach( @modules ) {
if( $_ =~ /snd_aloop/ ) {
$has_aloop = 1;
last;
}
}
if( $has_aloop == 0 ) {
print STDERR
"FATAL: You need to have the module 'snd_aloop' loaded. ";
print STDERR
"Please do 'sudo modprobe snd_aloop' to load it.\n";
exit;
}
print STDERR "Assuming PCM loop driver is card $card, recording device 1.\n";
my $command = "env ALSA_PCM_CARD=$card env ALSA_PCM_DEVICE=1 " .
join( " ", @ARGV ) . " 1> /dev/null";
if( fork() ) {
sleep(1);
system( $command );
}
else {
system( "arecord -D hw:$card,1 -f cd" );
}
(Note that you won’t hear stuff while recording.)
This month
| July | ||||||
|---|---|---|---|---|---|---|
| Mo | Tu | We | Th | Fr | Sa | Su |
| 28 | 29 | 30 | 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | 1 |


— Full post RSS feed
— Comment RSS feed
— CC-BY-NC license
— Valid XHTML 1.1
— Debian operated
— Powered by Ruby
— Co-Powered by Perl
— Made with kate