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";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.