spessasynth_lib 3.22.12 → 3.23.0

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.
Files changed (52) hide show
  1. package/@types/soundfont/basic_soundfont/basic_soundfont.d.ts +2 -0
  2. package/@types/soundfont/basic_soundfont/basic_zone.d.ts +16 -0
  3. package/@types/soundfont/basic_soundfont/generator.d.ts +2 -1
  4. package/@types/soundfont/basic_soundfont/riff_chunk.d.ts +2 -1
  5. package/@types/soundfont/basic_soundfont/write_dls/art2.d.ts +6 -0
  6. package/@types/soundfont/basic_soundfont/write_dls/articulator.d.ts +28 -0
  7. package/@types/soundfont/basic_soundfont/write_dls/combine_zones.d.ts +8 -0
  8. package/@types/soundfont/basic_soundfont/write_dls/ins.d.ts +7 -0
  9. package/@types/soundfont/basic_soundfont/write_dls/lins.d.ts +5 -0
  10. package/@types/soundfont/basic_soundfont/write_dls/modulator_converter.d.ts +11 -0
  11. package/@types/soundfont/basic_soundfont/write_dls/rgn2.d.ts +7 -0
  12. package/@types/soundfont/basic_soundfont/write_dls/wave.d.ts +6 -0
  13. package/@types/soundfont/basic_soundfont/write_dls/write_dls.d.ts +6 -0
  14. package/@types/soundfont/basic_soundfont/write_dls/wsmp.d.ts +12 -0
  15. package/@types/soundfont/basic_soundfont/write_dls/wvpl.d.ts +8 -0
  16. package/midi_parser/midi_editor.js +0 -0
  17. package/package.json +1 -1
  18. package/soundfont/basic_soundfont/basic_preset.js +23 -12
  19. package/soundfont/basic_soundfont/basic_soundfont.js +2 -0
  20. package/soundfont/basic_soundfont/basic_zone.js +30 -5
  21. package/soundfont/basic_soundfont/generator.js +10 -4
  22. package/soundfont/basic_soundfont/riff_chunk.js +20 -5
  23. package/soundfont/basic_soundfont/write_dls/art2.js +136 -0
  24. package/soundfont/basic_soundfont/write_dls/articulator.js +49 -0
  25. package/soundfont/basic_soundfont/write_dls/combine_zones.js +398 -0
  26. package/soundfont/basic_soundfont/write_dls/ins.js +103 -0
  27. package/soundfont/basic_soundfont/write_dls/lins.js +18 -0
  28. package/soundfont/basic_soundfont/write_dls/modulator_converter.js +324 -0
  29. package/soundfont/basic_soundfont/write_dls/rgn2.js +101 -0
  30. package/soundfont/basic_soundfont/write_dls/wave.js +74 -0
  31. package/soundfont/basic_soundfont/write_dls/write_dls.js +118 -0
  32. package/soundfont/basic_soundfont/write_dls/wsmp.js +74 -0
  33. package/soundfont/basic_soundfont/write_dls/wvpl.js +32 -0
  34. package/soundfont/basic_soundfont/write_sf2/igen.js +3 -3
  35. package/soundfont/basic_soundfont/write_sf2/pgen.js +2 -2
  36. package/soundfont/dls/articulator_converter.js +10 -0
  37. package/soundfont/dls/dls_sample.js +2 -2
  38. package/soundfont/dls/dls_soundfont.js +2 -1
  39. package/soundfont/dls/dls_zone.js +21 -18
  40. package/soundfont/dls/read_articulation.js +18 -30
  41. package/soundfont/dls/read_instrument.js +31 -1
  42. package/soundfont/dls/read_region.js +0 -4
  43. package/soundfont/dls/read_samples.js +17 -17
  44. package/synthetizer/key_modifier_manager.js +1 -1
  45. package/synthetizer/worklet_processor.min.js +13 -12
  46. package/synthetizer/worklet_system/main_processor.js +2 -0
  47. package/synthetizer/worklet_system/worklet_methods/note_on.js +2 -1
  48. package/synthetizer/worklet_system/worklet_methods/reset_controllers.js +4 -12
  49. package/synthetizer/worklet_system/worklet_methods/voice_control.js +4 -3
  50. package/synthetizer/worklet_system/worklet_methods/worklet_soundfont_manager/worklet_soundfont_manager.js +0 -0
  51. package/synthetizer/worklet_system/worklet_utilities/worklet_voice.js +1 -1
  52. package/utils/byte_functions/string.js +1 -1
