2019-08-26 17:55:04 +00:00
|
|
|
import {
|
|
|
|
AudioContext as stdAudioContext,
|
|
|
|
OfflineAudioContext as stdOfflineAudioContext,
|
|
|
|
} from "standardized-audio-context";
|
2019-08-19 18:15:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new AudioContext
|
|
|
|
*/
|
|
|
|
function createAudioContext(): AudioContext {
|
2019-08-26 17:55:04 +00:00
|
|
|
return new stdAudioContext() as unknown as AudioContext;
|
2019-08-19 18:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new OfflineAudioContext
|
|
|
|
*/
|
|
|
|
export function createOfflineAudioContext(channels: number, length: number, sampleRate: number): OfflineAudioContext {
|
2019-09-16 03:32:40 +00:00
|
|
|
return new stdOfflineAudioContext(channels, length, sampleRate) as unknown as OfflineAudioContext;
|
2019-08-19 18:15:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 16:49:04 +00:00
|
|
|
/**
|
|
|
|
* Either the online or offline audio context
|
|
|
|
*/
|
|
|
|
export type AnyAudioContext = AudioContext | OfflineAudioContext;
|
|
|
|
|
2019-07-25 22:28:17 +00:00
|
|
|
/**
|
|
|
|
* Interface for things that Tone.js adds to the window
|
|
|
|
*/
|
|
|
|
interface ToneWindow extends Window {
|
2019-08-16 16:49:04 +00:00
|
|
|
TONE_AUDIO_CONTEXT?: AnyAudioContext;
|
2019-07-25 22:28:17 +00:00
|
|
|
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
|
2019-08-26 17:55:04 +00:00
|
|
|
* @hidden
|
2019-07-25 22:28:17 +00:00
|
|
|
*/
|
|
|
|
export const theWindow: ToneWindow | null = typeof self === "object" ? self : null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the browser has a window object which has an AudioContext
|
2019-08-26 17:55:04 +00:00
|
|
|
* @hidden
|
2019-07-25 22:28:17 +00:00
|
|
|
*/
|
2019-08-16 16:49:04 +00:00
|
|
|
export const hasAudioContext = theWindow &&
|
|
|
|
(theWindow.hasOwnProperty("AudioContext") || theWindow.hasOwnProperty("webkitAudioContext"));
|
2019-07-25 22:28:17 +00:00
|
|
|
|
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
|
|
|
*/
|
2019-08-16 16:49:04 +00:00
|
|
|
let globalContext: AnyAudioContext;
|
2019-06-23 18:47:21 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2019-08-16 16:49:04 +00:00
|
|
|
export function getAudioContext(): AnyAudioContext {
|
2019-07-25 22:28:17 +00:00
|
|
|
if (!globalContext && hasAudioContext) {
|
2019-08-19 18:15:53 +00:00
|
|
|
setAudioContext(createAudioContext());
|
2019-06-23 18:47:21 +00:00
|
|
|
}
|
|
|
|
return globalContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default audio context
|
|
|
|
*/
|
2019-08-16 16:49:04 +00:00
|
|
|
export function setAudioContext(context: AnyAudioContext): void {
|
2019-06-23 18:47:21 +00:00
|
|
|
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
|
|
|
}
|