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,15 @@
1
+ import { SpessaSynthInfo } from "../../../utils/loggin.js";
2
+ import { consoleColors } from "../../../utils/other.js";
3
+ import { SynthesizerSnapshot } from "./synthesizer_snapshot.js";
4
+
5
+ /**
6
+ * Applies the snapshot to the synth
7
+ * @param snapshot {SynthesizerSnapshot}
8
+ * @this {SpessaSynthProcessor}
9
+ */
10
+ export function applySynthesizerSnapshot(snapshot)
11
+ {
12
+ SynthesizerSnapshot.applySnapshot(this, snapshot);
13
+ SpessaSynthInfo("%cFinished applying snapshot!", consoleColors.info);
14
+ this.resetAllControllers();
15
+ }
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Represents a snapshot of a single channel's state in the synthesizer.
3
+ */
4
+ export class ChannelSnapshot
5
+ {
6
+ /**
7
+ * The channel's MIDI program number.
8
+ * @type {number}
9
+ */
10
+ program;
11
+
12
+ /**
13
+ * The channel's bank number.
14
+ * @type {number}
15
+ */
16
+ bank;
17
+
18
+ /**
19
+ * If the bank is LSB. For restoring.
20
+ * @type {boolean}
21
+ */
22
+ isBankLSB;
23
+
24
+ /**
25
+ * The name of the patch currently loaded in the channel.
26
+ * @type {string}
27
+ */
28
+ patchName;
29
+
30
+ /**
31
+ * Indicates whether the channel's program change is disabled.
32
+ * @type {boolean}
33
+ */
34
+ lockPreset;
35
+
36
+ /**
37
+ * Indicates the MIDI system when the preset was locked
38
+ * @type {SynthSystem}
39
+ */
40
+ lockedSystem;
41
+
42
+ /**
43
+ * The array of all MIDI controllers (in 14-bit values) with the modulator sources at the end.
44
+ * @type {Int16Array}
45
+ */
46
+ midiControllers;
47
+
48
+ /**
49
+ * An array of booleans, indicating if the controller with a current index is locked.
50
+ * @type {boolean[]}
51
+ */
52
+ lockedControllers;
53
+
54
+ /**
55
+ * Array of custom (not SF2) control values such as RPN pitch tuning, transpose, modulation depth, etc.
56
+ * @type {Float32Array}
57
+ */
58
+ customControllers;
59
+
60
+ /**
61
+ * Indicates whether the channel vibrato is locked.
62
+ * @type {boolean}
63
+ */
64
+ lockVibrato;
65
+
66
+ /**
67
+ * The channel's vibrato settings.
68
+ * @type {Object}
69
+ * @property {number} depth - Vibrato depth, in gain.
70
+ * @property {number} delay - Vibrato delay from note on in seconds.
71
+ * @property {number} rate - Vibrato rate in Hz.
72
+ */
73
+ channelVibrato;
74
+
75
+ /**
76
+ * Key shift for the channel.
77
+ * @type {number}
78
+ */
79
+ channelTransposeKeyShift;
80
+
81
+ /**
82
+ * The channel's octave tuning in cents.
83
+ * @type {Int8Array}
84
+ */
85
+ channelOctaveTuning;
86
+
87
+ /**
88
+ * Indicates whether the channel is muted.
89
+ * @type {boolean}
90
+ */
91
+ isMuted;
92
+
93
+ /**
94
+ * Overrides velocity if greater than 0, otherwise disabled.
95
+ * @type {number}
96
+ */
97
+ velocityOverride;
98
+
99
+ /**
100
+ * Indicates whether the channel is a drum channel.
101
+ * @type {boolean}
102
+ */
103
+ drumChannel;
104
+
105
+ /**
106
+ * Creates a snapshot of a single channel's state in the synthesizer.
107
+ * @param workletProcessor {SpessaSynthProcessor}
108
+ * @param channelNumber {number}
109
+ * @returns {ChannelSnapshot}
110
+ */
111
+ static getChannelSnapshot(workletProcessor, channelNumber)
112
+ {
113
+ const channelObject = workletProcessor.midiAudioChannels[channelNumber];
114
+ const channelSnapshot = new ChannelSnapshot();
115
+ // program data
116
+ channelSnapshot.program = channelObject.preset.program;
117
+ channelSnapshot.bank = channelObject.getBankSelect();
118
+ channelSnapshot.isBankLSB = channelSnapshot.bank !== channelObject.bank;
119
+ channelSnapshot.lockPreset = channelObject.lockPreset;
120
+ channelSnapshot.lockedSystem = channelObject.lockedSystem;
121
+ channelSnapshot.patchName = channelObject.preset.presetName;
122
+
123
+ // controller data
124
+ channelSnapshot.midiControllers = channelObject.midiControllers;
125
+ channelSnapshot.lockedControllers = channelObject.lockedControllers;
126
+ channelSnapshot.customControllers = channelObject.customControllers;
127
+
128
+ // vibrato data
129
+ channelSnapshot.channelVibrato = channelObject.channelVibrato;
130
+ channelSnapshot.lockVibrato = channelObject.lockGSNRPNParams;
131
+
132
+ // tuning and transpose data
133
+ channelSnapshot.channelTransposeKeyShift = channelObject.channelTransposeKeyShift;
134
+ channelSnapshot.channelOctaveTuning = channelObject.channelOctaveTuning;
135
+
136
+ // other data
137
+ channelSnapshot.isMuted = channelObject.isMuted;
138
+ channelSnapshot.velocityOverride = channelObject.velocityOverride;
139
+ channelSnapshot.drumChannel = channelObject.drumChannel;
140
+ return channelSnapshot;
141
+ }
142
+
143
+ /**
144
+ * Applies the snapshot to the specified channel.
145
+ * @param workletProcessor {SpessaSynthProcessor}
146
+ * @param channelNumber {number}
147
+ * @param channelSnapshot {ChannelSnapshot}
148
+ */
149
+ static applyChannelSnapshot(workletProcessor, channelNumber, channelSnapshot)
150
+ {
151
+ const channelObject = workletProcessor.midiAudioChannels[channelNumber];
152
+ channelObject.muteChannel(channelSnapshot.isMuted);
153
+ channelObject.setDrums(channelSnapshot.drumChannel);
154
+
155
+ // restore controllers
156
+ channelObject.midiControllers = channelSnapshot.midiControllers;
157
+ channelObject.lockedControllers = channelSnapshot.lockedControllers;
158
+ channelObject.customControllers = channelSnapshot.customControllers;
159
+ channelObject.updateChannelTuning();
160
+
161
+ // restore vibrato and transpose
162
+ channelObject.channelVibrato = channelSnapshot.channelVibrato;
163
+ channelObject.lockGSNRPNParams = channelSnapshot.lockVibrato;
164
+ channelObject.channelTransposeKeyShift = channelSnapshot.channelTransposeKeyShift;
165
+ channelObject.channelOctaveTuning = channelSnapshot.channelOctaveTuning;
166
+ channelObject.velocityOverride = channelSnapshot.velocityOverride;
167
+
168
+ // restore preset and lock
169
+ channelObject.setPresetLock(false);
170
+ channelObject.setBankSelect(channelSnapshot.bank, channelSnapshot.isBankLSB);
171
+ channelObject.programChange(channelSnapshot.program);
172
+ channelObject.setPresetLock(channelSnapshot.lockPreset);
173
+ channelObject.lockedSystem = channelSnapshot.lockedSystem;
174
+ }
175
+ }
@@ -0,0 +1,116 @@
1
+ import { SpessaSynthInfo } from "../../../utils/loggin.js";
2
+ import { consoleColors } from "../../../utils/other.js";
3
+ import { ChannelSnapshot } from "./channel_snapshot.js";
4
+ import { masterParameterType } from "../engine_methods/controller_control/master_parameters.js";
5
+
6
+ /**
7
+ * Represents a snapshot of the synthesizer's state.
8
+ */
9
+ export class SynthesizerSnapshot
10
+ {
11
+ /**
12
+ * The individual channel snapshots.
13
+ * @type {ChannelSnapshot[]}
14
+ */
15
+ channelSnapshots;
16
+
17
+ /**
18
+ * Key modifiers.
19
+ * @type {KeyModifier[][]}
20
+ */
21
+ keyMappings;
22
+
23
+ /**
24
+ * Main synth volume (set by MIDI), from 0 to 1.
25
+ * @type {number}
26
+ */
27
+ mainVolume;
28
+
29
+ /**
30
+ * Master stereo panning, from -1 to 1.
31
+ * @type {number}
32
+ */
33
+ pan;
34
+
35
+ /**
36
+ * The synth's interpolation type.
37
+ * @type {interpolationTypes}
38
+ */
39
+ interpolation;
40
+
41
+ /**
42
+ * The synth's system. Values can be "gs", "gm", "gm2" or "xg".
43
+ * @type {SynthSystem}
44
+ */
45
+ system;
46
+
47
+ /**
48
+ * The current synth transposition in semitones. Can be a float.
49
+ * @type {number}
50
+ */
51
+ transposition;
52
+
53
+
54
+ /**
55
+ * Creates a snapshot of the synthesizer's state.
56
+ * @param workletProcessor {SpessaSynthProcessor}
57
+ * @returns {SynthesizerSnapshot}
58
+ */
59
+ static createSynthesizerSnapshot(workletProcessor)
60
+ {
61
+ const snapshot = new SynthesizerSnapshot();
62
+ // channel snapshots
63
+ snapshot.channelSnapshots =
64
+ workletProcessor.midiAudioChannels.map((_, i) =>
65
+ ChannelSnapshot.getChannelSnapshot(workletProcessor, i));
66
+
67
+ // key mappings
68
+ snapshot.keyMappings = workletProcessor.keyModifierManager.getMappings();
69
+ // pan and volume
70
+ snapshot.mainVolume = workletProcessor.midiVolume;
71
+ snapshot.pan = workletProcessor.pan;
72
+
73
+ // others
74
+ snapshot.system = workletProcessor.system;
75
+ snapshot.interpolation = workletProcessor.interpolationType;
76
+ snapshot.transposition = workletProcessor.transposition;
77
+
78
+ // effect config is stored on the main thread, leave it empty
79
+ snapshot.effectsConfig = {};
80
+ return snapshot;
81
+
82
+ }
83
+
84
+ /**
85
+ * Applies the snapshot to the synthesizer.
86
+ * @param workletProcessor {SpessaSynthProcessor}
87
+ * @param snapshot {SynthesizerSnapshot}
88
+ */
89
+ static applySnapshot(workletProcessor, snapshot)
90
+ {
91
+ // restore system
92
+ workletProcessor.setSystem(snapshot.system);
93
+
94
+ // restore pan and volume
95
+ workletProcessor.setMasterParameter(masterParameterType.mainVolume, snapshot.mainVolume);
96
+ workletProcessor.setMasterParameter(masterParameterType.masterPan, snapshot.pan);
97
+ workletProcessor.transposeAllChannels(snapshot.transposition);
98
+ workletProcessor.interpolationType = snapshot.interpolation;
99
+ workletProcessor.keyModifierManager.setMappings(snapshot.keyMappings);
100
+
101
+ // add channels if more needed
102
+ while (workletProcessor.midiAudioChannels.length < snapshot.channelSnapshots.length)
103
+ {
104
+ workletProcessor.createWorkletChannel();
105
+ }
106
+
107
+ // restore channels
108
+ snapshot.channelSnapshots.forEach((channelSnapshot, index) =>
109
+ {
110
+ ChannelSnapshot.applyChannelSnapshot(workletProcessor, index, channelSnapshot);
111
+ });
112
+
113
+ SpessaSynthInfo("%cFinished restoring controllers!", consoleColors.info);
114
+ }
115
+ }
116
+
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Synthesizer's default voice cap
3
+ * @type {number}
4
+ */
5
+ export const VOICE_CAP = 350;
6
+ /**
7
+ * Default MIDI drum channel
8
+ * @type {number}
9
+ */
10
+ export const DEFAULT_PERCUSSION = 9;
11
+ /**
12
+ * MIDI channel count
13
+ * @type {number}
14
+ */
15
+ export const MIDI_CHANNEL_COUNT = 16;
16
+ /**
17
+ * Default bank select and SysEx mode
18
+ * @type {string}
19
+ */
20
+ export const DEFAULT_SYNTH_MODE = "gs";
21
+
22
+ export const ALL_CHANNELS_OR_DIFFERENT_ACTION = -1;
@@ -1,4 +1,5 @@
1
1
  ## This is the utility folder.
