in reply to Simple tone to audio card speakers

The Audio::DSP module allows you to write generated tones to the soundcard. The PDL::Audio module generates realistic bird calls from data. :-) The script below, generates a series of numbers, that is fed to the soundcard, play with it for different tones.
#!/usr/bin/perl use warnings; use strict; use Audio::DSP; $| = 1; my $sample_rate = 22050; #44100 gives best sound, but highest cpu #8000 works my $channels = 1; my $format = AFMT_S16_LE; #8 bit won't work on SBLive my $buffer = 4096; my $dsp = new Audio::DSP(buffer => $buffer, channels => $channels, format => $format, rate => $sample_rate); $dsp->init() || die $dsp->errstr(); ############################################################ my $freq_adj = .1; my $vol = .5; my $toggle = 1; while(1){ make_tone($freq_adj, $vol); if(($freq_adj > 4) or ($freq_adj < .1)){ $toggle *= -1;} $freq_adj += ($toggle * .001); #higher increment numbers are slower #0 is pure tone } sub make_tone { my $rad = 0; my ($freq_adj, $vol) = @_; while ( $rad < 6.283 ){ $rad += $freq_adj; my $raw = ($vol*32768) * sin($rad); #max times my $num = pack( 'V', $raw ); $dsp->dwrite($num); } }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: Simple tone to audio card speakers
by Anonymous Monk on Jan 21, 2007 at 03:44 UTC
    Strange for both Audio::DSP and Audio::port I get a vast number of the same errors when trying to install either....
      Well I got the Audio::DSP to install on a different linux box, so I am playing with it now. Would it be too much to ask what the code would be to play a tone of 300 Hz? Thanks :-) I figured out what the frequency control is. I appears that Audio::DSP doe not lend itself to playing a tune. Am I mistaken?