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 #include "config-api.h"
26 #include "sfzloader.h"
38 static void sampler_layer_data_set_modulation(struct sampler_layer_data
*l
, enum sampler_modsrc src
, enum sampler_modsrc src2
, enum sampler_moddest dest
, float amount
, int flags
, gboolean propagating_defaults
)
40 GSList
*p
= l
->modulations
;
43 struct sampler_modulation
*sm
= p
->data
;
44 if (sm
->src
== src
&& sm
->src2
== src2
&& sm
->dest
== dest
)
46 // do not overwrite locally set value with defaults
47 if (propagating_defaults
&& sm
->has_value
)
51 sm
->has_value
= !propagating_defaults
;
56 struct sampler_modulation
*sm
= g_malloc0(sizeof(struct sampler_modulation
));
62 sm
->has_value
= !propagating_defaults
;
63 l
->modulations
= g_slist_prepend(l
->modulations
, sm
);
66 void sampler_layer_set_modulation1(struct sampler_layer
*l
, enum sampler_modsrc src
, enum sampler_moddest dest
, float amount
, int flags
)
68 sampler_layer_data_set_modulation(&l
->data
, src
, smsrc_none
, dest
, amount
, flags
, FALSE
);
71 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
)
73 sampler_layer_data_set_modulation(&l
->data
, src
, src2
, dest
, amount
, flags
, FALSE
);
76 void sampler_layer_data_add_nif(struct sampler_layer_data
*l
, SamplerNoteInitFunc notefunc
, int variant
, float param
, gboolean propagating_defaults
)
81 struct sampler_noteinitfunc
*nif
= p
->data
;
82 if (nif
->notefunc
== notefunc
&& nif
->variant
== variant
)
84 // do not overwrite locally set value with defaults
85 if (propagating_defaults
&& nif
->has_value
)
88 nif
->has_value
= !propagating_defaults
;
93 struct sampler_noteinitfunc
*nif
= malloc(sizeof(struct sampler_noteinitfunc
));
94 nif
->notefunc
= notefunc
;
95 nif
->variant
= variant
;
97 nif
->has_value
= !propagating_defaults
;
98 l
->nifs
= g_slist_prepend(l
->nifs
, nif
);
101 void sampler_layer_add_nif(struct sampler_layer
*l
, SamplerNoteInitFunc notefunc
, int variant
, float param
)
103 sampler_layer_data_add_nif(&l
->data
, notefunc
, variant
, param
, FALSE
);
106 static gboolean
sampler_layer_process_cmd(struct cbox_command_target
*ct
, struct cbox_command_target
*fb
, struct cbox_osc_command
*cmd
, GError
**error
)
108 struct sampler_layer
*layer
= ct
->user_data
;
109 if (!strcmp(cmd
->command
, "/status") && !strcmp(cmd
->arg_types
, ""))
111 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
114 if (!((!layer
->parent_program
|| cbox_execute_on(fb
, NULL
, "/parent_program", "o", error
, layer
->parent_program
)) &&
115 (!layer
->parent_group
|| cbox_execute_on(fb
, NULL
, "/parent_group", "o", error
, layer
->parent_group
)) &&
116 CBOX_OBJECT_DEFAULT_STATUS(layer
, fb
, error
)))
120 if ((!strcmp(cmd
->command
, "/as_string") || !strcmp(cmd
->command
, "/as_string_full")) && !strcmp(cmd
->arg_types
, ""))
122 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
124 gchar
*res
= sampler_layer_to_string(layer
, !strcmp(cmd
->command
, "/as_string_full"));
125 gboolean result
= cbox_execute_on(fb
, NULL
, "/value", "s", error
, res
);
129 if (!strcmp(cmd
->command
, "/set_param") && !strcmp(cmd
->arg_types
, "ss"))
131 const char *key
= CBOX_ARG_S(cmd
, 0);
132 const char *value
= CBOX_ARG_S(cmd
, 1);
133 if (sampler_layer_apply_param(layer
, key
, value
, error
))
135 sampler_layer_update(layer
);
136 sampler_program_update_layers(layer
->parent_program
);
141 if (!strcmp(cmd
->command
, "/new_region") && !strcmp(cmd
->arg_types
, ""))
143 // XXXKF needs a string argument perhaps
144 if (layer
->parent_group
)
146 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Cannot create a region within a region");
149 struct sampler_layer
*l
= sampler_layer_new(layer
->module
, layer
->parent_program
, layer
);
150 sampler_layer_data_finalize(&l
->data
, l
->parent_group
? &l
->parent_group
->data
: NULL
, layer
->parent_program
);
151 sampler_layer_reset_switches(l
, l
->module
);
152 sampler_layer_update(l
);
154 sampler_program_add_layer(layer
->parent_program
, l
);
155 sampler_program_update_layers(layer
->parent_program
);
157 return cbox_execute_on(fb
, NULL
, "/uuid", "o", error
, l
);
159 if (!strcmp(cmd
->command
, "/get_children") && !strcmp(cmd
->arg_types
, ""))
161 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
164 g_hash_table_iter_init(&iter
, layer
->child_layers
);
166 while(g_hash_table_iter_next(&iter
, &key
, &value
))
168 if (!cbox_execute_on(fb
, NULL
, "/region", "o", error
, key
))
173 // otherwise, treat just like an command on normal (non-aux) output
174 return cbox_object_default_process_cmd(ct
, fb
, cmd
, error
);
179 #define PROC_FIELDS_INITIALISER(type, name, def_value) \
180 ld->name = def_value; \
182 #define PROC_FIELDS_INITIALISER_string(name) \
184 ld->name##_changed = FALSE; \
186 #define PROC_FIELDS_INITIALISER_enum(type, name, def_value) \
187 PROC_FIELDS_INITIALISER(type, name, def_value)
188 #define PROC_FIELDS_INITIALISER_dBamp(type, name, def_value) \
189 ld->name = def_value; \
190 ld->name##_linearized = -1; \
192 #define PROC_FIELDS_INITIALISER_dahdsr(name, parname, index) \
193 DAHDSR_FIELDS(PROC_SUBSTRUCT_RESET_FIELD, name, ld); \
194 DAHDSR_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, ld)
195 #define PROC_FIELDS_INITIALISER_lfo(name, parname, index) \
196 LFO_FIELDS(PROC_SUBSTRUCT_RESET_FIELD, name, ld); \
197 LFO_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, ld)
198 #define PROC_FIELDS_INITIALISER_ccrange(name) \
199 ld->name##locc = 0; \
200 ld->name##hicc = 127; \
201 ld->name##cc_number = 0; \
202 ld->has_##name##locc = 0; \
203 ld->has_##name##hicc = 0;
205 CBOX_CLASS_DEFINITION_ROOT(sampler_layer
)
207 struct sampler_layer
*sampler_layer_new(struct sampler_module
*m
, struct sampler_program
*parent_program
, struct sampler_layer
*parent_group
)
209 struct sampler_layer
*l
= calloc(1, sizeof(struct sampler_layer
));
210 struct cbox_document
*doc
= CBOX_GET_DOCUMENT(parent_program
);
211 memset(l
, 0, sizeof(struct sampler_layer
));
212 CBOX_OBJECT_HEADER_INIT(l
, sampler_layer
, doc
);
213 cbox_command_target_init(&l
->cmd_target
, sampler_layer_process_cmd
, l
);
216 l
->child_layers
= g_hash_table_new(NULL
, NULL
);
219 sampler_layer_data_clone(&l
->data
, &parent_group
->data
, FALSE
);
220 l
->parent_program
= parent_program
;
221 l
->parent_group
= parent_group
;
222 g_hash_table_add(parent_group
->child_layers
, l
);
224 CBOX_OBJECT_REGISTER(l
);
227 l
->parent_program
= parent_program
;
229 struct sampler_layer_data
*ld
= &l
->data
;
230 SAMPLER_FIXED_FIELDS(PROC_FIELDS_INITIALISER
)
232 ld
->eff_waveform
= NULL
;
233 ld
->eff_freq
= 44100;
235 ld
->velcurve
[127] = 1;
236 for (int i
= 1; i
< 127; i
++)
237 ld
->velcurve
[i
] = -1;
238 ld
->modulations
= NULL
;
242 ld
->on_cc_number
= -1;
244 ld
->eff_use_keyswitch
= 0;
248 sampler_layer_set_modulation1(l
, 74, smdest_cutoff
, 9600, 2);
249 sampler_layer_set_modulation1(l
, 71, smdest_resonance
, 12, 2);
250 sampler_layer_set_modulation(l
, smsrc_pitchlfo
, 1, smdest_pitch
, 100, 0);
253 l
->unknown_keys
= NULL
;
254 CBOX_OBJECT_REGISTER(l
);
258 #define PROC_FIELDS_CLONE(type, name, def_value) \
259 dst->name = src->name; \
260 dst->has_##name = copy_hasattr ? src->has_##name : FALSE;
261 #define PROC_FIELDS_CLONE_string(name) \
262 dst->name = src->name ? g_strdup(src->name) : NULL; \
263 dst->name##_changed = src->name##_changed; \
264 dst->has_##name = copy_hasattr ? src->has_##name : FALSE;
265 #define PROC_FIELDS_CLONE_dBamp PROC_FIELDS_CLONE
266 #define PROC_FIELDS_CLONE_enum PROC_FIELDS_CLONE
267 #define PROC_FIELDS_CLONE_dahdsr(name, parname, index) \
268 DAHDSR_FIELDS(PROC_SUBSTRUCT_CLONE, name, dst, src) \
270 DAHDSR_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, dst)
271 #define PROC_FIELDS_CLONE_lfo(name, parname, index) \
272 LFO_FIELDS(PROC_SUBSTRUCT_CLONE, name, dst, src) \
274 LFO_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, dst)
275 #define PROC_FIELDS_CLONE_ccrange(name) \
276 dst->name##locc = src->name##locc; \
277 dst->name##hicc = src->name##hicc; \
278 dst->name##cc_number = src->name##cc_number; \
279 dst->has_##name##locc = copy_hasattr ? src->has_##name##locc : FALSE; \
280 dst->has_##name##hicc = copy_hasattr ? src->has_##name##hicc : FALSE;
282 void sampler_layer_data_clone(struct sampler_layer_data
*dst
, const struct sampler_layer_data
*src
, gboolean copy_hasattr
)
284 SAMPLER_FIXED_FIELDS(PROC_FIELDS_CLONE
)
285 memcpy(dst
->velcurve
, src
->velcurve
, 128 * sizeof(float));
286 dst
->modulations
= g_slist_copy(src
->modulations
);
287 for(GSList
*mod
= dst
->modulations
; mod
; mod
= mod
->next
)
289 struct sampler_modulation
*srcm
= mod
->data
;
290 struct sampler_modulation
*dstm
= g_malloc(sizeof(struct sampler_modulation
));
291 memcpy(dstm
, srcm
, sizeof(struct sampler_modulation
));
292 dstm
->has_value
= copy_hasattr
? srcm
->has_value
: FALSE
;
295 dst
->nifs
= g_slist_copy(src
->nifs
);
296 for(GSList
*nif
= dst
->nifs
; nif
; nif
= nif
->next
)
298 struct sampler_noteinitfunc
*dstn
= g_malloc(sizeof(struct sampler_noteinitfunc
));
299 struct sampler_noteinitfunc
*srcn
= nif
->data
;
300 memcpy(dstn
, srcn
, sizeof(struct sampler_noteinitfunc
));
301 dstn
->has_value
= copy_hasattr
? srcn
->has_value
: FALSE
;
304 dst
->eff_waveform
= src
->eff_waveform
;
305 if (dst
->eff_waveform
)
306 cbox_waveform_ref(dst
->eff_waveform
);
309 #define PROC_FIELDS_CLONEPARENT(type, name, def_value) \
310 if (!l->has_##name) \
311 l->name = parent ? parent->name : def_value;
312 #define PROC_FIELDS_CLONEPARENT_string(name) \
313 if (!l->has_##name && (!l->name || !parent->name || strcmp(l->name, parent->name))) { \
315 l->name = parent && parent->name ? g_strdup(parent->name) : NULL; \
316 l->name##_changed = parent && parent->name##_changed; \
318 #define PROC_FIELDS_CLONEPARENT_dBamp PROC_FIELDS_CLONEPARENT
319 #define PROC_FIELDS_CLONEPARENT_enum PROC_FIELDS_CLONEPARENT
320 #define PROC_FIELDS_CLONEPARENT_dahdsr(name, parname, index) \
321 DAHDSR_FIELDS(PROC_SUBSTRUCT_CLONEPARENT, name, l)
322 #define PROC_FIELDS_CLONEPARENT_lfo(name, parname, index) \
323 LFO_FIELDS(PROC_SUBSTRUCT_CLONEPARENT, name, l)
324 #define PROC_FIELDS_CLONEPARENT_ccrange(name) \
325 if (!l->has_##name##locc) \
326 l->name##locc = parent ? parent->name##locc : 0; \
327 if (!l->has_##name##hicc) \
328 l->name##hicc = parent ? parent->name##hicc : 127; \
329 if (!l->has_##name##locc && !l->has_##name##hicc) \
330 l->name##cc_number = parent ? parent->name##cc_number : -1;
332 static void sampler_layer_data_getdefaults(struct sampler_layer_data
*l
, struct sampler_layer_data
*parent
)
334 SAMPLER_FIXED_FIELDS(PROC_FIELDS_CLONEPARENT
)
335 // XXXKF: add handling for velcurve
338 // set NIFs used by parent
339 for(GSList
*mod
= parent
->nifs
; mod
; mod
= mod
->next
)
341 struct sampler_noteinitfunc
*nif
= mod
->data
;
342 sampler_layer_data_add_nif(l
, nif
->notefunc
, nif
->variant
, nif
->param
, TRUE
);
344 // set modulations used by parent
345 for(GSList
*mod
= parent
->modulations
; mod
; mod
= mod
->next
)
347 struct sampler_modulation
*srcm
= mod
->data
;
348 sampler_layer_data_set_modulation(l
, srcm
->src
, srcm
->src2
, srcm
->dest
, srcm
->amount
, srcm
->flags
, TRUE
);
353 #define PROC_FIELDS_FINALISER(type, name, def_value)
354 #define PROC_FIELDS_FINALISER_string(name)
355 #define PROC_FIELDS_FINALISER_enum(type, name, def_value)
356 #define PROC_FIELDS_FINALISER_dBamp(type, name, def_value) \
357 l->name##_linearized = dB2gain(l->name);
358 #define PROC_FIELDS_FINALISER_dahdsr(name, parname, index) \
359 cbox_envelope_init_dahdsr(&l->name##_shape, &l->name, m->module.srate / CBOX_BLOCK_SIZE, 100.f, &l->name##_shape == &l->amp_env_shape);
360 #define PROC_FIELDS_FINALISER_lfo(name, parname, index) /* no finaliser required */
361 #define PROC_FIELDS_FINALISER_ccrange(name) /* no finaliser required */
363 void sampler_layer_data_finalize(struct sampler_layer_data
*l
, struct sampler_layer_data
*parent
, struct sampler_program
*p
)
365 struct sampler_module
*m
= p
->module
;
366 SAMPLER_FIXED_FIELDS(PROC_FIELDS_FINALISER
)
368 sampler_layer_data_getdefaults(l
, parent
);
370 // Handle change of sample in the prent group without override on region level
371 if (parent
&& (l
->sample_changed
|| parent
->sample_changed
))
373 struct cbox_waveform
*oldwf
= l
->eff_waveform
;
374 if (l
->sample
&& *l
->sample
)
376 GError
*error
= NULL
;
377 l
->eff_waveform
= cbox_wavebank_get_waveform(p
->name
, p
->sample_dir
, l
->sample
, &error
);
378 if (!l
->eff_waveform
)
380 g_warning("Cannot load waveform %s: %s", l
->sample
, error
? error
->message
: "unknown error");
385 l
->eff_waveform
= NULL
;
387 cbox_waveform_unref(oldwf
);
388 l
->sample_changed
= FALSE
;
391 l
->eff_freq
= (l
->eff_waveform
&& l
->eff_waveform
->info
.samplerate
) ? l
->eff_waveform
->info
.samplerate
: 44100;
392 // XXXKF should have a distinction between 'configured' and 'effective' loop start/end
393 if (l
->loop_mode
== slm_unknown
)
395 if (l
->eff_waveform
&& l
->eff_waveform
->has_loop
)
396 l
->loop_mode
= slm_loop_continuous
;
398 l
->loop_mode
= l
->loop_end
== 0 ? slm_no_loop
: slm_loop_continuous
;
401 if (l
->loop_mode
== slm_one_shot
|| l
->loop_mode
== slm_no_loop
|| l
->loop_mode
== slm_one_shot_chokeable
)
404 if ((l
->loop_mode
== slm_loop_continuous
|| l
->loop_mode
== slm_loop_sustain
) && l
->loop_start
== -1)
406 if ((l
->loop_mode
== slm_loop_continuous
|| l
->loop_mode
== slm_loop_sustain
) && l
->loop_start
== 0 && l
->eff_waveform
&& l
->eff_waveform
->has_loop
)
407 l
->loop_start
= l
->eff_waveform
->loop_start
;
408 if (l
->loop_end
== 0 && l
->eff_waveform
!= NULL
&& l
->eff_waveform
->has_loop
)
409 l
->loop_end
= l
->eff_waveform
->loop_end
;
411 if (l
->off_mode
== som_unknown
)
412 l
->off_mode
= l
->off_by
!= 0 ? som_fast
: som_normal
;
414 // if no amp_velcurve_nnn setting, default to quadratic
415 if (l
->velcurve_quadratic
== -1)
416 l
->velcurve_quadratic
= 1;
418 if (l
->key
>= 0 && l
->key
<= 127)
419 l
->lokey
= l
->hikey
= l
->pitch_keycenter
= l
->key
;
421 // interpolate missing points in velcurve
423 for (int i
= 1; i
< 128; i
++)
425 if (l
->velcurve
[i
] == -1)
427 float sv
= l
->velcurve
[start
];
428 float ev
= l
->velcurve
[i
];
429 if (l
->velcurve_quadratic
)
431 for (int j
= start
; j
<= i
; j
++)
432 l
->eff_velcurve
[j
] = sv
+ (ev
- sv
) * (j
- start
) * (j
- start
) / ((i
- start
) * (i
- start
));
436 for (int j
= start
; j
<= i
; j
++)
437 l
->eff_velcurve
[j
] = sv
+ (ev
- sv
) * (j
- start
) / (i
- start
);
441 l
->eff_use_keyswitch
= ((l
->sw_down
!= -1) || (l
->sw_up
!= -1) || (l
->sw_last
!= -1) || (l
->sw_previous
!= -1));
443 // 'linearize' the virtual circular buffer - write 3 (or N) frames before end of the loop
444 // and 3 (N) frames at the start of the loop, and play it; in rare cases this will need to be
445 // repeated twice if output write pointer is close to CBOX_BLOCK_SIZE or playback rate is very low,
447 if (l
->eff_waveform
&& l
->eff_waveform
->preloaded_frames
== l
->eff_waveform
->info
.frames
)
449 int shift
= l
->eff_waveform
->info
.channels
== 2 ? 1 : 0;
450 uint32_t halfscratch
= MAX_INTERPOLATION_ORDER
<< shift
;
451 memcpy(&l
->scratch_loop
[0], &l
->eff_waveform
->data
[(l
->loop_end
- MAX_INTERPOLATION_ORDER
) << shift
], halfscratch
* sizeof(int16_t) );
452 memcpy(&l
->scratch_end
[0], &l
->eff_waveform
->data
[(l
->loop_end
- MAX_INTERPOLATION_ORDER
) << shift
], halfscratch
* sizeof(int16_t) );
453 memset(l
->scratch_end
+ halfscratch
, 0, halfscratch
* sizeof(int16_t));
454 if (l
->loop_start
!= (uint32_t)-1)
455 memcpy(l
->scratch_loop
+ halfscratch
, &l
->eff_waveform
->data
[l
->loop_start
<< shift
], halfscratch
* sizeof(int16_t));
457 memset(l
->scratch_loop
+ halfscratch
, 0, halfscratch
* sizeof(int16_t));
461 void sampler_layer_reset_switches(struct sampler_layer
*l
, struct sampler_module
*m
)
463 l
->last_key
= l
->data
.sw_lokey
;
464 l
->current_seq_position
= l
->data
.seq_position
;
467 struct layer_foreach_struct
469 struct sampler_layer
*layer
;
470 const char *cfg_section
;
473 static void layer_foreach_func(void *user_data
, const char *key
)
475 if (!strcmp(key
, "file"))
477 // import is handled in sampler_load_layer_overrides
478 if (!strcmp(key
, "import"))
480 struct layer_foreach_struct
*lfs
= user_data
;
481 const char *value
= cbox_config_get_string(lfs
->cfg_section
, key
);
482 GError
*error
= NULL
;
483 if (!sampler_layer_apply_param(lfs
->layer
, key
, value
, &error
))
486 g_warning("Error '%s', context: %s in section %s", error
->message
, key
, lfs
->cfg_section
);
488 g_warning("Unknown sample layer parameter: %s in section %s", key
, lfs
->cfg_section
);
492 void sampler_layer_load_overrides(struct sampler_layer
*l
, const char *cfg_section
)
494 char *imp
= cbox_config_get_string(cfg_section
, "import");
496 sampler_layer_load_overrides(l
, imp
);
498 struct layer_foreach_struct lfs
= {
500 .cfg_section
= cfg_section
502 cbox_config_foreach_key(layer_foreach_func
, cfg_section
, &lfs
);
505 struct sampler_layer
*sampler_layer_new_from_section(struct sampler_module
*m
, struct sampler_program
*parent_program
, const char *cfg_section
)
507 struct sampler_layer
*l
= sampler_layer_new(m
, parent_program
, NULL
);
508 sampler_layer_load_overrides(l
, cfg_section
);
509 sampler_layer_data_finalize(&l
->data
, l
->parent_group
? &l
->parent_group
->data
: NULL
, parent_program
);
510 sampler_layer_reset_switches(l
, m
);
514 static int sfz_note_from_string(const char *note
)
516 static const int semis
[] = {9, 11, 0, 2, 4, 5, 7};
518 int nn
= tolower(note
[0]);
520 if (nn
>= '0' && nn
<= '9')
522 if (nn
< 'a' && nn
> 'g')
524 nv
= semis
[nn
- 'a'];
526 for (pos
= 1; tolower(note
[pos
]) == 'b' || note
[pos
] == '#'; pos
++)
527 nv
+= (note
[pos
] != '#') ? -1 : +1;
529 if ((note
[pos
] == '-' && note
[pos
+ 1] == '1' && note
[pos
+ 2] == '\0') || (note
[pos
] >= '0' && note
[pos
] <= '9' && note
[pos
+ 1] == '\0'))
531 return nv
+ 12 * (1 + atoi(note
+ pos
));
537 static double atof_C(const char *value
)
539 return g_ascii_strtod(value
, NULL
);
542 static gboolean
parse_envelope_param(struct sampler_layer
*layer
, struct cbox_dahdsr
*env
, struct sampler_dahdsr_has_fields
*has_fields
, int env_type
, const char *key
, const char *value
)
544 static const enum sampler_modsrc srcs
[] = { smsrc_ampenv
, smsrc_filenv
, smsrc_pitchenv
};
545 static const enum sampler_moddest dests
[] = { smdest_gain
, smdest_cutoff
, smdest_pitch
};
546 enum sampler_modsrc src
= srcs
[env_type
];
547 enum sampler_moddest dest
= dests
[env_type
];
548 float fvalue
= atof_C(value
);
550 #define PROC_SET_ENV_FIELD(name, index, def_value) \
551 if (!strcmp(key, #name)) {\
552 env->name = fvalue; \
553 has_fields->name = 1; \
556 DAHDSR_FIELDS(PROC_SET_ENV_FIELD
)
557 if (!strcmp(key
, "depth"))
558 sampler_layer_set_modulation1(layer
, src
, dest
, atof_C(value
), 0);
559 else if (!strcmp(key
, "vel2delay"))
560 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 0, fvalue
);
561 else if (!strcmp(key
, "vel2attack"))
562 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 1, fvalue
);
563 else if (!strcmp(key
, "vel2hold"))
564 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 2, fvalue
);
565 else if (!strcmp(key
, "vel2decay"))
566 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 3, fvalue
);
567 else if (!strcmp(key
, "vel2sustain"))
568 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 4, fvalue
);
569 else if (!strcmp(key
, "vel2release"))
570 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 5, fvalue
);
571 else if (!strcmp(key
, "vel2depth"))
572 sampler_layer_set_modulation(layer
, src
, smsrc_vel
, dest
, atof_C(value
), 0);
573 else if (!strncmp(key
, "depthcc", 7))
575 int cc
= atoi(key
+ 7);
576 if (cc
> 0 && cc
< 120)
577 sampler_layer_set_modulation(layer
, src
, cc
, dest
, fvalue
, 0);
586 static gboolean
parse_lfo_param(struct sampler_layer
*layer
, struct sampler_lfo_params
*params
, struct sampler_lfo_has_fields
*has_fields
, int lfo_type
, const char *key
, const char *value
)
588 static const enum sampler_modsrc srcs
[] = { smsrc_amplfo
, smsrc_fillfo
, smsrc_pitchlfo
};
589 static const enum sampler_moddest dests
[] = { smdest_gain
, smdest_cutoff
, smdest_pitch
};
590 enum sampler_modsrc src
= srcs
[lfo_type
];
591 enum sampler_moddest dest
= dests
[lfo_type
];
593 #define PROC_SET_LFO_FIELD(name, index, def_value) \
594 if (!strcmp(key, #name)) {\
595 params->name = fvalue; \
596 has_fields->name = 1; \
599 float fvalue
= atof_C(value
);
600 LFO_FIELDS(PROC_SET_LFO_FIELD
)
601 if (!strcmp(key
, "depth"))
602 sampler_layer_set_modulation1(layer
, src
, dest
, fvalue
, 0);
603 else if (!strcmp(key
, "depthchanaft"))
604 sampler_layer_set_modulation(layer
, src
, smsrc_chanaft
, dest
, fvalue
, 0);
605 else if (!strcmp(key
, "depthpolyaft"))
606 sampler_layer_set_modulation(layer
, src
, smsrc_polyaft
, dest
, fvalue
, 0);
607 else if (!strncmp(key
, "depthcc", 7))
609 int cc
= atoi(key
+ 7);
610 if (cc
> 0 && cc
< 120)
611 sampler_layer_set_modulation(layer
, src
, cc
, dest
, fvalue
, 0);
620 #define PARSE_PARAM_midi_note_t(field, strname, valuestr) \
621 return ((l->data.field = sfz_note_from_string(value)), (l->data.has_##field = 1));
622 #define PARSE_PARAM_int(field, strname, valuestr) \
623 return ((l->data.field = atoi(value)), (l->data.has_##field = 1));
624 #define PARSE_PARAM_uint32_t(field, strname, valuestr) \
625 return ((l->data.field = (uint32_t)strtoul(value, NULL, 10)), (l->data.has_##field = 1));
626 #define PARSE_PARAM_float(field, strname, valuestr) \
627 return ((l->data.field = atof_C(value)), (l->data.has_##field = 1));
629 #define PROC_APPLY_PARAM(type, name, def_value) \
630 if (!strcmp(key, #name)) { \
631 PARSE_PARAM_##type(name, #name, value) \
633 #define PROC_APPLY_PARAM_string(name) \
634 if (!strcmp(key, #name)) { \
635 if (l->data.name && value && !strcmp(l->data.name, value)) \
636 return (l->data.has_##name = 1); \
637 g_free(l->data.name); \
638 return ((l->data.name = g_strdup(value)), (l->data.name##_changed = 1), (l->data.has_##name = 1)); \
640 #define PROC_APPLY_PARAM_dBamp(type, name, def_value) \
641 if (!strcmp(key, #name)) { \
642 return ((l->data.name = atof_C(value)), (l->data.has_##name = 1)); \
644 #define PROC_APPLY_PARAM_enum(enumtype, name, def_value) \
645 if (!strcmp(key, #name)) { \
646 if (!enumtype##_from_string(value, &l->data.name)) { \
647 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Value %s is not a correct value for %s", value, #name); \
650 l->data.has_##name = 1; \
653 // LFO and envelope need special handling now
654 #define PROC_APPLY_PARAM_dahdsr(name, parname, index) \
655 if (!strncmp(key, #parname "_", sizeof(#parname))) \
656 return parse_envelope_param(l, &l->data.name, &l->data.has_##name, index, key + sizeof(#parname), value);
657 #define PROC_APPLY_PARAM_lfo(name, parname, index) \
658 if (!strncmp(key, #parname "_", sizeof(#parname))) \
659 return parse_lfo_param(l, &l->data.name, &l->data.has_##name, index, key + sizeof(#parname), value);
660 #define PROC_APPLY_PARAM_ccrange(name) /* handled separately in apply_param */
662 static void sampler_layer_apply_unknown(struct sampler_layer
*l
, const char *key
, const char *value
)
664 if (!l
->unknown_keys
)
665 l
->unknown_keys
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
667 g_hash_table_insert(l
->unknown_keys
, g_strdup(key
), g_strdup(value
));
670 gboolean
sampler_layer_apply_param(struct sampler_layer
*l
, const char *key
, const char *value
, GError
**error
)
673 // XXXKF: this is seriously stupid code, this should use a hash table for
674 // fixed strings, or something else that doesn't explode O(N**2) with
675 // number of attributes. But premature optimization is a root of all evil.
677 SAMPLER_FIXED_FIELDS(PROC_APPLY_PARAM
)
679 // XXXKF: to make things worse, some attributes have names different from
680 // C field names, or have multiple names, or don't map 1:1 to internal model
682 if (!strcmp(key
, "lolev"))
683 l
->data
.lovel
= atoi(value
), l
->data
.has_lovel
= 1;
684 else if (!strcmp(key
, "hilev"))
685 l
->data
.hivel
= atoi(value
), l
->data
.has_hivel
= 1;
686 else if (!strcmp(key
, "benddown"))
687 l
->data
.bend_down
= atoi(value
), l
->data
.has_bend_down
= 1;
688 else if (!strcmp(key
, "bendup"))
689 l
->data
.bend_up
= atoi(value
), l
->data
.has_bend_up
= 1;
690 else if (!strcmp(key
, "loopstart"))
691 l
->data
.loop_start
= atoi(value
), l
->data
.has_loop_start
= 1;
692 else if (!strcmp(key
, "loopend"))
693 l
->data
.loop_end
= atoi(value
), l
->data
.has_loop_end
= 1;
694 else if (!strcmp(key
, "cutoff_chanaft"))
695 sampler_layer_set_modulation1(l
, smsrc_chanaft
, smdest_cutoff
, atof_C(value
), 0);
696 else if (!strcmp(key
, "amp_random"))
697 sampler_layer_add_nif(l
, sampler_nif_addrandom
, 0, atof_C(value
));
698 else if (!strcmp(key
, "fil_random"))
699 sampler_layer_add_nif(l
, sampler_nif_addrandom
, 1, atof_C(value
));
700 else if (!strcmp(key
, "pitch_random"))
701 sampler_layer_add_nif(l
, sampler_nif_addrandom
, 2, atof_C(value
));
702 else if (!strcmp(key
, "pitch_veltrack"))
703 sampler_layer_add_nif(l
, sampler_nif_vel2pitch
, 0, atof_C(value
));
704 else if (!strncmp(key
, "delay_cc", 8))
706 int ccno
= atoi(key
+ 8);
707 if (ccno
> 0 && ccno
< 120)
708 sampler_layer_add_nif(l
, sampler_nif_cc2delay
, ccno
, atof_C(value
));
712 else if (!strncmp(key
, "cutoff_cc", 9))
714 int ccno
= atoi(key
+ 9);
715 if (ccno
> 0 && ccno
< 120)
716 sampler_layer_set_modulation1(l
, ccno
, smdest_cutoff
, atof_C(value
), 0);
720 else if (!strncmp(key
, "gain_cc", 7))
722 int ccno
= atoi(key
+ 7);
723 if (ccno
> 0 && ccno
< 120)
724 sampler_layer_set_modulation1(l
, ccno
, smdest_gain
, atof_C(value
), 0);
728 else if (!strncmp(key
, "amp_velcurve_", 13))
730 // if not known yet, set to 0, it can always be overriden via velcurve_quadratic setting
731 if (l
->data
.velcurve_quadratic
== -1)
732 l
->data
.velcurve_quadratic
= 0;
733 int point
= atoi(key
+ 13);
734 if (point
>= 0 && point
<= 127)
736 l
->data
.velcurve
[point
] = atof_C(value
);
737 if (l
->data
.velcurve
[point
] < 0)
738 l
->data
.velcurve
[point
] = 0;
739 if (l
->data
.velcurve
[point
] > 1)
740 l
->data
.velcurve
[point
] = 1;
745 else if (!strncmp(key
, "on_locc", 7) || !strncmp(key
, "on_hicc", 7))
747 int cc
= atoi(key
+ 7);
748 if (cc
> 0 && cc
< 120)
752 l
->data
.on_cc_number
= cc
;
755 l
->data
.on_locc
= atoi(value
);
756 l
->data
.has_on_locc
= TRUE
;
760 l
->data
.on_hicc
= atoi(value
);
761 l
->data
.has_on_hicc
= TRUE
;
766 l
->data
.on_cc_number
= -1;
767 l
->data
.has_on_locc
= FALSE
;
768 l
->data
.has_on_hicc
= FALSE
;
774 else if (!strncmp(key
, "locc", 4) || !strncmp(key
, "hicc", 4))
776 int cc
= atoi(key
+ 4);
777 if (cc
> 0 && cc
< 120)
781 l
->data
.cc_number
= cc
;
784 l
->data
.locc
= atoi(value
);
785 l
->data
.has_locc
= TRUE
;
789 l
->data
.hicc
= atoi(value
);
790 l
->data
.has_hicc
= TRUE
;
795 l
->data
.cc_number
= -1;
796 l
->data
.has_locc
= FALSE
;
797 l
->data
.has_hicc
= FALSE
;
803 else if (!strcmp(key
, "loopmode"))
806 goto try_now
; // yes, goto, why not?
808 else if (!strcmp(key
, "offby"))
818 sampler_layer_apply_unknown(l
, key
, value
);
819 g_warning("Unknown SFZ property key: '%s'", key
);
823 #define TYPE_PRINTF_uint32_t(name, def_value) \
824 if (show_inherited || l->has_##name) \
825 g_string_append_printf(outstr, " %s=%u", #name, (unsigned)(l->name));
826 #define TYPE_PRINTF_int(name, def_value) \
827 if (show_inherited || l->has_##name) \
828 g_string_append_printf(outstr, " %s=%d", #name, (int)(l->name));
829 #define TYPE_PRINTF_midi_note_t(name, def_value) \
830 if (show_inherited || l->has_##name) { \
833 g_string_append_printf(outstr, " %s=-1", #name); \
835 g_string_append_printf(outstr, " %s=%c%s%d", #name, "ccddeffggaab"[val%12], "\000#\000#\000\000#\000#\000#\000#\000"+(val%12), (val/12-1)); \
837 #define TYPE_PRINTF_float(name, def_value) \
838 if (show_inherited || l->has_##name) \
839 g_string_append_printf(outstr, " %s=%s", #name, g_ascii_dtostr(floatbuf, floatbufsize, l->name));
841 #define PROC_FIELDS_TO_FILEPTR(type, name, def_value) \
842 TYPE_PRINTF_##type(name, def_value)
843 #define PROC_FIELDS_TO_FILEPTR_string(name) \
844 if (show_inherited || l->has_##name) \
845 g_string_append_printf(outstr, " %s=%s", #name, l->name ? l->name : "");
846 #define PROC_FIELDS_TO_FILEPTR_dBamp(type, name, def_value) \
847 if (show_inherited || l->has_##name) \
848 g_string_append_printf(outstr, " %s=%s", #name, g_ascii_dtostr(floatbuf, floatbufsize, l->name));
849 #define PROC_FIELDS_TO_FILEPTR_enum(enumtype, name, def_value) \
850 if ((show_inherited || l->has_##name) && (tmpstr = enumtype##_to_string(l->name)) != NULL) \
851 g_string_append_printf(outstr, " %s=%s", #name, tmpstr);
853 #define ENV_PARAM_OUTPUT(param, index, def_value, env, envfield, envname) \
854 if (show_inherited || l->has_##envfield.param) \
855 g_string_append_printf(outstr, " " #envname "_" #param "=%s", g_ascii_dtostr(floatbuf, floatbufsize, env.param));
857 #define PROC_FIELDS_TO_FILEPTR_dahdsr(name, parname, index) \
858 DAHDSR_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
859 #define PROC_FIELDS_TO_FILEPTR_lfo(name, parname, index) \
860 LFO_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
861 #define PROC_FIELDS_TO_FILEPTR_ccrange(name) \
862 if (l->has_##name##locc) \
863 g_string_append_printf(outstr, " " #name "locc%d=%d", l->name##cc_number, l->name##locc); \
864 if (l->has_##name##hicc) \
865 g_string_append_printf(outstr, " " #name "hicc%d=%d", l->name##cc_number, l->name##hicc);
867 gchar
*sampler_layer_to_string(struct sampler_layer
*lr
, gboolean show_inherited
)
869 struct sampler_layer_data
*l
= &lr
->data
;
870 GString
*outstr
= g_string_sized_new(200);
872 char floatbuf
[G_ASCII_DTOSTR_BUF_SIZE
];
873 int floatbufsize
= G_ASCII_DTOSTR_BUF_SIZE
;
874 SAMPLER_FIXED_FIELDS(PROC_FIELDS_TO_FILEPTR
)
876 static const char *addrandom_variants
[] = { "amp", "fil", "pitch" };
877 static const char *modsrc_names
[] = { "chanaft", "vel", "polyaft", "pitch", "pitcheg", "fileg", "ampeg", "pitchlfo", "fillfo", "amplfo", "" };
878 static const char *moddest_names
[] = { "gain", "pitch", "cutoff", "resonance" };
879 for(GSList
*nif
= l
->nifs
; nif
; nif
= nif
->next
)
881 struct sampler_noteinitfunc
*nd
= nif
->data
;
882 if (!nd
->has_value
&& !show_inherited
)
884 #define PROC_ENVSTAGE_NAME(name, index, def_value) #name,
885 static const char *env_stages
[] = { DAHDSR_FIELDS(PROC_ENVSTAGE_NAME
) };
887 g_ascii_dtostr(floatbuf
, floatbufsize
, nd
->param
);
889 if (nd
->notefunc
== sampler_nif_addrandom
&& v
>= 0 && v
<= 2)
890 g_string_append_printf(outstr
, " %s_random=%s", addrandom_variants
[nd
->variant
], floatbuf
);
891 else if (nd
->notefunc
== sampler_nif_vel2pitch
)
892 g_string_append_printf(outstr
, " pitch_veltrack=%s", floatbuf
);
893 else if (nd
->notefunc
== sampler_nif_cc2delay
&& v
>= 0 && v
< 120)
894 g_string_append_printf(outstr
, " delay_cc%d=%s", nd
->variant
, floatbuf
);
895 else if (nd
->notefunc
== sampler_nif_vel2env
&& v
>= 0 && (v
& 15) < 6 && (v
>> 4) < 3)
896 g_string_append_printf(outstr
, " %seg_vel2%s=%s", addrandom_variants
[nd
->variant
>> 4], env_stages
[1 + (v
& 15)], floatbuf
);
898 for(GSList
*mod
= l
->modulations
; mod
; mod
= mod
->next
)
900 struct sampler_modulation
*md
= mod
->data
;
901 if (!md
->has_value
&& !show_inherited
)
903 g_ascii_dtostr(floatbuf
, floatbufsize
, md
->amount
);
905 if (md
->src2
== smsrc_none
)
909 g_string_append_printf(outstr
, " %s_cc%d=%s", moddest_names
[md
->dest
], md
->src
, floatbuf
);
912 if (md
->src
< 120 + sizeof(modsrc_names
) / sizeof(modsrc_names
[0]))
914 if ((md
->src
== smsrc_filenv
&& md
->dest
== smdest_cutoff
) ||
915 (md
->src
== smsrc_pitchenv
&& md
->dest
== smdest_pitch
))
916 g_string_append_printf(outstr
, " %s_depth=%s", modsrc_names
[md
->src
- 120], floatbuf
);
918 g_string_append_printf(outstr
, " %s_%s=%s", moddest_names
[md
->dest
], modsrc_names
[md
->src
- 120], floatbuf
);
922 if ((md
->src
== smsrc_amplfo
&& md
->dest
== smdest_gain
) ||
923 (md
->src
== smsrc_fillfo
&& md
->dest
== smdest_cutoff
) ||
924 (md
->src
== smsrc_pitchlfo
&& md
->dest
== smdest_pitch
))
930 g_string_append_printf(outstr
, " %slfo_depth%s=%s", moddest_names
[md
->dest
], modsrc_names
[md
->src2
- 120], floatbuf
);
935 g_string_append_printf(outstr
, " %slfo_depthcc%d=%s", moddest_names
[md
->dest
], md
->src2
, floatbuf
);
942 if ((md
->src
== smsrc_ampenv
&& md
->dest
== smdest_gain
) ||
943 (md
->src
== smsrc_filenv
&& md
->dest
== smdest_cutoff
) ||
944 (md
->src
== smsrc_pitchenv
&& md
->dest
== smdest_pitch
))
946 if (md
->src2
== smsrc_vel
)
948 g_string_append_printf(outstr
, " %seg_vel2depth=%s", moddest_names
[md
->dest
], floatbuf
);
953 g_string_append_printf(outstr
, " %s_depthcc%d=%s", modsrc_names
[md
->src
- 120], md
->src2
, floatbuf
);
957 g_string_append_printf(outstr
, " genericmod_from_%d_and_%d_to_%d=%s", md
->src
, md
->src2
, md
->dest
, floatbuf
);
960 if (lr
->unknown_keys
)
964 g_hash_table_iter_init(&hti
, lr
->unknown_keys
);
965 while(g_hash_table_iter_next(&hti
, (gpointer
*)&key
, (gpointer
*)&value
))
966 g_string_append_printf(outstr
, " %s=%s", key
, value
);
969 gchar
*res
= outstr
->str
;
970 g_string_free(outstr
, FALSE
);
974 void sampler_layer_dump(struct sampler_layer
*l
, FILE *f
)
976 gchar
*str
= sampler_layer_to_string(l
, FALSE
);
977 fprintf(f
, "%s\n", str
);
980 void sampler_layer_data_close(struct sampler_layer_data
*l
)
982 g_slist_free_full(l
->nifs
, g_free
);
983 g_slist_free_full(l
->modulations
, g_free
);
986 cbox_waveform_unref(l
->eff_waveform
);
987 l
->eff_waveform
= NULL
;
992 void sampler_layer_data_destroy(struct sampler_layer_data
*l
)
994 sampler_layer_data_close(l
);
998 void sampler_layer_destroyfunc(struct cbox_objhdr
*objhdr
)
1000 struct sampler_layer
*l
= CBOX_H2O(objhdr
);
1001 struct sampler_program
*prg
= l
->parent_program
;
1002 assert(g_hash_table_size(l
->child_layers
) == 0);
1004 if (l
->parent_group
)
1006 g_hash_table_remove(l
->parent_group
->child_layers
, l
);
1007 if (prg
&& prg
->rll
)
1009 sampler_program_delete_layer(prg
, l
);
1010 sampler_program_update_layers(l
->parent_program
);
1012 l
->parent_group
= NULL
;
1014 sampler_layer_data_close(&l
->data
);
1016 sampler_layer_data_destroy(l
->runtime
);
1017 if (l
->unknown_keys
)
1018 g_hash_table_destroy(l
->unknown_keys
);
1019 if (l
->child_layers
)
1020 g_hash_table_destroy(l
->child_layers
);
1025 //////////////////////////////////////////////////////////////////////////
1027 struct sampler_layer_update_cmd
1029 struct sampler_module
*module
;
1030 struct sampler_layer
*layer
;
1031 struct sampler_layer_data
*new_data
;
1032 struct sampler_layer_data
*old_data
;
1035 static int sampler_layer_update_cmd_prepare(void *data
)
1037 struct sampler_layer_update_cmd
*cmd
= data
;
1038 cmd
->old_data
= cmd
->layer
->runtime
;
1039 cmd
->new_data
= calloc(1, sizeof(struct sampler_layer_data
));
1041 sampler_layer_data_clone(cmd
->new_data
, &cmd
->layer
->data
, TRUE
);
1042 sampler_layer_data_finalize(cmd
->new_data
, cmd
->layer
->parent_group
? &cmd
->layer
->parent_group
->data
: NULL
, cmd
->layer
->parent_program
);
1043 if (cmd
->layer
->runtime
== NULL
)
1045 // initial update of the layer, so none of the voices need updating yet
1046 // because the layer hasn't been allocated to any voice
1047 cmd
->layer
->runtime
= cmd
->new_data
;
1053 static int sampler_layer_update_cmd_execute(void *data
)
1055 struct sampler_layer_update_cmd
*cmd
= data
;
1057 for (int i
= 0; i
< 16; i
++)
1059 FOREACH_VOICE(cmd
->module
->channels
[i
].voices_running
, v
)
1061 if (v
->layer
== cmd
->old_data
)
1062 v
->layer
= cmd
->new_data
;
1065 cmd
->layer
->runtime
= cmd
->new_data
;
1069 static void sampler_layer_update_cmd_cleanup(void *data
)
1071 struct sampler_layer_update_cmd
*cmd
= data
;
1073 sampler_layer_data_destroy(cmd
->old_data
);
1076 void sampler_layer_update(struct sampler_layer
*l
)
1078 // if changing a group, update all child regions instead
1079 if (g_hash_table_size(l
->child_layers
))
1081 GHashTableIter iter
;
1082 g_hash_table_iter_init(&iter
, l
->child_layers
);
1083 gpointer key
, value
;
1084 while(g_hash_table_iter_next(&iter
, &key
, &value
))
1086 sampler_layer_data_finalize(&((struct sampler_layer
*)key
)->data
, &l
->data
, l
->parent_program
);
1087 sampler_layer_update((struct sampler_layer
*)key
);
1091 static struct cbox_rt_cmd_definition rtcmd
= {
1092 .prepare
= sampler_layer_update_cmd_prepare
,
1093 .execute
= sampler_layer_update_cmd_execute
,
1094 .cleanup
= sampler_layer_update_cmd_cleanup
,
1097 struct sampler_layer_update_cmd lcmd
;
1098 lcmd
.module
= l
->module
;
1100 lcmd
.new_data
= NULL
;
1101 lcmd
.old_data
= NULL
;
1103 // In order to be able to use the async call, it would be necessary to
1104 // identify old data by layer pointer, not layer data pointer. For now,
1105 // it might be good enough to just use sync calls for this.
1106 cbox_rt_execute_cmd_sync(l
->module
->module
.rt
, &rtcmd
, &lcmd
);