2
+
2
3
  There are various utilites here used by the SpessaSynth library.
3
4
 
4
5
  ### Note that the stbvorbis_sync.js is licensed under Apache-2.0.
@@ -0,0 +1,185 @@
1
+ /**
2
+ * @typedef {Object} WaveMetadata
3
+ * @property {string|undefined} title - the song's title
4
+ * @property {string|undefined} artist - the song's artist
5
+ * @property {string|undefined} album - the song's album
6
+ * @property {string|undefined} genre - the song's genre
7
+ */
8
+
9
+ import { combineArrays, IndexedByteArray } from "./indexed_array.js";
10
+ import { getStringBytes, writeStringAsBytes } from "./byte_functions/string.js";
11
+ import { writeRIFFOddSize } from "../soundfont/basic_soundfont/riff_chunk.js";
12
+ import { writeLittleEndian } from "./byte_functions/little_endian.js";
13
+
14
+ /**
15
+ *
16
+ * @param audioData {{leftChannel: Float32Array, rightChannel: Float32Array, sampleRate: number}}
17
+ * @param normalizeAudio {boolean} find the max sample point and set it to 1, and scale others with it
18
+ * @param metadata {WaveMetadata}
19
+ * @param loop {{start: number, end: number}} loop start and end points in seconds. Undefined if no loop
20
+ * @returns {ArrayBuffer}
21
+ */
22
+ export function audioToWav(audioData, normalizeAudio = true, metadata = {}, loop = undefined)
23
+ {
24
+ const channel1Data = audioData.leftChannel;
25
+ const channel2Data = audioData.rightChannel;
26
+ const length = channel1Data.length;
27
+ const sampleRate = audioData.sampleRate;
28
+
29
+ const bytesPerSample = 2; // 16-bit PCM
30
+
31
+ // prepare INFO chunk
32
+ let infoChunk = new IndexedByteArray(0);
33
+ const infoOn = Object.keys(metadata).length > 0;
34
+ // INFO chunk
35
+ if (infoOn)
36
+ {
37
+ const encoder = new TextEncoder();
38
+ const infoChunks = [
39
+ getStringBytes("INFO"),
40
+ writeRIFFOddSize("ICMT", encoder.encode("Created with SpessaSynth"), true)
41
+ ];
42
+ if (metadata.artist)
43
+ {
44
+ infoChunks.push(
45
+ writeRIFFOddSize("IART", encoder.encode(metadata.artist), true)
46
+ );
47
+ }
48
+ if (metadata.album)
49
+ {
50
+ infoChunks.push(
51
+ writeRIFFOddSize("IPRD", encoder.encode(metadata.album), true)
52
+ );
53
+ }
54
+ if (metadata.genre)
55
+ {
56
+ infoChunks.push(
57
+ writeRIFFOddSize("IGNR", encoder.encode(metadata.genre), true)
58
+ );
59
+ }
60
+ if (metadata.title)
61
+ {
62
+ infoChunks.push(
63
+ writeRIFFOddSize("INAM", encoder.encode(metadata.title), true)
64
+ );
65
+ }
66
+ infoChunk = writeRIFFOddSize("LIST", combineArrays(infoChunks));
67
+ }
68
+
69
+ // prepare CUE chunk
70
+ let cueChunk = new IndexedByteArray(0);
71
+ const cueOn = loop?.end !== undefined && loop?.start !== undefined;
72
+ if (cueOn)
73
+ {
74
+ const loopStartSamples = Math.floor(loop.start * sampleRate);
75
+ const loopEndSamples = Math.floor(loop.end * sampleRate);
76
+
77
+ const cueStart = new IndexedByteArray(24);
78
+ writeLittleEndian(cueStart, 0, 4); // dwIdentifier
79
+ writeLittleEndian(cueStart, 0, 4); // dwPosition
80
+ writeStringAsBytes(cueStart, "data"); // cue point ID
81
+ writeLittleEndian(cueStart, 0, 4); // chunkStart, always 0
82
+ writeLittleEndian(cueStart, 0, 4); // BlockStart, always 0
83
+ writeLittleEndian(cueStart, loopStartSamples, 4); // sampleOffset
84
+
85
+ const cueEnd = new IndexedByteArray(24);
86
+ writeLittleEndian(cueEnd, 1, 4); // dwIdentifier
87
+ writeLittleEndian(cueEnd, 0, 4); // dwPosition
88
+ writeStringAsBytes(cueEnd, "data"); // cue point ID
89
+ writeLittleEndian(cueEnd, 0, 4); // chunkStart, always 0
90
+ writeLittleEndian(cueEnd, 0, 4); // BlockStart, always 0
91
+ writeLittleEndian(cueEnd, loopEndSamples, 4); // sampleOffset
92
+
93
+ const out = combineArrays([
94
+ new IndexedByteArray([2, 0, 0, 0]), // cue points count,
95
+ cueStart,
96
+ cueEnd
97
+ ]);
98
+ cueChunk = writeRIFFOddSize("cue ", out);
99
+ }
100
+
101
+ // Prepare the header
102
+ const headerSize = 44;
103
+ const dataSize = length * 2 * bytesPerSample; // 2 channels, 16-bit per channel
104
+ const fileSize = headerSize + dataSize + infoChunk.length + cueChunk.length - 8; // total file size minus the first 8 bytes
105
+ const header = new Uint8Array(headerSize);
106
+
107
+ // 'RIFF'
108
+ header.set([82, 73, 70, 70], 0);
109
+ // file length
110
+ header.set(
111
+ new Uint8Array([fileSize & 0xff, (fileSize >> 8) & 0xff, (fileSize >> 16) & 0xff, (fileSize >> 24) & 0xff]),
112
+ 4
113
+ );
114
+ // 'WAVE'
115
+ header.set([87, 65, 86, 69], 8);
116
+ // 'fmt '
117
+ header.set([102, 109, 116, 32], 12);
118
+ // fmt chunk length
119
+ header.set([16, 0, 0, 0], 16); // 16 for PCM
120
+ // audio format (PCM)
121
+ header.set([1, 0], 20);
122
+ // number of channels (2)
123
+ header.set([2, 0], 22);
124
+ // sample rate
125
+ header.set(
126
+ new Uint8Array([sampleRate & 0xff, (sampleRate >> 8) & 0xff, (sampleRate >> 16) & 0xff, (sampleRate >> 24) & 0xff]),
127
+ 24
128
+ );
129
+ // byte rate (sample rate * block align)
130
+ const byteRate = sampleRate * 2 * bytesPerSample; // 2 channels, 16-bit per channel
131
+ header.set(
132
+ new Uint8Array([byteRate & 0xff, (byteRate >> 8) & 0xff, (byteRate >> 16) & 0xff, (byteRate >> 24) & 0xff]),
133
+ 28
134
+ );
135
+ // block align (channels * bytes per sample)
136
+ header.set([4, 0], 32); // 2 channels * 16-bit per channel / 8
137
+ // bits per sample
138
+ header.set([16, 0], 34); // 16-bit
139
+
140
+ // data chunk identifier 'data'
141
+ header.set([100, 97, 116, 97], 36);
142
+ // data chunk length
143
+ header.set(
144
+ new Uint8Array([dataSize & 0xff, (dataSize >> 8) & 0xff, (dataSize >> 16) & 0xff, (dataSize >> 24) & 0xff]),
145
+ 40
146
+ );
147
+
148
+ let wavData = new Uint8Array(fileSize + 8);
149
+ let offset = headerSize;
150
+ wavData.set(header, 0);
151
+
152
+ // Interleave audio data (combine channels)
153
+ let multiplier = 32767;
154
+ if (normalizeAudio)
155
+ {
156
+ // find min and max values to prevent clipping when converting to 16 bits
157
+ const maxAbsValue = channel1Data.map((v, i) => Math.max(Math.abs(v), Math.abs(channel2Data[i])))
158
+ .reduce((a, b) => Math.max(a, b));
159
+ multiplier = maxAbsValue > 0 ? (32767 / maxAbsValue) : 1;
160
+ }
161
+ for (let i = 0; i < length; i++)
162
+ {
163
+ // interleave both channels
164
+ const sample1 = Math.min(32767, Math.max(-32768, channel1Data[i] * multiplier));
165
+ const sample2 = Math.min(32767, Math.max(-32768, channel2Data[i] * multiplier));
166
+
167
+ // convert to 16-bit
168
+ wavData[offset++] = sample1 & 0xff;
169
+ wavData[offset++] = (sample1 >> 8) & 0xff;
170
+ wavData[offset++] = sample2 & 0xff;
171
+ wavData[offset++] = (sample2 >> 8) & 0xff;
172
+ }
173
+
174
+ if (infoOn)
175
+ {
176
+ wavData.set(infoChunk, offset);
177
+ offset += infoChunk.length;
178
+ }
179
+ if (cueOn)
180
+ {
181
+ wavData.set(cueChunk, offset);
182
+ }
183
+
184
+ return wavData.buffer;
185
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Reads as Big endian
3
+ * @param dataArray {IndexedByteArray}
4
+ * @param bytesAmount {number}
5
+ * @returns {number}
6
+ */
7
+ export function readBytesAsUintBigEndian(dataArray, bytesAmount)
8
+ {
9
+ let out = 0;
10
+ for (let i = 8 * (bytesAmount - 1); i >= 0; i -= 8)
11
+ {
12
+ out |= (dataArray[dataArray.currentIndex++] << i);
13
+ }
14
+ return out >>> 0;
15
+ }
16
+
17
+ /**
18
+ * @param number {number}
19
+ * @param bytesAmount {number}
20
+ * @returns {number[]}
21
+ */
22
+ export function writeBytesAsUintBigEndian(number, bytesAmount)
23
+ {
24
+ const bytes = new Array(bytesAmount).fill(0);
25
+ for (let i = bytesAmount - 1; i >= 0; i--)
26
+ {
27
+ bytes[i] = number & 0xFF;
28
+ number >>= 8;
29
+ }
30
+
31
+ return bytes;
32
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Reads as little endian
3
+ * @param dataArray {IndexedByteArray}
4
+ * @param bytesAmount {number}
5
+ * @returns {number}
6
+ */
7
+ export function readLittleEndian(dataArray, bytesAmount)
8
+ {
9
+ let out = 0;
10
+ for (let i = 0; i < bytesAmount; i++)
11
+ {
12
+ out |= (dataArray[dataArray.currentIndex++] << i * 8);
13
+ }
14
+ // make sure it stays unsigned
15
+ return out >>> 0;
16
+ }
17
+
18
+ /**
19
+ * Writes a number as little endian seems to also work for negative numbers so yay?
20
+ * @param dataArray {IndexedByteArray}
21
+ * @param number {number}
22
+ * @param byteTarget {number}
23
+ */
24
+ export function writeLittleEndian(dataArray, number, byteTarget)
25
+ {
26
+ for (let i = 0; i < byteTarget; i++)
27
+ {
28
+ dataArray[dataArray.currentIndex++] = (number >> (i * 8)) & 0xFF;
29
+ }
30
+ }
31
+
32
+ /**
33
+ * @param dataArray {IndexedByteArray}
34
+ * @param word {number}
35
+ */
36
+ export function writeWord(dataArray, word)
37
+ {
38
+ dataArray[dataArray.currentIndex++] = word & 0xFF;
39
+ dataArray[dataArray.currentIndex++] = word >> 8;
40
+ }
41
+
42
+ /**
43
+ * @param dataArray {IndexedByteArray}
44
+ * @param dword {number}
45
+ */
46
+ export function writeDword(dataArray, dword)
47
+ {
48
+ writeLittleEndian(dataArray, dword, 4);
49
+ }
50
+
51
+ /**
52
+ * @param byte1 {number}
53
+ * @param byte2 {number}
54
+ * @returns {number}
55
+ */
56
+ export function signedInt16(byte1, byte2)
57
+ {
58
+ let val = (byte2 << 8) | byte1;
59
+ if (val > 32767)
60
+ {
61
+ return val - 65536;
62
+ }
63
+ return val;
64
+ }
65
+
66
+ /**
67
+ * @param byte {number}
68
+ * @returns {number}
69
+ */
70
+ export function signedInt8(byte)
71
+ {
72
+ if (byte > 127)
73
+ {
74
+ return byte - 256;
75
+ }
76
+ return byte;
77
+ }