2019-05-23 18:00:49 +00:00
|
|
|
import { AbstractParam } from "../context/AbstractParam";
|
2019-04-12 14:37:47 +00:00
|
|
|
import { dbToGain, gainToDb } from "../type/Conversions";
|
2019-10-31 19:43:16 +00:00
|
|
|
import { AudioRange, Decibels, Frequency, NormalRange, Positive, Time, UnitMap, UnitName } from "../type/Units";
|
2019-08-19 17:01:37 +00:00
|
|
|
import { isAudioParam } from "../util/AdvancedTypeCheck";
|
2019-05-23 18:00:49 +00:00
|
|
|
import { optionsFromArguments } from "../util/Defaults";
|
|
|
|
import { Timeline } from "../util/Timeline";
|
2019-08-19 17:01:37 +00:00
|
|
|
import { isDefined } from "../util/TypeCheck";
|
2019-05-23 18:00:49 +00:00
|
|
|
import { ToneWithContext, ToneWithContextOptions } from "./ToneWithContext";
|
2019-10-28 20:41:01 +00:00
|
|
|
import { EQ } from "../util/Math";
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
export interface ParamOptions<TypeName extends UnitName> extends ToneWithContextOptions {
|
|
|
|
units: TypeName;
|
|
|
|
value?: UnitMap[TypeName];
|
|
|
|
param: AudioParam | Param<TypeName>;
|
2019-04-12 14:37:47 +00:00
|
|
|
convert: boolean;
|
2019-09-24 21:12:12 +00:00
|
|
|
minValue?: number;
|
|
|
|
maxValue?: number;
|
2019-09-29 16:20:54 +00:00
|
|
|
swappable?: boolean;
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the possible automation types
|
|
|
|
*/
|
2019-09-14 23:55:39 +00:00
|
|
|
type AutomationType = "linearRampToValueAtTime" | "exponentialRampToValueAtTime" | "setValueAtTime" | "setTargetAtTime" | "cancelScheduledValues";
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-09-06 20:17:49 +00:00
|
|
|
interface TargetAutomationEvent {
|
|
|
|
type: "setTargetAtTime";
|
|
|
|
time: number;
|
|
|
|
value: number;
|
|
|
|
constant: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface NormalAutomationEvent {
|
|
|
|
type: Exclude<AutomationType, "setTargetAtTime">;
|
2019-04-12 14:37:47 +00:00
|
|
|
time: number;
|
|
|
|
value: number;
|
|
|
|
}
|
2019-09-06 20:17:49 +00:00
|
|
|
/**
|
|
|
|
* The events on the automation
|
|
|
|
*/
|
|
|
|
export type AutomationEvent = NormalAutomationEvent | TargetAutomationEvent;
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-21 20:01:12 +00:00
|
|
|
* Param wraps the native Web Audio's AudioParam to provide
|
2019-04-12 14:37:47 +00:00
|
|
|
* additional unit conversion functionality. It also
|
|
|
|
* serves as a base-class for classes which have a single,
|
|
|
|
* automatable parameter.
|
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
export class Param<TypeName extends UnitName = "number">
|
|
|
|
extends ToneWithContext<ParamOptions<TypeName>>
|
|
|
|
implements AbstractParam<TypeName> {
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-08-21 20:01:12 +00:00
|
|
|
readonly name: string = "Param";
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-09-29 16:20:54 +00:00
|
|
|
readonly input: GainNode | AudioParam;
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-07-15 19:37:25 +00:00
|
|
|
readonly units: UnitName;
|
2019-04-12 14:37:47 +00:00
|
|
|
convert: boolean;
|
2019-11-17 18:09:19 +00:00
|
|
|
overridden = false;
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The timeline which tracks all of the automations.
|
|
|
|
*/
|
2019-05-23 18:00:49 +00:00
|
|
|
protected _events: Timeline<AutomationEvent>;
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* The native parameter to control
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
|
|
|
protected _param: AudioParam;
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* The default value before anything is assigned
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
2019-05-23 18:00:49 +00:00
|
|
|
protected _initialValue: number;
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* The minimum output value
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
2019-08-19 17:01:37 +00:00
|
|
|
private _minOutput = 1e-7;
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-09-24 21:12:12 +00:00
|
|
|
/**
|
|
|
|
* Private reference to the min and max values if passed into the constructor
|
|
|
|
*/
|
|
|
|
private readonly _minValue?: number;
|
|
|
|
private readonly _maxValue?: number;
|
|
|
|
|
2019-09-29 16:20:54 +00:00
|
|
|
/**
|
|
|
|
* If the underlying AudioParam can be swapped out
|
|
|
|
* using the setParam method.
|
|
|
|
*/
|
|
|
|
protected readonly _swappable: boolean;
|
|
|
|
|
2019-08-27 17:02:31 +00:00
|
|
|
/**
|
|
|
|
* @param param The AudioParam to wrap
|
|
|
|
* @param units The unit name
|
|
|
|
* @param convert Whether or not to convert the value to the target units
|
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
constructor(param: AudioParam, units?: TypeName, convert?: boolean);
|
|
|
|
constructor(options: Partial<ParamOptions<TypeName>>);
|
2019-04-12 14:37:47 +00:00
|
|
|
constructor() {
|
|
|
|
super(optionsFromArguments(Param.getDefaults(), arguments, ["param", "units", "convert"]));
|
|
|
|
|
|
|
|
const options = optionsFromArguments(Param.getDefaults(), arguments, ["param", "units", "convert"]);
|
|
|
|
|
2019-09-08 18:07:58 +00:00
|
|
|
this.assert(isDefined(options.param) &&
|
|
|
|
(isAudioParam(options.param) || options.param instanceof Param), "param must be an AudioParam");
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-09-08 18:07:58 +00:00
|
|
|
while (!isAudioParam(options.param)) {
|
|
|
|
options.param = options.param._param;
|
|
|
|
}
|
2019-09-27 21:53:42 +00:00
|
|
|
|
2019-09-29 16:20:54 +00:00
|
|
|
this._swappable = isDefined(options.swappable) ? options.swappable : false;
|
|
|
|
if (this._swappable) {
|
|
|
|
this.input = this.context.createGain();
|
|
|
|
// initialize
|
|
|
|
this._param = options.param;
|
|
|
|
this.input.connect(this._param);
|
|
|
|
} else {
|
|
|
|
this._param = this.input = options.param;
|
|
|
|
}
|
2019-04-12 14:37:47 +00:00
|
|
|
this._events = new Timeline<AutomationEvent>(1000);
|
2019-08-07 03:19:03 +00:00
|
|
|
this._initialValue = this._param.defaultValue;
|
2019-04-12 14:37:47 +00:00
|
|
|
this.units = options.units;
|
|
|
|
this.convert = options.convert;
|
2019-09-24 21:12:12 +00:00
|
|
|
this._minValue = options.minValue;
|
|
|
|
this._maxValue = options.maxValue;
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
// if the value is defined, set it immediately
|
2019-08-08 18:21:20 +00:00
|
|
|
if (isDefined(options.value) && options.value !== this._toType(this._initialValue)) {
|
2019-04-12 14:37:47 +00:00
|
|
|
this.setValueAtTime(options.value, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-29 16:20:54 +00:00
|
|
|
static getDefaults(): ParamOptions<any> {
|
|
|
|
return Object.assign(ToneWithContext.getDefaults(), {
|
|
|
|
convert: true,
|
|
|
|
units: "number" as UnitName,
|
|
|
|
} as ParamOptions<any>);
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
get value(): UnitMap[TypeName] {
|
2019-04-12 14:37:47 +00:00
|
|
|
const now = this.now();
|
|
|
|
return this.getValueAtTime(now);
|
|
|
|
}
|
2019-10-28 15:37:53 +00:00
|
|
|
set value(value) {
|
2019-04-12 14:37:47 +00:00
|
|
|
this._initialValue = this._fromType(value);
|
2019-08-07 15:38:31 +00:00
|
|
|
this.cancelScheduledValues(this.now());
|
|
|
|
this.setValueAtTime(value, this.now());
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get minValue(): number {
|
2019-09-24 21:12:12 +00:00
|
|
|
// if it's not the default minValue, return it
|
|
|
|
if (isDefined(this._minValue)) {
|
|
|
|
return this._minValue;
|
|
|
|
} else if (this.units === "time" || this.units === "frequency" ||
|
2019-04-12 14:37:47 +00:00
|
|
|
this.units === "normalRange" || this.units === "positive" ||
|
|
|
|
this.units === "transportTime" || this.units === "ticks" ||
|
|
|
|
this.units === "bpm" || this.units === "hertz" || this.units === "samples") {
|
|
|
|
return 0;
|
|
|
|
} else if (this.units === "audioRange") {
|
|
|
|
return -1;
|
|
|
|
} else if (this.units === "decibels") {
|
|
|
|
return -Infinity;
|
|
|
|
} else {
|
|
|
|
return this._param.minValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get maxValue(): number {
|
2019-09-24 21:12:12 +00:00
|
|
|
if (isDefined(this._maxValue)) {
|
|
|
|
return this._maxValue;
|
|
|
|
} else if (this.units === "normalRange" ||
|
2019-04-12 14:37:47 +00:00
|
|
|
this.units === "audioRange") {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return this._param.maxValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type guard based on the unit name
|
|
|
|
*/
|
2019-07-15 19:37:25 +00:00
|
|
|
private _is<T>(arg: any, type: UnitName): arg is T {
|
2019-04-12 14:37:47 +00:00
|
|
|
return this.units === type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* Convert the given value from the type specified by Param.units
|
|
|
|
* into the destination value (such as Gain or Frequency).
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
protected _fromType(val: UnitMap[TypeName]): number {
|
2019-04-12 14:37:47 +00:00
|
|
|
if (this.convert && !this.overridden) {
|
|
|
|
if (this._is<Time>(val, "time")) {
|
|
|
|
return this.toSeconds(val);
|
|
|
|
} else if (this._is<Decibels>(val, "decibels")) {
|
|
|
|
return dbToGain(val);
|
|
|
|
} else if (this._is<Frequency>(val, "frequency")) {
|
|
|
|
return this.toFrequency(val);
|
|
|
|
} else if (this._is<NormalRange>(val, "normalRange")) {
|
|
|
|
return Math.min(Math.max(val, 0), 1);
|
|
|
|
} else if (this._is<AudioRange>(val, "audioRange")) {
|
|
|
|
return Math.min(Math.max(val, -1), 1);
|
|
|
|
} else if (this._is<Positive>(val, "positive")) {
|
|
|
|
return Math.max(val, 0);
|
|
|
|
} else if (this._is<number>(val, "number")) {
|
|
|
|
return val;
|
|
|
|
} else {
|
|
|
|
return val as number;
|
|
|
|
}
|
2019-12-06 17:55:30 +00:00
|
|
|
} else if (this.overridden) {
|
|
|
|
// if it's overridden, should only schedule 0s
|
|
|
|
return 0;
|
2019-04-12 14:37:47 +00:00
|
|
|
} else {
|
|
|
|
return val as number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the parameters value into the units specified by Param.units.
|
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
protected _toType(val: number): UnitMap[TypeName] {
|
2019-04-12 14:37:47 +00:00
|
|
|
if (this.convert && this.units === "decibels") {
|
2019-10-28 15:37:53 +00:00
|
|
|
return gainToDb(val) as UnitMap[TypeName];
|
2019-04-12 14:37:47 +00:00
|
|
|
} else {
|
2019-10-28 15:37:53 +00:00
|
|
|
return val as UnitMap[TypeName];
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 22:06:46 +00:00
|
|
|
//-------------------------------------
|
2019-04-12 14:37:47 +00:00
|
|
|
// ABSTRACT PARAM INTERFACE
|
|
|
|
// all docs are generated from ParamInterface.ts
|
2019-09-14 22:06:46 +00:00
|
|
|
//-------------------------------------
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
setValueAtTime(value: UnitMap[TypeName], time: Time): this {
|
2019-07-24 13:30:58 +00:00
|
|
|
const computedTime = this.toSeconds(time);
|
2019-04-12 14:37:47 +00:00
|
|
|
const numericValue = this._fromType(value);
|
2019-08-07 03:19:03 +00:00
|
|
|
this.assert(isFinite(numericValue) && isFinite(computedTime),
|
|
|
|
`Invalid argument(s) to setValueAtTime: ${JSON.stringify(value)}, ${JSON.stringify(time)}`);
|
|
|
|
|
2019-09-06 20:17:49 +00:00
|
|
|
this.log(this.units, "setValueAtTime", value, computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
this._events.add({
|
2019-07-24 13:30:58 +00:00
|
|
|
time: computedTime,
|
2019-09-06 20:17:49 +00:00
|
|
|
type: "setValueAtTime",
|
2019-04-12 14:37:47 +00:00
|
|
|
value: numericValue,
|
|
|
|
});
|
2019-07-24 13:30:58 +00:00
|
|
|
this._param.setValueAtTime(numericValue, computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
getValueAtTime(time: Time): UnitMap[TypeName] {
|
2019-06-18 01:51:22 +00:00
|
|
|
const computedTime = Math.max(this.toSeconds(time), 0);
|
|
|
|
const after = this._events.getAfter(computedTime);
|
|
|
|
const before = this._events.get(computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
let value = this._initialValue;
|
|
|
|
// if it was set by
|
|
|
|
if (before === null) {
|
|
|
|
value = this._initialValue;
|
2019-09-06 20:17:49 +00:00
|
|
|
} else if (before.type === "setTargetAtTime" && (after === null || after.type === "setValueAtTime")) {
|
2019-04-12 14:37:47 +00:00
|
|
|
const previous = this._events.getBefore(before.time);
|
|
|
|
let previousVal;
|
|
|
|
if (previous === null) {
|
|
|
|
previousVal = this._initialValue;
|
|
|
|
} else {
|
|
|
|
previousVal = previous.value;
|
|
|
|
}
|
2019-09-06 20:17:49 +00:00
|
|
|
if (before.type === "setTargetAtTime") {
|
2019-06-18 01:51:22 +00:00
|
|
|
value = this._exponentialApproach(before.time, previousVal, before.value, before.constant, computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
} else if (after === null) {
|
|
|
|
value = before.value;
|
2019-09-06 20:17:49 +00:00
|
|
|
} else if (after.type === "linearRampToValueAtTime" || after.type === "exponentialRampToValueAtTime") {
|
2019-04-12 14:37:47 +00:00
|
|
|
let beforeValue = before.value;
|
2019-09-06 20:17:49 +00:00
|
|
|
if (before.type === "setTargetAtTime") {
|
2019-04-12 14:37:47 +00:00
|
|
|
const previous = this._events.getBefore(before.time);
|
|
|
|
if (previous === null) {
|
|
|
|
beforeValue = this._initialValue;
|
|
|
|
} else {
|
|
|
|
beforeValue = previous.value;
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 20:17:49 +00:00
|
|
|
if (after.type === "linearRampToValueAtTime") {
|
2019-06-18 01:51:22 +00:00
|
|
|
value = this._linearInterpolate(before.time, beforeValue, after.time, after.value, computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
} else {
|
2019-06-18 01:51:22 +00:00
|
|
|
value = this._exponentialInterpolate(before.time, beforeValue, after.time, after.value, computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value = before.value;
|
|
|
|
}
|
|
|
|
return this._toType(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
setRampPoint(time: Time): this {
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
let currentVal = this.getValueAtTime(time);
|
|
|
|
this.cancelAndHoldAtTime(time);
|
|
|
|
if (this._fromType(currentVal) === 0) {
|
|
|
|
currentVal = this._toType(this._minOutput);
|
|
|
|
}
|
|
|
|
this.setValueAtTime(currentVal, time);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
linearRampToValueAtTime(value: UnitMap[TypeName], endTime: Time): this {
|
2019-04-12 14:37:47 +00:00
|
|
|
const numericValue = this._fromType(value);
|
2019-07-24 13:30:58 +00:00
|
|
|
const computedTime = this.toSeconds(endTime);
|
2019-08-07 03:19:03 +00:00
|
|
|
this.assert(isFinite(numericValue) && isFinite(computedTime),
|
|
|
|
`Invalid argument(s) to linearRampToValueAtTime: ${JSON.stringify(value)}, ${JSON.stringify(endTime)}`);
|
2019-04-12 14:37:47 +00:00
|
|
|
this._events.add({
|
2019-07-24 13:30:58 +00:00
|
|
|
time: computedTime,
|
2019-09-06 20:17:49 +00:00
|
|
|
type: "linearRampToValueAtTime",
|
2019-09-14 23:55:39 +00:00
|
|
|
value: numericValue,
|
2019-04-12 14:37:47 +00:00
|
|
|
});
|
2019-09-06 20:17:49 +00:00
|
|
|
this.log(this.units, "linearRampToValueAtTime", value, computedTime);
|
2019-07-24 13:30:58 +00:00
|
|
|
this._param.linearRampToValueAtTime(numericValue, computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
exponentialRampToValueAtTime(value: UnitMap[TypeName], endTime: Time): this {
|
2019-04-12 14:37:47 +00:00
|
|
|
let numericValue = this._fromType(value);
|
|
|
|
numericValue = Math.max(this._minOutput, numericValue);
|
2019-07-24 13:30:58 +00:00
|
|
|
const computedTime = this.toSeconds(endTime);
|
2019-08-07 03:19:03 +00:00
|
|
|
this.assert(isFinite(numericValue) && isFinite(computedTime),
|
|
|
|
`Invalid argument(s) to exponentialRampToValueAtTime: ${JSON.stringify(value)}, ${JSON.stringify(endTime)}`);
|
2019-04-12 14:37:47 +00:00
|
|
|
// store the event
|
|
|
|
this._events.add({
|
2019-07-24 13:30:58 +00:00
|
|
|
time: computedTime,
|
2019-09-06 20:17:49 +00:00
|
|
|
type: "exponentialRampToValueAtTime",
|
2019-09-14 23:55:39 +00:00
|
|
|
value: numericValue,
|
2019-04-12 14:37:47 +00:00
|
|
|
});
|
2019-09-06 20:17:49 +00:00
|
|
|
this.log(this.units, "exponentialRampToValueAtTime", value, computedTime);
|
2019-07-24 13:30:58 +00:00
|
|
|
this._param.exponentialRampToValueAtTime(numericValue, computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
exponentialRampTo(value: UnitMap[TypeName], rampTime: Time, startTime?: Time): this {
|
2019-04-12 14:37:47 +00:00
|
|
|
startTime = this.toSeconds(startTime);
|
|
|
|
this.setRampPoint(startTime);
|
|
|
|
this.exponentialRampToValueAtTime(value, startTime + this.toSeconds(rampTime));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
linearRampTo(value: UnitMap[TypeName], rampTime: Time, startTime?: Time): this {
|
2019-04-12 14:37:47 +00:00
|
|
|
startTime = this.toSeconds(startTime);
|
|
|
|
this.setRampPoint(startTime);
|
|
|
|
this.linearRampToValueAtTime(value, startTime + this.toSeconds(rampTime));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
targetRampTo(value: UnitMap[TypeName], rampTime: Time, startTime?: Time): this {
|
2019-04-12 14:37:47 +00:00
|
|
|
startTime = this.toSeconds(startTime);
|
|
|
|
this.setRampPoint(startTime);
|
|
|
|
this.exponentialApproachValueAtTime(value, startTime, rampTime);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
exponentialApproachValueAtTime(value: UnitMap[TypeName], time: Time, rampTime: Time): this {
|
2019-04-12 14:37:47 +00:00
|
|
|
time = this.toSeconds(time);
|
2019-08-02 17:56:51 +00:00
|
|
|
rampTime = this.toSeconds(rampTime);
|
|
|
|
const timeConstant = Math.log(rampTime + 1) / Math.log(200);
|
|
|
|
this.setTargetAtTime(value, time, timeConstant);
|
|
|
|
// at 90% start a linear ramp to the final value
|
|
|
|
this.cancelAndHoldAtTime(time + rampTime * 0.9);
|
|
|
|
this.linearRampToValueAtTime(value, time + rampTime);
|
|
|
|
return this;
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
setTargetAtTime(value: UnitMap[TypeName], startTime: Time, timeConstant: Positive): this {
|
2019-04-12 14:37:47 +00:00
|
|
|
const numericValue = this._fromType(value);
|
|
|
|
// The value will never be able to approach without timeConstant > 0.
|
2019-07-24 13:30:58 +00:00
|
|
|
this.assert(isFinite(timeConstant) && timeConstant > 0, "timeConstant must be a number greater than 0");
|
|
|
|
const computedTime = this.toSeconds(startTime);
|
2019-08-07 03:19:03 +00:00
|
|
|
this.assert(isFinite(numericValue) && isFinite(computedTime),
|
|
|
|
`Invalid argument(s) to setTargetAtTime: ${JSON.stringify(value)}, ${JSON.stringify(startTime)}`);
|
2019-04-12 14:37:47 +00:00
|
|
|
this._events.add({
|
|
|
|
constant: timeConstant,
|
2019-07-24 13:30:58 +00:00
|
|
|
time: computedTime,
|
2019-09-06 20:17:49 +00:00
|
|
|
type: "setTargetAtTime",
|
2019-04-12 14:37:47 +00:00
|
|
|
value: numericValue,
|
|
|
|
});
|
2019-09-06 20:17:49 +00:00
|
|
|
this.log(this.units, "setTargetAtTime", value, computedTime, timeConstant);
|
2019-07-24 13:30:58 +00:00
|
|
|
this._param.setTargetAtTime(numericValue, computedTime, timeConstant);
|
2019-04-12 14:37:47 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-11-17 18:09:19 +00:00
|
|
|
setValueCurveAtTime(values: UnitMap[TypeName][], startTime: Time, duration: Time, scaling = 1): this {
|
2019-04-12 14:37:47 +00:00
|
|
|
duration = this.toSeconds(duration);
|
|
|
|
startTime = this.toSeconds(startTime);
|
|
|
|
const startingValue = this._fromType(values[0]) * scaling;
|
|
|
|
this.setValueAtTime(this._toType(startingValue), startTime);
|
|
|
|
const segTime = duration / (values.length - 1);
|
|
|
|
for (let i = 1; i < values.length; i++) {
|
|
|
|
const numericValue = this._fromType(values[i]) * scaling;
|
|
|
|
this.linearRampToValueAtTime(this._toType(numericValue), startTime + i * segTime);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelScheduledValues(time: Time): this {
|
2019-07-24 13:30:58 +00:00
|
|
|
const computedTime = this.toSeconds(time);
|
|
|
|
this.assert(isFinite(computedTime), `Invalid argument to cancelScheduledValues: ${JSON.stringify(time)}`);
|
2019-08-07 03:19:03 +00:00
|
|
|
this._events.cancel(computedTime);
|
2019-07-24 23:54:50 +00:00
|
|
|
this._param.cancelScheduledValues(computedTime);
|
2019-09-06 20:17:49 +00:00
|
|
|
this.log(this.units, "cancelScheduledValues", computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelAndHoldAtTime(time: Time): this {
|
2019-07-24 13:30:58 +00:00
|
|
|
const computedTime = this.toSeconds(time);
|
|
|
|
const valueAtTime = this._fromType(this.getValueAtTime(computedTime));
|
2019-04-12 14:37:47 +00:00
|
|
|
// remove the schedule events
|
2019-07-24 13:30:58 +00:00
|
|
|
this.assert(isFinite(computedTime), `Invalid argument to cancelAndHoldAtTime: ${JSON.stringify(time)}`);
|
2019-08-07 03:19:03 +00:00
|
|
|
|
|
|
|
this.log(this.units, "cancelAndHoldAtTime", computedTime, "value=" + valueAtTime);
|
2019-10-28 20:41:01 +00:00
|
|
|
|
2019-07-24 13:30:58 +00:00
|
|
|
// if there is an event at the given computedTime
|
2019-04-12 14:37:47 +00:00
|
|
|
// and that even is not a "set"
|
2019-07-24 13:30:58 +00:00
|
|
|
const before = this._events.get(computedTime);
|
|
|
|
const after = this._events.getAfter(computedTime);
|
2019-10-28 20:41:01 +00:00
|
|
|
if (before && EQ(before.time, computedTime)) {
|
2019-04-12 14:37:47 +00:00
|
|
|
// remove everything after
|
|
|
|
if (after) {
|
2019-10-28 20:41:01 +00:00
|
|
|
this._param.cancelScheduledValues(after.time);
|
2019-04-12 14:37:47 +00:00
|
|
|
this._events.cancel(after.time);
|
|
|
|
} else {
|
2019-10-28 20:41:01 +00:00
|
|
|
this._param.cancelAndHoldAtTime(computedTime);
|
2019-07-24 13:30:58 +00:00
|
|
|
this._events.cancel(computedTime + this.sampleTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
} else if (after) {
|
2019-10-28 20:41:01 +00:00
|
|
|
this._param.cancelScheduledValues(after.time);
|
2019-04-12 14:37:47 +00:00
|
|
|
// cancel the next event(s)
|
|
|
|
this._events.cancel(after.time);
|
2019-09-06 20:17:49 +00:00
|
|
|
if (after.type === "linearRampToValueAtTime") {
|
2019-07-24 13:30:58 +00:00
|
|
|
this.linearRampToValueAtTime(this._toType(valueAtTime), computedTime);
|
2019-09-06 20:17:49 +00:00
|
|
|
} else if (after.type === "exponentialRampToValueAtTime") {
|
2019-07-24 13:30:58 +00:00
|
|
|
this.exponentialRampToValueAtTime(this._toType(valueAtTime), computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 20:41:01 +00:00
|
|
|
// set the value at the given time
|
|
|
|
this._events.add({
|
|
|
|
time: computedTime,
|
|
|
|
type: "setValueAtTime",
|
|
|
|
value: valueAtTime,
|
|
|
|
});
|
|
|
|
this._param.setValueAtTime(valueAtTime, computedTime);
|
2019-04-12 14:37:47 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
rampTo(value: UnitMap[TypeName], rampTime: Time = 0.1, startTime?: Time): this {
|
2019-04-12 14:37:47 +00:00
|
|
|
if (this.units === "frequency" || this.units === "bpm" || this.units === "decibels") {
|
|
|
|
this.exponentialRampTo(value, rampTime, startTime);
|
|
|
|
} else {
|
|
|
|
this.linearRampTo(value, rampTime, startTime);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-09-06 20:18:32 +00:00
|
|
|
/**
|
|
|
|
* Apply all of the previously scheduled events to the passed in Param or AudioParam.
|
|
|
|
* The applied values will start at the context's current time and schedule
|
|
|
|
* all of the events which are scheduled on this Param onto the passed in param.
|
|
|
|
*/
|
|
|
|
apply(param: Param | AudioParam): this {
|
|
|
|
const now = this.context.currentTime;
|
|
|
|
// set the param's value at the current time and schedule everything else
|
|
|
|
param.setValueAtTime(this.getValueAtTime(now) as number, now);
|
|
|
|
// if the previous event was a curve, then set the rest of it
|
|
|
|
const previousEvent = this._events.get(now);
|
|
|
|
if (previousEvent && previousEvent.type === "setTargetAtTime") {
|
|
|
|
// approx it until the next event with linear ramps
|
|
|
|
const nextEvent = this._events.getAfter(previousEvent.time);
|
|
|
|
// or for 2 seconds if there is no event
|
|
|
|
const endTime = nextEvent ? nextEvent.time : now + 2;
|
|
|
|
const subdivisions = (endTime - now) / 10;
|
|
|
|
for (let i = now; i < endTime; i += subdivisions) {
|
|
|
|
param.linearRampToValueAtTime(this.getValueAtTime(i) as number, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._events.forEachAfter(this.context.currentTime, event => {
|
|
|
|
if (event.type === "cancelScheduledValues") {
|
|
|
|
param.cancelScheduledValues(event.time);
|
|
|
|
} else if (event.type === "setTargetAtTime") {
|
|
|
|
param.setTargetAtTime(event.value, event.time, event.constant);
|
|
|
|
} else {
|
|
|
|
param[event.type](event.value, event.time);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:53:42 +00:00
|
|
|
/**
|
|
|
|
* Replace the Param's internal AudioParam. Will apply scheduled curves
|
|
|
|
* onto the parameter and replace the connections.
|
|
|
|
*/
|
|
|
|
setParam(param: AudioParam): this {
|
2019-09-29 16:20:54 +00:00
|
|
|
this.assert(this._swappable, "The Param must be assigned as 'swappable' in the constructor");
|
|
|
|
const input = this.input as GainNode;
|
|
|
|
input.disconnect(this._param);
|
2019-09-27 21:53:42 +00:00
|
|
|
this.apply(param);
|
|
|
|
this._param = param;
|
2019-09-29 16:20:54 +00:00
|
|
|
input.connect(this._param);
|
2019-09-27 21:53:42 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-04-12 14:37:47 +00:00
|
|
|
dispose(): this {
|
2019-07-23 16:11:57 +00:00
|
|
|
super.dispose();
|
2019-04-12 14:37:47 +00:00
|
|
|
this._events.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
get defaultValue(): UnitMap[TypeName] {
|
2019-09-06 21:16:54 +00:00
|
|
|
return this._toType(this._param.defaultValue);
|
|
|
|
}
|
|
|
|
|
2019-09-14 22:06:46 +00:00
|
|
|
//-------------------------------------
|
2019-04-12 14:37:47 +00:00
|
|
|
// AUTOMATION CURVE CALCULATIONS
|
|
|
|
// MIT License, copyright (c) 2014 Jordan Santell
|
2019-09-14 22:06:46 +00:00
|
|
|
//-------------------------------------
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
// Calculates the the value along the curve produced by setTargetAtTime
|
2019-05-23 18:00:49 +00:00
|
|
|
protected _exponentialApproach(t0: number, v0: number, v1: number, timeConstant: number, t: number): number {
|
2019-04-12 14:37:47 +00:00
|
|
|
return v1 + (v0 - v1) * Math.exp(-(t - t0) / timeConstant);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculates the the value along the curve produced by linearRampToValueAtTime
|
2019-05-23 18:00:49 +00:00
|
|
|
protected _linearInterpolate(t0: number, v0: number, t1: number, v1: number, t: number): number {
|
2019-04-12 14:37:47 +00:00
|
|
|
return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculates the the value along the curve produced by exponentialRampToValueAtTime
|
2019-05-23 18:00:49 +00:00
|
|
|
protected _exponentialInterpolate(t0: number, v0: number, t1: number, v1: number, t: number): number {
|
2019-04-12 14:37:47 +00:00
|
|
|
return v0 * Math.pow(v1 / v0, (t - t0) / (t1 - t0));
|
|
|
|
}
|
|
|
|
}
|