@marmooo/midy 0.4.8 → 0.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/script/midy.js CHANGED
@@ -137,6 +137,173 @@ class Note {
137
137
  });
138
138
  }
139
139
  }
140
+ class Channel {
141
+ constructor(audioNodes, settings) {
142
+ Object.defineProperty(this, "isDrum", {
143
+ enumerable: true,
144
+ configurable: true,
145
+ writable: true,
146
+ value: false
147
+ });
148
+ Object.defineProperty(this, "programNumber", {
149
+ enumerable: true,
150
+ configurable: true,
151
+ writable: true,
152
+ value: 0
153
+ });
154
+ Object.defineProperty(this, "scheduleIndex", {
155
+ enumerable: true,
156
+ configurable: true,
157
+ writable: true,
158
+ value: 0
159
+ });
160
+ Object.defineProperty(this, "detune", {
161
+ enumerable: true,
162
+ configurable: true,
163
+ writable: true,
164
+ value: 0
165
+ });
166
+ Object.defineProperty(this, "bankMSB", {
167
+ enumerable: true,
168
+ configurable: true,
169
+ writable: true,
170
+ value: 121
171
+ });
172
+ Object.defineProperty(this, "bankLSB", {
173
+ enumerable: true,
174
+ configurable: true,
175
+ writable: true,
176
+ value: 0
177
+ });
178
+ Object.defineProperty(this, "dataMSB", {
179
+ enumerable: true,
180
+ configurable: true,
181
+ writable: true,
182
+ value: 0
183
+ });
184
+ Object.defineProperty(this, "dataLSB", {
185
+ enumerable: true,
186
+ configurable: true,
187
+ writable: true,
188
+ value: 0
189
+ });
190
+ Object.defineProperty(this, "rpnMSB", {
191
+ enumerable: true,
192
+ configurable: true,
193
+ writable: true,
194
+ value: 127
195
+ });
196
+ Object.defineProperty(this, "rpnLSB", {
197
+ enumerable: true,
198
+ configurable: true,
199
+ writable: true,
200
+ value: 127
201
+ });
202
+ Object.defineProperty(this, "mono", {
203
+ enumerable: true,
204
+ configurable: true,
205
+ writable: true,
206
+ value: false
207
+ }); // CC#124, CC#125
208
+ Object.defineProperty(this, "modulationDepthRange", {
209
+ enumerable: true,
210
+ configurable: true,
211
+ writable: true,
212
+ value: 50
213
+ }); // cent
214
+ Object.defineProperty(this, "fineTuning", {
215
+ enumerable: true,
216
+ configurable: true,
217
+ writable: true,
218
+ value: 0
219
+ }); // cent
220
+ Object.defineProperty(this, "coarseTuning", {
221
+ enumerable: true,
222
+ configurable: true,
223
+ writable: true,
224
+ value: 0
225
+ }); // cent
226
+ Object.defineProperty(this, "scheduledNotes", {
227
+ enumerable: true,
228
+ configurable: true,
229
+ writable: true,
230
+ value: []
231
+ });
232
+ Object.defineProperty(this, "sustainNotes", {
233
+ enumerable: true,
234
+ configurable: true,
235
+ writable: true,
236
+ value: []
237
+ });
238
+ Object.defineProperty(this, "sostenutoNotes", {
239
+ enumerable: true,
240
+ configurable: true,
241
+ writable: true,
242
+ value: []
243
+ });
244
+ Object.defineProperty(this, "controlTable", {
245
+ enumerable: true,
246
+ configurable: true,
247
+ writable: true,
248
+ value: new Int8Array(defaultControlValues)
249
+ });
250
+ Object.defineProperty(this, "scaleOctaveTuningTable", {
251
+ enumerable: true,
252
+ configurable: true,
253
+ writable: true,
254
+ value: new Float32Array(12)
255
+ }); // [-100, 100] cent
256
+ Object.defineProperty(this, "channelPressureTable", {
257
+ enumerable: true,
258
+ configurable: true,
259
+ writable: true,
260
+ value: new Int8Array(defaultPressureValues)
261
+ });
262
+ Object.defineProperty(this, "polyphonicKeyPressureTable", {
263
+ enumerable: true,
264
+ configurable: true,
265
+ writable: true,
266
+ value: new Int8Array(defaultPressureValues)
267
+ });
268
+ Object.defineProperty(this, "keyBasedTable", {
269
+ enumerable: true,
270
+ configurable: true,
271
+ writable: true,
272
+ value: new Int8Array(128 * 128).fill(-1)
273
+ });
274
+ Object.defineProperty(this, "keyBasedGainLs", {
275
+ enumerable: true,
276
+ configurable: true,
277
+ writable: true,
278
+ value: new Array(128)
279
+ });
280
+ Object.defineProperty(this, "keyBasedGainRs", {
281
+ enumerable: true,
282
+ configurable: true,
283
+ writable: true,
284
+ value: new Array(128)
285
+ });
286
+ Object.defineProperty(this, "currentBufferSource", {
287
+ enumerable: true,
288
+ configurable: true,
289
+ writable: true,
290
+ value: null
291
+ });
292
+ Object.assign(this, audioNodes);
293
+ Object.assign(this, settings);
294
+ this.state = new ControllerState();
295
+ }
296
+ resetSettings(settings) {
297
+ Object.assign(this, settings);
298
+ }
299
+ resetTable(channel) {
300
+ channel.controlTable.set(defaultControlValues);
301
+ channel.scaleOctaveTuningTable.fill(0); // [-100, 100] cent
302
+ channel.channelPressureTable.set(defaultPressureValues);
303
+ channel.polyphonicKeyPressureTable.set(defaultPressureValues);
304
+ channel.keyBasedTable.fill(-1);
305
+ }
306
+ }
140
307
  const drumExclusiveClassesByKit = new Array(57);
