2019-05-23 14:00:49 -04:00
|
|
|
import { version } from "../version";
|
2019-11-06 14:32:19 -08:00
|
|
|
import { AnyAudioContext, hasAudioContext, theWindow } from "./context/AudioContext";
|
2019-06-23 15:02:38 -04:00
|
|
|
import { Context } from "./context/Context";
|
2019-11-06 14:32:19 -08:00
|
|
|
import { DummyContext } from "./context/DummyContext";
|
|
|
|
import { BaseContext } from "./context/BaseContext";
|
2019-08-26 10:55:43 -07:00
|
|
|
import { OfflineContext } from "./context/OfflineContext";
|
|
|
|
import { isAudioContext, isOfflineAudioContext } from "./util/AdvancedTypeCheck";
|
2019-04-12 10:37:47 -04:00
|
|
|
|
2019-07-25 18:28:17 -04:00
|
|
|
/**
|
|
|
|
* This dummy context is used to avoid throwing immediate errors when importing in Node.js
|
|
|
|
*/
|
2019-11-06 14:32:19 -08:00
|
|
|
const dummyContext = new DummyContext();
|
2019-07-25 18:28:17 -04:00
|
|
|
|
2019-04-12 10:37:47 -04:00
|
|
|
/**
|
|
|
|
* The global audio context which is getable and assignable through
|
|
|
|
* getContext and setContext
|
|
|
|
*/
|
2019-11-06 14:32:19 -08:00
|
|
|
let globalContext: BaseContext = dummyContext;
|
2019-06-17 21:53:54 -04:00
|
|
|
|
2019-04-12 10:37:47 -04:00
|
|
|
/**
|
2019-09-16 11:05:24 -04:00
|
|
|
* Returns the default system-wide [[Context]]
|
|
|
|
* @category Core
|
2019-04-12 10:37:47 -04:00
|
|
|
*/
|
2019-11-06 14:32:19 -08:00
|
|
|
export function getContext(): BaseContext {
|
2019-07-25 18:32:58 -04:00
|
|
|
if (globalContext === dummyContext && hasAudioContext) {
|
2019-06-23 15:02:38 -04:00
|
|
|
setContext(new Context());
|
2019-04-12 10:37:47 -04:00
|
|
|
}
|
|
|
|
return globalContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default audio context
|
2019-09-16 11:05:24 -04:00
|
|
|
* @category Core
|
2019-04-12 10:37:47 -04:00
|
|
|
*/
|
2019-11-06 14:32:19 -08:00
|
|
|
export function setContext(context: BaseContext | AnyAudioContext): void {
|
2019-08-26 10:55:43 -07:00
|
|
|
if (isAudioContext(context)) {
|
|
|
|
globalContext = new Context(context);
|
|
|
|
} else if (isOfflineAudioContext(context)) {
|
|
|
|
globalContext = new OfflineContext(context);
|
|
|
|
} else {
|
|
|
|
globalContext = context;
|
|
|
|
}
|
2019-04-12 10:37:47 -04: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 18:01:27 -04:00
|
|
|
* document.querySelector("button").addEventListener("click", async () => {
|
|
|
|
* await Tone.start();
|
|
|
|
* console.log("context started");
|
|
|
|
* });
|
2019-09-16 11:05:24 -04:00
|
|
|
* @category Core
|
2019-04-12 10:37:47 -04:00
|
|
|
*/
|
2020-04-16 22:24:18 -04:00
|
|
|
export function start(): Promise<void> {
|
2019-06-23 15:02:38 -04:00
|
|
|
return globalContext.resume();
|
2019-04-12 10:37:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-23 14:00:49 -04:00
|
|
|
* Log Tone.js + version in the console.
|
2019-04-12 10:37:47 -04:00
|
|
|
*/
|
2019-07-25 18:28:17 -04:00
|
|
|
if (theWindow && !theWindow.TONE_SILENCE_LOGGING) {
|
2019-05-23 14:00:49 -04:00
|
|
|
let prefix = "v";
|
|
|
|
if (version === "dev") {
|
|
|
|
prefix = "";
|
|
|
|
}
|
|
|
|
const printString = ` * Tone.js ${prefix}${version} * `;
|
2019-09-14 18:12:44 -04:00
|
|
|
// eslint-disable-next-line no-console
|
2019-05-23 14:00:49 -04:00
|
|
|
console.log(`%c${printString}`, "background: #000; color: #fff");
|
|
|
|
}
|