Tone.js/Tone/core/Tone.ts

106 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-04-12 14:37:47 +00:00
/**
2019-09-14 20:39:18 +00:00
* Tone.js
* @author Yotam Mann
* @license http://opensource.org/licenses/MIT MIT License
* @copyright 2014-2019 Yotam Mann
2019-04-12 14:37:47 +00:00
*/
import { version } from "../version";
import { theWindow } from "./context/AudioContext";
2019-12-17 16:56:56 +00:00
import { log } from "./util/Debug";
2019-04-12 14:37:47 +00:00
//-------------------------------------
2019-04-12 14:37:47 +00:00
// TONE
//-------------------------------------
2019-04-12 14:37:47 +00:00
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface BaseToneOptions { }
2019-04-12 14:37:47 +00:00
/**
* @class Tone is the base class of all other classes.
* @constructor
2019-04-12 14:37:47 +00:00
*/
export abstract class Tone {
/**
* The version number semver
*/
static version: string = version;
/**
* The name of the class
*/
protected abstract name: string;
/**
2019-09-12 19:52:38 +00:00
* Returns all of the default options belonging to the class.
2019-04-12 14:37:47 +00:00
*/
static getDefaults(): BaseToneOptions {
return {};
}
//-------------------------------------
2019-04-12 14:37:47 +00:00
// DEBUGGING
//-------------------------------------
2019-04-12 14:37:47 +00:00
/**
* Set this debug flag to log all events that happen in this class.
*/
2019-11-17 18:09:19 +00:00
debug = false;
2019-04-12 14:37:47 +00:00
/**
* Prints the outputs to the console log for debugging purposes.
* Prints the contents only if either the object has a property
* called `debug` set to true, or a variable called TONE_DEBUG_CLASS
* is set to the name of the class.
* @example
* const osc = new Tone.Oscillator();
2019-10-24 22:01:27 +00:00
* // prints all logs originating from this oscillator
* osc.debug = true;
* // calls to start/stop will print in the console
* osc.start();
2019-04-12 14:37:47 +00:00
*/
protected log(...args: any[]): void {
2019-04-12 14:37:47 +00:00
// if the object is either set to debug = true
// or if there is a string on the Tone.global.with the class name
if (this.debug || (theWindow && this.toString() === theWindow.TONE_DEBUG_CLASS)) {
log(this, ...args);
2019-07-23 15:24:38 +00:00
}
2019-04-12 14:37:47 +00:00
}
//-------------------------------------
// DISPOSING
//-------------------------------------
/**
* Indicates if the instance was disposed
*/
2019-11-17 18:09:19 +00:00
private _wasDisposed = false;
/**
2019-09-14 20:39:18 +00:00
* disconnect and dispose.
*/
dispose(): this {
this._wasDisposed = true;
return this;
}
/**
* Indicates if the instance was disposed. 'Disposing' an
* instance means that all of the Web Audio nodes that were
* created for the instance are disconnected and freed for garbage collection.
*/
get disposed(): boolean {
return this._wasDisposed;
}
2019-04-12 14:37:47 +00:00
/**
* Convert the class to a string
* @example
* const osc = new Tone.Oscillator();
2019-10-24 22:01:27 +00:00
* console.log(osc.toString());
2019-04-12 14:37:47 +00:00
*/
toString(): string {
return this.name;
}
}