# make a sine wave, output as a 16-bit, 44.1khz mono wave file # # usage: perl make_sine.pl [freq] [length] [output] # # freq frequency in hz # length length in seconds # output output filename # use Audio::Wav; my $wav = new Audio::Wav; my $sample_rate = 44100; my $bits_sample = 16; my $details = { 'bits_sample' => $bits_sample, 'sample_rate' => $sample_rate, 'channels' => 1, # if you'd like this module not to use a write cache, uncomment the next line #'no_cache' => 1, }; my $write = $wav -> write( $ARGV[2], $details ); &add_sine( $ARGV[0], $ARGV[1] ); sub add_sine { my $hz = shift; my $length = shift; my $pi = ( 22 / 7 ) * 2; $length *= $sample_rate; my $max_no = ( 2 ** $bits_sample ) / 2; for my $pos ( 0 .. $length ) { $time = $pos / $sample_rate; $time *= $hz; my $val = sin $pi * $time; my $samp = $val * $max_no; $write -> write( $samp ); } } $write -> finish();