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,150 @@
1
+ /**
2
+ * A manager for custom key overrides for channels
3
+ */
4
+
5
+ export class KeyModifier
6
+ {
7
+
8
+ /**
9
+ * The new override velocity. -1 means unchanged
10
+ * @type {number}
11
+ */
12
+ velocity = -1;
13
+ /**
14
+ * The patch this key uses. -1 on either means default
15
+ * @type {{bank: number, program: number}}
16
+ */
17
+ patch = { bank: -1, program: -1 };
18
+
19
+ /**
20
+ * Linear gain override for the voice
21
+ */
22
+ gain = 1;
23
+
24
+ /**
25
+ * @param velocity {number}
26
+ * @param bank {number}
27
+ * @param program {number}
28
+ * @param gain {number}
29
+ */
30
+ constructor(velocity = -1, bank = -1, program = -1, gain = 1)
31
+ {
32
+ this.velocity = velocity;
33
+ this.patch = {
34
+ bank: bank,
35
+ program: program
36
+ };
37
+ this.gain = gain;
38
+ }
39
+ }
40
+
41
+ export class KeyModifierManager
42
+ {
43
+ /**
44
+ * The velocity override mappings for MIDI keys
45
+ * @type {KeyModifier[][]}
46
+ * @private
47
+ */
48
+ _keyMappings = [];
49
+
50
+ // noinspection JSUnusedGlobalSymbols
51
+ /**
52
+ * @param channel {number}
53
+ * @param midiNote {number}
54
+ * @param mapping {KeyModifier}
55
+ */
56
+ addMapping(channel, midiNote, mapping)
57
+ {
58
+ if (this._keyMappings[channel] === undefined)
59
+ {
60
+ this._keyMappings[channel] = [];
61
+ }
62
+ this._keyMappings[channel][midiNote] = mapping;
63
+ }
64
+
65
+ // noinspection JSUnusedGlobalSymbols
66
+ /**
67
+ * @param channel {number}
68
+ * @param midiNote {number}
69
+ */
70
+ deleteMapping(channel, midiNote)
71
+ {
72
+ if (this._keyMappings[channel]?.[midiNote] === undefined)
73
+ {
74
+ return;
75
+ }
76
+ this._keyMappings[channel][midiNote] = undefined;
77
+ }
78
+
79
+ // noinspection JSUnusedGlobalSymbols
80
+ /**
81
+ * Clear all mappings
82
+ */
83
+ clearMappings()
84
+ {
85
+ this._keyMappings = [];
86
+ }
87
+
88
+ /**
89
+ * @param mappings {KeyModifier[][]}
90
+ */
91
+ setMappings(mappings)
92
+ {
93
+ this._keyMappings = mappings;
94
+ }
95
+
96
+ /**
97
+ * @returns {KeyModifier[][]}
98
+ */
99
+ getMappings()
100
+ {
101
+ return this._keyMappings;
102
+ }
103
+
104
+ /**
105
+ * @param channel {number}
106
+ * @param midiNote {number}
107
+ * @returns {number} velocity, -1 if unchanged
108
+ */
109
+ getVelocity(channel, midiNote)
110
+ {
111
+ return this._keyMappings[channel]?.[midiNote]?.velocity ?? -1;
112
+ }
113
+
114
+ /**
115
+ * @param channel {number}
116
+ * @param midiNote {number}
117
+ * @returns {number} linear gain
118
+ */
119
+ getGain(channel, midiNote)
120
+ {
121
+ return this._keyMappings[channel]?.[midiNote]?.gain ?? 1;
122
+ }
123
+
124
+ /**
125
+ * @param channel {number}
126
+ * @param midiNote {number}
127
+ * @returns {boolean}
128
+ */
129
+ hasOverridePatch(channel, midiNote)
130
+ {
131
+ const bank = this._keyMappings[channel]?.[midiNote]?.patch?.bank;
132
+ return bank !== undefined && bank >= 0;
133
+ }
134
+
135
+ /**
136
+ * @param channel {number}
137
+ * @param midiNote {number}
138
+ * @returns {{bank: number, program: number}} -1 if unchanged
139
+ */
140
+ getPatch(channel, midiNote)
141
+ {
142
+ const modifier = this._keyMappings[channel]?.[midiNote];
143
+ if (modifier)
144
+ {
145
+ return modifier.patch;
146
+ }
147
+ throw new Error("No modifier.");
148
+ }
149
+
150
+ }
@@ -10,14 +10,17 @@
10
10
  * @param currentTime {number} seconds
