[Stk] Re-use of Stk code
Ross Clement
rossclement at gmail.com
Sun Apr 17 23:38:25 PDT 2011
Dear all,
This is not an incredibly serious project, but I'm writing an audio
synthesis and processing library for Flash ActionScript. Yes, I know
that Flash ActionScript is not actually a highly optimised,
computationally quick platform for audio synthesis, but I'd like to be
able to do more with audio in Flash.
Are there any objections, or procedures I should follow, if I'm going
to do things like the attached class.
Regards,
Ross Clement
package uk.ac.dmu.tech.flashsyn
{
import flash.utils.ByteArray;
/*
* Generate a bandlimited sawtooth waveform. This class uses adapted
code from BlitSaw.h and BlitSaw.cpp from
* the Stk (Stanford Synthesis Toolkit)
*
* Original code based on algorithm by Stiltson and Smith.
*
* Stk C++ code written by:
* Based on initial code of Robin Davies, 2005.
* Modified algorithm code by Gary Scavone, 2005 - 2006.
*
* ActionScript code by Ross Clement, 16th April 2011
*/
public class StkBlitSaw extends Oscillator
{
// Parameter for FREQUENCY
private var frequencyParam:Parameter;
private var omega:Number = 0; // Current phase of the oscillator
private var state:Number = 0; // Current state of the oscillator
public static var FREQUENCY:uint = 0;
private var PI:Number = Math.PI;
/*
* Constructor. Just add the output port, and frequency and
amplitude ports. The input port
* is null, it's not possible to FM (or otherwise) modulate this
oscillator with an audio
* frequency signal.
*/
public function StkBlitSaw()
{
output = new AudioOutputPort( "output", 1 ); // Create mono output port
addOutputPort( output );
// Configure and add parameters
frequencyParam = new Parameter( Parameter.FLOATING, "frequency" );
frequencyParam.setDefault( 440.0 );
addParameter( frequencyParam );
}
/*
* Init chooses a default starting value for "state", the state of
the oscillator.
*/
override public function init()
{
var period:Number = 44100.0 / (Number) (frequencyParam.getValue());
var C2_:Number = 1.0 / period;
var rate:Number = PI * C2_;
var maxHarmonics:uint = (uint) (0.5 * period);
var m_ = 2 * maxHarmonics + 1;
var a_ = m_ / period;
state = -0.5 * a_;
}
/*
* Process audio.
*/
override public function process()
{
var ad:AudioData = output.getAudioData();
var data:ByteArray = ad.getChannel( 0 );
var f:Number = (Number) (frequencyParam.getValue());
var increment:Number = f * Math.PI / 44100.0; // Initialise a
large number of parameters
var denominator:Number;
var period:Number = 44100.0 / f;
var C2_:Number = 1.0 / period;
var rate:Number = PI * C2_;
var maxHarmonics:uint = (uint) (0.5 * period);
var m_ = 2 * maxHarmonics + 1;
var a_ = m_ / period;
var tmp:Number;
for ( var i:uint = 0; i < bufferSize; i++ )
{
denominator = Math.sin( omega );
if ( denominator == 0 )
{
tmp = a_;
}
else
{
tmp = Math.sin( m_ * omega );
tmp /= period * denominator;
}
tmp += state - C2_;
state = tmp * 0.995;
data.writeFloat( state );
omega = (omega + increment) % PI;
}
}
}
}
More information about the Stk
mailing list