2024-05-03 18:31:14 +00:00
|
|
|
import * as Classes from "./classes.js";
|
|
|
|
import { TransportClass } from "./core/clock/Transport.js";
|
|
|
|
import { Context } from "./core/context/Context.js";
|
|
|
|
import { ListenerClass } from "./core/context/Listener.js";
|
|
|
|
import { DestinationClass } from "./core/context/Destination.js";
|
|
|
|
import { FrequencyClass } from "./core/type/Frequency.js";
|
|
|
|
import { MidiClass } from "./core/type/Midi.js";
|
|
|
|
import { TicksClass } from "./core/type/Ticks.js";
|
|
|
|
import { TimeClass } from "./core/type/Time.js";
|
|
|
|
import { TransportTimeClass } from "./core/type/TransportTime.js";
|
|
|
|
import { isDefined, isFunction } from "./core/util/TypeCheck.js";
|
|
|
|
import { omitFromObject } from "./core/util/Defaults.js";
|
|
|
|
import { DrawClass } from "./core/util/Draw.js";
|
2019-10-30 18:49:02 +00:00
|
|
|
|
2024-05-03 18:31:14 +00:00
|
|
|
type ClassesWithoutSingletons = Omit<
|
|
|
|
typeof Classes,
|
|
|
|
"Transport" | "Destination" | "Draw"
|
|
|
|
>;
|
2019-08-30 16:03:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The exported Tone object. Contains all of the classes that default
|
|
|
|
* to the same context and contains a singleton Transport and Destination node.
|
|
|
|
*/
|
2021-02-26 04:02:05 +00:00
|
|
|
type ToneObject = {
|
2024-04-28 17:05:26 +00:00
|
|
|
Transport: TransportClass;
|
|
|
|
Destination: DestinationClass;
|
|
|
|
Listener: ListenerClass;
|
|
|
|
Draw: DrawClass;
|
2019-10-30 18:49:02 +00:00
|
|
|
context: Context;
|
2019-08-30 16:03:08 +00:00
|
|
|
now: () => number;
|
2019-10-30 18:49:02 +00:00
|
|
|
immediate: () => number;
|
|
|
|
} & ClassesWithoutSingletons;
|
2019-08-30 16:03:08 +00:00
|
|
|
|
2019-08-30 16:28:45 +00:00
|
|
|
/**
|
|
|
|
* Bind the TimeBaseClass to the context
|
|
|
|
*/
|
|
|
|
function bindTypeClass(context: Context, type) {
|
2023-01-26 18:31:38 +00:00
|
|
|
return (...args: unknown[]) => new type(context, ...args);
|
2019-08-30 16:28:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 16:03:08 +00:00
|
|
|
/**
|
|
|
|
* Return an object with all of the classes bound to the passed in context
|
|
|
|
* @param context The context to bind all of the nodes to
|
|
|
|
*/
|
2021-02-26 04:02:05 +00:00
|
|
|
export function fromContext(context: Context): ToneObject {
|
2019-10-30 18:49:02 +00:00
|
|
|
const classesWithContext: Partial<ClassesWithoutSingletons> = {};
|
2024-05-03 18:31:14 +00:00
|
|
|
Object.keys(
|
|
|
|
omitFromObject(Classes, ["Transport", "Destination", "Draw"])
|
|
|
|
).map((key) => {
|
2019-08-30 16:03:08 +00:00
|
|
|
const cls = Classes[key];
|
|
|
|
if (isDefined(cls) && isFunction(cls.getDefaults)) {
|
2019-10-30 18:49:02 +00:00
|
|
|
classesWithContext[key] = class ToneFromContextNode extends cls {
|
2019-08-30 16:03:08 +00:00
|
|
|
get defaultContext(): Context {
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// otherwise just copy it over
|
2019-10-30 18:49:02 +00:00
|
|
|
classesWithContext[key] = Classes[key];
|
2019-08-30 16:03:08 +00:00
|
|
|
}
|
|
|
|
});
|
2019-10-30 18:49:02 +00:00
|
|
|
|
2021-02-26 04:02:05 +00:00
|
|
|
const toneFromContext: ToneObject = {
|
2019-10-30 18:49:02 +00:00
|
|
|
...(classesWithContext as ClassesWithoutSingletons),
|
2019-10-31 18:54:49 +00:00
|
|
|
now: context.now.bind(context),
|
|
|
|
immediate: context.immediate.bind(context),
|
2019-10-30 18:49:02 +00:00
|
|
|
Transport: context.transport,
|
|
|
|
Destination: context.destination,
|
2019-12-22 02:01:45 +00:00
|
|
|
Listener: context.listener,
|
2019-10-30 18:49:02 +00:00
|
|
|
Draw: context.draw,
|
|
|
|
context,
|
|
|
|
// the type functions
|
|
|
|
Midi: bindTypeClass(context, MidiClass),
|
|
|
|
Time: bindTypeClass(context, TimeClass),
|
|
|
|
Frequency: bindTypeClass(context, FrequencyClass),
|
|
|
|
Ticks: bindTypeClass(context, TicksClass),
|
|
|
|
TransportTime: bindTypeClass(context, TransportTimeClass),
|
|
|
|
};
|
2019-08-30 16:28:45 +00:00
|
|
|
// return the object
|
2019-10-30 18:49:02 +00:00
|
|
|
return toneFromContext;
|
2019-08-30 16:03:08 +00:00
|
|
|
}
|