Stoppt die 
Vorratsdatenspeicherung! Jetzt klicken & handeln!

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.)

re: 0 (view/add your own)  / about : , , ,