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}
),
}
);
where a
and b
are Float32Array
s initialized by
Pulse Wave
Pulse wave with duty ratio r:1-r
is defined
Coefficients a
and b
for the wave are
where
and σ
is additional coefficient for removing ringing artifacts:
and N is the length of Float32Array
.
sinc function
We define sinc function for numerical stability:
function sinc(x) {
const πx = Math.PI * x;
if (Math.abs(x) < 1e-9) {
// Taylor series centered at x=0
const πx_sq = πx * πx;
return (
1 - (
1 - (
1 - πx_sq / 42
) * πx_sq / 20
) * πx_sq / 6
);
} else {
return Math.sin(πx) / (πx);
}
}
Top comments (0)