问题
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
}
}
回答1:
Wikipedia gives simple equations for the square, triangle, and sawtooth waves. Here are probably the simplest (which all have period 1):
square(t) = sgn(sin(2πt)) sawtooth(t) = t - floor(t + 1/2) triangle(t) = abs(sawtooth(t))
来源:https://stackoverflow.com/questions/3387159/actionscript-creating-square-triangle-sawtooth-waves-from-math-sin