1 | import { Volume } from "../component/channel/Volume.js";
|
2 | import { ToneAudioNode, } from "../core/context/ToneAudioNode.js";
|
3 | import { optionsFromArguments } from "../core/util/Defaults.js";
|
4 | import { readOnly } from "../core/util/Interface.js";
|
5 |
|
6 |
|
7 |
|
8 | export class Instrument extends ToneAudioNode {
|
9 | constructor() {
|
10 | const options = optionsFromArguments(Instrument.getDefaults(), arguments);
|
11 | super(options);
|
12 | |
13 |
|
14 |
|
15 |
|
16 | this._scheduledEvents = [];
|
17 | |
18 |
|
19 |
|
20 | this._synced = false;
|
21 | this._original_triggerAttack = this.triggerAttack;
|
22 | this._original_triggerRelease = this.triggerRelease;
|
23 | |
24 |
|
25 |
|
26 | this._syncedRelease = (time) => this._original_triggerRelease(time);
|
27 | this._volume = this.output = new Volume({
|
28 | context: this.context,
|
29 | volume: options.volume,
|
30 | });
|
31 | this.volume = this._volume.volume;
|
32 | readOnly(this, "volume");
|
33 | }
|
34 | static getDefaults() {
|
35 | return Object.assign(ToneAudioNode.getDefaults(), {
|
36 | volume: 0,
|
37 | });
|
38 | }
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | sync() {
|
54 | if (this._syncState()) {
|
55 | this._syncMethod("triggerAttack", 1);
|
56 | this._syncMethod("triggerRelease", 0);
|
57 | this.context.transport.on("stop", this._syncedRelease);
|
58 | this.context.transport.on("pause", this._syncedRelease);
|
59 | this.context.transport.on("loopEnd", this._syncedRelease);
|
60 | }
|
61 | return this;
|
62 | }
|
63 | |
64 |
|
65 |
|
66 | _syncState() {
|
67 | let changed = false;
|
68 | if (!this._synced) {
|
69 | this._synced = true;
|
70 | changed = true;
|
71 | }
|
72 | return changed;
|
73 | }
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 | _syncMethod(method, timePosition) {
|
80 | const originalMethod = (this["_original_" + method] = this[method]);
|
81 | this[method] = (...args) => {
|
82 | const time = args[timePosition];
|
83 | const id = this.context.transport.schedule((t) => {
|
84 | args[timePosition] = t;
|
85 | originalMethod.apply(this, args);
|
86 | }, time);
|
87 | this._scheduledEvents.push(id);
|
88 | };
|
89 | }
|
90 | |
91 |
|
92 |
|
93 | unsync() {
|
94 | this._scheduledEvents.forEach((id) => this.context.transport.clear(id));
|
95 | this._scheduledEvents = [];
|
96 | if (this._synced) {
|
97 | this._synced = false;
|
98 | this.triggerAttack = this._original_triggerAttack;
|
99 | this.triggerRelease = this._original_triggerRelease;
|
100 | this.context.transport.off("stop", this._syncedRelease);
|
101 | this.context.transport.off("pause", this._syncedRelease);
|
102 | this.context.transport.off("loopEnd", this._syncedRelease);
|
103 | }
|
104 | return this;
|
105 | }
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | triggerAttackRelease(note, duration, time, velocity) {
|
119 | const computedTime = this.toSeconds(time);
|
120 | const computedDuration = this.toSeconds(duration);
|
121 | this.triggerAttack(note, computedTime, velocity);
|
122 | this.triggerRelease(computedTime + computedDuration);
|
123 | return this;
|
124 | }
|
125 | |
126 |
|
127 |
|
128 |
|
129 | dispose() {
|
130 | super.dispose();
|
131 | this._volume.dispose();
|
132 | this.unsync();
|
133 | this._scheduledEvents = [];
|
134 | return this;
|
135 | }
|
136 | }
|
137 |
|
\ | No newline at end of file |