2024-05-03 18:31:14 +00:00
|
|
|
import { FrequencyClass } from "../core/type/Frequency.js";
|
|
|
|
import {
|
|
|
|
Cents,
|
|
|
|
Frequency,
|
|
|
|
NormalRange,
|
|
|
|
Seconds,
|
|
|
|
Time,
|
|
|
|
} from "../core/type/Units.js";
|
|
|
|
import { optionsFromArguments } from "../core/util/Defaults.js";
|
|
|
|
import { noOp } from "../core/util/Interface.js";
|
|
|
|
import { Instrument, InstrumentOptions } from "../instrument/Instrument.js";
|
|
|
|
import { Signal } from "../signal/Signal.js";
|
|
|
|
import { timeRange } from "../core/util/Decorator.js";
|
2019-07-18 18:07:25 +00:00
|
|
|
|
2019-08-12 04:13:13 +00:00
|
|
|
type onSilenceCallback = (instrument: Monophonic<any>) => void;
|
|
|
|
|
2019-07-18 18:07:25 +00:00
|
|
|
export interface MonophonicOptions extends InstrumentOptions {
|
|
|
|
portamento: Seconds;
|
2019-08-12 04:13:13 +00:00
|
|
|
onsilence: onSilenceCallback;
|
2019-09-04 00:09:46 +00:00
|
|
|
detune: Cents;
|
2019-07-18 18:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-18 18:57:13 +00:00
|
|
|
* Abstract base class for other monophonic instruments to extend.
|
2019-07-18 18:07:25 +00:00
|
|
|
*/
|
2024-05-03 18:31:14 +00:00
|
|
|
export abstract class Monophonic<
|
|
|
|
Options extends MonophonicOptions,
|
|
|
|
> extends Instrument<Options> {
|
2019-07-18 18:07:25 +00:00
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* The glide time between notes.
|
2019-07-18 18:07:25 +00:00
|
|
|
*/
|
2019-12-17 17:42:40 +00:00
|
|
|
@timeRange(0)
|
2019-07-18 18:07:25 +00:00
|
|
|
portamento: Seconds;
|
|
|
|
|
|
|
|
/**
|
2019-08-12 04:13:13 +00:00
|
|
|
* Invoked when the release has finished and the output is silent.
|
2019-07-18 18:07:25 +00:00
|
|
|
*/
|
2019-08-12 04:13:13 +00:00
|
|
|
onsilence: onSilenceCallback;
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The instrument's frequency signal.
|
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
abstract readonly frequency: Signal<"frequency">;
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The instrument's detune control signal.
|
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
abstract readonly detune: Signal<"cents">;
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
constructor(options?: Partial<MonophonicOptions>);
|
|
|
|
constructor() {
|
2024-05-03 18:31:14 +00:00
|
|
|
const options = optionsFromArguments(
|
|
|
|
Monophonic.getDefaults(),
|
|
|
|
arguments
|
|
|
|
);
|
2024-05-06 14:55:55 +00:00
|
|
|
super(options);
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
this.portamento = options.portamento;
|
2019-08-12 04:13:13 +00:00
|
|
|
this.onsilence = options.onsilence;
|
2019-07-18 18:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getDefaults(): MonophonicOptions {
|
|
|
|
return Object.assign(Instrument.getDefaults(), {
|
2019-09-04 00:09:46 +00:00
|
|
|
detune: 0,
|
2019-08-12 04:13:13 +00:00
|
|
|
onsilence: noOp,
|
2019-07-18 18:07:25 +00:00
|
|
|
portamento: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-12 04:13:13 +00:00
|
|
|
* Trigger the attack of the note optionally with a given velocity.
|
|
|
|
* @param note The note to trigger.
|
|
|
|
* @param time When the note should start.
|
2021-03-30 23:46:02 +00:00
|
|
|
* @param velocity The velocity determines how "loud" the note will be.
|
2019-08-12 04:13:13 +00:00
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* const synth = new Tone.Synth().toDestination();
|
2019-10-24 22:01:27 +00:00
|
|
|
* // trigger the note a half second from now at half velocity
|
2019-07-18 18:07:25 +00:00
|
|
|
* synth.triggerAttack("C4", "+0.5", 0.5);
|
|
|
|
*/
|
2024-05-03 18:31:14 +00:00
|
|
|
triggerAttack(
|
|
|
|
note: Frequency | FrequencyClass,
|
|
|
|
time?: Time,
|
|
|
|
velocity: NormalRange = 1
|
|
|
|
): this {
|
2019-07-18 18:07:25 +00:00
|
|
|
this.log("triggerAttack", note, time, velocity);
|
2019-07-30 19:35:27 +00:00
|
|
|
const seconds = this.toSeconds(time);
|
|
|
|
this._triggerEnvelopeAttack(seconds, velocity);
|
|
|
|
this.setNote(note, seconds);
|
2019-07-18 18:07:25 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-30 23:46:02 +00:00
|
|
|
* Trigger the release portion of the envelope.
|
|
|
|
* @param time If no time is given, the release happens immediately.
|
2019-08-12 04:13:13 +00:00
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* const synth = new Tone.Synth().toDestination();
|
2019-10-24 22:01:27 +00:00
|
|
|
* synth.triggerAttack("C4");
|
|
|
|
* // trigger the release a second from now
|
|
|
|
* synth.triggerRelease("+1");
|
2019-07-18 18:07:25 +00:00
|
|
|
*/
|
|
|
|
triggerRelease(time?: Time): this {
|
|
|
|
this.log("triggerRelease", time);
|
2019-07-30 19:35:27 +00:00
|
|
|
const seconds = this.toSeconds(time);
|
|
|
|
this._triggerEnvelopeRelease(seconds);
|
2019-07-18 18:07:25 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-12 04:13:13 +00:00
|
|
|
* Internal method which starts the envelope attack
|
2019-07-18 18:07:25 +00:00
|
|
|
*/
|
2024-05-03 18:31:14 +00:00
|
|
|
protected abstract _triggerEnvelopeAttack(
|
|
|
|
time: Seconds,
|
|
|
|
velocity: NormalRange
|
|
|
|
): void;
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-12 04:13:13 +00:00
|
|
|
* Internal method which starts the envelope release
|
2019-07-18 18:07:25 +00:00
|
|
|
*/
|
|
|
|
protected abstract _triggerEnvelopeRelease(time: Seconds): void;
|
|
|
|
|
2019-08-12 04:21:34 +00:00
|
|
|
/**
|
|
|
|
* Get the level of the output at the given time. Measures
|
|
|
|
* the envelope(s) value at the time.
|
|
|
|
* @param time The time to query the envelope value
|
|
|
|
* @return The output level between 0-1
|
|
|
|
*/
|
2019-11-03 22:41:47 +00:00
|
|
|
abstract getLevelAtTime(time: Time): NormalRange;
|
2019-08-12 04:21:34 +00:00
|
|
|
|
2019-07-18 18:07:25 +00:00
|
|
|
/**
|
2019-08-12 04:13:13 +00:00
|
|
|
* Set the note at the given time. If no time is given, the note
|
|
|
|
* will set immediately.
|
|
|
|
* @param note The note to change to.
|
|
|
|
* @param time The time when the note should be set.
|
2019-07-18 18:07:25 +00:00
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* const synth = new Tone.Synth().toDestination();
|
2019-10-24 22:01:27 +00:00
|
|
|
* synth.triggerAttack("C4");
|
|
|
|
* // change to F#6 in one quarter note from now.
|
2019-07-18 18:07:25 +00:00
|
|
|
* synth.setNote("F#6", "+4n");
|
|
|
|
*/
|
|
|
|
setNote(note: Frequency | FrequencyClass, time?: Time): this {
|
2019-07-30 19:35:27 +00:00
|
|
|
const computedTime = this.toSeconds(time);
|
2024-05-03 18:31:14 +00:00
|
|
|
const computedFrequency =
|
|
|
|
note instanceof FrequencyClass ? note.toFrequency() : note;
|
2019-07-30 19:35:27 +00:00
|
|
|
if (this.portamento > 0 && this.getLevelAtTime(computedTime) > 0.05) {
|
2019-07-18 18:07:25 +00:00
|
|
|
const portTime = this.toSeconds(this.portamento);
|
2024-05-03 18:31:14 +00:00
|
|
|
this.frequency.exponentialRampTo(
|
|
|
|
computedFrequency,
|
|
|
|
portTime,
|
|
|
|
computedTime
|
|
|
|
);
|
2019-07-18 18:07:25 +00:00
|
|
|
} else {
|
2019-07-30 19:35:27 +00:00
|
|
|
this.frequency.setValueAtTime(computedFrequency, computedTime);
|
2019-07-18 18:07:25 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|