http://www.perlmonks.org?node_id=1029452


in reply to Audio::WAV data problem

I would like to understand what this means:

... a WAV file that I am using for storing data logger information.

What is "data logger information"? is it simply a stream of 16-bit littel-endian binary integer data? If so, why put a WAV header on it? (That is, why not just have a "raw" binary file of "data logger information", stored as stream of 16-bit little-endian binary data?)

In case it makes sense for you to start with a "raw" (headerless) file containing only zeros - or some other "test pattern" - the first question is: how big should the file be (how many short ints do you want)? Let's say you want 64K:

# create a file with 65536 bytes of zeros (i.e. 32768 short int zeros) +: perl -e 'print "\0" x 65536' > zeros.raw # create a file with 32768 short int zeros followed by 32768 short int + ones: perl -e 'print pack("s",0) x 32768; print pack("s",1) x 32768' > zeros +-ones.raw
To turn a raw file into a WAV file, I'd use SoX (http://sox.sourceforge.net), but it would involve making up stuff about sample-rate, etc, which might be irrelevant to what you're actually trying to accomplish:
sox -t raw -e signed-integer -b 16 -r 8000 -c 1 zeros.raw zeros.wav
(SoX command-line usage is pretty "unique"... but like I said, do you really need it to be a WAV file?)

As for reading binary 16-bit little-endian integer values from a raw data file (e.g. one of the "*.raw" files created above):

#!/usr/bin/perl use strict; use warnings; my $infile = shift or die "Usage: $0 raw_input_file_name\n"; my $file_size = -s $infile; open( my $fh, $infile ) or die "$infile: $!\n"; read( $fh, my $buf, $file_size ); my @ints=unpack("s*", $buf); print scalar @ints, " short-int elements read\n"; print " ... which seems right\n" if ( @ints == $file_size/2 ); my %h; $h{$ints[$_]}++ for (0..$#ints); print "distinct int values found in $infile:\n"; print " $_ occurs $h{$_} times\n" for (keys %h);