DEV Community

Shinya Kitaoka
Shinya Kitaoka

Posted on • Updated on

APU-like Triangle in WebAudio

PeriodicWave

We can define OscillatorNode for any periodic function f(t), defined by the domain [0, 2π), using PeriodicWave:

const ω = new OscillatorNode(
    audioContext,
    {
        type: "custom",
        frequency: frequency,
        periodicWave: new PeriodicWave(
            audioContext,
            {real: a, imag: b}
        ),
    }
);
Enter fullscreen mode Exit fullscreen mode

where a and b are Float32Arrays.

APU-like Triangle

NES's pseudo-triangle wave repeats FEDCBA9876543210 and 0123456789ABCDEF:

begin end value
0 π/16 1
π/16 π/8 13/15
π/8 3/16 π 11/15
3/16 π π/4 3/5
π/4 5/16 π 7/15
5/16 π 3/8 π 1/3
3/8 π 7/16 π 1/5
7/16 π π/2 1/15
π/2 9/16 π -1/15
9/16 π 5/8 π -1/5
5/8 π 11/16 π -1/3
11/16 π 3/4 π -7/15
3/4 π 13/16 π -3/5
13/16 π 7/8 π -11/15
7/8 π 15/16 π -13/15
15/16 π π -1

As a result, we obtain follows:

a0=0ak=415πkn=115sinπkn16bk=0 \begin{aligned} a_{0} &= 0 \\ a_{k} &= \frac{4}{15\pi{k}} \sum_{n=1}^{15} \sin\frac{\pi{kn}}{16} \\ b_{k} &= 0 \end{aligned}

Top comments (0)