动作 - 创建正方形,三角形,锯齿波从Math.sin()? [英] ActionScript - Creating Square, Triangle, Sawtooth Waves From Math.sin()?

查看:358
本文介绍了动作 - 创建正方形,三角形,锯齿波从Math.sin()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有产生方波,三角波,锯齿波或使用数学类中的任何其他自定义波形共同code可用?

下面是一个基本功能,一个处理的SampleDataEvent和播放中间-C(440赫兹)的正弦波。我想通过把方形,三角形等波改变音色。

  VAR位置:= 0;

VAR的声音:声音=新的声音();
sound.addEventListener(的SampleDataEvent.SAMPLE_DATA,sampleDataHandler);
SOUND.PLAY();

功能sampleDataHandler(事件:的SampleDataEvent):无效
    {
    对于(VAR我:= 0; I< 2048;我++)
        {
        VAR阶段:数=位置/ 44100 * Math.PI * 2;
        位置++;

        VAR样:数= Math.sin(相* 440);
        event.data.writeFloat(样品); // 剩下
        event.data.writeFloat(样品); // 对
        }
    }
 

解决方案

维基百科给出了简单的公式为三角和的锯齿波。这可能是最简单的(所有有期限1):

方(T)= SGN(SIN(2πT))
锯齿(T)= T  - 地面(T + 1/2)
三角形(吨)= ABS(锯齿(t))的

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
        }
    }

解决方案

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))

这篇关于动作 - 创建正方形,三角形,锯齿波从Math.sin()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