spessasynth_core 1.1.2 → 1.1.4

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 (189) hide show
  1. package/LICENSE +3 -26
  2. package/README.md +156 -474
  3. package/index.js +73 -8
  4. package/package.json +21 -8
  5. package/src/externals/fflate/LICENSE +21 -0
  6. package/src/externals/fflate/fflate.min.js +1 -0
  7. package/src/externals/stbvorbis_sync/@types/stbvorbis_sync.d.ts +12 -0
  8. package/src/externals/stbvorbis_sync/LICENSE +202 -0
  9. package/src/externals/stbvorbis_sync/NOTICE +6 -0
  10. package/src/externals/stbvorbis_sync/stbvorbis_sync.min.js +1 -0
  11. package/src/midi/README.md +32 -0
  12. package/src/midi/basic_midi.js +567 -0
  13. package/src/midi/midi_builder.js +202 -0
  14. package/src/midi/midi_loader.js +324 -0
  15. package/{spessasynth_core/midi_parser → src/midi}/midi_message.js +58 -35
  16. package/src/midi/midi_sequence.js +224 -0
  17. package/src/midi/midi_tools/get_note_times.js +154 -0
  18. package/src/midi/midi_tools/midi_editor.js +611 -0
  19. package/src/midi/midi_tools/midi_writer.js +99 -0
  20. package/src/midi/midi_tools/rmidi_writer.js +567 -0
  21. package/src/midi/midi_tools/used_keys_loaded.js +238 -0
  22. package/src/midi/xmf_loader.js +454 -0
  23. package/src/sequencer/README.md +5 -0
  24. package/src/sequencer/events.js +81 -0
  25. package/src/sequencer/play.js +349 -0
  26. package/src/sequencer/process_event.js +165 -0
  27. package/{spessasynth_core/sequencer/worklet_sequencer → src/sequencer}/process_tick.js +103 -84
  28. package/src/sequencer/sequencer_engine.js +367 -0
  29. package/src/sequencer/song_control.js +201 -0
  30. package/src/soundfont/README.md +13 -0
  31. package/src/soundfont/basic_soundfont/basic_instrument.js +77 -0
  32. package/src/soundfont/basic_soundfont/basic_preset.js +336 -0
  33. package/src/soundfont/basic_soundfont/basic_sample.js +206 -0
  34. package/src/soundfont/basic_soundfont/basic_soundfont.js +565 -0
  35. package/src/soundfont/basic_soundfont/basic_zone.js +64 -0
  36. package/src/soundfont/basic_soundfont/basic_zones.js +43 -0
  37. package/src/soundfont/basic_soundfont/generator.js +220 -0
  38. package/src/soundfont/basic_soundfont/modulator.js +378 -0
  39. package/src/soundfont/basic_soundfont/riff_chunk.js +149 -0
  40. package/src/soundfont/basic_soundfont/write_dls/art2.js +173 -0
  41. package/src/soundfont/basic_soundfont/write_dls/articulator.js +49 -0
  42. package/src/soundfont/basic_soundfont/write_dls/combine_zones.js +400 -0
  43. package/src/soundfont/basic_soundfont/write_dls/ins.js +103 -0
  44. package/src/soundfont/basic_soundfont/write_dls/lins.js +18 -0
  45. package/src/soundfont/basic_soundfont/write_dls/modulator_converter.js +330 -0
  46. package/src/soundfont/basic_soundfont/write_dls/rgn2.js +121 -0
  47. package/src/soundfont/basic_soundfont/write_dls/wave.js +94 -0
  48. package/src/soundfont/basic_soundfont/write_dls/write_dls.js +119 -0
  49. package/src/soundfont/basic_soundfont/write_dls/wsmp.js +78 -0
  50. package/src/soundfont/basic_soundfont/write_dls/wvpl.js +32 -0
  51. package/src/soundfont/basic_soundfont/write_sf2/ibag.js +39 -0
  52. package/src/soundfont/basic_soundfont/write_sf2/igen.js +80 -0
  53. package/src/soundfont/basic_soundfont/write_sf2/imod.js +46 -0
  54. package/src/soundfont/basic_soundfont/write_sf2/inst.js +34 -0
  55. package/src/soundfont/basic_soundfont/write_sf2/pbag.js +39 -0
  56. package/src/soundfont/basic_soundfont/write_sf2/pgen.js +82 -0
  57. package/src/soundfont/basic_soundfont/write_sf2/phdr.js +42 -0
  58. package/src/soundfont/basic_soundfont/write_sf2/pmod.js +46 -0
  59. package/src/soundfont/basic_soundfont/write_sf2/sdta.js +80 -0
  60. package/src/soundfont/basic_soundfont/write_sf2/shdr.js +55 -0
  61. package/src/soundfont/basic_soundfont/write_sf2/write.js +222 -0
  62. package/src/soundfont/dls/articulator_converter.js +396 -0
  63. package/src/soundfont/dls/dls_destinations.js +38 -0
  64. package/src/soundfont/dls/dls_preset.js +44 -0
  65. package/src/soundfont/dls/dls_sample.js +75 -0
  66. package/src/soundfont/dls/dls_soundfont.js +186 -0
  67. package/src/soundfont/dls/dls_sources.js +62 -0
  68. package/src/soundfont/dls/dls_zone.js +95 -0
  69. package/src/soundfont/dls/read_articulation.js +299 -0
  70. package/src/soundfont/dls/read_instrument.js +121 -0
  71. package/src/soundfont/dls/read_instrument_list.js +17 -0
  72. package/src/soundfont/dls/read_lart.js +35 -0
  73. package/src/soundfont/dls/read_region.js +152 -0
  74. package/src/soundfont/dls/read_samples.js +270 -0
  75. package/src/soundfont/load_soundfont.js +21 -0
  76. package/src/soundfont/read_sf2/generators.js +46 -0
  77. package/{spessasynth_core/soundfont/chunk → src/soundfont/read_sf2}/instruments.js +20 -14
  78. package/src/soundfont/read_sf2/modulators.js +36 -0
  79. package/src/soundfont/read_sf2/presets.js +80 -0
  80. package/src/soundfont/read_sf2/samples.js +304 -0
  81. package/src/soundfont/read_sf2/soundfont.js +305 -0
  82. package/{spessasynth_core/soundfont/chunk → src/soundfont/read_sf2}/zones.js +68 -69
  83. package/src/synthetizer/README.md +7 -0
  84. package/src/synthetizer/audio_engine/README.md +9 -0
  85. package/src/synthetizer/audio_engine/engine_components/compute_modulator.js +266 -0
  86. package/src/synthetizer/audio_engine/engine_components/controller_tables.js +88 -0
  87. package/src/synthetizer/audio_engine/engine_components/key_modifier_manager.js +150 -0
  88. package/{spessasynth_core/synthetizer/worklet_system/worklet_utilities → src/synthetizer/audio_engine/engine_components}/lfo.js +9 -6
  89. package/src/synthetizer/audio_engine/engine_components/lowpass_filter.js +282 -0
  90. package/src/synthetizer/audio_engine/engine_components/midi_audio_channel.js +467 -0
  91. package/src/synthetizer/audio_engine/engine_components/modulation_envelope.js +181 -0
  92. package/{spessasynth_core/synthetizer/worklet_system/worklet_utilities → src/synthetizer/audio_engine/engine_components}/modulator_curves.js +33 -30
  93. package/src/synthetizer/audio_engine/engine_components/soundfont_manager.js +221 -0
  94. package/src/synthetizer/audio_engine/engine_components/stereo_panner.js +120 -0
  95. package/{spessasynth_core/synthetizer/worklet_system/worklet_utilities → src/synthetizer/audio_engine/engine_components}/unit_converter.js +11 -4
  96. package/src/synthetizer/audio_engine/engine_components/voice.js +519 -0
  97. package/src/synthetizer/audio_engine/engine_components/volume_envelope.js +401 -0
  98. package/src/synthetizer/audio_engine/engine_components/wavetable_oscillator.js +263 -0
  99. package/src/synthetizer/audio_engine/engine_methods/controller_control/controller_change.js +132 -0
  100. package/src/synthetizer/audio_engine/engine_methods/controller_control/master_parameters.js +48 -0
  101. package/src/synthetizer/audio_engine/engine_methods/controller_control/reset_controllers.js +241 -0
  102. package/src/synthetizer/audio_engine/engine_methods/create_midi_channel.js +27 -0
  103. package/src/synthetizer/audio_engine/engine_methods/data_entry/data_entry_coarse.js +253 -0
  104. package/src/synthetizer/audio_engine/engine_methods/data_entry/data_entry_fine.js +66 -0
  105. package/src/synthetizer/audio_engine/engine_methods/mute_channel.js +17 -0
  106. package/src/synthetizer/audio_engine/engine_methods/note_on.js +175 -0
  107. package/src/synthetizer/audio_engine/engine_methods/portamento_time.js +92 -0
  108. package/src/synthetizer/audio_engine/engine_methods/program_change.js +61 -0
  109. package/src/synthetizer/audio_engine/engine_methods/render_voice.js +196 -0
  110. package/src/synthetizer/audio_engine/engine_methods/soundfont_management/clear_sound_font.js +30 -0
  111. package/src/synthetizer/audio_engine/engine_methods/soundfont_management/get_preset.js +22 -0
  112. package/src/synthetizer/audio_engine/engine_methods/soundfont_management/reload_sound_font.js +28 -0
  113. package/src/synthetizer/audio_engine/engine_methods/soundfont_management/send_preset_list.js +31 -0
  114. package/src/synthetizer/audio_engine/engine_methods/soundfont_management/set_embedded_sound_font.js +21 -0
  115. package/src/synthetizer/audio_engine/engine_methods/stopping_notes/kill_note.js +20 -0
  116. package/src/synthetizer/audio_engine/engine_methods/stopping_notes/note_off.js +55 -0
  117. package/src/synthetizer/audio_engine/engine_methods/stopping_notes/stop_all_channels.js +16 -0
  118. package/src/synthetizer/audio_engine/engine_methods/stopping_notes/stop_all_notes.js +30 -0
  119. package/src/synthetizer/audio_engine/engine_methods/stopping_notes/voice_killing.js +63 -0
  120. package/src/synthetizer/audio_engine/engine_methods/system_exclusive.js +776 -0
  121. package/src/synthetizer/audio_engine/engine_methods/tuning_control/channel_pressure.js +24 -0
  122. package/src/synthetizer/audio_engine/engine_methods/tuning_control/pitch_wheel.js +33 -0
  123. package/src/synthetizer/audio_engine/engine_methods/tuning_control/poly_pressure.js +31 -0
  124. package/src/synthetizer/audio_engine/engine_methods/tuning_control/set_master_tuning.js +15 -0
  125. package/src/synthetizer/audio_engine/engine_methods/tuning_control/set_modulation_depth.js +27 -0
  126. package/src/synthetizer/audio_engine/engine_methods/tuning_control/set_octave_tuning.js +19 -0
  127. package/src/synthetizer/audio_engine/engine_methods/tuning_control/set_tuning.js +27 -0
  128. package/src/synthetizer/audio_engine/engine_methods/tuning_control/transpose_all_channels.js +15 -0
  129. package/src/synthetizer/audio_engine/engine_methods/tuning_control/transpose_channel.js +34 -0
  130. package/src/synthetizer/audio_engine/main_processor.js +804 -0
  131. package/src/synthetizer/audio_engine/snapshot/apply_synthesizer_snapshot.js +15 -0
  132. package/src/synthetizer/audio_engine/snapshot/channel_snapshot.js +175 -0
  133. package/src/synthetizer/audio_engine/snapshot/synthesizer_snapshot.js +116 -0
  134. package/src/synthetizer/synth_constants.js +22 -0
  135. package/{spessasynth_core → src}/utils/README.md +1 -0
  136. package/src/utils/buffer_to_wav.js +185 -0
  137. package/src/utils/byte_functions/big_endian.js +32 -0
  138. package/src/utils/byte_functions/little_endian.js +77 -0
  139. package/src/utils/byte_functions/string.js +107 -0
  140. package/src/utils/byte_functions/variable_length_quantity.js +42 -0
  141. package/src/utils/fill_with_defaults.js +21 -0
  142. package/src/utils/indexed_array.js +52 -0
  143. package/{spessasynth_core → src}/utils/loggin.js +70 -78
  144. package/src/utils/other.js +92 -0
  145. package/src/utils/sysex_detector.js +58 -0
  146. package/src/utils/xg_hacks.js +193 -0
  147. package/.idea/inspectionProfiles/Project_Default.xml +0 -10
  148. package/.idea/jsLibraryMappings.xml +0 -6
  149. package/.idea/modules.xml +0 -8
  150. package/.idea/spessasynth_core.iml +0 -12
  151. package/.idea/vcs.xml +0 -6
  152. package/spessasynth_core/midi_parser/README.md +0 -3
  153. package/spessasynth_core/midi_parser/midi_loader.js +0 -386
  154. package/spessasynth_core/sequencer/sequencer.js +0 -202
  155. package/spessasynth_core/sequencer/worklet_sequencer/play.js +0 -209
  156. package/spessasynth_core/sequencer/worklet_sequencer/process_event.js +0 -120
  157. package/spessasynth_core/sequencer/worklet_sequencer/song_control.js +0 -112
  158. package/spessasynth_core/soundfont/README.md +0 -4
  159. package/spessasynth_core/soundfont/chunk/generators.js +0 -205
  160. package/spessasynth_core/soundfont/chunk/modulators.js +0 -232
  161. package/spessasynth_core/soundfont/chunk/presets.js +0 -264
  162. package/spessasynth_core/soundfont/chunk/riff_chunk.js +0 -46
  163. package/spessasynth_core/soundfont/chunk/samples.js +0 -250
  164. package/spessasynth_core/soundfont/soundfont_parser.js +0 -301
  165. package/spessasynth_core/synthetizer/README.md +0 -6
  166. package/spessasynth_core/synthetizer/synthesizer.js +0 -303
  167. package/spessasynth_core/synthetizer/worklet_system/README.md +0 -3
  168. package/spessasynth_core/synthetizer/worklet_system/worklet_methods/controller_control.js +0 -290
  169. package/spessasynth_core/synthetizer/worklet_system/worklet_methods/data_entry.js +0 -280
  170. package/spessasynth_core/synthetizer/worklet_system/worklet_methods/note_off.js +0 -102
  171. package/spessasynth_core/synthetizer/worklet_system/worklet_methods/note_on.js +0 -77
  172. package/spessasynth_core/synthetizer/worklet_system/worklet_methods/program_control.js +0 -140
  173. package/spessasynth_core/synthetizer/worklet_system/worklet_methods/system_exclusive.js +0 -266
  174. package/spessasynth_core/synthetizer/worklet_system/worklet_methods/tuning_control.js +0 -104
  175. package/spessasynth_core/synthetizer/worklet_system/worklet_methods/vibrato_control.js +0 -29
  176. package/spessasynth_core/synthetizer/worklet_system/worklet_methods/voice_control.js +0 -222
  177. package/spessasynth_core/synthetizer/worklet_system/worklet_utilities/lowpass_filter.js +0 -95
  178. package/spessasynth_core/synthetizer/worklet_system/worklet_utilities/modulation_envelope.js +0 -73
  179. package/spessasynth_core/synthetizer/worklet_system/worklet_utilities/stereo_panner.js +0 -76
  180. package/spessasynth_core/synthetizer/worklet_system/worklet_utilities/volume_envelope.js +0 -194
  181. package/spessasynth_core/synthetizer/worklet_system/worklet_utilities/wavetable_oscillator.js +0 -83
  182. package/spessasynth_core/synthetizer/worklet_system/worklet_utilities/worklet_modulator.js +0 -173
  183. package/spessasynth_core/synthetizer/worklet_system/worklet_utilities/worklet_processor_channel.js +0 -106
  184. package/spessasynth_core/synthetizer/worklet_system/worklet_utilities/worklet_voice.js +0 -313
  185. package/spessasynth_core/utils/buffer_to_wav.js +0 -70
  186. package/spessasynth_core/utils/byte_functions.js +0 -141
  187. package/spessasynth_core/utils/other.js +0 -49
  188. package/spessasynth_core/utils/shiftable_array.js +0 -26
  189. package/spessasynth_core/utils/stbvorbis_sync.js +0 -1877
@@ -0,0 +1,400 @@
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, keyNumToVolEnvDecay 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.m min larger than the 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
179
+ // (this makes a new modulator
180
+ // because otherwise it would overwrite the one in the soundfont!
181
+ finalModList[identicalInstMod] = finalModList[identicalInstMod].sumTransform(
182
+ mod);
183
+ }
184
+ else
185
+ {
186
+ finalModList.push(mod);
187
+ }
188
+ }
189
+
190
+ // clone the generators as the values are modified during DLS conversion (keyNumToSomething)
191
+ let finalGenList = instGenerators.map(g => new Generator(g.generatorType, g.generatorValue));
192
+ for (const gen of presetGenerators)
193
+ {
194
+ if (gen.generatorType === generatorTypes.velRange ||
195
+ gen.generatorType === generatorTypes.keyRange ||
196
+ gen.generatorType === generatorTypes.instrument ||
197
+ gen.generatorType === generatorTypes.endOper ||
198
+ gen.generatorType === generatorTypes.sampleModes)
199
+ {
200
+ continue;
201
+ }
202
+ const identicalInstGen = instGenerators.findIndex(g => g.generatorType === gen.generatorType);
203
+ if (identicalInstGen !== -1)
204
+ {
205
+ // if exists, sum to that generator
206
+ const newAmount = finalGenList[identicalInstGen].generatorValue + gen.generatorValue;
207
+ finalGenList[identicalInstGen] = new Generator(gen.generatorType, newAmount);
208
+ }
209
+ else
210
+ {
211
+ // if not, sum to the default generator
212
+ const newAmount = generatorLimits[gen.generatorType].def + gen.generatorValue;
213
+ finalGenList.push(new Generator(gen.generatorType, newAmount));
214
+ }
215
+ }
216
+
217
+ // remove unwanted
218
+ finalGenList = finalGenList.filter(g =>
219
+ g.generatorType !== generatorTypes.sampleID &&
220
+ g.generatorType !== generatorTypes.keyRange &&
221
+ g.generatorType !== generatorTypes.velRange &&
222
+ g.generatorType !== generatorTypes.endOper &&
223
+ g.generatorType !== generatorTypes.instrument &&
224
+ g.generatorValue !== generatorLimits[g.generatorType].def
225
+ );
226
+
227
+ // create the zone and copy over values
228
+ const zone = new BasicInstrumentZone();
229
+ zone.keyRange = instZoneKeyRange;
230
+ zone.velRange = instZoneVelRange;
231
+ if (zone.keyRange.min === 0 && zone.keyRange.max === 127)
232
+ {
233
+ zone.keyRange.min = -1;
234
+ }
235
+ if (zone.velRange.min === 0 && zone.velRange.max === 127)
236
+ {
237
+ zone.velRange.min = -1;
238
+ }
239
+ zone.isGlobal = false;
240
+ zone.sample = instZone.sample;
241
+ zone.generators = finalGenList;
242
+ zone.modulators = finalModList;
243
+ finalZones.push(zone);
244
+ }
245
+ }
246
+
247
+ if (globalize)
248
+ {
249
+ // create a global zone and add repeating generators to it
250
+ // also modulators
251
+ const globalZone = new BasicInstrumentZone();
252
+ globalZone.isGlobal = true;
253
+ // iterate over every type of generator
254
+ for (let checkedType = 0; checkedType < 58; checkedType++)
255
+ {
256
+ // not these though
257
+ if (notGlobalizedTypes.has(checkedType))
258
+ {
259
+ continue;
260
+ }
261
+ /**
262
+ * @type {Object<string, number>}
263
+ */
264
+ let occurencesForValues = {};
265
+ const defaultForChecked = generatorLimits[checkedType]?.def || 0;
266
+ occurencesForValues[defaultForChecked] = 0;
267
+ for (const z of finalZones)
268
+ {
269
+ const gen = z.generators.find(g => g.generatorType === checkedType);
270
+ if (gen)
271
+ {
272
+ const value = gen.generatorValue;
273
+ if (occurencesForValues[value] === undefined)
274
+ {
275
+ occurencesForValues[value] = 1;
276
+ }
277
+ else
278
+ {
279
+ occurencesForValues[value]++;
280
+ }
281
+ }
282
+ else
283
+ {
284
+ occurencesForValues[defaultForChecked]++;
285
+ }
286
+
287
+ // if the checked type has the keyNumTo something generator set, it cannot be globalized.
288
+ let relativeCounterpart;
289
+ switch (checkedType)
290
+ {
291
+ default:
292
+ continue;
293
+
294
+ case generatorTypes.decayVolEnv:
295
+ relativeCounterpart = generatorTypes.keyNumToVolEnvDecay;
296
+ break;
297
+ case generatorTypes.holdVolEnv:
298
+ relativeCounterpart = generatorTypes.keyNumToVolEnvHold;
299
+ break;
300
+ case generatorTypes.decayModEnv:
301
+ relativeCounterpart = generatorTypes.keyNumToModEnvDecay;
302
+ break;
303
+ case generatorTypes.holdModEnv:
304
+ relativeCounterpart = generatorTypes.keyNumToModEnvHold;
305
+ }
306
+ const relative = z.generators.find(g => g.generatorType === relativeCounterpart);
307
+ if (relative !== undefined)
308
+ {
309
+ occurencesForValues = {};
310
+ break;
311
+ }
312
+ }
313
+ // if at least one occurrence, find the most used one and add it to global
314
+ if (Object.keys(occurencesForValues).length > 0)
315
+ {
316
+ // [value, occurrences]
317
+ const valueToGlobalize = Object.entries(occurencesForValues).reduce((max, curr) =>
318
+ {
319
+ if (max[1] < curr[1])
320
+ {
321
+ return curr;
322
+ }
323
+ return max;
324
+ }, [0, 0]);
325
+ const targetValue = parseInt(valueToGlobalize[0]);
326
+
327
+ // if the global value is the default value just remove it, no need to add it
328
+ if (targetValue !== defaultForChecked)
329
+ {
330
+ globalZone.generators.push(new Generator(checkedType, targetValue));
331
+ }
332
+ // remove from the zones
333
+ finalZones.forEach(z =>
334
+ {
335
+ const gen = z.generators.findIndex(g =>
336
+ g.generatorType === checkedType);
337
+ if (gen !== -1)
338
+ {
339
+ if (z.generators[gen].generatorValue === targetValue)
340
+ {
341
+ // That exact value exists. Since it's global now, remove it
342
+ z.generators.splice(gen, 1);
343
+ }
344
+ }
345
+ else
346
+ {
347
+ // That type does not exist at all here.
348
+ // Since we're globalizing, we need to add the default here.
349
+ if (targetValue !== defaultForChecked)
350
+ {
351
+ z.generators.push(new Generator(checkedType, defaultForChecked));
352
+ }
353
+ }
354
+ });
355
+ }
356
+ }
357
+
358
+ // globalize only modulators that exist in all zones
359
+ const firstZone = finalZones.find(z => !z.isGlobal);
360
+ const modulators = firstZone.modulators.map(m => Modulator.copy(m));
361
+ for (const checkedModulator of modulators)
362
+ {
363
+ let existsForAllZones = true;
364
+ for (const zone of finalZones)
365
+ {
366
+ if (zone.isGlobal || !existsForAllZones)
367
+ {
368
+ continue;
369
+ }
370
+ // check if that zone has an existing modulator
371
+ const mod = zone.modulators.find(m => Modulator.isIdentical(m, checkedModulator));
372
+ if (!mod)
373
+ {
374
+ // does not exist for this zone, so it's not global.
375
+ existsForAllZones = false;
376
+ }
377
+ // exists.
378
+
379
+ }
380
+ if (existsForAllZones === true)
381
+ {
382
+ globalZone.modulators.push(Modulator.copy(checkedModulator));
383
+ // delete it from local zones.
384
+ for (const zone of finalZones)
385
+ {
386
+ const modulator = zone.modulators.find(m => Modulator.isIdentical(m, checkedModulator));
387
+ // Check if the amount is correct.
388
+ // If so, delete it since it's global.
389
+ // If not, then it will simply override global as it's identical.
390
+ if (modulator.transformAmount === checkedModulator.transformAmount)
391
+ {
392
+ zone.modulators.splice(zone.modulators.indexOf(modulator), 1);
393
+ }
394
+ }
395
+ }
396
+ }
397
+ finalZones.splice(0, 0, globalZone);
398
+ }
399
+ return finalZones;
400
+ }
@@ -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 { getStringBytesZero } 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 {BasicSoundBank}
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 the region list
69
+ const lrgnData = combineArrays(combined.reduce((arrs, z) =>
70
+ {
71
+ if (!z.isGlobal)
72
+ {
73
+ arrs.push(writeDLSRegion.apply(this, [z, globalZone]));
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
+ getStringBytesZero(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 {BasicSoundBank}
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
+ }