In case of running out of prefetch pipes, truncate the sample to the preloaded part.
[calfbox.git] / sampler_layer.h
blob4ba35d8400a86bd0b536cb1f6f1d1931172b79db
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(float, reloffset, 0) \
267 MACRO##_dahdsr(amp_env, ampeg, 0) \
268 MACRO##_dahdsr(filter_env, fileg, 1) \
269 MACRO##_dahdsr(pitch_env, pitcheg, 2) \
270 MACRO##_lfo(amp_lfo, amplfo, 0) \
271 MACRO##_lfo(filter_lfo, fillfo, 1) \
272 MACRO##_lfo(pitch_lfo, pitchlfo, 2) \
273 MACRO##_eq(eq1, eq1, 0) \
274 MACRO##_eq(eq2, eq2, 1) \
275 MACRO##_eq(eq3, eq3, 2) \
276 MACRO##_ccrange(on_) \
277 MACRO##_ccrange() \
279 // XXXKF: consider making send1gain the dBamp type... except it's
280 // a linear percentage value in SFZ spec - bit weird!
282 #define DAHDSR_FIELDS(MACRO, ...) \
283 MACRO(start, 0, 0, ## __VA_ARGS__) \
284 MACRO(delay, 1, 0, ## __VA_ARGS__) \
285 MACRO(attack, 2, 0, ## __VA_ARGS__) \
286 MACRO(hold, 3, 0, ## __VA_ARGS__) \
287 MACRO(decay, 4, 0, ## __VA_ARGS__) \
288 MACRO(sustain, 5, 100, ## __VA_ARGS__) \
289 MACRO(release, 6, 0.05, ## __VA_ARGS__) \
291 #define LFO_FIELDS(MACRO, ...) \
292 MACRO(freq, 0, 0, ## __VA_ARGS__) \
293 MACRO(delay, 1, 0, ## __VA_ARGS__) \
294 MACRO(fade, 2, 0, ## __VA_ARGS__) \
296 #define EQ_FIELDS(MACRO, ...) \
297 MACRO(freq, 0, 0, ## __VA_ARGS__) \
298 MACRO(bw, 1, 1, ## __VA_ARGS__) \
299 MACRO(gain, 2, 0, ## __VA_ARGS__) \
300 MACRO(vel2freq, 3, 0, ## __VA_ARGS__) \
301 MACRO(vel2gain, 5, 0, ## __VA_ARGS__) \
303 #define PROC_SUBSTRUCT_HAS_FIELD(name, index, param, def_value) \
304 unsigned int name:1;
305 #define PROC_SUBSTRUCT_RESET_FIELD(name, index, def_value, param, dst) \
306 dst->param.name = def_value;
307 #define PROC_SUBSTRUCT_RESET_HAS_FIELD(name, index, def_value, param, dst) \
308 dst->has_##param.name = 0;
309 #define PROC_SUBSTRUCT_CLONE(name, index, def_value, param, dst, src) \
310 dst->param.name = src->param.name; \
311 dst->has_##param.name = src->has_##param.name;
312 #define PROC_SUBSTRUCT_CLONEPARENT(name, index, def_value, param, l) \
313 if (!l->has_##param.name) \
314 l->param.name = parent ? parent->param.name : def_value;
316 struct sampler_dahdsr_has_fields
318 DAHDSR_FIELDS(PROC_SUBSTRUCT_HAS_FIELD, name)
321 struct sampler_lfo_has_fields
323 LFO_FIELDS(PROC_SUBSTRUCT_HAS_FIELD, name)
326 struct sampler_eq_has_fields
328 EQ_FIELDS(PROC_SUBSTRUCT_HAS_FIELD, name)
331 #define PROC_FIELDS_TO_STRUCT(type, name, def_value) \
332 type name;
333 #define PROC_FIELDS_TO_STRUCT_string(name) \
334 gchar *name; \
335 gboolean name##_changed;
336 #define PROC_FIELDS_TO_STRUCT_dBamp(type, name, def_value) \
337 type name; \
338 type name##_linearized;
339 #define PROC_FIELDS_TO_STRUCT_enum(enumtype, name, def_value) \
340 enum enumtype name;
341 #define PROC_FIELDS_TO_STRUCT_dahdsr(name, parname, index) \
342 struct cbox_dahdsr name; \
343 struct cbox_envelope_shape name##_shape;
344 #define PROC_FIELDS_TO_STRUCT_lfo(name, parname, index) \
345 struct sampler_lfo_params name;
346 #define PROC_FIELDS_TO_STRUCT_eq(name, parname, index) \
347 struct sampler_eq_params name;
348 #define PROC_FIELDS_TO_STRUCT_ccrange(name) \
349 int8_t name##locc; \
350 int8_t name##hicc; \
351 int8_t name##cc_number;
353 #define PROC_HAS_FIELD(type, name, def_value) \
354 unsigned int has_##name:1;
355 #define PROC_HAS_FIELD_string(name) \
356 unsigned int has_##name:1;
357 #define PROC_HAS_FIELD_dBamp(type, name, def_value) \
358 PROC_HAS_FIELD(type, name, def_value)
359 #define PROC_HAS_FIELD_enum(enumtype, name, def_value) \
360 PROC_HAS_FIELD(type, name, def_value)
362 #define PROC_HAS_FIELD_dahdsr(name, parname, index) \
363 struct sampler_dahdsr_has_fields has_##name;
364 #define PROC_HAS_FIELD_lfo(name, parname, index) \
365 struct sampler_lfo_has_fields has_##name;
366 #define PROC_HAS_FIELD_eq(name, parname, index) \
367 struct sampler_eq_has_fields has_##name;
369 #define PROC_HAS_FIELD_ccrange(name) \
370 unsigned int has_##name##locc:1; \
371 unsigned int has_##name##hicc:1;
373 CBOX_EXTERN_CLASS(sampler_layer)
375 struct sampler_layer_data
377 SAMPLER_FIXED_FIELDS(PROC_FIELDS_TO_STRUCT)
378 SAMPLER_FIXED_FIELDS(PROC_HAS_FIELD)
380 float velcurve[128];
381 float eff_velcurve[128];
383 GSList *modulations;
384 GSList *nifs;
386 // computed values:
387 float eff_freq;
388 int eff_use_keyswitch;
389 enum sampler_loop_mode eff_loop_mode;
390 struct cbox_waveform *eff_waveform;
391 int16_t scratch_loop[2 * MAX_INTERPOLATION_ORDER * 2];
392 int16_t scratch_end[2 * MAX_INTERPOLATION_ORDER * 2];
393 float resonance_scaled;
394 float logcutoff;
395 uint32_t eq_bitmask;
398 struct sampler_layer
400 CBOX_OBJECT_HEADER()
401 struct cbox_command_target cmd_target;
403 struct sampler_layer_data data, *runtime;
405 struct sampler_module *module;
406 struct sampler_program *parent_program;
407 struct sampler_layer *parent_group;
409 int last_key, current_seq_position;
411 GHashTable *unknown_keys;
412 GHashTable *child_layers;
415 extern struct sampler_layer *sampler_layer_new(struct sampler_module *m, struct sampler_program *parent_program, struct sampler_layer *parent_group);
416 extern struct sampler_layer *sampler_layer_new_from_section(struct sampler_module *m, struct sampler_program *parent_program, struct sampler_layer *parent_group, const char *cfg_section);
417 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);
418 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);
419 extern void sampler_layer_set_modulation1(struct sampler_layer *l, enum sampler_modsrc src, enum sampler_moddest dest, float amount, int flags);
420 extern void sampler_layer_add_nif(struct sampler_layer *l, SamplerNoteInitFunc notefunc, int variant, float param);
421 extern void sampler_layer_load_overrides(struct sampler_layer *l, const char *cfg_section);
422 extern void sampler_layer_data_finalize(struct sampler_layer_data *l, struct sampler_layer_data *parent, struct sampler_program *p);
423 extern void sampler_layer_reset_switches(struct sampler_layer *l, struct sampler_module *m);
424 extern gboolean sampler_layer_apply_param(struct sampler_layer *l, const char *key, const char *value, GError **error);
425 extern gchar *sampler_layer_to_string(struct sampler_layer *l, gboolean show_inherited);
426 extern void sampler_layer_dump(struct sampler_layer *l, FILE *f);
427 extern void sampler_layer_update(struct sampler_layer *l);
429 extern void sampler_layer_data_clone(struct sampler_layer_data *dst, const struct sampler_layer_data *src, gboolean copy_hasattr);
430 extern void sampler_layer_data_close(struct sampler_layer_data *l);
431 extern void sampler_layer_data_destroy(struct sampler_layer_data *l);
433 extern void sampler_nif_vel2pitch(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
434 extern void sampler_nif_vel2reloffset(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
435 extern void sampler_nif_vel2env(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
436 extern void sampler_nif_cc2delay(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
437 extern void sampler_nif_cc2reloffset(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
438 extern void sampler_nif_addrandom(struct sampler_noteinitfunc *nif, struct sampler_voice *v);
440 static inline gboolean sampler_layer_data_is_4pole(struct sampler_layer_data *v)
442 if (v->cutoff == -1)
443 return FALSE;
444 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;
447 #endif