spessasynth_core 1.1.3 → 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 -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,519 @@
1
+ /**
2
+ * voice.js
3
+ * purpose: prepares Voices from sample and generator data and manages sample dumping
4
+ * note: sample dumping means sending it over to the AudioWorkletGlobalScope
5
+ */
6
+ import { MIN_EXCLUSIVE_LENGTH, MIN_NOTE_LENGTH } from "../main_processor.js";
7
+ import { SpessaSynthWarn } from "../../../utils/loggin.js";
8
+ import { WorkletLowpassFilter } from "./lowpass_filter.js";
9
+ import { VolumeEnvelope } from "./volume_envelope.js";
10
+ import { ModulationEnvelope } from "./modulation_envelope.js";
11
+ import { addAndClampGenerator, generatorTypes } from "../../../soundfont/basic_soundfont/generator.js";
12
+ import { Modulator } from "../../../soundfont/basic_soundfont/modulator.js";
13
+ import { isSystemXG } from "../../../utils/xg_hacks.js";
14
+
15
+ const EXCLUSIVE_CUTOFF_TIME = -2320;
16
+ const EXCLUSIVE_MOD_CUTOFF_TIME = -1130; // less because filter shenanigans
17
+
18
+ class AudioSample
19
+ {
20
+ /**
21
+ * the sample's audio data
22
+ * @type {Float32Array}
23
+ */
24
+ sampleData;
25
+ /**
26
+ * Current playback step (rate)
27
+ * @type {number}
28
+ */
29
+ playbackStep = 0;
30
+ /**
31
+ * Current position in the sample
32
+ * @type {number}
33
+ */
34
+ cursor = 0;
35
+ /**
36
+ * MIDI root key of the sample
37
+ * @type {number}
38
+ */
39
+ rootKey = 0;
40
+ /**
41
+ * Start position of the loop
42
+ * @type {number}
43
+ */
44
+ loopStart = 0;
45
+ /**
46
+ * End position of the loop
47
+ * @type {number}
48
+ */
49
+ loopEnd = 0;
50
+ /**
51
+ * End position of the sample
52
+ * @type {number}
53
+ */
54
+ end = 0;
55
+ /**
56
+ * Looping mode of the sample:
57
+ * 0 - no loop
58
+ * 1 - loop
59
+ * 2 - UNOFFICIAL: polyphone 2.4 added start on release
60
+ * 3 - loop then play when released
61
+ * @type {0|1|2|3}
62
+ */
63
+ loopingMode = 0;
64
+ /**
65
+ * Indicates if the sample is currently looping
66
+ * @type {boolean}
67
+ */
68
+ isLooping = false;
69
+
70
+ /**
71
+ * @param data {Float32Array}
72
+ * @param playbackStep {number} the playback step, a single increment
73
+ * @param cursorStart {number} the sample id which starts the playback
74
+ * @param rootKey {number} MIDI root key
75
+ * @param loopStart {number} loop start index
76
+ * @param loopEnd {number} loop end index
77
+ * @param endIndex {number} sample end index (for end offset)
78
+ * @param loopingMode {number} sample looping mode
79
+ */
80
+ constructor(
81
+ data,
82
+ playbackStep,
83
+ cursorStart,
84
+ rootKey,
85
+ loopStart,
86
+ loopEnd,
87
+ endIndex,
88
+ loopingMode
89
+ )
90
+ {
91
+ this.sampleData = data;
92
+ this.playbackStep = playbackStep;
93
+ this.cursor = cursorStart;
94
+ this.rootKey = rootKey;
95
+ this.loopStart = loopStart;
96
+ this.loopEnd = loopEnd;
97
+ this.end = endIndex;
98
+ this.loopingMode = loopingMode;
99
+ this.isLooping = this.loopingMode === 1 || this.loopingMode === 3;
100
+ }
101
+ }
102
+
103
+
104
+ /**
105
+ * Voice represents a single instance of the
106
+ * SoundFont2 synthesis model.
107
+ * That is:
108
+ * A wavetable oscillator (sample)
109
+ * A volume envelope (volumeEnvelope)
110
+ * A modulation envelope (modulationEnvelope)
111
+ * Generators (generators and modulatedGenerators)
112
+ * Modulators (modulators)
113
+ * And MIDI params such as channel, MIDI note, velocity
114
+ */
115
+ class Voice
116
+ {
117
+ /**
118
+ * The sample of the voice.
119
+ * @type {AudioSample}
120
+ */
121
+ sample;
122
+
123
+ /**
124
+ * Lowpass filter applied to the voice.
125
+ * @type {WorkletLowpassFilter}
126
+ */
127
+ filter;
128
+
129
+ /**
130
+ * Linear gain of the voice. Used with Key Modifiers.
131
+ * @type {number}
132
+ */
133
+ gain = 1;
134
+
135
+ /**
136
+ * The unmodulated (copied to) generators of the voice.
137
+ * @type {Int16Array}
138
+ */
139
+ generators;
140
+
141
+ /**
142
+ * The voice's modulators.
143
+ * @type {Modulator[]}
144
+ */
145
+ modulators = [];
146
+
147
+ /**
148
+ * The generators in real-time, affected by modulators.
149
+ * This is used during rendering.
150
+ * @type {Int16Array}
151
+ */
152
+ modulatedGenerators;
153
+
154
+ /**
155
+ * Indicates if the voice is finished.
156
+ * @type {boolean}
157
+ */
158
+ finished = false;
159
+
160
+ /**
161
+ * Indicates if the voice is in the release phase.
162
+ * @type {boolean}
163
+ */
164
+ isInRelease = false;
165
+
166
+ /**
167
+ * MIDI channel number.
168
+ * @type {number}
169
+ */
170
+ channelNumber = 0;
171
+
172
+ /**
173
+ * Velocity of the note.
174
+ * @type {number}
175
+ */
176
+ velocity = 0;
177
+
178
+ /**
179
+ * MIDI note number.
180
+ * @type {number}
181
+ */
182
+ midiNote = 0;
183
+
184
+ /**
185
+ * The pressure of the voice
186
+ * @type {number}
187
+ */
188
+ pressure = 0;
189
+
190
+ /**
191
+ * Target key for the note.
192
+ * @type {number}
193
+ */
194
+ targetKey = 0;
195
+
196
+ /**
197
+ * Modulation envelope.
198
+ * @type {ModulationEnvelope}
199
+ */
200
+ modulationEnvelope = new ModulationEnvelope();
201
+
202
+ /**
203
+ * Volume envelope.
204
+ * @type {VolumeEnvelope}
205
+ */
206
+ volumeEnvelope;
207
+
208
+ /**
209
+ * Start time of the voice, absolute.
210
+ * @type {number}
211
+ */
212
+ startTime = 0;
213
+
214
+ /**
215
+ * Start time of the release phase, absolute.
216
+ * @type {number}
217
+ */
218
+ releaseStartTime = Infinity;
219
+
220
+ /**
221
+ * Current tuning in cents.
222
+ * @type {number}
223
+ */
224
+ currentTuningCents = 0;
225
+
226
+ /**
227
+ * Current calculated tuning. (as in ratio)
228
+ * @type {number}
229
+ */
230
+ currentTuningCalculated = 1;
231
+
232
+ /**
233
+ * From -500 to 500.
234
+ * @param {number}
235
+ */
236
+ currentPan = 0;
237
+
238
+ /**
239
+ * If MIDI Tuning Standard is already applied (at note-on time),
240
+ * this will be used to take the values at real-time tuning as "midiNote"
241
+ * property contains the tuned number.
242
+ * see #29 comment by @paulikaro
243
+ * @type {number}
244
+ */
245
+ realKey;
246
+
247
+ /**
248
+ * @type {number} Initial key to glide from, MIDI Note number. If -1, the portamento is OFF.
249
+ */
250
+ portamentoFromKey = -1;
251
+
252
+ /**
253
+ * Duration of the linear glide, in seconds.
254
+ * @type {number}
255
+ */
256
+ portamentoDuration = 0;
257
+
258
+ /**
259
+ * From -500 to 500, where zero means disabled (use the channel pan). Used for random pan.
260
+ * @type {number}
261
+ */
262
+ overridePan = 0;
263
+
264
+ /**
265
+ * Exclusive class number for hi-hats etc.
266
+ * @type {number}
267
+ */
268
+ exclusiveClass = 0;
269
+
270
+ /**
271
+ * Creates a Voice
272
+ * @param sampleRate {number}
273
+ * @param workletSample {AudioSample}
274
+ * @param midiNote {number}
275
+ * @param velocity {number}
276
+ * @param channel {number}
277
+ * @param currentTime {number}
278
+ * @param targetKey {number}
279
+ * @param realKey {number}
280
+ * @param generators {Int16Array}
281
+ * @param modulators {Modulator[]}
282
+ */
283
+ constructor(
284
+ sampleRate,
285
+ workletSample,
286
+ midiNote,
287
+ velocity,
288
+ channel,
289
+ currentTime,
290
+ targetKey,
291
+ realKey,
292
+ generators,
293
+ modulators
294
+ )
295
+ {
296
+ this.sample = workletSample;
297
+ this.generators = generators;
298
+ this.exclusiveClass = this.generators[generatorTypes.exclusiveClass];
299
+ this.modulatedGenerators = new Int16Array(generators);
300
+ this.modulators = modulators;
301
+ this.filter = new WorkletLowpassFilter(sampleRate);
302
+
303
+ this.velocity = velocity;
304
+ this.midiNote = midiNote;
305
+ this.channelNumber = channel;
306
+ this.startTime = currentTime;
307
+ this.targetKey = targetKey;
308
+ this.realKey = realKey;
309
+ this.volumeEnvelope = new VolumeEnvelope(sampleRate, generators[generatorTypes.sustainVolEnv]);
310
+ }
311
+
312
+ /**
313
+ * copies the voice
314
+ * @param voice {Voice}
315
+ * @param currentTime {number}
316
+ * @param realKey {number}
317
+ * @returns Voice
318
+ */
319
+ static copy(voice, currentTime, realKey)
320
+ {
321
+ const sampleToCopy = voice.sample;
322
+ const sample = new AudioSample(
323
+ sampleToCopy.sampleData,
324
+ sampleToCopy.playbackStep,
325
+ sampleToCopy.cursor,
326
+ sampleToCopy.rootKey,
327
+ sampleToCopy.loopStart,
328
+ sampleToCopy.loopEnd,
329
+ sampleToCopy.end,
330
+ sampleToCopy.loopingMode
331
+ );
332
+ return new Voice(
333
+ voice.volumeEnvelope.sampleRate,
334
+ sample,
335
+ voice.midiNote,
336
+ voice.velocity,
337
+ voice.channelNumber,
338
+ currentTime,
339
+ voice.targetKey,
340
+ realKey,
341
+ voice.generators,
342
+ voice.modulators.map(m => Modulator.copy(m))
343
+ );
344
+ }
345
+
346
+ /**
347
+ * Releases the voice as exclusiveClass
348
+ * @param currentTime {number}
349
+ */
350
+ exclusiveRelease(currentTime)
351
+ {
352
+ this.release(currentTime, MIN_EXCLUSIVE_LENGTH);
353
+ this.modulatedGenerators[generatorTypes.releaseVolEnv] = EXCLUSIVE_CUTOFF_TIME; // make the release nearly instant
354
+ this.modulatedGenerators[generatorTypes.releaseModEnv] = EXCLUSIVE_MOD_CUTOFF_TIME;
355
+ VolumeEnvelope.recalculate(this);
356
+ ModulationEnvelope.recalculate(this);
357
+ }
358
+
359
+ /**
360
+ * Stops the voice
361
+ * @param currentTime {number}
362
+ * @param minNoteLength {number} minimum note length in seconds
363
+ */
364
+ release(currentTime, minNoteLength = MIN_NOTE_LENGTH)
365
+ {
366
+ this.releaseStartTime = currentTime;
367
+ // check if the note is shorter than the min note time, if so, extend it
368
+ if (this.releaseStartTime - this.startTime < minNoteLength)
369
+ {
370
+ this.releaseStartTime = this.startTime + minNoteLength;
371
+ }
372
+ }
373
+ }
374
+
375
+ /**
376
+ * @param channel {number} a hint for the processor to recalculate sample cursors when sample dumping
377
+ * @param midiNote {number} the MIDI note to use
378
+ * @param velocity {number} the velocity to use
379
+ * @param realKey {number} the real MIDI note if the "midiNote" was changed by MIDI Tuning Standard
380
+ * @this {SpessaSynthProcessor}
381
+ * @returns {Voice[]} output is an array of Voices
382
+ */
383
+ export function getVoices(channel,
384
+ midiNote,
385
+ velocity,
386
+ realKey)
387
+ {
388
+ /**
389
+ * @type {Voice[]}
390
+ */
391
+ let voices;
392
+ const channelObject = this.midiAudioChannels[channel];
393
+
394
+ // override patch
395
+ const overridePatch = this.keyModifierManager.hasOverridePatch(channel, midiNote);
396
+
397
+ let bank = channelObject.getBankSelect();
398
+ let program = channelObject.preset.program;
399
+ if (overridePatch)
400
+ {
401
+ const override = this.keyModifierManager.getPatch(channel, midiNote);
402
+ bank = override.bank;
403
+ program = override.program;
404
+ }
405
+
406
+ const cached = this.getCachedVoice(bank, program, midiNote, velocity);
407
+ // if cached, return it!
408
+ if (cached !== undefined)
409
+ {
410
+ return cached.map(v => Voice.copy(v, this.currentSynthTime, realKey));
411
+ }
412
+
413
+ // not cached...
414
+ let preset = channelObject.preset;
415
+ if (overridePatch)
416
+ {
417
+ preset = this.soundfontManager.getPreset(bank, program, isSystemXG(this.system));
418
+ }
419
+ /**
420
+ * @returns {Voice[]}
421
+ */
422
+ voices = preset.getSamplesAndGenerators(midiNote, velocity)
423
+ .reduce((voices, sampleAndGenerators) =>
424
+ {
425
+ if (sampleAndGenerators.sample.getAudioData() === undefined)
426
+ {
427
+ SpessaSynthWarn(`Discarding invalid sample: ${sampleAndGenerators.sample.sampleName}`);
428
+ return voices;
429
+ }
430
+
431
+ // create the generator list
432
+ const generators = new Int16Array(60);
433
+ // apply and sum the gens
434
+ for (let i = 0; i < 60; i++)
435
+ {
436
+ generators[i] = addAndClampGenerator(
437
+ i,
438
+ sampleAndGenerators.presetGenerators,
439
+ sampleAndGenerators.instrumentGenerators
440
+ );
441
+ }
442
+
443
+ // !! EMU initial attenuation correction, multiply initial attenuation by 0.4
444
+ generators[generatorTypes.initialAttenuation] = Math.floor(generators[generatorTypes.initialAttenuation] * 0.4);
445
+
446
+ // key override
447
+ let rootKey = sampleAndGenerators.sample.samplePitch;
448
+ if (generators[generatorTypes.overridingRootKey] > -1)
449
+ {
450
+ rootKey = generators[generatorTypes.overridingRootKey];
451
+ }
452
+
453
+ let targetKey = midiNote;
454
+ if (generators[generatorTypes.keyNum] > -1)
455
+ {
456
+ targetKey = generators[generatorTypes.keyNum];
457
+ }
458
+
459
+ // determine looping mode now. if the loop is too small, disable
460
+ let loopStart = sampleAndGenerators.sample.sampleLoopStartIndex;
461
+ let loopEnd = sampleAndGenerators.sample.sampleLoopEndIndex;
462
+ let loopingMode = generators[generatorTypes.sampleModes];
463
+ /**
464
+ * create the worklet sample
465
+ * offsets are calculated at note on time (to allow for modulation of them)
466
+ * @type {AudioSample}
467
+ */
468
+ const workletSample = new AudioSample(
469
+ sampleAndGenerators.sample.sampleData,
470
+ (sampleAndGenerators.sample.sampleRate / this.sampleRate) * Math.pow(
471
+ 2,
472
+ sampleAndGenerators.sample.samplePitchCorrection / 1200
473
+ ), // cent tuning
474
+ 0,
475
+ rootKey,
476
+ loopStart,
477
+ loopEnd,
478
+ Math.floor(sampleAndGenerators.sample.sampleData.length) - 1,
479
+ loopingMode
480
+ );
481
+ // velocity override
482
+ if (generators[generatorTypes.velocity] > -1)
483
+ {
484
+ velocity = generators[generatorTypes.velocity];
485
+ }
486
+
487
+ // uncomment to print debug info
488
+ // SpessaSynthTable([{
489
+ // Sample: sampleAndGenerators.sample.sampleName,
490
+ // Generators: generators,
491
+ // Modulators: sampleAndGenerators.modulators.map(m => Modulator.debugString(m)),
492
+ // Velocity: velocity,
493
+ // TargetKey: targetKey,
494
+ // MidiNote: midiNote,
495
+ // AudioSample: workletSample
496
+ // }]);
497
+
498
+
499
+ voices.push(
500
+ new Voice(
501
+ this.sampleRate,
502
+ workletSample,
503
+ midiNote,
504
+ velocity,
505
+ channel,
506
+ this.currentSynthTime,
507
+ targetKey,
508
+ realKey,
509
+ generators,
510
+ sampleAndGenerators.modulators.map(m => Modulator.copy(m))
511
+ )
512
+ );
513
+ return voices;
514
+ }, []);
515
+ // cache the voice
516
+ this.setCachedVoice(bank, program, midiNote, velocity, voices.map(v =>
517
+ Voice.copy(v, this.currentSynthTime, realKey)));
518
+ return voices;
519
+ }