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?
CodePudding user response:
I "mute" sound by disconnect
and connect
:
muteAudioMorse(muteValue) {
if(this.audioCtx.state === 'running') {
if(muteValue === 0) this.gainNode.disconnect(this.audioCtx.destination)
else if(muteValue === 1) this.gainNode.connect(this.audioCtx.destination)
}
}