I have class MorseCodeAudio to create sound for Morse'a code. I'm using window.AudioContext. In my class I have an method with forEach:
morseCode() {
// irrelevant code
this.text.split('').forEach(letter => {
switch(letter) {
case '.':
this.gainNode.gain.setValueAtTime(1, this.time)
this.time += this.period
this.gainNode.gain.setValueAtTime(0, this.time)
this.time += this.period
break
case '-':
this.gainNode.gain.setValueAtTime(1, this.time)
this.time += 3 * this.period
this.gainNode.gain.setValueAtTime(0, this.time)
this.time += this.period
break
case ' ':
this.time += 7 * this.period
break
}
})
}
And method to mute sound:
muteAudioMorse(muteValue) {
if(this.audioCtx.state !== 'closed') this.gainNode.gain.value = muteValue
}
I can't mute the sound because forEach is still executing. If I remove forEach and add one line with sound, like this:
this.gainNode.gain.setValueAtTime(1, 0)
then muteAudioMorse method work correctly.
How I can change gain.value when setValueAtTime from forEach still working?
I "mute" sound by
disconnectandconnect: