Forbid compiling with python 2.x (it's not gonna work).
[calfbox.git] / sampler_layer.h
blob78eb65387087a6723f80c31b58a42b68692defd7
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,
158 smdest_tonectl,
160 smdestcount
163 struct sampler_modulation
165 enum sampler_modsrc src;
166 enum sampler_modsrc src2;
167 enum sampler_moddest dest;
168 float amount;
169 int flags:31;
170 unsigned int has_value:1;
173 typedef void (*SamplerNoteInitFunc)(struct sampler_noteinitfunc *nif, struct sampler_voice *voice);
175 struct sampler_noteinitfunc
177 SamplerNoteInitFunc notefunc;
178 int variant:31;
179 unsigned int has_value:1;
180 float param;
181 // XXXKF no destructor for now - might not be necessary
184 struct sampler_lfo_params
186 float freq;
187 float delay;
188 float fade;
191 struct sampler_eq_params
193 float freq;
194 float bw;
195 float gain;
196 float effective_freq;
197 float vel2freq;
198 float vel2gain;
201 typedef int midi_note_t;
204 * Transforms:
205 * notransform - self-explanatory
206 * dBamp - amplitude/gain stored as dB
209 #define SAMPLER_FIXED_FIELDS(MACRO) \
210 MACRO##_string(sample) \
211 MACRO(uint32_t, offset, 0) \
212 MACRO(uint32_t, offset_random, 0) \
213 MACRO(uint32_t, loop_start, 0) \
214 MACRO(uint32_t, loop_end, 0) \
215 MACRO(uint32_t, end, 0) \
216 MACRO(uint32_t, loop_overlap, -1) \
217 MACRO##_enum(sampler_loop_mode, loop_mode, slm_unknown) \
218 MACRO##_enum(sampler_trigger, trigger, stm_attack) \
219 MACRO##_dBamp(float, volume, 0) \
220 MACRO(float, pan, 0) \
221 MACRO(float, tune, 0) \
222 MACRO(int, transpose, 0) \
223 MACRO(int, lochan, 1) \
224 MACRO(int, hichan, 16) \
225 MACRO(float, lorand, 0) \
226 MACRO(float, hirand, 1) \
227 MACRO(midi_note_t, key, -1) \
228 MACRO(midi_note_t, lokey, 0) \
229 MACRO(midi_note_t, hikey, 127) \
230 MACRO(midi_note_t, pitch_keycenter, 60) \
231 MACRO(int, pitch_keytrack, 100) \
232 MACRO(midi_note_t, fil_keycenter, 60) \
233 MACRO(int, fil_keytrack, 0) \
234 MACRO(int, fil_veltrack, 0) \
235 MACRO(int, amp_veltrack, 100) \
236 MACRO(int, lovel, 0) \
237 MACRO(int, hivel, 127) \
238 MACRO(int, velcurve_quadratic, -1) \
239 MACRO##_enum(sampler_filter_type, fil_type, sft_lp12) \
240 MACRO##_enum(sampler_off_mode, off_mode, som_unknown) \
241 MACRO(float, cutoff, -1) \
242 MACRO##_dBamp(float, resonance, 0) \
243 MACRO(midi_note_t, sw_lokey, 0) \
244 MACRO(midi_note_t, sw_hikey, 127) \
245 MACRO(midi_note_t, sw_last, -1) \
246 MACRO(midi_note_t, sw_down, -1) \
247 MACRO(midi_note_t, sw_up, -1) \
248 MACRO(midi_note_t, sw_previous, -1) \
249 MACRO(int, seq_position, 1) \
250 MACRO(int, seq_length, 1) \
251 MACRO(int, effect1bus, 1) \
252 MACRO(int, effect2bus, 2) \
253 MACRO(float, effect1, 0) \
254 MACRO(float, effect2, 0) \
255 MACRO(float, delay, 0) \
256 MACRO(float, delay_random, 0) \
257 MACRO(int, output, 0) \
258 MACRO(int, group, 0) \
259 MACRO(int, off_by, 0) \
260 MACRO(int, count, 0) \
261 MACRO(int, bend_up, 200) \
262 MACRO(int, bend_down, 200) \
263 MACRO(float, rt_decay, 0) \
264 MACRO(float, tonectl, 0) \
265 MACRO(float, tonectl_freq, 0) \
266 MACRO##_dahdsr(amp_env, ampeg, 0) \
267 MACRO##_dahdsr(filter_env, fileg, 1) \
268 MACRO##_dahdsr(pitch_env, pitcheg, 2) \
269 MACRO##_lfo(amp_lfo, amplfo, 0) \
270 MACRO##_lfo(filter_lfo, fillfo, 1) \
271 MACRO##_lfo(pitch_lfo, pitchlfo, 2) \
272 MACRO##_eq(eq1, eq1, 0) \
273 MACRO##_eq(eq2, eq2, 1) \
274 MACRO##_eq(eq3, eq3, 2) \
275 MACRO##_ccrange(on_) \
276 MACRO##_ccrange() \
278 // XXXKF: consider making send1gain the dBamp type... except it's
279 // a linear percentage value in SFZ spec - bit weird!
281 #define DAHDSR_FIELDS(MACRO, ...) \
282 MACRO(start, 0, 0, ## __VA_ARGS__) \
283 MACRO(delay, 1, 0, ## __VA_ARGS__) \
284 MACRO(attack, 2, 0, ## __VA_ARGS__) \
285 MACRO(hold, 3, 0, ## __VA_ARGS__) \
286 MACRO(decay, 4, 0, ## __VA_ARGS__) \
287 MACRO(sustain, 5, 100, ## __VA_ARGS__) \
288 MACRO(release, 6, 0.05, ## __VA_ARGS__) \
290 #define LFO_FIELDS(MACRO, ...) \
291 MACRO(freq, 0, 0, ## __VA_ARGS__) \
292 MACRO(delay, 1, 0, ## __VA_ARGS__) \
293 MACRO(fade, 2, 0, ## __VA_ARGS__) \
295 #define EQ_FIELDS(MACRO, ...) \
296 MACRO(freq, 0, 0, ## __VA_ARGS__) \
297 MACRO(bw, 1, 1, ## __VA_ARGS__) \
298 MACRO(gain, 2, 0, ## __VA_ARGS__) \
299 MACRO(vel2freq, 3, 0, ## __VA_ARGS__) \
300 MACRO(vel2gain, 5, 0, ## __VA_ARGS__) \
302 #define PROC_SUBSTRUCT_HAS_FIELD(name, index, param, def_value) \
303 unsigned int name:1;
304 #define PROC_SUBSTRUCT_RESET_FIELD(name, index, def_value, param, dst) \
305 dst->param.name = def_value;
306 #define PROC_SUBSTRUCT_RESET_HAS_FIELD(name, index, def_value, param, dst) \
307 dst->has_##param.name = 0;
308 #define PROC_SUBSTRUCT_CLONE(name, index, def_value, param, dst, src) \
309 dst->param.name = src->param.name; \
310 dst->has_##param.name = src->has_##param.name;
311 #define PROC_SUBSTRUCT_CLONEPARENT(name, index, def_value, param, l) \
312 if (!l->has_##param.name) \
313 l->param.name = parent ? parent->param.name : def_value;
315 struct sampler_dahdsr_has_fields
317 DAHDSR_FIELDS(PROC_SUBSTRUCT_HAS_FIELD, name)
320 struct sampler_lfo_has_fields
322 LFO_FIELDS(PROC_SUBSTRUCT_HAS_FIELD, name)
325 struct sampler_eq_has_fields
327 EQ_FIELDS(PROC_SUBSTRUCT_HAS_FIELD, name)
330 #define PROC_FIELDS_TO_STRUCT(type, name, def_value) \
331 type name;
332 #define PROC_FIELDS_TO_STRUCT_string(name) \
333 gchar *name; \
334 gboolean name##_changed;
335 #define PROC_FIELDS_TO_STRUCT_dBamp(type, name, def_value) \
336 type name; \
337 type name##_linearized;
338 #define PROC_FIELDS_TO_STRUCT_enum(enumtype, name, def_value) \
339 enum enumtype name;
340 #define PROC_FIELDS_TO_STRUCT_dahdsr(name, parname, index) \
341 struct cbox_dahdsr name; \
342 struct cbox_envelope_shape name##_shape;
343 #define PROC_FIELDS_TO_STRUCT_lfo(name, parname, index) \
344 struct sampler_lfo_params name;
345 #define PROC_FIELDS_TO_STRUCT_eq(name, parname, index) \
346 struct sampler_eq_params name;
347 #define PROC_FIELDS_TO_STRUCT_ccrange(name) \
348 int8_t name##locc; \
349 int8_t name##hicc; \
350 int8_t name##cc_number;
352 #define PROC_HAS_FIELD(type, name, def_value) \
353 unsigned int has_##name:1;
354 #define PROC_HAS_FIELD_string(name) \
355 unsigned int has_##name:1;
356 #define PROC_HAS_FIELD_dBamp(type, name, def_value) \
357 PROC_HAS_FIELD(type, name, def_value)
358 #define PROC_HAS_FIELD_enum(enumtype, name, def_value) \
359 PROC_HAS_FIELD(type, name, def_value)
361 #define PROC_HAS_FIELD_dahdsr(name, parname, index) \
362 struct sampler_dahdsr_has_fields has_##name;
363 #define PROC_HAS_FIELD_lfo(name, parname, index) \
364 struct sampler_lfo_has_fields has_##name;
365 #define PROC_HAS_FIELD_eq(name, parname, index) \
366 struct sampler_eq_has_fields has_##name;
368 #define PROC_HAS_FIELD_ccrange(name) \
369 unsigned int has_##name##locc:1; \
370 unsigned int has_##name##hicc:1;
372 CBOX_EXTERN_CLASS(sampler_layer)
374 struct sampler_layer_data
376 SAMPLER_FIXED_FIELDS(PROC_FIELDS_TO_STRUCT)
377 SAMPLER_FIXED_FIELDS(PROC_HAS_FIELD)
379 float velcurve[128];
380 float eff_velcurve[128];
382 GSList *modulations;
383 GSList *nifs;
385 // computed values:
386 float eff_freq;
387 int eff_use_keyswitch;
388 struct cbox_waveform *eff_waveform;
389 int16_t scratch_loop[2 * MAX_INTERPOLATION_ORDER * 2];
390 int16_t scratch_end[2 * MAX_INTERPOLATION_ORDER * 2];
391 float resonance_scaled;
392 float logcutoff;
393 uint32_t eq_bitmask;
396 struct sampler_layer
398 CBOX_OBJECT_HEADER()
399 struct cbox_command_target cmd_target;
401 struct sampler_layer_data data, *runtime;
403 struct sampler_module *module;
404 struct sampler_program *parent_program;
405 struct sampler_layer *parent_group;
407 int last_key, current_seq_position;
409 GHashTable *unknown_keys;
410 GHashTable *child_layers;
413 extern struct sampler_layer *sampler_layer_new(struct sampler_module *m, struct sampler_program *parent_program, struct sampler_layer *parent_group);
414 extern struct sampler_layer *sampler_layer_new_from_section(struct sampler_module *m, struct sampler_program *parent_program, const char *cfg_section);
415 extern struct sampler_layer *sampler_layer_new_clone(struct sampler_layer *layer, struct sampler_module *m, struct sampler_program *parent_program, struct sampler_layer *parent_group);
416 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);
417 extern void sampler_layer_set_modulation1(struct sampler_layer *l, enum sampler_modsrc src, enum sampler_moddest dest, float amount, int flags);
418 extern void sampler_layer_add_nif(struct sampler_layer *l, SamplerNoteInitFunc notefunc, int variant, float param);
419 extern void sampler_layer_load_overrides(struct sampler_layer *l, const char *cfg_section);
420 extern void sampler_layer_data_finalize(struct sampler_layer_data *l, struct sampler_layer_data *parent, struct sampler_program *p);
421 extern void sampler_layer_reset_switches(struct sampler_layer *l, struct sampler_module *m);
422 extern gboolean sampler_layer_apply_param(struct sampler_layer *l, const char *key, const char *value, GError **error);
423 extern gchar *sampler_layer_to_string(struct sampler_layer *l, gboolean show_inherited);
424 extern void sampler_layer_dump(struct sampler_layer *l, FILE *f);
425 extern void sampler_layer_update(struct sampler_layer *l);
427 extern void sampler_layer_data_clone(struct sampler_layer_data *dst, const struct sampler_layer_data *src, gboolean copy_hasattr);
428 extern void sampler_layer_data_close(struct sampler_layer_data *l);
429 extern void sampler_layer_data_destroy(struct sampler_layer_data *l);
431 extern void sampler_nif_vel2pitch(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
432 extern void sampler_nif_vel2env(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
433 extern void sampler_nif_cc2delay(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
434 extern void sampler_nif_addrandom(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
436 static inline gboolean sampler_layer_data_is_4pole(struct sampler_layer_data *v)
438 if (v->cutoff == -1)
439 return FALSE;
440 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;
443 #endif