back

Make_sine.pl

A simple one this, it makes a sine wave, output as a 16-bit, 44.1khz mono wave file. Uses code from Audio::Wav readme.

usage: perl make_sine.pl freq length outputfile
# 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
#
#
# Audio::Wav and some of the code below is (C) Nick Peskett and Kurt George Gjerde

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();

All this code is © 2006 - 2015, except for the noted pieces which are © to their authors as noted. Feel free to use anything you find here, I'm not that precious about it. Take it, improve it, make lots of cash with it, it's a gift.

back