2019-05-23 18:00:49 +00:00
|
|
|
import { version } from "../version";
|
2019-11-06 22:32:19 +00:00
|
|
|
import { AnyAudioContext, hasAudioContext, theWindow } from "./context/AudioContext";
|
2019-06-23 19:02:38 +00:00
|
|
|
import { Context } from "./context/Context";
|
2019-11-06 22:32:19 +00:00
|
|
|
import { DummyContext } from "./context/DummyContext";
|
|
|
|
import { BaseContext } from "./context/BaseContext";
|
2019-08-26 17:55:43 +00:00
|
|
|
import { OfflineContext } from "./context/OfflineContext";
|
|
|
|
import { isAudioContext, isOfflineAudioContext } from "./util/AdvancedTypeCheck";
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-07-25 22:28:17 +00:00
|
|
|
/**
|
|
|
|
* This dummy context is used to avoid throwing immediate errors when importing in Node.js
|
|
|
|
*/
|
2019-11-06 22:32:19 +00:00
|
|
|
const dummyContext = new DummyContext();
|
2019-07-25 22:28:17 +00:00
|
|
|
|
2019-04-12 14:37:47 +00:00
|
|
|
/**
|
|
|
|
* The global audio context which is getable and assignable through
|
|
|
|
* getContext and setContext
|
|
|
|
*/
|
2019-11-06 22:32:19 +00:00
|
|
|
let globalContext: BaseContext = dummyContext;
|
2019-06-18 01:53:54 +00:00
|
|
|
|
2019-04-12 14:37:47 +00:00
|
|
|
/**
|
2019-09-16 15:05:24 +00:00
|
|
|
* Returns the default system-wide [[Context]]
|
|
|
|
* @category Core
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
2019-11-06 22:32:19 +00:00
|
|
|
export function getContext(): BaseContext {
|
2019-07-25 22:32:58 +00:00
|
|
|
if (globalContext === dummyContext && hasAudioContext) {
|
2019-06-23 19:02:38 +00:00
|
|
|
setContext(new Context());
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
return globalContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default audio context
|
2021-04-21 03:46:28 +00:00
|
|
|
* @param context
|
|
|
|
* @param disposeOld Pass `true` if you don't need the old context to dispose it.
|
2019-09-16 15:05:24 +00:00
|
|
|
* @category Core
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
2021-04-21 03:46:28 +00:00
|
|
|
export function setContext(context: BaseContext | AnyAudioContext, disposeOld = false): void {
|
|
|
|
if (disposeOld) {
|
|
|
|
globalContext.dispose();
|
|
|
|
}
|
|
|
|
|
2019-08-26 17:55:43 +00:00
|
|
|
if (isAudioContext(context)) {
|
|
|
|
globalContext = new Context(context);
|
|
|
|
} else if (isOfflineAudioContext(context)) {
|
|
|
|
globalContext = new OfflineContext(context);
|
|
|
|
} else {
|
|
|
|
globalContext = context;
|
|
|
|
}
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Most browsers will not play _any_ audio until a user
|
|
|
|
* clicks something (like a play button). Invoke this method
|
|
|
|
* on a click or keypress event handler to start the audio context.
|
|
|
|
* More about the Autoplay policy
|
|
|
|
* [here](https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio)
|
|
|
|
* @example
|
2019-10-24 22:01:27 +00:00
|
|
|
* document.querySelector("button").addEventListener("click", async () => {
|
|
|
|
* await Tone.start();
|
|
|
|
* console.log("context started");
|
|
|
|
* });
|
2019-09-16 15:05:24 +00:00
|
|
|
* @category Core
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
2020-04-17 02:24:18 +00:00
|
|
|
export function start(): Promise<void> {
|
2019-06-23 19:02:38 +00:00
|
|
|
return globalContext.resume();
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-23 18:00:49 +00:00
|
|
|
* Log Tone.js + version in the console.
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
2019-07-25 22:28:17 +00:00
|
|
|
if (theWindow && !theWindow.TONE_SILENCE_LOGGING) {
|
2019-05-23 18:00:49 +00:00
|
|
|
let prefix = "v";
|
|
|
|
if (version === "dev") {
|
|
|
|
prefix = "";
|
|
|
|
}
|
|
|
|
const printString = ` * Tone.js ${prefix}${version} * `;
|
2019-09-14 22:12:44 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2019-05-23 18:00:49 +00:00
|
|
|
console.log(`%c${printString}`, "background: #000; color: #fff");
|
|
|
|
}
|