Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

sound-level from /dev/dsp

by zentara (Archbishop)
on Sep 26, 2005 at 15:41 UTC ( [id://495145]=CUFP: print w/replies, xml ) Need Help??

This node Tk-applause-meter made me think about getting a sound-level direct from /dev/dsp, instead of relying on on an external application. This snippet just shows the basic idea.
#!/usr/bin/perl use Audio::DSP; #alsamixer must be setup right, AC'97 capture adjusts sensitivity, #just turn it up to 100 for this to work as intended ($buf, $chan, $fmt, $rate) = (4096, 1, 16 , 8000); $dsp = new Audio::DSP(buffer => $buf, channels => $chan, format => $fmt, rate => $rate); $dsp->init() || die $dsp->errstr(); my $samples = 1500; my $num_tot = 0; my $div_tot = 0; # Record x samples of sound for (1..$samples) { #read 16 bits of raw data my $data = $dsp->dread(16) || die $dsp->errstr(); my $num = unpack( 'v', $data ); #filter out baseline noise if($num > 65000){next}else{ print "$num\n"; $num_tot += $num; $div_tot += 32768; #gives a good average } } $dsp->close(); print "\n\nAverage ",sprintf('%0.2f',$num_tot/$div_tot),"\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://495145]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found