I need to create a specific custom waveform for an oscillator for use with Web Audio API.
I have a Javascript function to output the desired waveform (calculating a y between -1 and 1 for any given x), and the plotted result looks like this:
However the Web Audio API documentation only lets you create custom wavetables based on harmonic tables via the createPeriodicWave
function, which can then be used to configure a custom oscillator via setPeriodicWave
. Is there a generic technique that can be used to compute the harmonic tables based on my waveform function?
A DFT (or FFT) with a length of exactly one period of your custom waveform will produce a harmonic table. Just low-pass filter and sample your waveform 2^N times, and feed that to a generic library FFT. (Choose a large enough 2^N to be at least more than 2X the low-pass filter's or your waveform's intrinsic highest frequency content). The magnitudes of the FFT's resulting complex bins will be your harmonic power levels.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With