UNPKG

2.52 kBPlain TextView Raw
1/**
2 * Tone.js
3 * @author Yotam Mann
4 * @license http://opensource.org/licenses/MIT MIT License
5 * @copyright 2014-2024 Yotam Mann
6 */
7import { version } from "../version.js";
8import { theWindow } from "./context/AudioContext.js";
9import { log } from "./util/Debug.js";
10
11//-------------------------------------
12// TONE
13//-------------------------------------
14
15// eslint-disable-next-line @typescript-eslint/no-empty-interface
16export interface BaseToneOptions {}
17
18/**
19 * Tone is the base class of all other classes.
20 *
21 * @category Core
22 * @constructor
23 */
24export abstract class Tone {
25 /**
26 * The version number semver
27 */
28 static version: string = version;
29
30 /**
31 * The name of the class
32 */
33 protected abstract name: string;
34
35 /**
36 * Returns all of the default options belonging to the class.
37 */
38 static getDefaults(): BaseToneOptions {
39 return {};
40 }
41
42 //-------------------------------------
43 // DEBUGGING
44 //-------------------------------------
45
46 /**
47 * Set this debug flag to log all events that happen in this class.
48 */
49 debug = false;
50
51 /**
52 * Prints the outputs to the console log for debugging purposes.
53 * Prints the contents only if either the object has a property
54 * called `debug` set to true, or a variable called TONE_DEBUG_CLASS
55 * is set to the name of the class.
56 * @example
57 * const osc = new Tone.Oscillator();
58 * // prints all logs originating from this oscillator
59 * osc.debug = true;
60 * // calls to start/stop will print in the console
61 * osc.start();
62 */
63 protected log(...args: any[]): void {
64 // if the object is either set to debug = true
65 // or if there is a string on the Tone.global.with the class name
66 if (
67 this.debug ||
68 (theWindow && this.toString() === theWindow.TONE_DEBUG_CLASS)
69 ) {
70 log(this, ...args);
71 }
72 }
73
74 //-------------------------------------
75 // DISPOSING
76 //-------------------------------------
77
78 /**
79 * Indicates if the instance was disposed
80 */
81 private _wasDisposed = false;
82
83 /**
84 * disconnect and dispose.
85 */
86 dispose(): this {
87 this._wasDisposed = true;
88 return this;
89 }
90
91 /**
92 * Indicates if the instance was disposed. 'Disposing' an
93 * instance means that all of the Web Audio nodes that were
94 * created for the instance are disconnected and freed for garbage collection.
95 */
96 get disposed(): boolean {
97 return this._wasDisposed;
98 }
99
100 /**
101 * Convert the class to a string
102 * @example
103 * const osc = new Tone.Oscillator();
104 * console.log(osc.toString());
105 */
106 toString(): string {
107 return this.name;
108 }
109}