2

I'm building a simple synthesizer with WebMIDI control. The gain node has no effect on the oscillator, it's at full volume the entire time. Also when I play chords the frequencies are correct but there is a wobbling and screeching effect. The problems are present when playing with my MIDI controller and when starting and stopping the synthesizer using the console.

Here's my synthesizer code:

var synth = {
  voices: {},

  start: function (note, vol) {
      this.voices[note] = {
          gain: audio.createGain(),
          osc: audio.createOscillator()
      }

      this.voices[note].gain.connect(audio.destination);

      this.voices[note].osc.frequency.value = noteToFreq(note);
      this.voices[note].osc.connect(this.voices[note].gain);

      this.voices[note].osc.start(0);
      this.voices[note].gain.gain.setTargetAtTime(vol, audio.currentTime, 0.5);
  },

  stop: function (note) {
    this.voices[note].gain.gain.setTargetAtTime(0, audio.currentTime, 2);
    this.voices[note].osc.stop(audio.currentTime + 2);
  }
}
MBR-6161
  • 145
  • 1
  • 4

1 Answers1

4

Oscillators are full-range - i.e. [-1,+1]. When you sum two signals (e.g. connect them to the same output node - they're in the range of [-2,+2], which will clip some of the time. Run them through a gain node with value=0.5 and see if it eliminates the problem. (Ideally, you'd drop the gain a little bit and run them through a compressor/limiter.)

cwilso
  • 13,610
  • 1
  • 30
  • 35