Michael Chapman | 3 Jan 2008 14:54

Re: Get the length of a .wav-file... How?


I have been using a (homegrown Perl script (it's on the Web if anyone wants 
the URL) -it runs as a nice little shell script), the core of which is:

<CODE>
sub wav_length {
	# usage wav_length(file.wav)
	# This calcualtes the playtime
	# we need that for other functions
	
	my $file = $_[0];
	
	my $size = ( -s "$file"); unless ($size){$size="0"}
	if ($size < 256 ){ die "file: $file has a trivial size ($size bytes). 
Quitting.\n"}

	# Copy the cksize (bytes 5 - 8) and nAvgBytesPerSec (29 - 32)  to a $string
	my $string; my $offset;
	open ( IN , "$file" ) or die "subroutine wav_length: cannot open file: $file 
\n";
	read ( IN , $string , 32);
	close (IN);
	$string =~ s/RIFF(.{4}).{20}(.{4})//;
	my $length = egg($1); # in Bytes
	my $Bps    = egg($2); # Bytes per second
	
	if ( wavex_check($file)) { $offset = 72 } else { $offset = 36 }
	$length = ( $length - $offset ) / $Bps ; # seconds
	
	return $length;
	}
</CODE>

I suspect it may only work on a sub-set of WAV files.

"egg" just des some Endian-arithmetic (?hexadec to decimal as well).
"wavex_check" determines if 'extensible' Wavex or not (but omitting it only 
alters the result by 36 bytes (if you have a omputer generated exactly 10 
second clip, this does avoid umpteen digits after the decimal point (e.g 
9.9845123 seconds instead of 10, but for practical use 36 ytes is not 
much;-)>).

Michael

(The whole lot is GPLed if anyone wats to improve it;-)> MC

On Thursday 03 January 2008 11:53 am, Rafal Maszkowski wrote:
> On Wed, Jan 02, 2008 at 10:32:45PM -0500, Frank Peters wrote:
> > > i want to mix two files, but i do not the legth of the
> > > first-/above-file. how can i find out the length of a file?

> The standard method with sox is
>
> sox file.wav -e stat |& awk '/^Length/ { print $NF }'
>
> (tcsh) but it takes some time. It would be useful to have some option to
> stat to print only the data which can be read without swallowing the whole
> file. It would be an easy change but I cannot promise to find time to do
> it.
>
> sndfile-info prints a lot of data immediately but then stops to calculate
> the signal maximum so it has the same disadvantage and no options to avoid
> this either.
>
> R.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Gmane