2020-01-08 19:12:11 +00:00
|
|
|
import { connect } from "../core/context/ToneAudioNode";
|
2019-09-06 21:10:32 +00:00
|
|
|
import { Param } from "../core/context/Param";
|
2019-10-31 19:43:16 +00:00
|
|
|
import { Seconds, Time, UnitMap, UnitName } from "../core/type/Units";
|
2019-09-06 21:10:32 +00:00
|
|
|
import { optionsFromArguments } from "../core/util/Defaults";
|
|
|
|
import { OneShotSource, OneShotSourceOptions } from "../source/OneShotSource";
|
|
|
|
|
2019-10-28 15:37:53 +00:00
|
|
|
export interface ToneConstantSourceOptions<TypeName extends UnitName> extends OneShotSourceOptions {
|
2019-09-06 21:10:32 +00:00
|
|
|
convert: boolean;
|
2019-10-28 15:37:53 +00:00
|
|
|
offset: UnitMap[TypeName];
|
|
|
|
units: TypeName;
|
2019-12-11 15:11:40 +00:00
|
|
|
minValue?: number;
|
|
|
|
maxValue?: number;
|
2019-09-06 21:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around the native fire-and-forget ConstantSource.
|
|
|
|
* Adds the ability to reschedule the stop method.
|
2019-09-16 14:15:23 +00:00
|
|
|
* @category Signal
|
2019-09-06 21:10:32 +00:00
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
export class ToneConstantSource<TypeName extends UnitName = "number"> extends OneShotSource<ToneConstantSourceOptions<TypeName>> {
|
2019-09-06 21:10:32 +00:00
|
|
|
|
|
|
|
readonly name: string = "ToneConstantSource";
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* The signal generator
|
2019-09-06 21:10:32 +00:00
|
|
|
*/
|
|
|
|
private _source = this.context.createConstantSource();
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* The offset of the signal generator
|
2019-09-06 21:10:32 +00:00
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
readonly offset: Param<TypeName>;
|
2019-09-06 21:10:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param offset The offset value
|
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
constructor(offset: UnitMap[TypeName]);
|
|
|
|
constructor(options?: Partial<ToneConstantSourceOptions<TypeName>>);
|
2019-09-06 21:10:32 +00:00
|
|
|
constructor() {
|
|
|
|
|
|
|
|
super(optionsFromArguments(ToneConstantSource.getDefaults(), arguments, ["offset"]));
|
|
|
|
const options = optionsFromArguments(ToneConstantSource.getDefaults(), arguments, ["offset"]);
|
|
|
|
|
|
|
|
connect(this._source, this._gainNode);
|
|
|
|
|
|
|
|
this.offset = new Param({
|
|
|
|
context: this.context,
|
|
|
|
convert: options.convert,
|
2019-09-14 22:12:44 +00:00
|
|
|
param: this._source.offset,
|
|
|
|
units: options.units,
|
|
|
|
value: options.offset,
|
2019-12-11 15:11:40 +00:00
|
|
|
minValue: options.minValue,
|
|
|
|
maxValue: options.maxValue,
|
2019-09-06 21:10:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDefaults(): ToneConstantSourceOptions<any> {
|
|
|
|
return Object.assign(OneShotSource.getDefaults(), {
|
|
|
|
convert: true,
|
|
|
|
offset: 1,
|
|
|
|
units: "number" as UnitName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the source node at the given time
|
|
|
|
* @param time When to start the source
|
|
|
|
*/
|
|
|
|
start(time?: Time): this {
|
|
|
|
const computedTime = this.toSeconds(time);
|
|
|
|
this.log("start", computedTime);
|
|
|
|
this._startGain(computedTime);
|
|
|
|
this._source.start(computedTime);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _stopSource(time?: Seconds): void {
|
|
|
|
this._source.stop(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose(): this {
|
|
|
|
super.dispose();
|
|
|
|
if (this.state === "started") {
|
|
|
|
this.stop();
|
|
|
|
}
|
|
|
|
this._source.disconnect();
|
|
|
|
this.offset.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|