Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/core/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import { getContext as ToneGetContext, setContext as ToneSetContext } from "tone/build/esm/core/Global.js";
import { start as ToneStart } from "tone/build/esm/core/Global.js";

let audioContext = null;

function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
Expand All @@ -19,11 +17,10 @@ function clamp(value, min, max) {
* @return {AudioContext} the audio context
*/
function getAudioContext() {
if (!audioContext) {
audioContext = new window.AudioContext();
ToneSetContext(audioContext);
}
return audioContext;
// Return the AudioContext that Tone.js owns rather than creating our own.
// Tone wraps its context with standardized-audio-context, which polyfills
// the Web Audio gaps Firefox and Safari have.
return ToneGetContext().rawContext;
}

/**
Expand All @@ -32,7 +29,7 @@ function getAudioContext() {
* @param {AudioContext} the desired AudioContext.
*/
function setAudioContext(context) {
audioContext = context;
// Hand the supplied context to Tone; getAudioContext() then reflects it.
ToneSetContext(context);
}

Expand All @@ -49,8 +46,7 @@ function userStartAudio() {
* @function userStopAudio
*/
function userStopAudio() {
const context = audioContext || ToneGetContext();
context.suspend();
getAudioContext().suspend();
}

export { clamp, getAudioContext, setAudioContext, userStartAudio, userStopAudio };