is there common code available that produces square, triangle, sawtooth or any other custom waveforms using the math class?
below is a basic function that handles a SampleDataEvent and plays a middle-c (440 Hz) sine wave. i'd like to change the tone by incorporating square, triangle and other waves.
var position:int = 0;
var sound:Sound = new Sound();
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, sampleDataHandler);
sound.play();
function sampleDataHandler(event:SampleDataEvent):void
{
for(var i:int = 0; i < 2048; i++)
{
var phase:Number = position / 44100 * Math.PI * 2;
position ++;
var sample:Number = Math.sin(phase * 440);
event.data.writeFloat(sample); // left
event.data.writeFloat(sample); // right
}
}