UNPKG

1.93 kBJavaScriptView Raw
1import { connect } from "../core/context/ToneAudioNode.js";
2import { Param } from "../core/context/Param.js";
3import { optionsFromArguments } from "../core/util/Defaults.js";
4import { OneShotSource, } from "../source/OneShotSource.js";
5/**
6 * Wrapper around the native fire-and-forget ConstantSource.
7 * Adds the ability to reschedule the stop method.
8 * @category Signal
9 */
10export class ToneConstantSource extends OneShotSource {
11 constructor() {
12 const options = optionsFromArguments(ToneConstantSource.getDefaults(), arguments, ["offset"]);
13 super(options);
14 this.name = "ToneConstantSource";
15 /**
16 * The signal generator
17 */
18 this._source = this.context.createConstantSource();
19 connect(this._source, this._gainNode);
20 this.offset = new Param({
21 context: this.context,
22 convert: options.convert,
23 param: this._source.offset,
24 units: options.units,
25 value: options.offset,
26 minValue: options.minValue,
27 maxValue: options.maxValue,
28 });
29 }
30 static getDefaults() {
31 return Object.assign(OneShotSource.getDefaults(), {
32 convert: true,
33 offset: 1,
34 units: "number",
35 });
36 }
37 /**
38 * Start the source node at the given time
39 * @param time When to start the source
40 */
41 start(time) {
42 const computedTime = this.toSeconds(time);
43 this.log("start", computedTime);
44 this._startGain(computedTime);
45 this._source.start(computedTime);
46 return this;
47 }
48 _stopSource(time) {
49 this._source.stop(time);
50 }
51 dispose() {
52 super.dispose();
53 if (this.state === "started") {
54 this.stop();
55 }
56 this._source.disconnect();
57 this.offset.dispose();
58 return this;
59 }
60}
61//# sourceMappingURL=ToneConstantSource.js.map
\No newline at end of file