Ensure zeroGain is cleaned up after use; move start/stop to after callback assigned for safety
This commit is contained in:
parent
64f7bc4442
commit
659071a4ee
1 changed files with 10 additions and 3 deletions
|
|
@ -211,18 +211,25 @@ export function getVibratoOscillator(param, value, t) {
|
||||||
export function webAudioTimeout(audioContext, onComplete, startTime, stopTime) {
|
export function webAudioTimeout(audioContext, onComplete, startTime, stopTime) {
|
||||||
const constantNode = new ConstantSourceNode(audioContext);
|
const constantNode = new ConstantSourceNode(audioContext);
|
||||||
|
|
||||||
// Safari requires audio nodes to be connected in order for their onended events
|
// Certain browsers requires audio nodes to be connected in order for their onended events
|
||||||
// to fire, so we _mute it_ and then connect it to the destination
|
// to fire, so we _mute it_ and then connect it to the destination
|
||||||
const zeroGain = gainNode(0);
|
const zeroGain = gainNode(0);
|
||||||
zeroGain.connect(audioContext.destination);
|
zeroGain.connect(audioContext.destination);
|
||||||
constantNode.connect(zeroGain);
|
constantNode.connect(zeroGain);
|
||||||
constantNode.start(startTime);
|
|
||||||
|
|
||||||
// Schedule the `onComplete` callback to occur at `stopTime`
|
// Schedule the `onComplete` callback to occur at `stopTime`
|
||||||
constantNode.stop(stopTime);
|
|
||||||
constantNode.onended = () => {
|
constantNode.onended = () => {
|
||||||
|
// Ensure garbage collection
|
||||||
|
try {
|
||||||
|
zeroGain.disconnect();
|
||||||
|
} catch {}
|
||||||
|
try {
|
||||||
|
constantNode.disconnect();
|
||||||
|
} catch {}
|
||||||
onComplete();
|
onComplete();
|
||||||
};
|
};
|
||||||
|
constantNode.start(startTime);
|
||||||
|
constantNode.stop(stopTime);
|
||||||
return constantNode;
|
return constantNode;
|
||||||
}
|
}
|
||||||
const mod = (freq, range = 1, type = 'sine') => {
|
const mod = (freq, range = 1, type = 'sine') => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue