2019-07-25 22:28:17 +00:00
|
|
|
/**
|
|
|
|
* Interface for things that Tone.js adds to the window
|
|
|
|
*/
|
|
|
|
interface ToneWindow extends Window {
|
|
|
|
TONE_AUDIO_CONTEXT?: BaseAudioContext;
|
|
|
|
TONE_SILENCE_LOGGING?: boolean;
|
2019-08-08 21:00:42 +00:00
|
|
|
TONE_DEBUG_CLASS?: string;
|
2019-07-25 22:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the window object
|
|
|
|
*/
|
|
|
|
export const theWindow: ToneWindow | null = typeof self === "object" ? self : null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the browser has a window object which has an AudioContext
|
|
|
|
*/
|
|
|
|
export const hasAudioContext = theWindow && theWindow.hasOwnProperty("AudioContext");
|
|
|
|
|
2019-06-23 18:47:21 +00:00
|
|
|
/**
|
|
|
|
* The global audio context which is getable and assignable through
|
2019-07-23 15:28:33 +00:00
|
|
|
* getAudioContext and setAudioContext
|
2019-06-23 18:47:21 +00:00
|
|
|
*/
|
|
|
|
let globalContext: BaseAudioContext;
|
|
|
|
|
2019-07-25 22:28:17 +00:00
|
|
|
// if it was created already, use that one
|
|
|
|
// this enables multiple versions of Tone.js to run on the same page.
|
|
|
|
if (theWindow && theWindow.TONE_AUDIO_CONTEXT) {
|
|
|
|
globalContext = theWindow.TONE_AUDIO_CONTEXT;
|
|
|
|
}
|
2019-06-23 18:47:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the default system-wide AudioContext
|
|
|
|
*/
|
|
|
|
export function getAudioContext(): BaseAudioContext {
|
2019-07-25 22:28:17 +00:00
|
|
|
if (!globalContext && hasAudioContext) {
|
2019-06-23 18:47:21 +00:00
|
|
|
setAudioContext(new AudioContext());
|
|
|
|
}
|
|
|
|
return globalContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default audio context
|
|
|
|
*/
|
|
|
|
export function setAudioContext(context: BaseAudioContext): void {
|
|
|
|
globalContext = context;
|
2019-07-25 22:28:17 +00:00
|
|
|
if (theWindow) {
|
|
|
|
theWindow.TONE_AUDIO_CONTEXT = globalContext;
|
|
|
|
}
|
2019-06-23 18:47:21 +00:00
|
|
|
}
|