@@ -0,0 +1,398 @@
1
+ import { Modulator } from "../modulator.js";
2
+ import { BasicInstrumentZone } from "../basic_zones.js";
3
+ import { Generator, generatorLimits, generatorTypes } from "../generator.js";
4
+
5
+ const notGlobalizedTypes = new Set([
6
+ generatorTypes.velRange,
7
+ generatorTypes.keyRange,
8
+ generatorTypes.instrument,
9
+ generatorTypes.exclusiveClass,
10
+ generatorTypes.endOper,
11
+ generatorTypes.sampleModes,
12
+ generatorTypes.startloopAddrsOffset,
13
+ generatorTypes.startloopAddrsCoarseOffset,
14
+ generatorTypes.endloopAddrsOffset,
15
+ generatorTypes.endloopAddrsCoarseOffset,
16
+ generatorTypes.startAddrsOffset,
17
+ generatorTypes.startAddrsCoarseOffset,
18
+ generatorTypes.endAddrOffset,
19
+ generatorTypes.endAddrsCoarseOffset,
20
+ generatorTypes.initialAttenuation, // written into wsmp, there's no global wsmp
21
+ generatorTypes.fineTune, // written into wsmp, there's no global wsmp
22
+ generatorTypes.coarseTune, // written into wsmp, there's no global wsmp
23
+ generatorTypes.keyNumToVolEnvHold, // KEY TO SOMETHING:
24
+ generatorTypes.keyNumToVolEnvDecay,// cannot be globalized as they modify their respective generators
25
+ generatorTypes.keyNumToModEnvHold, // (for example KNTVED modifies VolEnvDecay)
26
+ generatorTypes.keyNumToModEnvDecay
27
+ ]);
28
+
29
+ /**
30
+ * Combines preset zones
31
+ * @param preset {BasicPreset}
32
+ * @param globalize {boolean}
33
+ * @returns {BasicInstrumentZone[]}
34
+ */
35
+ export function combineZones(preset, globalize = true)
36
+ {
37
+ /**
38
+ * @param main {Generator[]}
39
+ * @param adder {Generator[]}
40
+ */
41
+ function addUnique(main, adder)
42
+ {
43
+ main.push(...adder.filter(g => !main.find(mg => mg.generatorType === g.generatorType)));
44
+ }
45
+
46
+ /**
47
+ * @param r1 {SoundFontRange}
48
+ * @param r2 {SoundFontRange}
49
+ * @returns {SoundFontRange}
50
+ */
51
+ function subtractRanges(r1, r2)
52
+ {
53
+ return { min: Math.max(r1.min, r2.min), max: Math.min(r1.max, r2.max) };
54
+ }
55
+
56
+ /**
57
+ * @param main {Modulator[]}
58
+ * @param adder {Modulator[]}
59
+ */
60
+ function addUniqueMods(main, adder)
61
+ {
62
+ main.push(...adder.filter(m => !main.find(mm => Modulator.isIdentical(m, mm))));
63
+ }
64
+
65
+ /**
66
+ * @type {BasicInstrumentZone[]}
67
+ */
68
+ const finalZones = [];
69
+
70
+ /**
71
+ * @type {Generator[]}
72
+ */
73
+ const globalPresetGenerators = [];
74
+ /**
75
+ * @type {Modulator[]}
76
+ */
77
+ const globalPresetModulators = [];
78
+ let globalPresetKeyRange = { min: 0, max: 127 };
79
+ let globalPresetVelRange = { min: 0, max: 127 };
80
+
81
+ // find the global zone and apply ranges, generators and modulators
82
+ const globalPresetZone = preset.presetZones.find(z => z.isGlobal);
83
+ if (globalPresetZone)
84
+ {
85
+ globalPresetGenerators.push(...globalPresetZone.generators);
86
+ globalPresetModulators.push(...globalPresetZone.modulators);
87
+ globalPresetKeyRange = globalPresetZone.keyRange;
88
+ globalPresetVelRange = globalPresetZone.velRange;
89
+ }
90
+ // for each non-global preset zone
91
+ for (const presetZone of preset.presetZones)
92
+ {
93
+ if (presetZone.isGlobal)
94
+ {
95
+ continue;
96
+ }
97
+ // use global ranges if not provided
98
+ let presetZoneKeyRange = presetZone.keyRange;
99
+ if (!presetZone.hasKeyRange)
100
+ {
101
+ presetZoneKeyRange = globalPresetKeyRange;
102
+ }
103
+ let presetZoneVelRange = presetZone.velRange;
104
+ if (!presetZone.hasVelRange)
105
+ {
106
+ presetZoneVelRange = globalPresetVelRange;
107
+ }
108
+ // add unique generators and modulators from the global zone
109
+ const presetGenerators = presetZone.generators.map(g => new Generator(g.generatorType, g.generatorValue));
110
+ addUnique(presetGenerators, globalPresetGenerators);
111
+ const presetModulators = [...presetZone.modulators];
112
+ addUniqueMods(presetModulators, globalPresetModulators);
113
+
114
+ const iZones = presetZone.instrument.instrumentZones;
115
+ /**
116
+ * @type {Generator[]}
117
+ */
118
+ const globalInstGenerators = [];
119
+ /**
120
+ * @type {Modulator[]}
121
+ */
122
+ const globalInstModulators = [];
123
+ let globalInstKeyRange = { min: 0, max: 127 };
124
+ let globalInstVelRange = { min: 0, max: 127 };
125
+ const globalInstZone = iZones.find(z => z.isGlobal);
126
+ if (globalInstZone)
127
+ {
128
+ globalInstGenerators.push(...globalInstZone.generators);
129
+ globalInstModulators.push(...globalInstZone.modulators);
130
+ globalInstKeyRange = globalInstZone.keyRange;
131
+ globalInstVelRange = globalInstZone.velRange;
132
+ }
133
+ // for each non-global instrument zone
134
+ for (const instZone of iZones)
135
+ {
136
+ if (instZone.isGlobal)
137
+ {
138
+ continue;
139
+ }
140
+ // use global ranges if not provided
141
+ let instZoneKeyRange = instZone.keyRange;
142
+ if (!instZone.hasKeyRange)
143
+ {
144
+ instZoneKeyRange = globalInstKeyRange;
145
+ }
146
+ let instZoneVelRange = instZone.velRange;
147
+ if (!instZone.hasVelRange)
148
+ {
149
+ instZoneVelRange = globalInstVelRange;
150
+ }
151
+ instZoneKeyRange = subtractRanges(instZoneKeyRange, presetZoneKeyRange);
152
+ instZoneVelRange = subtractRanges(instZoneVelRange, presetZoneVelRange);
153
+
154
+ // if either of the zones is out of range (i.e. min larger than max)
155
+ // then we discard that zone
156
+ if (instZoneKeyRange.max < instZoneKeyRange.min || instZoneVelRange.max < instZoneVelRange.min)
157
+ {
158
+ continue;
159
+ }
160
+
161
+ // add unique generators and modulators from the global zone
162
+ const instGenerators = instZone.generators.map(g => new Generator(g.generatorType, g.generatorValue));
163
+ addUnique(instGenerators, globalInstGenerators);
164
+ const instModulators = [...instZone.modulators];
165
+ addUniqueMods(instModulators, globalInstModulators);
166
+
167
+ /**
168
+ * sum preset modulators to instruments (amount) sf spec page 54
169
+ * @type {Modulator[]}
170
+ */
171
+ const finalModList = [...instModulators];
172
+ for (const mod of presetModulators)
173
+ {
174
+ const identicalInstMod = finalModList.findIndex(
175
+ m => Modulator.isIdentical(mod, m));
176
+ if (identicalInstMod !== -1)
177
+ {
178
+ // sum the amounts (this makes a new modulator because otherwise it would overwrite the one in the soundfont!!!
179
+ finalModList[identicalInstMod] = finalModList[identicalInstMod].sumTransform(
180
+ mod);
181
+ }
182
+ else
183
+ {
184
+ finalModList.push(mod);
185
+ }
186
+ }
187
+
188
+ // clone the generators as the values are modified during DLS conversion (keyNumToSomething)
189
+ let finalGenList = instGenerators.map(g => new Generator(g.generatorType, g.generatorValue));
190
+ for (const gen of presetGenerators)
191
+ {
192
+ if (gen.generatorType === generatorTypes.velRange ||
193
+ gen.generatorType === generatorTypes.keyRange ||
194
+ gen.generatorType === generatorTypes.instrument ||
195
+ gen.generatorType === generatorTypes.endOper ||
196
+ gen.generatorType === generatorTypes.sampleModes)
197
+ {
198
+ continue;
199
+ }
200
+ const identicalInstGen = instGenerators.findIndex(g => g.generatorType === gen.generatorType);
201
+ if (identicalInstGen !== -1)
202
+ {
203
+ // if exists, sum to that generator
204
+ const newAmount = finalGenList[identicalInstGen].generatorValue + gen.generatorValue;
205
+ finalGenList[identicalInstGen] = new Generator(gen.generatorType, newAmount);
206
+ }
207
+ else
208
+ {
209
+ // if not, sum to the default generator
210
+ const newAmount = generatorLimits[gen.generatorType].def + gen.generatorValue;
211
+ finalGenList.push(new Generator(gen.generatorType, newAmount));
212
+ }
213
+ }
214
+
215
+ // remove unwanted
216
+ finalGenList = finalGenList.filter(g =>
217
+ g.generatorType !== generatorTypes.sampleID &&
218
+ g.generatorType !== generatorTypes.keyRange &&
219
+ g.generatorType !== generatorTypes.velRange &&
220
+ g.generatorType !== generatorTypes.endOper &&
221
+ g.generatorType !== generatorTypes.instrument &&
222
+ g.generatorValue !== generatorLimits[g.generatorType].def
223
+ );
224
+
225
+ // create the zone and copy over values
226
+ const zone = new BasicInstrumentZone();
227
+ zone.keyRange = instZoneKeyRange;
228
+ zone.velRange = instZoneVelRange;
229
+ if (zone.keyRange.min === 0 && zone.keyRange.max === 127)
230
+ {
231
+ zone.keyRange.min = -1;
232
+ }
233
+ if (zone.velRange.min === 0 && zone.velRange.max === 127)
234
+ {
235
+ zone.velRange.min = -1;
236
+ }
237
+ zone.isGlobal = false;
238
+ zone.sample = instZone.sample;
239
+ zone.generators = finalGenList;
240
+ zone.modulators = finalModList;
241
+ finalZones.push(zone);
242
+ }
243
+ }
244
+
245
+ if (globalize)
246
+ {
247
+ // create a global zone and add repeating generators to it
248
+ // also modulators
249
+ const globalZone = new BasicInstrumentZone();
250
+ globalZone.isGlobal = true;
251
+ // iterate over every type of generator
252
+ for (let checkedType = 0; checkedType < 58; checkedType++)
253
+ {
254
+ // not these though
255
+ if (notGlobalizedTypes.has(checkedType))
256
+ {
257
+ continue;
258
+ }
259
+ /**
260
+ * @type {Object<string, number>}
261
+ */
262
+ let occurencesForValues = {};
263
+ const defaultForChecked = generatorLimits[checkedType]?.def || 0;
264
+ occurencesForValues[defaultForChecked] = 0;
265
+ for (const z of finalZones)
266
+ {
267
+ const gen = z.generators.find(g => g.generatorType === checkedType);
268
+ if (gen)
269
+ {
270
+ const value = gen.generatorValue;
271
+ if (occurencesForValues[value] === undefined)
272
+ {
273
+ occurencesForValues[value] = 1;
274
+ }
275
+ else
276
+ {
277
+ occurencesForValues[value]++;
278
+ }
279
+ }
280
+ else
281
+ {
282
+ occurencesForValues[defaultForChecked]++;
283
+ }
284
+
285
+ // if the checked type has the keyNumTo something generator set, it cannot be globalized.
286
+ let relativeCounterpart;
287
+ switch (checkedType)
288
+ {
289
+ default:
290
+ continue;
291
+
292
+ case generatorTypes.decayVolEnv:
293
+ relativeCounterpart = generatorTypes.keyNumToVolEnvDecay;
294
+ break;
295
+ case generatorTypes.holdVolEnv:
296
+ relativeCounterpart = generatorTypes.keyNumToVolEnvHold;
297
+ break;
298
+ case generatorTypes.decayModEnv:
299
+ relativeCounterpart = generatorTypes.keyNumToModEnvDecay;
300
+ break;
301
+ case generatorTypes.holdModEnv:
302
+ relativeCounterpart = generatorTypes.keyNumToModEnvHold;
303
+ }
304
+ const relative = z.generators.find(g => g.generatorType === relativeCounterpart);
305
+ if (relative !== undefined)
306
+ {
307
+ occurencesForValues = {};
308
+ break;
309
+ }
310
+ }
311
+ // if at least one occurence, find the most used one and add it to global
312
+ if (Object.keys(occurencesForValues).length > 0)
313
+ {
314
+ // [value, occurences]
315
+ const valueToGlobalize = Object.entries(occurencesForValues).reduce((max, curr) =>
316
+ {
317
+ if (max[1] < curr[1])
318
+ {
319
+ return curr;
320
+ }
321
+ return max;
322
+ }, [0, 0]);
323
+ const targetValue = parseInt(valueToGlobalize[0]);
324
+
325
+ // if the global value is the default value just remove it, no need to add it
326
+ if (targetValue !== defaultForChecked)
327
+ {
328
+ globalZone.generators.push(new Generator(checkedType, targetValue));
329
+ }
330
+ // remove from the zones
331
+ finalZones.forEach(z =>
332
+ {
333
+ const gen = z.generators.findIndex(g =>
334
+ g.generatorType === checkedType);
335
+ if (gen !== -1)
336
+ {
337
+ if (z.generators[gen].generatorValue === targetValue)
338
+ {
339
+ // that exact value exists. Since it's global now, remove it
340
+ z.generators.splice(gen, 1);
341
+ }
342
+ }
343
+ else
344
+ {
345
+ // that type does not exist at all here.
346
+ // Since we're globalizing, we need to add the default here.
347
+ if (targetValue !== defaultForChecked)
348
+ {
349
+ z.generators.push(new Generator(checkedType, defaultForChecked));
350
+ }
351
+ }
352
+ });
353
+ }
354
+ }
355
+
356
+ // globalize only modulators that exist in all zones
357
+ const firstZone = finalZones.find(z => !z.isGlobal);
358
+ const modulators = firstZone.modulators.map(m => Modulator.copy(m));
359
+ for (const checkedModulator of modulators)
360
+ {
361
+ let existsForAllZones = true;
362
+ for (const zone of finalZones)
363
+ {
364
+ if (zone.isGlobal || !existsForAllZones)
365
+ {
366
+ continue;
367
+ }
368
+ // check if that zone has an existing modulator
369
+ const mod = zone.modulators.find(m => Modulator.isIdentical(m, checkedModulator));
370
+ if (!mod)
371
+ {
372
+ // does not exist for this zone, so it's not global.
373
+ existsForAllZones = false;
374
+ }
375
+ // exists.
376
+
377
+ }
378
+ if (existsForAllZones === true)
379
+ {
380
+ globalZone.modulators.push(Modulator.copy(checkedModulator));
381
+ // delete from local zones.
382
+ for (const zone of finalZones)
383
+ {
384
+ const modulator = zone.modulators.find(m => Modulator.isIdentical(m, checkedModulator));
385
+ // Check if the amount is correct.
386
+ // If so, delete it since it's global.
387
+ // if not, then it will simply override global as it's identical.
388
+ if (modulator.transformAmount === checkedModulator.transformAmount)
389
+ {
390
+ zone.modulators.splice(zone.modulators.indexOf(modulator), 1);
391
+ }
392
+ }
393
+ }
394
+ }
395
+ finalZones.splice(0, 0, globalZone);
396
+ }
397
+ return finalZones;
398
+ }
@@ -0,0 +1,103 @@
1
+ import { combineArrays, IndexedByteArray } from "../../../utils/indexed_array.js";
2
+ import { combineZones } from "./combine_zones.js";
3
+ import { writeRIFFOddSize } from "../riff_chunk.js";
4
+ import { writeDword } from "../../../utils/byte_functions/little_endian.js";
5
+ import { writeDLSRegion } from "./rgn2.js";
6
+ import { getStringBytes } from "../../../utils/byte_functions/string.js";
7
+ import { writeArticulator } from "./art2.js";
8
+ import { SpessaSynthGroupCollapsed, SpessaSynthGroupEnd } from "../../../utils/loggin.js";
9
+ import { consoleColors } from "../../../utils/other.js";
10
+
11
+ /**
12
+ * @this {BasicSoundFont}
13
+ * @param preset {BasicPreset}
14
+ * @returns {IndexedByteArray}
15
+ */
16
+ export function writeIns(preset)
17
+ {
18
+ SpessaSynthGroupCollapsed(
19
+ `%cWriting %c${preset.presetName}%c...`,
20
+ consoleColors.info,
21
+ consoleColors.recognized,
22
+ consoleColors.info
23
+ );
24
+ // combine preset and instrument zones into a single instrument zone (region) list
25
+ const combined = combineZones(preset);
26
+
27
+ const nonGlobalRegionsCount = combined.reduce((sum, z) =>
28
+ {
29
+ if (!z.isGlobal)
30
+ {
31
+ return sum + 1;
32
+ }
33
+ return sum;
34
+ }, 0);
35
+
36
+ // insh: instrument header
37
+ const inshData = new IndexedByteArray(12);
38
+ writeDword(inshData, nonGlobalRegionsCount); // cRegions
39
+ // bank MSB is in bits 8-14
40
+ let ulBank = (preset.bank & 127) << 8;
41
+ // bit 32 means drums
42
+ if (preset.bank === 128)
43
+ {
44
+ ulBank |= (1 << 31);
45
+ }
46
+ writeDword(inshData, ulBank); // ulBank
47
+ writeDword(inshData, preset.program & 127); // ulInstrument
48
+
49
+ const insh = writeRIFFOddSize(
50
+ "insh",
51
+ inshData
52
+ );
53
+
54
+ // write global zone
55
+ let lar2 = new IndexedByteArray(0);
56
+ const globalZone = combined.find(z => z.isGlobal === true);
57
+ if (globalZone)
58
+ {
59
+ const art2 = writeArticulator(globalZone);
60
+ lar2 = writeRIFFOddSize(
61
+ "lar2",
62
+ art2,
63
+ false,
64
+ true
65
+ );
66
+ }
67
+
68
+ // write region list
69
+ const lrgnData = combineArrays(combined.reduce((arrs, z) =>
70
+ {
71
+ if (!z.isGlobal)
72
+ {
73
+ arrs.push(writeDLSRegion.apply(this, [z]));
74
+ }
75
+ return arrs;
76
+ }, []));
77
+ const lrgn = writeRIFFOddSize(
78
+ "lrgn",
79
+ lrgnData,
80
+ false,
81
+ true
82
+ );
83
+
84
+ // writeINFO
85
+ const inam = writeRIFFOddSize(
86
+ "INAM",
87
+ getStringBytes(preset.presetName)
88
+ );
89
+ const info = writeRIFFOddSize(
90
+ "INFO",
91
+ inam,
92
+ false,
93
+ true
94
+ );
95
+
96
+ SpessaSynthGroupEnd();
97
+ return writeRIFFOddSize(
98
+ "ins ",
99
+ combineArrays([insh, lrgn, lar2, info]),
100
+ false,
101
+ true
102
+ );
103
+ }
@@ -0,0 +1,18 @@
1
+ import { writeRIFFOddSize } from "../riff_chunk.js";
2
+ import { combineArrays } from "../../../utils/indexed_array.js";
3
+ import { writeIns } from "./ins.js";
4
+
5
+ /**
6
+ * @this {BasicSoundFont}
7
+ * @returns {IndexedByteArray}
8
+ */
9
+ export function writeLins()
10
+ {
11
+ const lins = combineArrays(this.presets.map(p => writeIns.apply(this, [p])));
12
+ return writeRIFFOddSize(
13
+ "lins",
14
+ lins,
15
+ false,
16
+ true
17
+ );
18
+ }