spessasynth_core 1.1.3 → 1.1.5

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 +74 -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 -313
  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 -223
  177. package/spessasynth_core/synthetizer/worklet_system/worklet_utilities/lowpass_filter.js +0 -133
  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 -272
  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 -175
  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 -285
  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,330 @@
1
+ import { midiControllers } from "../../../midi/midi_message.js";
2
+ import { DLSSources } from "../../dls/dls_sources.js";
3
+ import { modulatorCurveTypes, modulatorSources } from "../modulator.js";
4
+ import { generatorTypes } from "../generator.js";
5
+ import { DLSDestinations } from "../../dls/dls_destinations.js";
6
+ import { Articulator } from "./articulator.js";
7
+ import { SpessaSynthWarn } from "../../../utils/loggin.js";
8
+
9
+
10
+ /**
11
+ * @param cc {boolean}
12
+ * @param index {number}
13
+ * @returns {number|undefined}
14
+ */
15
+ function getDLSSourceFromSf2Source(cc, index)
16
+ {
17
+ if (cc)
18
+ {
19
+ switch (index)
20
+ {
21
+ default:
22
+ // DLS supports limited controllers
23
+ return undefined;
24
+
25
+ case midiControllers.modulationWheel:
26
+ return DLSSources.modulationWheel;
27
+ case midiControllers.mainVolume:
28
+ return DLSSources.volume;
29
+ case midiControllers.pan:
30
+ return DLSSources.pan;
31
+ case midiControllers.expressionController:
32
+ return DLSSources.expression;
33
+ case midiControllers.chorusDepth:
34
+ return DLSSources.chorus;
35
+ case midiControllers.reverbDepth:
36
+ return DLSSources.reverb;
37
+ }
38
+ }
39
+ else
40
+ {
41
+ switch (index)
42
+ {
43
+ default:
44
+ // cannot be a DLS articulator
45
+ return undefined;
46
+
47
+ case modulatorSources.noteOnKeyNum:
48
+ return DLSSources.keyNum;
49
+ case modulatorSources.noteOnVelocity:
50
+ return DLSSources.velocity;
51
+ case modulatorSources.noController:
52
+ return DLSSources.none;
53
+ case modulatorSources.polyPressure:
54
+ return DLSSources.polyPressure;
55
+ case modulatorSources.channelPressure:
56
+ return DLSSources.channelPressure;
57
+ case modulatorSources.pitchWheel:
58
+ return DLSSources.pitchWheel;
59
+ case modulatorSources.pitchWheelRange:
60
+ return DLSSources.pitchWheelRange;
61
+ }
62
+ }
63
+ }
64
+
65
+ /**
66
+ * @param dest {number}
67
+ * @param amount {number}
68
+ * @returns {number|undefined|{dest: number, amount: number}}
69
+ */
70
+ function getDLSDestinationFromSf2(dest, amount)
71
+ {
72
+ switch (dest)
73
+ {
74
+ default:
75
+ return undefined;
76
+
77
+ case generatorTypes.initialAttenuation:
78
+ // the amount does not get EMU corrected here, as this only applies to modulator attenuation
79
+ // the generator (affected) attenuation is handled in wsmp.
80
+ return { dest: DLSDestinations.gain, amount: -amount };
81
+ case generatorTypes.fineTune:
82
+ return DLSDestinations.pitch;
83
+ case generatorTypes.pan:
84
+ return DLSDestinations.pan;
85
+ case generatorTypes.keyNum:
86
+ return DLSDestinations.keyNum;
87
+
88
+ case generatorTypes.reverbEffectsSend:
89
+ return DLSDestinations.reverbSend;
90
+ case generatorTypes.chorusEffectsSend:
91
+ return DLSDestinations.chorusSend;
92
+
93
+ case generatorTypes.freqModLFO:
94
+ return DLSDestinations.modLfoFreq;
95
+ case generatorTypes.delayModLFO:
96
+ return DLSDestinations.modLfoDelay;
97
+
98
+ case generatorTypes.delayVibLFO:
99
+ return DLSDestinations.vibLfoDelay;
100
+ case generatorTypes.freqVibLFO:
101
+ return DLSDestinations.vibLfoFreq;
102
+
103
+ case generatorTypes.delayVolEnv:
104
+ return DLSDestinations.volEnvDelay;
105
+ case generatorTypes.attackVolEnv:
106
+ return DLSDestinations.volEnvAttack;
107
+ case generatorTypes.holdVolEnv:
108
+ return DLSDestinations.volEnvHold;
109
+ case generatorTypes.decayVolEnv:
110
+ return DLSDestinations.volEnvDecay;
111
+ case generatorTypes.sustainVolEnv:
112
+ return { dest: DLSDestinations.volEnvSustain, amount: 1000 - amount };
113
+ case generatorTypes.releaseVolEnv:
114
+ return DLSDestinations.volEnvRelease;
115
+
116
+ case generatorTypes.delayModEnv:
117
+ return DLSDestinations.modEnvDelay;
118
+ case generatorTypes.attackModEnv:
119
+ return DLSDestinations.modEnvAttack;
120
+ case generatorTypes.holdModEnv:
121
+ return DLSDestinations.modEnvHold;
122
+ case generatorTypes.decayModEnv:
123
+ return DLSDestinations.modEnvDecay;
124
+ case generatorTypes.sustainModEnv:
125
+ return { dest: DLSDestinations.modEnvSustain, amount: 1000 - amount };
126
+ case generatorTypes.releaseModEnv:
127
+ return DLSDestinations.modEnvRelease;
128
+
129
+ case generatorTypes.initialFilterFc:
130
+ return DLSDestinations.filterCutoff;
131
+ case generatorTypes.initialFilterQ:
132
+ return DLSDestinations.filterQ;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * @param dest {number}
138
+ * @param amt {number}
139
+ * @returns {{source: DLSSources, dest: DLSDestinations, amt: number, isBipolar: boolean}|undefined}
140
+ */
141
+ function checkSF2SpecialCombos(dest, amt)
142
+ {
143
+
144
+ switch (dest)
145
+ {
146
+ default:
147
+ return undefined;
148
+ // mod env
149
+ case generatorTypes.modEnvToFilterFc:
150
+ return { source: DLSSources.modEnv, dest: DLSDestinations.filterCutoff, amt: amt, isBipolar: false };
151
+ case generatorTypes.modEnvToPitch:
152
+ return { source: DLSSources.modEnv, dest: DLSDestinations.pitch, amt: amt, isBipolar: false };
153
+
154
+ // mod lfo
155
+ case generatorTypes.modLfoToFilterFc:
156
+ return { source: DLSSources.modLfo, dest: DLSDestinations.filterCutoff, amt: amt, isBipolar: true };
157
+ case generatorTypes.modLfoToVolume:
158
+ return { source: DLSSources.modLfo, dest: DLSDestinations.gain, amt: amt, isBipolar: true };
159
+ case generatorTypes.modLfoToPitch:
160
+ return { source: DLSSources.modLfo, dest: DLSDestinations.pitch, amt: amt, isBipolar: true };
161
+
162
+ // vib lfo
163
+ case generatorTypes.vibLfoToPitch:
164
+ return { source: DLSSources.vibratoLfo, dest: DLSDestinations.pitch, amt: amt, isBipolar: true };
165
+
166
+ // key to something
167
+ case generatorTypes.keyNumToVolEnvHold:
168
+ return {
169
+ source: DLSSources.keyNum,
170
+ dest: DLSDestinations.volEnvHold,
171
+ amt: amt,
172
+ isBipolar: true
173
+ };
174
+ case generatorTypes.keyNumToVolEnvDecay:
175
+ return {
176
+ source: DLSSources.keyNum,
177
+ dest: DLSDestinations.volEnvDecay,
178
+ amt: amt,
179
+ isBipolar: true
180
+ };
181
+ case generatorTypes.keyNumToModEnvHold:
182
+ return {
183
+ source: DLSSources.keyNum,
184
+ dest: DLSDestinations.modEnvHold,
185
+ amt: amt,
186
+ isBipolar: true
187
+ };
188
+ case generatorTypes.keyNumToModEnvDecay:
189
+ return {
190
+ source: DLSSources.keyNum,
191
+ dest: DLSDestinations.modEnvDecay,
192
+ amt: amt,
193
+ isBipolar: true
194
+ };
195
+
196
+ // Scale tuning is implemented in DLS via an articulator:
197
+ // keyNum to relative pitch at 12,800 cents.
198
+ // Change that to scale tuning * 128.
199
+ // Therefore, a regular scale is still 12,800, half is 6400, etc.
200
+ case generatorTypes.scaleTuning:
201
+ return {
202
+ source: DLSSources.keyNum,
203
+ dest: DLSDestinations.pitch,
204
+ amt: amt * 128,
205
+ isBipolar: false // according to table 4, this should be false.
206
+ };
207
+ }
208
+ }
209
+
210
+ /**
211
+ * @param gen {Generator}
212
+ * @returns {Articulator|undefined}
213
+ */
214
+ export function getDLSArticulatorFromSf2Generator(gen)
215
+ {
216
+ const dest = getDLSDestinationFromSf2(gen.generatorType, gen.generatorValue);
217
+ let destination = dest;
218
+ let source = 0;
219
+ let amount = gen.generatorValue;
220
+ if (dest?.amount !== undefined)
221
+ {
222
+ amount = dest.amount;
223
+ destination = dest.dest;
224
+ }
225
+ // check for special combo
226
+ const combo = checkSF2SpecialCombos(gen.generatorType, gen.generatorValue);
227
+ if (combo !== undefined)
228
+ {
229
+ amount = combo.amt;
230
+ destination = combo.dest;
231
+ source = combo.source;
232
+ }
233
+ else if (destination === undefined)
234
+ {
235
+ SpessaSynthWarn(`Invalid generator type: ${gen.generatorType}`);
236
+ return undefined;
237
+ }
238
+ return new Articulator(
239
+ source,
240
+ 0,
241
+ destination,
242
+ amount,
243
+ 0
244
+ );
245
+ }
246
+
247
+
248
+ /**
249
+ * @param mod {Modulator}
250
+ * @returns {Articulator|undefined}
251
+ */
252
+ export function getDLSArticulatorFromSf2Modulator(mod)
253
+ {
254
+ if (mod.transformType !== 0)
255
+ {
256
+ SpessaSynthWarn("Other transform types are not supported.");
257
+ return undefined;
258
+ }
259
+ let source = getDLSSourceFromSf2Source(mod.sourceUsesCC, mod.sourceIndex);
260
+ let sourceTransformType = mod.sourceCurveType;
261
+ let sourceBipolar = mod.sourcePolarity;
262
+ let sourceDirection = mod.sourceDirection;
263
+ if (source === undefined)
264
+ {
265
+ SpessaSynthWarn(`Invalid source: ${mod.sourceIndex}, CC: ${mod.sourceUsesCC}`);
266
+ return undefined;
267
+ }
268
+ // Attenuation is the opposite of gain. Invert.
269
+ if (mod.modulatorDestination === generatorTypes.initialAttenuation)
270
+ {
271
+ sourceDirection = sourceDirection === 1 ? 0 : 1;
272
+ }
273
+ let control = getDLSSourceFromSf2Source(mod.secSrcUsesCC, mod.secSrcIndex);
274
+ let controlTransformType = mod.secSrcCurveType;
275
+ let controlBipolar = mod.secSrcPolarity;
276
+ let controlDirection = mod.secSrcDirection;
277
+ if (control === undefined)
278
+ {
279
+ SpessaSynthWarn(`Invalid secondary source: ${mod.secSrcIndex}, CC: ${mod.secSrcUsesCC}`);
280
+ return undefined;
281
+ }
282
+ let dlsDestinationFromSf2 = getDLSDestinationFromSf2(mod.modulatorDestination, mod.transformAmount);
283
+ let destination = dlsDestinationFromSf2;
284
+ let amt = mod.transformAmount;
285
+ if (dlsDestinationFromSf2?.dest !== undefined)
286
+ {
287
+ destination = dlsDestinationFromSf2.dest;
288
+ amt = dlsDestinationFromSf2.amount;
289
+ }
290
+ const specialCombo = checkSF2SpecialCombos(mod.modulatorDestination, mod.transformAmount);
291
+ if (specialCombo !== undefined)
292
+ {
293
+ amt = specialCombo.amt;
294
+ // move the source to control
295
+ control = source;
296
+ controlTransformType = sourceTransformType;
297
+ controlBipolar = sourceBipolar;
298
+ controlDirection = sourceDirection;
299
+
300
+ // set source as static as it's either: env, lfo or key num
301
+ sourceTransformType = modulatorCurveTypes.linear;
302
+ sourceBipolar = specialCombo.isBipolar ? 1 : 0;
303
+ sourceDirection = 0;
304
+ source = specialCombo.source;
305
+ destination = specialCombo.dest;
306
+ }
307
+ else if (destination === undefined)
308
+ {
309
+ SpessaSynthWarn(`Invalid destination: ${mod.modulatorDestination}`);
310
+ return undefined;
311
+ }
312
+
313
+ // source curve type maps to a desfont curve type in section 2.10, table 9
314
+ let transform = 0;
315
+ transform |= controlTransformType << 4;
316
+ transform |= controlBipolar << 8;
317
+ transform |= controlDirection << 9;
318
+
319
+ // use the source curve in output transform
320
+ transform |= sourceTransformType;
321
+ transform |= sourceBipolar << 14;
322
+ transform |= sourceDirection << 15;
323
+ return new Articulator(
324
+ source,
325
+ control,
326
+ destination,
327
+ amt,
328
+ transform
329
+ );
330
+ }
@@ -0,0 +1,121 @@
1
+ import { combineArrays, IndexedByteArray } from "../../../utils/indexed_array.js";
2
+ import { writeDword, writeWord } from "../../../utils/byte_functions/little_endian.js";
3
+ import { generatorTypes } from "../generator.js";
4
+ import { writeRIFFOddSize } from "../riff_chunk.js";
5
+ import { writeWavesample } from "./wsmp.js";
6
+ import { writeArticulator } from "./art2.js";
7
+
8
+ /**
9
+ * @param zone {BasicInstrumentZone}
10
+ * @param globalZone {BasicInstrumentZone}
11
+ * @this {BasicSoundBank}
12
+ * @returns {IndexedByteArray}
13
+ */
14
+ export function writeDLSRegion(zone, globalZone)
15
+ {
16
+ // region header
17
+ const rgnhData = new IndexedByteArray(12);
18
+ // keyRange
19
+ writeWord(rgnhData, Math.max(zone.keyRange.min, 0));
20
+ writeWord(rgnhData, zone.keyRange.max);
21
+ // velRange
22
+ writeWord(rgnhData, Math.max(zone.velRange.min, 0));
23
+ writeWord(rgnhData, zone.velRange.max);
24
+ // fusOptions: 0 it seems
25
+ writeWord(rgnhData, 0);
26
+ // keyGroup (exclusive class)
27
+ const exclusive = zone.getGeneratorValue(generatorTypes.exclusiveClass, 0);
28
+ writeWord(rgnhData, exclusive);
29
+ // usLayer
30
+ writeWord(rgnhData, 0);
31
+ const rgnh = writeRIFFOddSize(
32
+ "rgnh",
33
+ rgnhData
34
+ );
35
+
36
+ let rootKey = zone.getGeneratorValue(generatorTypes.overridingRootKey, zone.sample.samplePitch);
37
+
38
+ // a lot of soundfonts like to set scale tuning to 0 in drums and keep the key at 60
39
+ // since we implement scale tuning via a dls articulator and fluid doesn't support these,
40
+ // change the root key here
41
+ const scaleTuning = zone.getGeneratorValue(
42
+ generatorTypes.scaleTuning,
43
+ globalZone.getGeneratorValue(generatorTypes.scaleTuning, 100)
44
+ );
45
+ if (scaleTuning === 0 && zone.keyRange.max - zone.keyRange.min === 0)
46
+ {
47
+ rootKey = zone.keyRange.min;
48
+ }
49
+
50
+ // wave sample (Wsmp)
51
+ const wsmp = writeWavesample(
52
+ zone.sample,
53
+ rootKey,
54
+ zone.getGeneratorValue(
55
+ generatorTypes.fineTune,
56
+ 0
57
+ ) + zone.getGeneratorValue(generatorTypes.coarseTune, 0) * 100
58
+ + zone.sample.samplePitchCorrection,
59
+ zone.getGeneratorValue(generatorTypes.initialAttenuation, 0),
60
+ // calculate loop with offsets
61
+ zone.sample.sampleLoopStartIndex
62
+ + zone.getGeneratorValue(generatorTypes.startloopAddrsOffset, 0)
63
+ + zone.getGeneratorValue(generatorTypes.startloopAddrsCoarseOffset, 0) * 32768,
64
+ zone.sample.sampleLoopEndIndex
65
+ + zone.getGeneratorValue(generatorTypes.endloopAddrsOffset, 0)
66
+ + zone.getGeneratorValue(generatorTypes.endloopAddrsCoarseOffset, 0) * 32768,
67
+ zone.getGeneratorValue(generatorTypes.sampleModes, 0)
68
+ );
69
+
70
+ // wave link (wlnk)
71
+ const wlnkData = new IndexedByteArray(12);
72
+ writeWord(wlnkData, 0); // fusOptions
73
+ writeWord(wlnkData, 0); // usPhaseGroup
74
+ // let sampleType = 0;
75
+ // switch (zone.sample.sampleType)
76
+ // {
77
+ // default:
78
+ // case 1:
79
+ // case 4:
80
+ // // mono/left
81
+ // sampleType = 0;
82
+ // break;
83
+ //
84
+ // case 2:
85
+ // // right
86
+ // sampleType = 1;
87
+ // }
88
+ // 1 means that the first bit is on so mono/left
89
+ writeDword(wlnkData, 1); // ulChannel
90
+ writeDword(wlnkData, this.samples.indexOf(zone.sample)); // ulTableIndex
91
+ const wlnk = writeRIFFOddSize(
92
+ "wlnk",
93
+ wlnkData
94
+ );
95
+
96
+ // art
97
+ let lar2 = new IndexedByteArray(0);
98
+ if (zone.modulators.length + zone.generators.length > 0)
99
+ {
100
+ const art2 = writeArticulator(zone);
101
+
102
+ lar2 = writeRIFFOddSize(
103
+ "lar2",
104
+ art2,
105
+ false,
106
+ true
107
+ );
108
+ }
109
+
110
+ return writeRIFFOddSize(
111
+ "rgn2",
112
+ combineArrays([
113
+ rgnh,
114
+ wsmp,
115
+ wlnk,
116
+ lar2
117
+ ]),
118
+ false,
119
+ true
120
+ );
121
+ }
@@ -0,0 +1,94 @@
1
+ import { combineArrays, IndexedByteArray } from "../../../utils/indexed_array.js";
2
+ import { writeDword, writeWord } from "../../../utils/byte_functions/little_endian.js";
3
+ import { writeRIFFOddSize } from "../riff_chunk.js";
4
+ import { writeWavesample } from "./wsmp.js";
5
+ import { getStringBytesZero } from "../../../utils/byte_functions/string.js";
6
+ import { SpessaSynthInfo } from "../../../utils/loggin.js";
7
+ import { consoleColors } from "../../../utils/other.js";
8
+
9
+ /**
10
+ * @param sample {BasicSample}
11
+ * @returns {IndexedByteArray}
12
+ */
13
+ export function writeDLSSample(sample)
14
+ {
15
+ const fmtData = new IndexedByteArray(18);
16
+ writeWord(fmtData, 1); // wFormatTag
17
+ writeWord(fmtData, 1); // wChannels
18
+ writeDword(fmtData, sample.sampleRate);
19
+ writeDword(fmtData, sample.sampleRate * 2); // 16-bit samples
20
+ writeWord(fmtData, 2); // wBlockAlign
21
+ writeWord(fmtData, 16); // wBitsPerSample
22
+ const fmt = writeRIFFOddSize(
23
+ "fmt ",
24
+ fmtData
25
+ );
26
+ let loop = 1;
27
+ if (sample.sampleLoopStartIndex + Math.abs(sample.getAudioData().length - sample.sampleLoopEndIndex) < 2)
28
+ {
29
+ loop = 0;
30
+ }
31
+ const wsmp = writeWavesample(
32
+ sample,
33
+ sample.samplePitch,
34
+ sample.samplePitchCorrection,
35
+ 0,
36
+ sample.sampleLoopStartIndex,
37
+ sample.sampleLoopEndIndex,
38
+ loop
39
+ );
40
+ const audio = sample.getAudioData();
41
+ let data;
42
+ // if sample is compressed, getRawData cannot be used
43
+ if (sample.isCompressed)
44
+ {
45
+ const data16 = new Int16Array(audio.length);
46
+
47
+ for (let i = 0; i < audio.length; i++)
48
+ {
49
+ // 32,767, as 32,768 may cause overflow (because vorbis can go above 1 sometimes)
50
+ data16[i] = audio[i] * 32767;
51
+ }
52
+
53
+
54
+ data = writeRIFFOddSize(
55
+ "data",
56
+ new IndexedByteArray(data16.buffer)
57
+ );
58
+ }
59
+ else
60
+ {
61
+ data = writeRIFFOddSize(
62
+ "data",
63
+ sample.getRawData()
64
+ );
65
+ }
66
+
67
+ const inam = writeRIFFOddSize(
68
+ "INAM",
69
+ getStringBytesZero(sample.sampleName)
70
+ );
71
+ const info = writeRIFFOddSize(
72
+ "INFO",
73
+ inam,
74
+ false,
75
+ true
76
+ );
77
+ SpessaSynthInfo(
78
+ `%cSaved %c${sample.sampleName}%c succesfully!`,
79
+ consoleColors.recognized,
80
+ consoleColors.value,
81
+ consoleColors.recognized
82
+ );
83
+ return writeRIFFOddSize(
84
+ "wave",
85
+ combineArrays([
86
+ fmt,
87
+ wsmp,
88
+ data,
89
+ info
90
+ ]),
91
+ false,
92
+ true
93
+ );
94
+ }
@@ -0,0 +1,119 @@
1
+ import { writeRIFFOddSize } from "../riff_chunk.js";
2
+ import { writeDword } from "../../../utils/byte_functions/little_endian.js";
3
+ import { combineArrays, IndexedByteArray } from "../../../utils/indexed_array.js";
4
+ import { writeLins } from "./lins.js";
5
+ import { getStringBytesZero, writeStringAsBytes } from "../../../utils/byte_functions/string.js";
6
+ import { writeWavePool } from "./wvpl.js";
7
+ import { SpessaSynthGroupCollapsed, SpessaSynthGroupEnd, SpessaSynthInfo } from "../../../utils/loggin.js";
8
+ import { consoleColors } from "../../../utils/other.js";
9
+
10
+ /**
11
+ * Write the soundfont as a .dls file. Experimental
12
+ * @this {BasicSoundBank}
13
+ * @returns {Uint8Array}
14
+ */
15
+ export function writeDLS()
16
+ {
17
+ SpessaSynthGroupCollapsed(
18
+ "%cSaving DLS...",
19
+ consoleColors.info
20
+ );
21
+ // write colh
22
+ const colhNum = new IndexedByteArray(4);
23
+ writeDword(colhNum, this.presets.length);
24
+ const colh = writeRIFFOddSize(
25
+ "colh",
26
+ colhNum
27
+ );
28
+ SpessaSynthGroupCollapsed(
29
+ "%cWriting instruments...",
30
+ consoleColors.info
31
+ );
32
+ const lins = writeLins.apply(this);
33
+ SpessaSynthInfo(
34
+ "%cSuccess!",
35
+ consoleColors.recognized
36
+ );
37
+ SpessaSynthGroupEnd();
38
+
39
+ SpessaSynthGroupCollapsed(
40
+ "%cWriting WAVE samples...",
41
+ consoleColors.info
42
+ );
43
+ const wavepool = writeWavePool.apply(this);
44
+ const wvpl = wavepool.data;
45
+ const ptblOffsets = wavepool.indexes;
46
+ SpessaSynthInfo("%cSucceeded!", consoleColors.recognized);
47
+ SpessaSynthGroupEnd();
48
+
49
+ // write ptbl
50
+ const ptblData = new IndexedByteArray(8 + 4 * ptblOffsets.length);
51
+ writeDword(ptblData, 8);
52
+ writeDword(ptblData, ptblOffsets.length);
53
+ for (const offset of ptblOffsets)
54
+ {
55
+ writeDword(ptblData, offset);
56
+ }
57
+ const ptbl = writeRIFFOddSize(
58
+ "ptbl",
59
+ ptblData
60
+ );
61
+
62
+ this.soundFontInfo["ICMT"] = (this.soundFontInfo["ICMT"] || "Soundfont") + "\nConverted from SF2 to DLS using SpessaSynth";
63
+ this.soundFontInfo["ISFT"] = "SpessaSynth";
64
+ // write INFO
65
+ const infos = [];
66
+ for (const [info, data] of Object.entries(this.soundFontInfo))
67
+ {
68
+ if (
69
+ info !== "ICMT" &&
70
+ info !== "INAM" &&
71
+ info !== "ICRD" &&
72
+ info !== "IENG" &&
73
+ info !== "ICOP" &&
74
+ info !== "ISFT" &&
75
+ info !== "ISBJ"
76
+ )
77
+ {
78
+ continue;
79
+ }
80
+ infos.push(
81
+ writeRIFFOddSize(
82
+ info,
83
+ getStringBytesZero(data),
84
+ true
85
+ )
86
+ );
87
+ }
88
+ const info = writeRIFFOddSize(
89
+ "INFO",
90
+ combineArrays(infos),
91
+ false,
92
+ true
93
+ );
94
+
95
+ const out = new IndexedByteArray(
96
+ colh.length
97
+ + lins.length
98
+ + ptbl.length
99
+ + wvpl.length
100
+ + info.length
101
+ + 4);
102
+ writeStringAsBytes(out, "DLS ");
103
+ out.set(combineArrays([
104
+ colh,
105
+ lins,
106
+ ptbl,
107
+ wvpl,
108
+ info
109
+ ]), 4);
110
+ SpessaSynthInfo(
111
+ "%cSaved succesfully!",
112
+ consoleColors.recognized
113
+ );
114
+ SpessaSynthGroupEnd();
115
+ return writeRIFFOddSize(
116
+ "RIFF",
117
+ out
118
+ );
119
+ }