Apply sprite's volume to music extension notes and drums (#1735)

* Use a gain node for volume instead of effects chain

* Use a gain node for volume for drums too
This commit is contained in:
Eric Rosenbaum 2018-11-26 11:46:25 -05:00 committed by GitHub
parent 5802723dc7
commit 045ebb5026
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -886,9 +886,10 @@ class Scratch3MusicBlocks {
} }
const engine = util.runtime.audioEngine; const engine = util.runtime.audioEngine;
const chain = engine.createEffectChain(); const context = engine.audioContext;
chain.setEffectsFromTarget(util.target); const volumeGain = context.createGain();
player.connect(chain); volumeGain.gain.setValueAtTime(util.target.volume / 100, engine.currentTime);
volumeGain.connect(engine.getInputNode());
this._concurrencyCounter++; this._concurrencyCounter++;
player.once('stop', () => { player.once('stop', () => {
@ -896,6 +897,10 @@ class Scratch3MusicBlocks {
}); });
player.play(); player.play();
// Connect the player to the gain node.
player.connect({getInputNode () {
return volumeGain;
}});
} }
/** /**
@ -997,18 +1002,18 @@ class Scratch3MusicBlocks {
player.take(); player.take();
} }
const chain = engine.createEffectChain();
chain.setEffectsFromTarget(util.target);
// Set its pitch. // Set its pitch.
const sampleNote = sampleArray[sampleIndex]; const sampleNote = sampleArray[sampleIndex];
const notePitchInterval = this._ratioForPitchInterval(note - sampleNote); const notePitchInterval = this._ratioForPitchInterval(note - sampleNote);
// Create a gain node for this note, and connect it to the sprite's // Create gain nodes for this note's volume and release, and chain them
// simulated effectChain. // to the output.
const context = engine.audioContext; const context = engine.audioContext;
const volumeGain = context.createGain();
volumeGain.gain.setValueAtTime(util.target.volume / 100, engine.currentTime);
const releaseGain = context.createGain(); const releaseGain = context.createGain();
releaseGain.connect(chain.getInputNode()); volumeGain.connect(releaseGain);
releaseGain.connect(engine.getInputNode());
// Schedule the release of the note, ramping its gain down to zero, // Schedule the release of the note, ramping its gain down to zero,
// and then stopping the sound. // and then stopping the sound.
@ -1030,7 +1035,7 @@ class Scratch3MusicBlocks {
player.play(); player.play();
// Connect the player to the gain node. // Connect the player to the gain node.
player.connect({getInputNode () { player.connect({getInputNode () {
return releaseGain; return volumeGain;
}}); }});
// Set playback now after play creates the outputNode. // Set playback now after play creates the outputNode.
player.outputNode.playbackRate.value = notePitchInterval; player.outputNode.playbackRate.value = notePitchInterval;