数学クラスを使用して、正方形、三角形、のこぎり波、またはその他のカスタム波形を生成する一般的なコードはありますか?
以下は、SampleDataEvent を処理し、中央 c (440 Hz) の正弦波を再生する基本的な関数です。四角や三角などの波を取り入れて音色を変えたいです。
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
}
}