Mark the ad-hoc pattern as completed only after all the notes have been processed.
[calfbox.git] / sampler_layer.h
blobfade92d9fa344cbb0cd409ae76d8d52b547d850b
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2011 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef CBOX_SAMPLER_LAYER_H
20 #define CBOX_SAMPLER_LAYER_H
22 #include "dom.h"
23 #include "wavebank.h"
24 #include <stdio.h>
25 #include <stdint.h>
27 struct sampler_program;
28 struct sampler_voice;
29 struct sampler_noteinitfunc;
30 struct sampler_module;
32 enum sampler_player_type
34 spt_inactive,
35 spt_mono16,
36 spt_stereo16,
37 spt_finished
40 enum sampler_loop_mode
42 slm_unknown,
43 slm_no_loop,
44 slm_one_shot,
45 slm_loop_continuous,
46 slm_loop_sustain, // unsupported
47 slm_one_shot_chokeable,
48 slmcount
51 #define ENUM_VALUES_sampler_loop_mode(MACRO) \
52 MACRO("no_loop", slm_no_loop) \
53 MACRO("one_shot", slm_one_shot) \
54 MACRO("loop_continuous", slm_loop_continuous) \
55 MACRO("loop_sustain", slm_loop_sustain) \
56 MACRO("one_shot_chokeable", slm_one_shot_chokeable)
58 enum sampler_off_mode
60 som_unknown,
61 som_normal,
62 som_fast
65 #define ENUM_VALUES_sampler_off_mode(MACRO) \
66 MACRO("normal", som_normal) \
67 MACRO("fast", som_fast)
69 enum sampler_trigger
71 stm_attack,
72 stm_release,
73 stm_first,
74 stm_legato,
77 #define ENUM_VALUES_sampler_trigger(MACRO) \
78 MACRO("attack", stm_attack) \
79 MACRO("release", stm_release) \
80 MACRO("first", stm_first) \
81 MACRO("legato", stm_legato)
83 enum sampler_filter_type
85 sft_unknown,
86 sft_lp12,
87 sft_hp12,
88 sft_bp6,
89 sft_lp24,
90 sft_hp24,
91 sft_bp12,
92 sft_lp6,
93 sft_hp6,
94 sft_lp12nr,
95 sft_hp12nr,
96 sft_lp24nr,
97 sft_hp24nr,
98 sft_lp24hybrid,
101 #define ENUM_VALUES_sampler_filter_type(MACRO) \
102 MACRO("lpf_2p", sft_lp12) \
103 MACRO("hpf_2p", sft_hp12) \
104 MACRO("bpf_2p", sft_bp6) \
105 MACRO("lpf_4p", sft_lp24) \
106 MACRO("hpf_4p", sft_hp24) \
107 MACRO("bpf_4p", sft_bp12) \
108 MACRO("lpf_1p", sft_lp6) \
109 MACRO("hpf_1p", sft_hp6) \
110 MACRO("lpf_2p_nores", sft_lp12nr) \
111 MACRO("hpf_2p_nores", sft_hp12nr) \
112 MACRO("lpf_4p_nores", sft_lp24nr) \
113 MACRO("hpf_4p_nores", sft_hp24nr) \
114 MACRO("lpf_4p_hybrid", sft_lp24hybrid) \
116 #define ENUM_LIST(MACRO) \
117 MACRO(sampler_loop_mode) \
118 MACRO(sampler_off_mode) \
119 MACRO(sampler_trigger) \
120 MACRO(sampler_filter_type) \
122 #define MAKE_FROM_TO_STRING_EXTERN(enumtype) \
123 extern const char *enumtype##_to_string(enum enumtype value); \
124 extern gboolean enumtype##_from_string(const char *name, enum enumtype *value);
126 ENUM_LIST(MAKE_FROM_TO_STRING_EXTERN)
128 enum sampler_modsrc
130 smsrc_cc0 = 0,
131 smsrc_chanaft = 120,
133 // those are per-note, not per-channel
134 smsrc_vel,
135 smsrc_polyaft,
136 smsrc_pitch,
137 smsrc_pitchenv,
138 smsrc_filenv,
139 smsrc_ampenv,
140 smsrc_pitchlfo,
141 smsrc_fillfo,
142 smsrc_amplfo,
143 smsrc_none,
145 smsrccount,
146 smsrc_perchan_offset = 0,
147 smsrc_perchan_count = smsrc_vel,
148 smsrc_pernote_offset = smsrc_vel,
149 smsrc_pernote_count = smsrccount - smsrc_pernote_offset,
152 enum sampler_moddest
154 smdest_gain,
155 smdest_pitch,
156 smdest_cutoff,
157 smdest_resonance,
159 smdestcount
162 struct sampler_modulation
164 enum sampler_modsrc src;
165 enum sampler_modsrc src2;
166 enum sampler_moddest dest;
167 float amount;
168 int flags:31;
169 unsigned int has_value:1;
172 typedef void (*SamplerNoteInitFunc)(struct sampler_noteinitfunc *nif, struct sampler_voice *voice);
174 struct sampler_noteinitfunc
176 SamplerNoteInitFunc notefunc;
177 int variant:31;
178 unsigned int has_value:1;
179 float param;
180 // XXXKF no destructor for now - might not be necessary
183 struct sampler_lfo_params
185 float freq;
186 float delay;
187 float fade;
190 struct sampler_eq_params
192 float freq;
193 float bw;
194 float gain;
195 float effective_freq;
196 float vel2freq;
197 float vel2gain;
200 typedef int midi_note_t;
203 * Transforms:
204 * notransform - self-explanatory
205 * dBamp - amplitude/gain stored as dB
208 #define SAMPLER_FIXED_FIELDS(MACRO) \
209 MACRO##_string(sample) \
210 MACRO(uint32_t, offset, 0) \
211 MACRO(uint32_t, offset_random, 0) \
212 MACRO(uint32_t, loop_start, 0) \
213 MACRO(uint32_t, loop_end, 0) \
214 MACRO(uint32_t, end, 0) \
215 MACRO(uint32_t, loop_overlap, -1) \
216 MACRO##_enum(sampler_loop_mode, loop_mode, slm_unknown) \
217 MACRO##_enum(sampler_trigger, trigger, stm_attack) \
218 MACRO##_dBamp(float, volume, 0) \
219 MACRO(float, pan, 0) \
220 MACRO(float, tune, 0) \
221 MACRO(int, transpose, 0) \
222 MACRO(int, lochan, 1) \
223 MACRO(int, hichan, 16) \
224 MACRO(float, lorand, 0) \
225 MACRO(float, hirand, 1) \
226 MACRO(midi_note_t, key, -1) \
227 MACRO(midi_note_t, lokey, 0) \
228 MACRO(midi_note_t, hikey, 127) \
229 MACRO(midi_note_t, pitch_keycenter, 60) \
230 MACRO(int, pitch_keytrack, 100) \
231 MACRO(midi_note_t, fil_keycenter, 60) \
232 MACRO(int, fil_keytrack, 0) \
233 MACRO(int, fil_veltrack, 0) \
234 MACRO(int, amp_veltrack, 100) \
235 MACRO(int, lovel, 0) \
236 MACRO(int, hivel, 127) \
237 MACRO(int, velcurve_quadratic, -1) \
238 MACRO##_enum(sampler_filter_type, fil_type, sft_lp12) \
239 MACRO##_enum(sampler_off_mode, off_mode, som_unknown) \
240 MACRO(float, cutoff, -1) \
241 MACRO##_dBamp(float, resonance, 0) \
242 MACRO(midi_note_t, sw_lokey, 0) \
243 MACRO(midi_note_t, sw_hikey, 127) \
244 MACRO(midi_note_t, sw_last, -1) \
245 MACRO(midi_note_t, sw_down, -1) \
246 MACRO(midi_note_t, sw_up, -1) \
247 MACRO(midi_note_t, sw_previous, -1) \
248 MACRO(int, seq_position, 1) \
249 MACRO(int, seq_length, 1) \
250 MACRO(int, effect1bus, 1) \
251 MACRO(int, effect2bus, 2) \
252 MACRO(float, effect1, 0) \
253 MACRO(float, effect2, 0) \
254 MACRO(float, delay, 0) \
255 MACRO(float, delay_random, 0) \
256 MACRO(int, output, 0) \
257 MACRO(int, group, 0) \
258 MACRO(int, off_by, 0) \
259 MACRO(int, count, 0) \
260 MACRO(int, bend_up, 200) \
261 MACRO(int, bend_down, 200) \
262 MACRO(float, rt_decay, 0) \
263 MACRO##_dahdsr(amp_env, ampeg, 0) \
264 MACRO##_dahdsr(filter_env, fileg, 1) \
265 MACRO##_dahdsr(pitch_env, pitcheg, 2) \
266 MACRO##_lfo(amp_lfo, amplfo, 0) \
267 MACRO##_lfo(filter_lfo, fillfo, 1) \
268 MACRO##_lfo(pitch_lfo, pitchlfo, 2) \
269 MACRO##_eq(eq1, eq1, 0) \
270 MACRO##_eq(eq2, eq2, 1) \
271 MACRO##_eq(eq3, eq3, 2) \
272 MACRO##_ccrange(on_) \
273 MACRO##_ccrange() \
275 // XXXKF: consider making send1gain the dBamp type... except it's
276 // a linear percentage value in SFZ spec - bit weird!
278 #define DAHDSR_FIELDS(MACRO, ...) \
279 MACRO(start, 0, 0, ## __VA_ARGS__) \
280 MACRO(delay, 1, 0, ## __VA_ARGS__) \
281 MACRO(attack, 2, 0, ## __VA_ARGS__) \
282 MACRO(hold, 3, 0, ## __VA_ARGS__) \
283 MACRO(decay, 4, 0, ## __VA_ARGS__) \
284 MACRO(sustain, 5, 100, ## __VA_ARGS__) \
285 MACRO(release, 6, 0.05, ## __VA_ARGS__) \
287 #define LFO_FIELDS(MACRO, ...) \
288 MACRO(freq, 0, 0, ## __VA_ARGS__) \
289 MACRO(delay, 1, 0, ## __VA_ARGS__) \
290 MACRO(fade, 2, 0, ## __VA_ARGS__) \
292 #define EQ_FIELDS(MACRO, ...) \
293 MACRO(freq, 0, 0, ## __VA_ARGS__) \
294 MACRO(bw, 1, 1, ## __VA_ARGS__) \
295 MACRO(gain, 2, 0, ## __VA_ARGS__) \
296 MACRO(vel2freq, 3, 0, ## __VA_ARGS__) \
297 MACRO(vel2gain, 5, 0, ## __VA_ARGS__) \
299 #define PROC_SUBSTRUCT_HAS_FIELD(name, index, param, def_value) \
300 unsigned int name:1;
301 #define PROC_SUBSTRUCT_RESET_FIELD(name, index, def_value, param, dst) \
302 dst->param.name = def_value;
303 #define PROC_SUBSTRUCT_RESET_HAS_FIELD(name, index, def_value, param, dst) \
304 dst->has_##param.name = 0;
305 #define PROC_SUBSTRUCT_CLONE(name, index, def_value, param, dst, src) \
306 dst->param.name = src->param.name;
307 #define PROC_SUBSTRUCT_CLONEPARENT(name, index, def_value, param, l) \
308 if (!l->has_##param.name) \
309 l->param.name = parent ? parent->param.name : def_value;
311 struct sampler_dahdsr_has_fields
313 DAHDSR_FIELDS(PROC_SUBSTRUCT_HAS_FIELD, name)
316 struct sampler_lfo_has_fields
318 LFO_FIELDS(PROC_SUBSTRUCT_HAS_FIELD, name)
321 struct sampler_eq_has_fields
323 EQ_FIELDS(PROC_SUBSTRUCT_HAS_FIELD, name)
326 #define PROC_FIELDS_TO_STRUCT(type, name, def_value) \
327 type name;
328 #define PROC_FIELDS_TO_STRUCT_string(name) \
329 gchar *name; \
330 gboolean name##_changed;
331 #define PROC_FIELDS_TO_STRUCT_dBamp(type, name, def_value) \
332 type name; \
333 type name##_linearized;
334 #define PROC_FIELDS_TO_STRUCT_enum(enumtype, name, def_value) \
335 enum enumtype name;
336 #define PROC_FIELDS_TO_STRUCT_dahdsr(name, parname, index) \
337 struct cbox_dahdsr name; \
338 struct cbox_envelope_shape name##_shape;
339 #define PROC_FIELDS_TO_STRUCT_lfo(name, parname, index) \
340 struct sampler_lfo_params name;
341 #define PROC_FIELDS_TO_STRUCT_eq(name, parname, index) \
342 struct sampler_eq_params name;
343 #define PROC_FIELDS_TO_STRUCT_ccrange(name) \
344 int8_t name##locc; \
345 int8_t name##hicc; \
346 int8_t name##cc_number;
348 #define PROC_HAS_FIELD(type, name, def_value) \
349 unsigned int has_##name:1;
350 #define PROC_HAS_FIELD_string(name) \
351 unsigned int has_##name:1;
352 #define PROC_HAS_FIELD_dBamp(type, name, def_value) \
353 PROC_HAS_FIELD(type, name, def_value)
354 #define PROC_HAS_FIELD_enum(enumtype, name, def_value) \
355 PROC_HAS_FIELD(type, name, def_value)
357 #define PROC_HAS_FIELD_dahdsr(name, parname, index) \
358 struct sampler_dahdsr_has_fields has_##name;
359 #define PROC_HAS_FIELD_lfo(name, parname, index) \
360 struct sampler_lfo_has_fields has_##name;
361 #define PROC_HAS_FIELD_eq(name, parname, index) \
362 struct sampler_eq_has_fields has_##name;
364 #define PROC_HAS_FIELD_ccrange(name) \
365 unsigned int has_##name##locc:1; \
366 unsigned int has_##name##hicc:1;
368 CBOX_EXTERN_CLASS(sampler_layer)
370 struct sampler_layer_data
372 SAMPLER_FIXED_FIELDS(PROC_FIELDS_TO_STRUCT)
373 SAMPLER_FIXED_FIELDS(PROC_HAS_FIELD)
375 float velcurve[128];
376 float eff_velcurve[128];
378 GSList *modulations;
379 GSList *nifs;
381 // computed values:
382 float eff_freq;
383 int eff_use_keyswitch;
384 struct cbox_waveform *eff_waveform;
385 int16_t scratch_loop[2 * MAX_INTERPOLATION_ORDER * 2];
386 int16_t scratch_end[2 * MAX_INTERPOLATION_ORDER * 2];
387 float resonance_scaled;
388 float logcutoff;
389 uint32_t eq_bitmask;
392 struct sampler_layer
394 CBOX_OBJECT_HEADER()
395 struct cbox_command_target cmd_target;
397 struct sampler_layer_data data, *runtime;
399 struct sampler_module *module;
400 struct sampler_program *parent_program;
401 struct sampler_layer *parent_group;
403 int last_key, current_seq_position;
405 GHashTable *unknown_keys;
406 GHashTable *child_layers;
409 extern struct sampler_layer *sampler_layer_new(struct sampler_module *m, struct sampler_program *parent_program, struct sampler_layer *parent_group);
410 extern struct sampler_layer *sampler_layer_new_from_section(struct sampler_module *m, struct sampler_program *parent_program, const char *cfg_section);
411 extern void sampler_layer_set_modulation(struct sampler_layer *l, enum sampler_modsrc src, enum sampler_modsrc src2, enum sampler_moddest dest, float amount, int flags);
412 extern void sampler_layer_set_modulation1(struct sampler_layer *l, enum sampler_modsrc src, enum sampler_moddest dest, float amount, int flags);
413 extern void sampler_layer_add_nif(struct sampler_layer *l, SamplerNoteInitFunc notefunc, int variant, float param);
414 extern void sampler_layer_load_overrides(struct sampler_layer *l, const char *cfg_section);
415 extern void sampler_layer_data_finalize(struct sampler_layer_data *l, struct sampler_layer_data *parent, struct sampler_program *p);
416 extern void sampler_layer_reset_switches(struct sampler_layer *l, struct sampler_module *m);
417 extern gboolean sampler_layer_apply_param(struct sampler_layer *l, const char *key, const char *value, GError **error);
418 extern gchar *sampler_layer_to_string(struct sampler_layer *l, gboolean show_inherited);
419 extern void sampler_layer_dump(struct sampler_layer *l, FILE *f);
420 extern void sampler_layer_update(struct sampler_layer *l);
422 extern void sampler_layer_data_clone(struct sampler_layer_data *dst, const struct sampler_layer_data *src, gboolean copy_hasattr);
423 extern void sampler_layer_data_close(struct sampler_layer_data *l);
424 extern void sampler_layer_data_destroy(struct sampler_layer_data *l);
426 extern void sampler_nif_vel2pitch(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
427 extern void sampler_nif_vel2env(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
428 extern void sampler_nif_cc2delay(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
429 extern void sampler_nif_addrandom(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
431 static inline gboolean sampler_layer_data_is_4pole(struct sampler_layer_data *v)
433 if (v->cutoff == -1)
434 return FALSE;
435 return v->fil_type == sft_lp24hybrid || v->fil_type == sft_lp24 || v->fil_type == sft_lp24nr || v->fil_type == sft_hp24 || v->fil_type == sft_hp24nr || v->fil_type == sft_bp12;
438 #endif