UNPKG

2.35 kBJavaScriptView Raw
1import { version } from "../version.js";
2import { hasAudioContext, theWindow, } from "./context/AudioContext.js";
3import { Context } from "./context/Context.js";
4import { DummyContext } from "./context/DummyContext.js";
5import { OfflineContext } from "./context/OfflineContext.js";
6import { isAudioContext, isOfflineAudioContext, } from "./util/AdvancedTypeCheck.js";
7/**
8 * This dummy context is used to avoid throwing immediate errors when importing in Node.js
9 */
10const dummyContext = new DummyContext();
11/**
12 * The global audio context which is getable and assignable through
13 * getContext and setContext
14 */
15let globalContext = dummyContext;
16/**
17 * Returns the default system-wide {@link Context}
18 * @category Core
19 */
20export function getContext() {
21 if (globalContext === dummyContext && hasAudioContext) {
22 setContext(new Context());
23 }
24 return globalContext;
25}
26/**
27 * Set the default audio context
28 * @param context
29 * @param disposeOld Pass `true` if you don't need the old context to dispose it.
30 * @category Core
31 */
32export function setContext(context, disposeOld = false) {
33 if (disposeOld) {
34 globalContext.dispose();
35 }
36 if (isAudioContext(context)) {
37 globalContext = new Context(context);
38 }
39 else if (isOfflineAudioContext(context)) {
40 globalContext = new OfflineContext(context);
41 }
42 else {
43 globalContext = context;
44 }
45}
46/**
47 * Most browsers will not play _any_ audio until a user
48 * clicks something (like a play button). Invoke this method
49 * on a click or keypress event handler to start the audio context.
50 * More about the Autoplay policy
51 * [here](https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio)
52 * @example
53 * document.querySelector("button").addEventListener("click", async () => {
54 * await Tone.start();
55 * console.log("context started");
56 * });
57 * @category Core
58 */
59export function start() {
60 return globalContext.resume();
61}
62/**
63 * Log Tone.js + version in the console.
64 */
65if (theWindow && !theWindow.TONE_SILENCE_LOGGING) {
66 let prefix = "v";
67 if (version === "dev") {
68 prefix = "";
69 }
70 const printString = ` * Tone.js ${prefix}${version} * `;
71 // eslint-disable-next-line no-console
72 console.log(`%c${printString}`, "background: #000; color: #fff");
73}
74//# sourceMappingURL=Global.js.map
\No newline at end of file