Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small doc updates and tweaks #1239

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions Tone/core/context/ToneWithContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ import { TimeClass } from "../type/Time";
import { TransportTimeClass } from "../type/TransportTime";
import { Frequency, Hertz, Seconds, Ticks, Time } from "../type/Units";
import { assertUsedScheduleTime } from "../util/Debug";
import { getDefaultsFromInstance, optionsFromArguments } from "../util/Defaults";
import {
getDefaultsFromInstance,
optionsFromArguments,
} from "../util/Defaults";
import { RecursivePartial } from "../util/Interface";
import { isArray, isBoolean, isDefined, isNumber, isString, isUndef } from "../util/TypeCheck";
import {
isArray,
isBoolean,
isDefined,
isNumber,
isString,
isUndef,
} from "../util/TypeCheck";
import { BaseContext } from "./BaseContext";
import type { TransportClass } from "../clock/Transport";

/**
* A unit which process audio
Expand All @@ -20,8 +31,9 @@ export interface ToneWithContextOptions {
/**
* The Base class for all nodes that have an AudioContext.
*/
export abstract class ToneWithContext<Options extends ToneWithContextOptions> extends Tone {

export abstract class ToneWithContext<
Options extends ToneWithContextOptions
> extends Tone {
/**
* The context belonging to the node.
*/
Expand All @@ -37,11 +49,15 @@ export abstract class ToneWithContext<Options extends ToneWithContextOptions> ex
/**
* Pass in a constructor as the first argument
*/
constructor(context?: BaseContext)
constructor(context?: BaseContext);
constructor(options?: Partial<ToneWithContextOptions>);
constructor() {
super();
const options = optionsFromArguments(ToneWithContext.getDefaults(), arguments, ["context"]);
const options = optionsFromArguments(
ToneWithContext.getDefaults(),
arguments,
["context"]
);
if (this.defaultContext) {
this.context = this.defaultContext;
} else {
Expand Down Expand Up @@ -94,7 +110,7 @@ export abstract class ToneWithContext<Options extends ToneWithContextOptions> ex
}

/**
* Convert the incoming time to seconds.
* Convert the incoming time to seconds.
* This is calculated against the current {@link TransportClass} bpm
* @example
* const gain = new Tone.Gain();
Expand Down Expand Up @@ -137,7 +153,7 @@ export abstract class ToneWithContext<Options extends ToneWithContextOptions> ex
protected _getPartialProperties(props: Options): Partial<Options> {
const options = this.get();
// remove attributes from the prop that are not in the partial
Object.keys(options).forEach(name => {
Object.keys(options).forEach((name) => {
if (isUndef(props[name])) {
delete options[name];
}
Expand All @@ -153,15 +169,26 @@ export abstract class ToneWithContext<Options extends ToneWithContextOptions> ex
*/
get(): Options {
const defaults = getDefaultsFromInstance(this) as Options;
Object.keys(defaults).forEach(attribute => {
Object.keys(defaults).forEach((attribute) => {
if (Reflect.has(this, attribute)) {
const member = this[attribute];
if (isDefined(member) && isDefined(member.value) && isDefined(member.setValueAtTime)) {
if (
isDefined(member) &&
isDefined(member.value) &&
isDefined(member.setValueAtTime)
) {
defaults[attribute] = member.value;
} else if (member instanceof ToneWithContext) {
defaults[attribute] = member._getPartialProperties(defaults[attribute]);
defaults[attribute] = member._getPartialProperties(
defaults[attribute]
);
// otherwise make sure it's a serializable type
} else if (isArray(member) || isNumber(member) || isString(member) || isBoolean(member)) {
} else if (
isArray(member) ||
isNumber(member) ||
isString(member) ||
isBoolean(member)
) {
defaults[attribute] = member;
} else {
// remove all undefined and unserializable attributes
Expand All @@ -186,9 +213,13 @@ export abstract class ToneWithContext<Options extends ToneWithContextOptions> ex
* player.autostart = true;
*/
set(props: RecursivePartial<Options>): this {
Object.keys(props).forEach(attribute => {
Object.keys(props).forEach((attribute) => {
if (Reflect.has(this, attribute) && isDefined(this[attribute])) {
if (this[attribute] && isDefined(this[attribute].value) && isDefined(this[attribute].setValueAtTime)) {
if (
this[attribute] &&
isDefined(this[attribute].value) &&
isDefined(this[attribute].setValueAtTime)
) {
// small optimization
if (this[attribute].value !== props[attribute]) {
this[attribute].value = props[attribute];
Expand Down
4 changes: 2 additions & 2 deletions Tone/event/Part.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class Part<ValueType = any> extends ToneEvent<ValueType> {

/**
* @param callback The callback to invoke on each event
* @param events the array of events
* @param value the array of events
*/
constructor(callback?: ToneEventCallback<CallbackType<ValueType>>, value?: ValueType[]);
constructor(options?: Partial<PartOptions<ValueType>>);
Expand Down Expand Up @@ -209,7 +209,7 @@ export class Part<ValueType = any> extends ToneEvent<ValueType> {
* Add a an event to the part.
* @param time The time the note should start. If an object is passed in, it should
* have a 'time' attribute and the rest of the object will be used as the 'value'.
* @param value
* @param value Any value to add to the timeline
* @example
* const part = new Tone.Part();
* part.add("1m", "C#+11");
Expand Down
4 changes: 2 additions & 2 deletions Tone/event/Sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isArray, isString } from "../core/util/TypeCheck";
import { Part } from "./Part";
import { ToneEvent, ToneEventCallback, ToneEventOptions } from "./ToneEvent";

type SequenceEventDescription<T> = Array<T | Array<T | Array<T | Array<T | Array<T | T[]>>>>>;
type SequenceEventDescription<T> = Array<T | SequenceEventDescription<T>>;

interface SequenceOptions<T> extends Omit<ToneEventOptions<T>, "value"> {
loopStart: number;
Expand Down Expand Up @@ -59,7 +59,7 @@ export class Sequence<ValueType = any> extends ToneEvent<ValueType> {

/**
* @param callback The callback to invoke with every note
* @param sequence The sequence
* @param events The sequence of events
* @param subdivision The subdivision between which events are placed.
*/
constructor(
Expand Down
22 changes: 13 additions & 9 deletions Tone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import { ToneAudioBuffer } from "./core/context/ToneAudioBuffer";
export { start } from "./core/Global";
import { Seconds } from "./core/type/Units";
export { supported } from "./core/context/AudioContext";
import type { TransportClass } from "./core/clock/Transport";
import type { DestinationClass } from "./core/context/Destination";
import type { DrawClass } from "./core/util/Draw";
import type { ListenerClass } from "./core/context/Listener";

/**
* The current audio context time of the global {@link BaseContext}.
* The current audio context time of the global {@link BaseContext}.
* @see {@link Context.now}
* @category Core
*/
Expand Down Expand Up @@ -39,7 +43,7 @@ export const Transport = getContext().transport;
* @see {@link TransportClass}
* @category Core
*/
export function getTransport(): import("./core/clock/Transport").TransportClass {
export function getTransport(): TransportClass {
return getContext().transport;
}

Expand All @@ -61,7 +65,7 @@ export const Master = getContext().destination;
* @see {@link DestinationClass}
* @category Core
*/
export function getDestination(): import("./core/context/Destination").DestinationClass {
export function getDestination(): DestinationClass {
return getContext().destination;
}

Expand All @@ -76,25 +80,25 @@ export const Listener = getContext().listener;
* The {@link ListenerClass} belonging to the global Tone.js Context.
* @category Core
*/
export function getListener(): import("./core/context/Listener").ListenerClass {
export function getListener(): ListenerClass {
return getContext().listener;
}

/**
* Draw is used to synchronize the draw frame with the Transport's callbacks.
* Draw is used to synchronize the draw frame with the Transport's callbacks.
* @see {@link DrawClass}
* @category Core
* @deprecated Use {@link getDraw} instead
*/
export const Draw = getContext().draw;

/**
* Get the singleton attached to the global context.
* Draw is used to synchronize the draw frame with the Transport's callbacks.
* Get the singleton attached to the global context.
* Draw is used to synchronize the draw frame with the Transport's callbacks.
* @see {@link DrawClass}
* @category Core
*/
export function getDraw(): import("./core/util/Draw").DrawClass {
export function getDraw(): DrawClass {
return getContext().draw;
}

Expand All @@ -106,7 +110,7 @@ export function getDraw(): import("./core/util/Draw").DrawClass {
export const context = getContext();

/**
* Promise which resolves when all of the loading promises are resolved.
* Promise which resolves when all of the loading promises are resolved.
* Alias for static {@link ToneAudioBuffer.loaded} method.
* @category Core
*/
Expand Down
1 change: 1 addition & 0 deletions Tone/signal/SyncedSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { optionsFromArguments } from "../core/util/Defaults";
import { TransportTimeClass } from "../core/type/TransportTime";
import { ToneConstantSource } from "./ToneConstantSource";
import { OutputNode } from "../core/context/ToneAudioNode";
import type { TransportClass } from "../core/clock/Transport";

/**
* Adds the ability to synchronize the signal to the {@link TransportClass}
Expand Down
2 changes: 1 addition & 1 deletion Tone/signal/WaveShaper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class WaveShaper extends SignalOperator<WaveShaperOptions> {
* signal is an AudioRange [-1, 1] value and the output
* signal can take on any numerical values.
*
* @param bufferLen The length of the WaveShaperNode buffer.
* @param length The length of the WaveShaperNode buffer.
*/
constructor(mapping?: WaveShaperMapping, length?: number);
constructor(options?: Partial<WaveShaperOptions>);
Expand Down