javascriptweb-audio-apisound-synthesis

Wavetable Synthesis - WebAudioApi


I am trying to create a wavetable synthesizer using the Web Audio Api. What i would like to achieve is the possibility to linearly swap from a wave form to another one (like Massive or Serum).

For example: starting from a sine wave, i rotate a knob that will gradually transform it into a square wave.

I've searched on the documentation and so far i found how to create a custom waveform:

var real = new Float32Array(2);
var imag = new Float32Array(2);
var ac = new AudioContext();
var osc = ac.createOscillator();

real[0] = 0;
imag[0] = 0;
real[1] = 1;
imag[1] = 0;

var wave = ac.createPeriodicWave(real, imag, {disableNormalization: true});

osc.setPeriodicWave(wave);

osc.connect(ac.destination);

osc.start();
osc.stop(2);

The main problem is that this waveform is static, i am not able to change it gradually into something else.

How can i achieve my goal? I was thinking about 2 gain nodes placed after each wave that will work complementary to each other.

For example: my sine wave goes into Gain1 which is 10 and my square wave into Gain2 which is 0. Then i change them complementary, Gain1=5,Gain2=5 and so on.

Is it a valid approach?


Solution

  • IIUC, I don't think using a set of gain nodes will produce what you want. And there's no builtin node to do this.

    I think you will have to do this yourself with an AudioWorkletNode.