Skip to content

Commit

Permalink
Adjust volume level by connecting oscillator nodes with an intermediate
Browse files Browse the repository at this point in the history
gain node #25
  • Loading branch information
sonph committed Jun 11, 2020
1 parent 634b9d4 commit 4586f60
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/js/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ class Audio {

this.uiData = {
sampleName: BEEP,
gainLevel: 1.0,
}

this.gainNode = this.audioContext.createGain();
// TODO(sonph): save this gain value in local storage.
this.gainNode.gain.value = this.uiData.gainLevel;
this.gainNode.connect(this.audioContext.destination);
}

// Play silent buffer to unlock the audio.
Expand Down Expand Up @@ -78,7 +84,7 @@ class Audio {
freq = 220.0;
}
let osc = this.audioContext.createOscillator();
osc.connect(this.audioContext.destination);
osc.connect(this.gainNode);
osc.frequency.value = freq;
osc.start(noteTime);
osc.stop(noteTime + BEEP_DURATION);
Expand All @@ -89,7 +95,7 @@ class Audio {
}
let node = this.audioContext.createBufferSource();
node.buffer = this.buffers[this.uiData.sampleName];
node.connect(this.audioContext.destination);
node.connect(this.gainNode);
node.start(noteTime);
}
}
Expand Down

0 comments on commit 4586f60

Please sign in to comment.