141
308
  const drumExclusiveClassCount = 10;
142
309
  const standardSet = new Uint8Array(128);
@@ -700,34 +867,9 @@ class Midy extends EventTarget {
700
867
  merger,
701
868
  };
702
869
  }
703
- resetChannelTable(channel) {
704
- channel.controlTable.set(defaultControlValues);
705
- channel.scaleOctaveTuningTable.fill(0); // [-100, 100] cent
706
- channel.channelPressureTable.set(defaultPressureValues);
707
- channel.polyphonicKeyPressureTable.set(defaultPressureValues);
708
- channel.keyBasedTable.fill(-1);
709
- }
710
870
  createChannels(audioContext) {
711
- const channels = Array.from({ length: this.numChannels }, () => {
712
- return {
713
- currentBufferSource: null,
714
- isDrum: false,
715
- state: new ControllerState(),
716
- ...this.constructor.channelSettings,
717
- ...this.createChannelAudioNodes(audioContext),
718
- scheduledNotes: [],
719
- sustainNotes: [],
720
- sostenutoNotes: [],
721
- controlTable: new Int8Array(defaultControlValues),
722
- scaleOctaveTuningTable: new Float32Array(12), // [-100, 100] cent
723
- channelPressureTable: new Int8Array(defaultPressureValues),
724
- polyphonicKeyPressureTable: new Int8Array(defaultPressureValues),
725
- keyBasedTable: new Int8Array(128 * 128).fill(-1),
726
- keyBasedGainLs: new Array(128),
727
- keyBasedGainRs: new Array(128),
728
- };
729
- });
730
- return channels;
871
+ const settings = this.constructor.channelSettings;
872
+ return Array.from({ length: this.numChannels }, () => new Channel(this.createChannelAudioNodes(audioContext), settings));
731
873
  }
732
874
  decodeOggVorbis(sample) {
733
875
  const task = decoderQueue.then(async () => {
@@ -1786,16 +1928,15 @@ class Midy extends EventTarget {
1786
1928
  }
1787
1929
  async noteOn(channelNumber, noteNumber, velocity, startTime) {
1788
1930
  if (this.mpeEnabled) {
1789
- const note = await this.startNote(channelNumber, noteNumber, velocity, startTime);
1931
+ const channel = this.channels[channelNumber];
1932
+ const noteIndex = channel.scheduledNotes.length;
1790
1933
  if (!this.mpeState.channelToNotes.has(channelNumber)) {
1791
1934
  this.mpeState.channelToNotes.set(channelNumber, new Set());
1792
1935
  }
1793
- this.mpeState.channelToNotes.get(channelNumber).add(note.index);
1794
- this.mpeState.noteToChannel.set(note.index, channelNumber);
1795
- }
1796
- else {
1797
- await this.startNote(channelNumber, noteNumber, velocity, startTime);
1936
+ this.mpeState.channelToNotes.get(channelNumber).add(noteIndex);
1937
+ this.mpeState.noteToChannel.set(noteIndex, channelNumber);
1798
1938
  }
1939
+ await this.startNote(channelNumber, noteNumber, velocity, startTime);
1799
1940
  }
1800
1941
  async startNote(channelNumber, noteNumber, velocity, startTime) {
1801
1942
  const channel = this.channels[channelNumber];
@@ -2969,10 +3110,8 @@ class Midy extends EventTarget {
2969
3110
  state[key] = defaultValue;
2970
3111
  }
2971
3112
  }
2972
- for (const key of Object.keys(this.constructor.channelSettings)) {
2973
- channel[key] = this.constructor.channelSettings[key];
2974
- }
2975
- this.resetChannelTable(channel);
3113
+ channel.resetSettings(this.constructor.channelSettings);
3114
+ this.resetTable(channel);
2976
3115
  this.mode = "GM2";
2977
3116
  this.masterFineTuning = 0; // cent
2978
3117
  this.masterCoarseTuning = 0; // cent