1 | import { version } from "../version.js";
|
2 | import {
|
3 | AnyAudioContext,
|
4 | hasAudioContext,
|
5 | theWindow,
|
6 | } from "./context/AudioContext.js";
|
7 | import { Context } from "./context/Context.js";
|
8 | import { DummyContext } from "./context/DummyContext.js";
|
9 | import { BaseContext } from "./context/BaseContext.js";
|
10 | import { OfflineContext } from "./context/OfflineContext.js";
|
11 | import {
|
12 | isAudioContext,
|
13 | isOfflineAudioContext,
|
14 | } from "./util/AdvancedTypeCheck.js";
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const dummyContext = new DummyContext();
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | let globalContext: BaseContext = dummyContext;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export function getContext(): BaseContext {
|
32 | if (globalContext === dummyContext && hasAudioContext) {
|
33 | setContext(new Context());
|
34 | }
|
35 | return globalContext;
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | export function setContext(
|
45 | context: BaseContext | AnyAudioContext,
|
46 | disposeOld = false
|
47 | ): void {
|
48 | if (disposeOld) {
|
49 | globalContext.dispose();
|
50 | }
|
51 |
|
52 | if (isAudioContext(context)) {
|
53 | globalContext = new Context(context);
|
54 | } else if (isOfflineAudioContext(context)) {
|
55 | globalContext = new OfflineContext(context);
|
56 | } else {
|
57 | globalContext = context;
|
58 | }
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | export function start(): Promise<void> {
|
75 | return globalContext.resume();
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | if (theWindow && !theWindow.TONE_SILENCE_LOGGING) {
|
82 | let prefix = "v";
|
83 | if (version === "dev") {
|
84 | prefix = "";
|
85 | }
|
86 | const printString = ` * Tone.js ${prefix}${version} * `;
|
87 |
|
88 | console.log(`%c${printString}`, "background: #000; color: #fff");
|
89 | }
|