use VolumeEffect in AudioPlayer

This commit is contained in:
Michael "Z" Goddard 2018-06-06 14:42:05 -04:00
parent 1d9e530df2
commit 65702e7722
No known key found for this signature in database
GPG key ID: 762CD40DD5349872

View file

@ -1,5 +1,6 @@
const PitchEffect = require('./effects/PitchEffect');
const PanEffect = require('./effects/PanEffect'); const PanEffect = require('./effects/PanEffect');
const PitchEffect = require('./effects/PitchEffect');
const VolumeEffect = require('./effects/VolumeEffect');
const SoundPlayer = require('./SoundPlayer'); const SoundPlayer = require('./SoundPlayer');
@ -17,9 +18,11 @@ class AudioPlayer {
this.outputNode = this.audioEngine.audioContext.createGain(); this.outputNode = this.audioEngine.audioContext.createGain();
// Create the audio effects // Create the audio effects
const pitchEffect = new PitchEffect(this.audioEngine, this, null); const volumeEffect = new VolumeEffect(this.audioEngine, this, null);
const pitchEffect = new PitchEffect(this.audioEngine, this, volumeEffect);
const panEffect = new PanEffect(this.audioEngine, this, pitchEffect); const panEffect = new PanEffect(this.audioEngine, this, pitchEffect);
this.effects = { this.effects = {
volume: volumeEffect,
pitch: pitchEffect, pitch: pitchEffect,
pan: panEffect pan: panEffect
}; };
@ -28,6 +31,7 @@ class AudioPlayer {
// outputNode -> "pitchEffect" -> panEffect -> audioEngine.input // outputNode -> "pitchEffect" -> panEffect -> audioEngine.input
panEffect.connect(this.audioEngine); panEffect.connect(this.audioEngine);
pitchEffect.connect(panEffect); pitchEffect.connect(panEffect);
volumeEffect.connect(pitchEffect);
// reset effects to their default parameters // reset effects to their default parameters
this.clearEffects(); this.clearEffects();
@ -123,9 +127,6 @@ class AudioPlayer {
for (const effectName in this.effects) { for (const effectName in this.effects) {
this.effects[effectName].clear(); this.effects[effectName].clear();
} }
if (this.audioEngine === null) return;
this.outputNode.gain.setTargetAtTime(1.0, 0, this.audioEngine.DECAY_TIME);
} }
/** /**
@ -133,8 +134,7 @@ class AudioPlayer {
* @param {number} value - the volume in range 0-100 * @param {number} value - the volume in range 0-100
*/ */
setVolume (value) { setVolume (value) {
if (this.audioEngine === null) return; this.setEffect('volume', value);
this.outputNode.gain.setTargetAtTime(value / 100, 0, this.audioEngine.DECAY_TIME);
} }
/** /**
@ -150,6 +150,7 @@ class AudioPlayer {
* Clean up and disconnect audio nodes. * Clean up and disconnect audio nodes.
*/ */
dispose () { dispose () {
this.effects.volume.dispose();
this.effects.pitch.dispose(); this.effects.pitch.dispose();
this.effects.pan.dispose(); this.effects.pan.dispose();