2019-07-25 01:53:35 +00:00
|
|
|
import { TicksClass } from "../core/type/Ticks";
|
2019-07-30 19:35:27 +00:00
|
|
|
import { NormalRange, Positive, Seconds, Ticks, Time, TransportTime } from "../core/type/Units";
|
2019-07-25 01:53:35 +00:00
|
|
|
import { omitFromObject, optionsFromArguments } from "../core/util/Defaults";
|
|
|
|
import { isArray, isString } from "../core/util/TypeCheck";
|
|
|
|
import { Part } from "./Part";
|
|
|
|
import { ToneEvent, ToneEventCallback, ToneEventOptions } from "./ToneEvent";
|
|
|
|
|
2019-07-26 15:50:29 +00:00
|
|
|
type SequenceEventDescription<T> = Array<T | T[]>;
|
2019-07-25 01:53:35 +00:00
|
|
|
|
2019-07-26 15:50:29 +00:00
|
|
|
interface SequenceOptions<T> extends Omit<ToneEventOptions<T>, "value"> {
|
2019-07-25 01:53:35 +00:00
|
|
|
loopStart: number;
|
|
|
|
loopEnd: number;
|
|
|
|
subdivision: Time;
|
2019-07-26 15:50:29 +00:00
|
|
|
events: SequenceEventDescription<T>;
|
2019-07-25 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A sequence is an alternate notation of a part. Instead
|
|
|
|
* of passing in an array of [time, event] pairs, pass
|
|
|
|
* in an array of events which will be spaced at the
|
|
|
|
* given subdivision. Sub-arrays will subdivide that beat
|
|
|
|
* by the number of items are in the array.
|
|
|
|
* Sequence notation inspiration from [Tidal](http://yaxu.org/tidal/)
|
|
|
|
* @example
|
2019-10-24 22:01:27 +00:00
|
|
|
* import { Sequence, Synth, Transport } from "tone";
|
|
|
|
* const synth = new Synth().toDestination();
|
|
|
|
* const seq = new Sequence((time, note) => {
|
|
|
|
* synth.triggerAttackRelease(note, 0.1, time);
|
|
|
|
* // subdivisions are given as subarrays
|
|
|
|
* }, ["C4", ["E4", "D4", "E4"], "G4", ["A4", "G4"]]).start(0);
|
|
|
|
* Transport.start();
|
2019-09-16 14:49:30 +00:00
|
|
|
* @category Event
|
2019-07-25 01:53:35 +00:00
|
|
|
*/
|
2019-07-26 15:50:29 +00:00
|
|
|
export class Sequence<ValueType = any> extends ToneEvent<ValueType> {
|
2019-07-25 01:53:35 +00:00
|
|
|
|
2019-09-04 23:18:44 +00:00
|
|
|
readonly name: string = "Sequence";
|
2019-07-25 01:53:35 +00:00
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* The subdivison of each note
|
2019-07-25 01:53:35 +00:00
|
|
|
*/
|
|
|
|
private _subdivision: Ticks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The object responsible for scheduling all of the events
|
|
|
|
*/
|
|
|
|
private _part: Part = new Part({
|
|
|
|
callback: this._seqCallback.bind(this),
|
|
|
|
context: this.context,
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* private reference to all of the sequence proxies
|
|
|
|
*/
|
2019-07-26 15:50:29 +00:00
|
|
|
private _events: ValueType[] = [];
|
2019-07-25 01:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The proxied array
|
|
|
|
*/
|
2019-07-26 15:50:29 +00:00
|
|
|
private _eventsArray: ValueType[] = [];
|
2019-07-25 01:53:35 +00:00
|
|
|
|
2019-08-27 15:58:40 +00:00
|
|
|
/**
|
|
|
|
* @param callback The callback to invoke with every note
|
|
|
|
* @param sequence The sequence
|
|
|
|
* @param subdivision The subdivision between which events are placed.
|
|
|
|
*/
|
2019-07-25 01:53:35 +00:00
|
|
|
constructor(
|
2019-07-26 15:50:29 +00:00
|
|
|
callback?: ToneEventCallback<ValueType>,
|
|
|
|
events?: SequenceEventDescription<ValueType>,
|
2019-07-25 01:53:35 +00:00
|
|
|
subdivision?: Time,
|
|
|
|
);
|
2019-07-26 15:50:29 +00:00
|
|
|
constructor(options?: Partial<SequenceOptions<ValueType>>);
|
2019-07-25 01:53:35 +00:00
|
|
|
constructor() {
|
|
|
|
|
|
|
|
super(optionsFromArguments(Sequence.getDefaults(), arguments, ["callback", "events", "subdivision"]));
|
|
|
|
const options = optionsFromArguments(Sequence.getDefaults(), arguments, ["callback", "events", "subdivision"]);
|
|
|
|
|
|
|
|
this._subdivision = this.toTicks(options.subdivision);
|
|
|
|
|
|
|
|
this.events = options.events;
|
|
|
|
|
|
|
|
// set all of the values
|
|
|
|
this.loop = options.loop;
|
|
|
|
this.loopStart = options.loopStart;
|
|
|
|
this.loopEnd = options.loopEnd;
|
|
|
|
this.playbackRate = options.playbackRate;
|
|
|
|
this.probability = options.probability;
|
|
|
|
this.humanize = options.humanize;
|
|
|
|
this.mute = options.mute;
|
|
|
|
this.playbackRate = options.playbackRate;
|
|
|
|
}
|
|
|
|
|
2019-07-26 15:50:29 +00:00
|
|
|
static getDefaults(): SequenceOptions<any> {
|
2019-09-14 22:12:44 +00:00
|
|
|
return Object.assign(omitFromObject(ToneEvent.getDefaults(), ["value"]), {
|
2019-07-25 01:53:35 +00:00
|
|
|
events: [],
|
|
|
|
loop: true,
|
2019-09-14 22:12:44 +00:00
|
|
|
loopEnd: 0,
|
2019-07-25 01:53:35 +00:00
|
|
|
loopStart: 0,
|
|
|
|
subdivision: "8n",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal callback for when an event is invoked
|
|
|
|
*/
|
|
|
|
private _seqCallback(time: Seconds, value: any): void {
|
|
|
|
if (value !== null) {
|
|
|
|
this.callback(time, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The sequence
|
|
|
|
*/
|
|
|
|
get events(): any[] {
|
|
|
|
return this._events;
|
|
|
|
}
|
|
|
|
set events(s) {
|
|
|
|
this.clear();
|
|
|
|
this._eventsArray = s;
|
|
|
|
this._events = this._createSequence(this._eventsArray);
|
|
|
|
this._eventsUpdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* Start the part at the given time.
|
2019-08-30 16:06:38 +00:00
|
|
|
* @param time When to start the part.
|
|
|
|
* @param offset The offset index to start at
|
2019-07-25 01:53:35 +00:00
|
|
|
*/
|
|
|
|
start(time?: TransportTime, offset?: number): this {
|
|
|
|
this._part.start(time, offset ? this._indexTime(offset) : offset);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* Stop the part at the given time.
|
2019-08-30 16:06:38 +00:00
|
|
|
* @param time When to stop the part.
|
2019-07-25 01:53:35 +00:00
|
|
|
*/
|
|
|
|
stop(time?: TransportTime): this {
|
|
|
|
this._part.stop(time);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* The subdivision of the sequence. This can only be
|
|
|
|
* set in the constructor. The subdivision is the
|
|
|
|
* interval between successive steps.
|
2019-07-25 01:53:35 +00:00
|
|
|
*/
|
|
|
|
get subdivision(): Seconds {
|
|
|
|
return new TicksClass(this.context, this._subdivision).toSeconds();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a sequence proxy which can be monitored to create subsequences
|
|
|
|
*/
|
|
|
|
private _createSequence(array: any[]): any[] {
|
|
|
|
return new Proxy(array, {
|
|
|
|
get: (target: any[], property: PropertyKey): any => {
|
|
|
|
// property is index in this case
|
|
|
|
return target[property];
|
|
|
|
},
|
|
|
|
set: (target: any[], property: PropertyKey, value: any): boolean => {
|
|
|
|
if (isString(property) && isFinite(parseInt(property, 10))) {
|
|
|
|
if (isArray(value)) {
|
|
|
|
target[property] = this._createSequence(value);
|
|
|
|
} else {
|
|
|
|
target[property] = value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target[property] = value;
|
|
|
|
}
|
|
|
|
this._eventsUpdated();
|
|
|
|
// return true to accept the changes
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the sequence has changed, all of the events need to be recreated
|
|
|
|
*/
|
|
|
|
private _eventsUpdated(): void {
|
|
|
|
this._part.clear();
|
|
|
|
this._rescheduleSequence(this._eventsArray, this._subdivision, this.startOffset);
|
|
|
|
// update the loopEnd
|
|
|
|
this.loopEnd = this.loopEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* reschedule all of the events that need to be rescheduled
|
|
|
|
*/
|
|
|
|
private _rescheduleSequence(sequence: any[], subdivision: Ticks, startOffset: Ticks): void {
|
|
|
|
sequence.forEach((value, index) => {
|
|
|
|
const eventOffset = index * (subdivision) + startOffset;
|
|
|
|
if (isArray(value)) {
|
|
|
|
this._rescheduleSequence(value, subdivision / value.length, eventOffset);
|
|
|
|
} else {
|
|
|
|
const startTime = new TicksClass(this.context, eventOffset, "i").toSeconds();
|
|
|
|
this._part.add(startTime, value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* Get the time of the index given the Sequence's subdivision
|
2019-08-30 16:06:38 +00:00
|
|
|
* @param index
|
|
|
|
* @return The time of that index
|
2019-07-25 01:53:35 +00:00
|
|
|
*/
|
|
|
|
private _indexTime(index: number): Seconds {
|
|
|
|
return new TicksClass(this.context, index * (this._subdivision) + this.startOffset).toSeconds();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all of the events
|
|
|
|
*/
|
|
|
|
clear(): this {
|
|
|
|
this._part.clear();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose(): this {
|
|
|
|
super.dispose();
|
|
|
|
this._part.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-09-14 22:06:46 +00:00
|
|
|
//-------------------------------------
|
2019-07-25 01:53:35 +00:00
|
|
|
// PROXY CALLS
|
2019-09-14 22:06:46 +00:00
|
|
|
//-------------------------------------
|
2019-07-25 01:53:35 +00:00
|
|
|
|
|
|
|
get loop(): boolean | number {
|
|
|
|
return this._part.loop;
|
|
|
|
}
|
|
|
|
set loop(l) {
|
|
|
|
if (this._part) {
|
|
|
|
this._part.loop = l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The index at which the sequence should start looping
|
|
|
|
*/
|
|
|
|
get loopStart(): number {
|
|
|
|
return this._loopStart;
|
|
|
|
}
|
|
|
|
set loopStart(index) {
|
|
|
|
this._loopStart = index;
|
|
|
|
if (this._part) {
|
|
|
|
this._part.loopStart = this._indexTime(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The index at which the sequence should end looping
|
|
|
|
*/
|
|
|
|
get loopEnd(): number {
|
|
|
|
return this._loopEnd;
|
|
|
|
}
|
|
|
|
set loopEnd(index) {
|
|
|
|
this._loopEnd = index;
|
|
|
|
if (this._part) {
|
|
|
|
if (index === 0) {
|
|
|
|
this._part.loopEnd = this._indexTime(this._eventsArray.length);
|
|
|
|
} else {
|
|
|
|
this._part.loopEnd = this._indexTime(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get startOffset(): Ticks {
|
|
|
|
return this._part.startOffset;
|
|
|
|
}
|
|
|
|
set startOffset(start) {
|
|
|
|
if (this._part) {
|
|
|
|
this._part.startOffset = start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get playbackRate(): Positive {
|
|
|
|
return this._part.playbackRate;
|
|
|
|
}
|
|
|
|
set playbackRate(rate) {
|
|
|
|
if (this._part) {
|
|
|
|
this._part.playbackRate = rate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get probability(): NormalRange {
|
|
|
|
return this._part.probability;
|
|
|
|
}
|
|
|
|
set probability(prob) {
|
|
|
|
if (this._part) {
|
|
|
|
this._part.probability = prob;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get humanize(): boolean | Time {
|
|
|
|
return this._part.humanize;
|
|
|
|
}
|
|
|
|
set humanize(variation) {
|
|
|
|
if (this._part) {
|
|
|
|
this._part.humanize = variation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of scheduled events
|
|
|
|
*/
|
|
|
|
get length(): number {
|
|
|
|
return this._part.length;
|
|
|
|
}
|
|
|
|
}
|