11
11
  * @return {number} the value from -1 to 1
12
12
  */
13
- export function getLFOValue(startTime, frequency, currentTime) {
14
- if (currentTime < startTime) {
13
+ export function getLFOValue(startTime, frequency, currentTime)
14
+ {
15
+ if (currentTime < startTime)
16
+ {
15
17
  return 0;
16
18
  }
17
-
18
- const xVal = (currentTime - startTime) / (1 / frequency) - 0.25;
19
- // offset by -0.25, otherwise we start at -1 and can have unexpected jump in pitch or lowpass (happened with Synth Strings 2)
20
-
19
+
20
+ const xVal = (currentTime - startTime) / (1 / frequency) + 0.25;
21
+ // offset by -0.25, otherwise we start at -1 and can have unexpected jump in pitch or low-pass
22
+ // (happened with Synth Strings 2)
23
+
21
24
  // triangle, not sine
22
25
  return Math.abs(xVal - (~~(xVal + 0.5))) * 4 - 1;
23
26
  }
@@ -0,0 +1,282 @@
1
+ import { absCentsToHz, decibelAttenuationToGain } from "./unit_converter.js";
2
+ import { generatorTypes } from "../../../soundfont/basic_soundfont/generator.js";
3
+
4
+ /**
5
+ * lowpass_filter.js
6
+ * purpose: applies a low pass filter to a voice
7
+ * note to self: a lot of tricks and come from fluidsynth.
8
+ * They are the real smart guys.
9
+ * Shoutout to them!
10
+ * Give their repo a star over at:
11
+ * https://github.com/FluidSynth/fluidsynth
12
+ */
13
+
14
+ export const FILTER_SMOOTHING_FACTOR = 0.1;
15
+
16
+ /**
17
+ * @typedef {Object} CachedCoefficient
18
+ * @property {number} a0 - Filter coefficient 1
19
+ * @property {number} a1 - Filter coefficient 2
20
+ * @property {number} a2 - Filter coefficient 3
21
+ * @property {number} a3 - Filter coefficient 4
22
+ * @property {number} a4 - Filter coefficient 5
23
+ */
24
+
25
+ export class WorkletLowpassFilter
26
+ {
27
+ /**
28
+ * Cached coefficient calculations
29
+ * stored as cachedCoefficients[resonanceCb][currentInitialFc]
30
+ * @type {CachedCoefficient[][]}
31
+ * @private
32
+ */
33
+ static cachedCoefficients = [];
34
+ /**
35
+ * Filter coefficient 1
36
+ * @type {number}
37
+ */
38
+ a0 = 0;
39
+
40
+ /**
41
+ * Filter coefficient 2
42
+ * @type {number}
43
+ */
44
+ a1 = 0;
45
+
46
+ /**
47
+ * Filter coefficient 3
48
+ * @type {number}
49
+ */
50
+ a2 = 0;
51
+
52
+ /**
53
+ * Filter coefficient 4
54
+ * @type {number}
55
+ */
56
+ a3 = 0;
57
+
58
+ /**
59
+ * Filter coefficient 5
60
+ * @type {number}
61
+ */
62
+ a4 = 0;
63
+
64
+ /**
65
+ * Input history 1
66
+ * @type {number}
67
+ */
68
+ x1 = 0;
69
+
70
+ /**
71
+ * Input history 2
72
+ * @type {number}
73
+ */
74
+ x2 = 0;
75
+
76
+ /**
77
+ * Output history 1
78
+ * @type {number}
79
+ */
80
+ y1 = 0;
81
+
82
+ /**
83
+ * Output history 2
84
+ * @type {number}
85
+ */
86
+ y2 = 0;
87
+
88
+ /**
89
+ * Resonance in centibels
90
+ * @type {number}
91
+ */
92
+ resonanceCb = 0;
93
+
94
+ /**
95
+ * Cutoff frequency in absolute cents
96
+ * @type {number}
97
+ */
98
+ currentInitialFc = 13500;
99
+
100
+ /**
101
+ * For tracking the last cutoff frequency in the apply method, absolute cents
102
+ * Set to infinity to force recalculation
103
+ * @type {number}
104
+ */
105
+ lastTargetCutoff = Infinity;
106
+
107
+ /**
108
+ * used for tracking if the filter has been initialized
109
+ * @type {boolean}
110
+ */
111
+ initialized = false;
112
+ /**
113
+ * Hertz
114
+ * @type {number}
115
+ */
116
+ sampleRate;
117
+
118
+ /**
119
+ * @param sampleRate {number}
120
+ */
121
+ constructor(sampleRate)
122
+ {
123
+ this.sampleRate = sampleRate;
124
+ /**
125
+ * @type {number}
126
+ */
127
+ this.maxCutoff = sampleRate * 0.45;
128
+ }
129
+
130
+ /**
131
+ * Applies a low-pass filter to the given buffer
132
+ * @param voice {Voice} the voice we're working on
133
+ * @param outputBuffer {Float32Array} the buffer to apply the filter to
134
+ * @param fcExcursion {number} the addition of modenv and mod lfo in cents to the filter
135
+ * @param smoothingFactor {number} filter's cutoff frequency smoothing factor
136
+ */
137
+ static apply(voice, outputBuffer, fcExcursion, smoothingFactor)
138
+ {
139
+ const initialFc = voice.modulatedGenerators[generatorTypes.initialFilterFc];
140
+ const filter = voice.filter;
141
+
142
+
143
+ if (!filter.initialized)
144
+ {
145
+ // filter initialization, set the current fc to target
146
+ filter.initialized = true;
147
+ filter.currentInitialFc = initialFc;
148
+ }
149
+ else
150
+ {
151
+ /* Note:
152
+ * We only smooth out the initialFc part,
153
+ * the modulation envelope and LFO excursions are not smoothed.
154
+ */
155
+ filter.currentInitialFc += (initialFc - filter.currentInitialFc) * smoothingFactor;
156
+ }
157
+
158
+ // the final cutoff for this calculation
159
+ const targetCutoff = filter.currentInitialFc + fcExcursion;
160
+ const modulatedResonance = voice.modulatedGenerators[generatorTypes.initialFilterQ];
161
+ /* note:
162
+ * the check for initialFC is because of the filter optimization
163
+ * (if cents are the maximum then the filter is open)
164
+ * filter cannot use this optimization if it's dynamic (see #53), and
165
+ * the filter can only be dynamic if the initial filter is not open
166
+ */
167
+ if (filter.currentInitialFc > 13499 && targetCutoff > 13499 && modulatedResonance === 0)
168
+ {
169
+ filter.currentInitialFc = 13500;
170
+ return; // filter is open
171
+ }
172
+
173
+ // check if the frequency has changed. if so, calculate new coefficients
174
+ if (Math.abs(filter.lastTargetCutoff - targetCutoff) > 1 || filter.resonanceCb !== modulatedResonance)
175
+ {
176
+ filter.lastTargetCutoff = targetCutoff;
177
+ filter.resonanceCb = modulatedResonance;
178
+ WorkletLowpassFilter.calculateCoefficients(filter, targetCutoff);
179
+ }
180
+
181
+ // filter the input
182
+ // initial filtering code was ported from meltysynth created by sinshu.
183
+ for (let i = 0; i < outputBuffer.length; i++)
184
+ {
185
+ let input = outputBuffer[i];
186
+ let filtered = filter.a0 * input
187
+ + filter.a1 * filter.x1
188
+ + filter.a2 * filter.x2
189
+ - filter.a3 * filter.y1
190
+ - filter.a4 * filter.y2;
191
+
192
+ // set buffer
193
+ filter.x2 = filter.x1;
194
+ filter.x1 = input;
195
+ filter.y2 = filter.y1;
196
+ filter.y1 = filtered;
197
+
198
+ outputBuffer[i] = filtered;
199
+ }
200
+ }
201
+
202
+ /**
203
+ * @param filter {WorkletLowpassFilter}
204
+ * @param cutoffCents {number}
205
+ */
206
+ static calculateCoefficients(filter, cutoffCents)
207
+ {
208
+ cutoffCents = ~~cutoffCents; // Math.floor
209
+ const qCb = filter.resonanceCb;
210
+ // check if these coefficients were already cached
211
+ const cached = WorkletLowpassFilter.cachedCoefficients?.[qCb]?.[cutoffCents];
212
+ if (cached !== undefined)
213
+ {
214
+ filter.a0 = cached.a0;
215
+ filter.a1 = cached.a1;
216
+ filter.a2 = cached.a2;
217
+ filter.a3 = cached.a3;
218
+ filter.a4 = cached.a4;
219
+ return;
220
+ }
221
+ let cutoffHz = absCentsToHz(cutoffCents);
222
+
223
+ // fix cutoff on low sample rates
224
+ cutoffHz = Math.min(cutoffHz, filter.maxCutoff);
225
+
226
+ // the coefficient calculation code was originally ported from meltysynth by sinshu.
227
+ // turn resonance to gain, -3.01 so it gives a non-resonant peak
228
+ const qDb = qCb / 10;
229
+ // -1 because it's attenuation, and we don't want attenuation
230
+ const resonanceGain = decibelAttenuationToGain(-(qDb - 3.01));
231
+
232
+ // the sfspec asks for a reduction in gain based on the Q value.
233
+ // note that we calculate it again,
234
+ // without the 3.01-peak offset as it only applies to the coefficients, not the gain.
235
+ const qGain = 1 / Math.sqrt(decibelAttenuationToGain(-qDb));
236
+
237
+
238
+ // note: no sin or cos tables here as the coefficients are cached
239
+ let w = 2 * Math.PI * cutoffHz / filter.sampleRate;
240
+ let cosw = Math.cos(w);
241
+ let alpha = Math.sin(w) / (2 * resonanceGain);
242
+
243
+ let b1 = (1 - cosw) * qGain;
244
+ let b0 = b1 / 2;
245
+ let b2 = b0;
246
+ let a0 = 1 + alpha;
247
+ let a1 = -2 * cosw;
248
+ let a2 = 1 - alpha;
249
+
250
+ /**
251
+ * set coefficients
252
+ * @type {CachedCoefficient}
253
+ */
254
+ const toCache = {};
255
+ toCache.a0 = b0 / a0;
256
+ toCache.a1 = b1 / a0;
257
+ toCache.a2 = b2 / a0;
258
+ toCache.a3 = a1 / a0;
259
+ toCache.a4 = a2 / a0;
260
+ filter.a0 = toCache.a0;
261
+ filter.a1 = toCache.a1;
262
+ filter.a2 = toCache.a2;
263
+ filter.a3 = toCache.a3;
264
+ filter.a4 = toCache.a4;
265
+
266
+ if (WorkletLowpassFilter.cachedCoefficients[qCb] === undefined)
267
+ {
268
+ WorkletLowpassFilter.cachedCoefficients[qCb] = [];
269
+ }
270
+ WorkletLowpassFilter.cachedCoefficients[qCb][cutoffCents] = toCache;
271
+ }
272
+ }
273
+
274
+ // precompute all the cutoffs for 0q (most common)
275
+ const dummy = new WorkletLowpassFilter(44100);
276
+ dummy.resonanceCb = 0;
277
+ // sfspec section 8.1.3: initialFilterFc ranges from 1500 to 13,500 cents
278
+ for (let i = 1500; i < 13500; i++)
279
+ {
280
+ dummy.currentInitialFc = i;
281
+ WorkletLowpassFilter.calculateCoefficients(dummy, i);
282
+ }