From 3cb0784e392a3288fd43dfb23df505b8719d2250 Mon Sep 17 00:00:00 2001 From: ergz Date: Wed, 7 Feb 2024 23:41:04 -0800 Subject: [PATCH] work on synth class --- single-voice-synth.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/single-voice-synth.js b/single-voice-synth.js index 3cfc1a4..ab74064 100644 --- a/single-voice-synth.js +++ b/single-voice-synth.js @@ -3,15 +3,34 @@ class Synth { this.audioContext = audioContext; this.oscWaveType = waveType; this.oscFrequency = frequency; + + // osc setup this.osc = this.audioContext.createOscillator(); this.osc.type = this.oscWaveType; this.osc.frequency.setValueAtTime(this.oscFrequency, this.audioContext.currentTime); + this.osc.connect(this.audioContext.destination); } - + setOscWaveType(type) { this.oscWaveType = type; this.osc.type = this.oscWaveType; } + + startOsc() { + this.osc.start(); + } + + stopOsc() { + // first get all the parameters for this osc + let currentFreq = this.osc.frequency.value; + let currentType = this.osc.type; + + this.osc.stop(); + this.osc = this.audioContext.createOscillator(); + console.log(this.osc); + this.osc.type = currentType; + this.osc.frequency.setValueAtTime(currentFreq, this.audioContext.currentTime); + } }