Audio::NoiseGen - Unit Generator Based Sound Synthesizer
use Audio::NoiseGen ':all'; # Connect to your sound engine/hardware Audio::NoiseGen::init(); play( gen => envelope( attack => 0.2, sustain => 14.5, release => 0.2, gen => combine( gens => [ segment(notes => ' E D C D E E E R D D D R E E E R E D C D E E E/2 E D D E D C '), segment(notes => 'A2 R R R'), segment(notes => 'C3/2 E3/4 E3/4 C3/2 F3 R'), ], ), ) );
"Unit Generators" have long been a method to synthesize digital sound (music, dare I say). This module provides a suite of such generators. You first create an instance of a generator, and then each time that instance is invoked it returns a sample.
These variables can be exported if you want. They are used by some of the generators to help calculate things like timing.
$sample_rate - samples/second sent to soundcard
$sample_rate
$time_step - 1/$sample_rate to ease calculations
$time_step
$pi - Er.... pi :)
$pi
%note_freq - map of note names to frequencies
%note_freq
This sets up our Audio::PortAudio interface. All parameters are optional, and without any you will get the default provided by PortAudio.
$filename is optional.
$filename
Invokes the $gen and sends the resulting samples to the output device (soundcard).
$gen
Generates a sine-wave.
Amplify the output of $g. If amount is greather than 1 this will make it louder, less than one to make it quieter.
$g
Just return silence forever.
Return random samples.
Triangle wave.
Square wave.
Build up a simple envelope. So far this supports attack, sustain, and release (I need to implement decay).
This will fade the volume up for $attack seconds (linear), keep it there for $sustain seconds, and then fade down for $release seconds (linear).
Returns undef at the end of the release.
Plays each of the provided generators all at once.
Once all the generators return undef, combine will return undef. Subsequent calls will start it over.
Play a list of generators, one after another. Plays the first generator until it returns undef, then goes on to the second, and so on. Returns undef when it completes the last generator in it's sequence, and then keeps returning undef after that.
Plays a named note, looking it up in %note_freq. Actually builds an envelope for the note, so it can take a subref 'instrument' to use for the actual sound.
Note that 'instrument' isn't a generator, it is a generator-creator that will be passed a freq. The default is \&sine. Tricky.
\&sine
You can also pass any of the evelope parameters. It defaults to zero attack and release, and 0.1 second sustain.
Play silence for a fixed set of time, 'length', in seconds.
Generate a sequence of notes by parsing the 'notes' param. Pretty minimal for now.
Plays a formula. Takes 'formula', 'bits', and 'sample_rate'. 'bits' defaults to 8, 'sample_rate' defaults to 8000.
Formula uses $_ instead of 't', but is otherwise similar to what is described at http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html.
$_
To install Audio::NoiseGen, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Audio::NoiseGen
CPAN shell
perl -MCPAN -e shell install Audio::NoiseGen
For more information on module installation, please visit the detailed CPAN module installation guide.