ActionScript - Creating Square, Triangle, Sawtooth Waves From Math.sin()?

限于喜欢 提交于 2019-12-24 12:53:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!