in reply to Re: Measuring the sound level (dB(A)) with PERL
in thread Measuring the sound level (dB(A)) with PERL

actually, that part is correct. John-Robie is trying to calculate the amplitude every 8000 samples. Assuming it's an 8kHz sample rate on the mic, that would be related to the power for the last second of sound, which is quite reasonable thing to do.

  • Comment on Re^2: Measuring the sound level (dB(A)) with PERL

Replies are listed 'Best First'.
Re^3: Measuring the sound level (dB(A)) with PERL
by Monk::Thomas (Friar) on Nov 09, 2016 at 15:40 UTC

    If I point the open() to a file containing a single byte (I don't have a /dev/dsp1), then the loop will happily run 80,000 times in less than 0.01 seconds. That's why I'm suspicious if that loop is really doing what it's supposed to do.

      Yes it does. There are just 8000 samples per second and your system is capable doing more buffer loads per second. But this does not matter to the code - this part of the code works. It there are no values from the USB device /dev/dsp1 just no values will be collected...

        Can you show me the output of the following code? It's a slightly modified version of your original code. According to you the else-branch should trigger if there's nothing to be read from /dev/dsp1.

        #!/usr/bin/perl use strict; use warnings; # input device my $input = "/dev/dsp1"; # open filehandler open(my $fh, '<', $input) or die("ERROR open $input ($!)\n"); binmode($fh); while(1) { my $buffer; # read one byte read($fh, $buffer, 1); if(defined($buffer)) { # ignore } else { print "nothing to collect\n"; } } close($fh);

        If the else-branch does indeed trigger then I wonder what's going on. From my understanding of Perl the if-condition is always true, even if there's nothing to be read.