2019-05-23 18:00:49 +00:00
|
|
|
import { version } from "../version";
|
2019-07-25 22:28:17 +00:00
|
|
|
import { hasAudioContext, theWindow } from "./context/AudioContext";
|
2019-06-23 19:02:38 +00:00
|
|
|
import { Context } from "./context/Context";
|
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
|
|
|
|
*/
|
|
|
|
const dummyContext: Context = {
|
|
|
|
destination: {},
|
|
|
|
transport: {},
|
|
|
|
} as Context;
|
|
|
|
|
2019-04-12 14:37:47 +00:00
|
|
|
/**
|
|
|
|
* The global audio context which is getable and assignable through
|
|
|
|
* getContext and setContext
|
|
|
|
*/
|
2019-07-25 22:28:17 +00:00
|
|
|
let globalContext: Context = dummyContext;
|
2019-06-18 01:53:54 +00:00
|
|
|
|
2019-04-12 14:37:47 +00:00
|
|
|
/**
|
|
|
|
* Returns the default system-wide AudioContext
|
|
|
|
*/
|
2019-06-23 19:02:38 +00:00
|
|
|
export function getContext(): Context {
|
2019-07-25 22:28:17 +00:00
|
|
|
if (!globalContext && 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
|
|
|
|
*/
|
2019-06-23 19:02:38 +00:00
|
|
|
export function setContext(context: Context): void {
|
2019-04-12 14:37:47 +00:00
|
|
|
globalContext = context;
|
2019-06-23 19:02:38 +00:00
|
|
|
context.initialize();
|
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
|
|
|
|
* document.querySelector('#playbutton').addEventListener('click', () => Tone.start())
|
|
|
|
*/
|
2019-06-19 14:18:33 +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} * `;
|
|
|
|
// tslint:disable-next-line: no-console
|
|
|
|
console.log(`%c${printString}`, "background: #000; color: #fff");
|
|
|
|
}
|