#!/usr/bin/perl use strict; use warnings; use POSIX qw(log10); # input device my $input="/dev/dsp1"; # open filehandler open(my $fh, '<', $input) or die("ERROR open $input ($!)\n"); binmode($fh); # 8 bit # 8000 hertz my $cnt = 0; my $value = 0; my $lastts = 0; while(1) { if ($cnt == 8000) { # the amplitude >= 0 <= 1 my $amplitude=$value / $cnt / 255; # calculate dba = 20 * log10(amplitude / 2 * 10**-5) my $dba = 20 * log10($amplitude / 0.00002); print "Calculated dB(A): $dba\n"; $cnt = 0; $value = 0; } my $buffer; # read one byte read($fh, $buffer, 1); if(defined($buffer)) { # get an unsigned char my $v = unpack("C", $buffer); # add the read byte to the values $value+=$v; # print "Byte read: $v\n"; $cnt++; } } close($fh);