#! /usr/bin/perl

use Fcntl;

my($snddata, $index, $sndbuf, $sndsize, $chunksize, $mousse, $thres);

$chunksize = 16384;
$index = 0;
$thres = 2250;
$mousse = $thres;

open(SOUND, "<foo2.raw");
read(SOUND, $snddata, -s SOUND, 0);
close(SOUND);

$sndsize = length($snddata);

#sysopen(DSP, "/dev/dsp", O_WRONLY|O_NONBLOCK);
sysopen(DSP, "/dev/dsp", O_WRONLY);

print "chunk size = $chunksize\n";

while($index < $sndsize) {
	if($mousse == $thres) {
		$sndbuf = substr($snddata, $index, $chunksize);
		syswrite(DSP, $sndbuf, length($sndbuf));
		$index += $chunksize;
		$mousse = 0;
 	}
	$mousse++;
	print "index = $index/$sndsize ($mousse)\n";
}

close(DSP);
