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"
37 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
)
39 GSList
*p
= l
->modulations
;
42 struct sampler_modulation
*sm
= p
->data
;
43 if (sm
->src
== src
&& sm
->src2
== src2
&& sm
->dest
== dest
)
45 // do not overwrite locally set value with defaults
46 if (propagating_defaults
&& sm
->has_value
)
50 sm
->has_value
= !propagating_defaults
;
55 struct sampler_modulation
*sm
= g_malloc0(sizeof(struct sampler_modulation
));
61 sm
->has_value
= !propagating_defaults
;
62 l
->modulations
= g_slist_prepend(l
->modulations
, sm
);
65 void sampler_layer_set_modulation1(struct sampler_layer
*l
, enum sampler_modsrc src
, enum sampler_moddest dest
, float amount
, int flags
)
67 sampler_layer_data_set_modulation(&l
->data
, src
, smsrc_none
, dest
, amount
, flags
, FALSE
);
70 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
)
72 sampler_layer_data_set_modulation(&l
->data
, src
, src2
, dest
, amount
, flags
, FALSE
);
75 void sampler_layer_data_add_nif(struct sampler_layer_data
*l
, SamplerNoteInitFunc notefunc
, int variant
, float param
, gboolean propagating_defaults
)
80 struct sampler_noteinitfunc
*nif
= p
->data
;
81 if (nif
->notefunc
== notefunc
&& nif
->variant
== variant
)
83 // do not overwrite locally set value with defaults
84 if (propagating_defaults
&& nif
->has_value
)
87 nif
->has_value
= !propagating_defaults
;
92 struct sampler_noteinitfunc
*nif
= malloc(sizeof(struct sampler_noteinitfunc
));
93 nif
->notefunc
= notefunc
;
94 nif
->variant
= variant
;
96 nif
->has_value
= !propagating_defaults
;
97 l
->nifs
= g_slist_prepend(l
->nifs
, nif
);
100 void sampler_layer_add_nif(struct sampler_layer
*l
, SamplerNoteInitFunc notefunc
, int variant
, float param
)
102 sampler_layer_data_add_nif(&l
->data
, notefunc
, variant
, param
, FALSE
);
105 static gboolean
sampler_layer_process_cmd(struct cbox_command_target
*ct
, struct cbox_command_target
*fb
, struct cbox_osc_command
*cmd
, GError
**error
)
107 struct sampler_layer
*layer
= ct
->user_data
;
108 if (!strcmp(cmd
->command
, "/status") && !strcmp(cmd
->arg_types
, ""))
110 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
113 if (!((!layer
->parent_program
|| cbox_execute_on(fb
, NULL
, "/parent_program", "o", error
, layer
->parent_program
)) &&
114 (!layer
->parent_group
|| cbox_execute_on(fb
, NULL
, "/parent_group", "o", error
, layer
->parent_group
)) &&
115 CBOX_OBJECT_DEFAULT_STATUS(layer
, fb
, error
)))
119 if ((!strcmp(cmd
->command
, "/as_string") || !strcmp(cmd
->command
, "/as_string_full")) && !strcmp(cmd
->arg_types
, ""))
121 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
123 gchar
*res
= sampler_layer_to_string(layer
, !strcmp(cmd
->command
, "/as_string_full"));
124 gboolean result
= cbox_execute_on(fb
, NULL
, "/value", "s", error
, res
);
128 if (!strcmp(cmd
->command
, "/set_param") && !strcmp(cmd
->arg_types
, "ss"))
130 const char *key
= CBOX_ARG_S(cmd
, 0);
131 const char *value
= CBOX_ARG_S(cmd
, 1);
132 if (sampler_layer_apply_param(layer
, key
, value
, error
))
134 sampler_layer_update(layer
);
135 sampler_program_update_layers(layer
->parent_program
);
140 if (!strcmp(cmd
->command
, "/new_region") && !strcmp(cmd
->arg_types
, ""))
142 // XXXKF needs a string argument perhaps
143 if (layer
->parent_group
)
145 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Cannot create a region within a region");
148 struct sampler_layer
*l
= sampler_layer_new(layer
->module
, layer
->parent_program
, layer
);
149 sampler_layer_data_finalize(&l
->data
, l
->parent_group
? &l
->parent_group
->data
: NULL
, layer
->parent_program
);
150 sampler_layer_reset_switches(l
, l
->module
);
151 sampler_layer_update(l
);
153 sampler_program_add_layer(layer
->parent_program
, l
);
154 sampler_program_update_layers(layer
->parent_program
);
156 return cbox_execute_on(fb
, NULL
, "/uuid", "o", error
, l
);
158 if (!strcmp(cmd
->command
, "/get_children") && !strcmp(cmd
->arg_types
, ""))
160 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
163 g_hash_table_iter_init(&iter
, layer
->child_layers
);
165 while(g_hash_table_iter_next(&iter
, &key
, &value
))
167 if (!cbox_execute_on(fb
, NULL
, "/region", "o", error
, key
))
172 // otherwise, treat just like an command on normal (non-aux) output
173 return cbox_object_default_process_cmd(ct
, fb
, cmd
, error
);
178 #define PROC_FIELDS_INITIALISER(type, name, def_value) \
179 ld->name = def_value; \
181 #define PROC_FIELDS_INITIALISER_string(name) \
183 ld->name##_changed = FALSE; \
185 #define PROC_FIELDS_INITIALISER_enum(type, name, def_value) \
186 PROC_FIELDS_INITIALISER(type, name, def_value)
187 #define PROC_FIELDS_INITIALISER_dBamp(type, name, def_value) \
188 ld->name = def_value; \
189 ld->name##_linearized = -1; \
191 #define PROC_FIELDS_INITIALISER_dahdsr(name, parname, index) \
192 DAHDSR_FIELDS(PROC_SUBSTRUCT_RESET_FIELD, name, ld); \
193 DAHDSR_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, ld)
194 #define PROC_FIELDS_INITIALISER_lfo(name, parname, index) \
195 LFO_FIELDS(PROC_SUBSTRUCT_RESET_FIELD, name, ld); \
196 LFO_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, ld)
197 #define PROC_FIELDS_INITIALISER_eq(name, parname, index) \
198 EQ_FIELDS(PROC_SUBSTRUCT_RESET_FIELD, name, ld); \
199 EQ_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, ld)
200 #define PROC_FIELDS_INITIALISER_ccrange(name) \
201 ld->name##locc = 0; \
202 ld->name##hicc = 127; \
203 ld->name##cc_number = 0; \
204 ld->has_##name##locc = 0; \
205 ld->has_##name##hicc = 0;
207 CBOX_CLASS_DEFINITION_ROOT(sampler_layer
)
209 struct sampler_layer
*sampler_layer_new(struct sampler_module
*m
, struct sampler_program
*parent_program
, struct sampler_layer
*parent_group
)
211 struct sampler_layer
*l
= calloc(1, sizeof(struct sampler_layer
));
212 struct cbox_document
*doc
= CBOX_GET_DOCUMENT(parent_program
);
213 memset(l
, 0, sizeof(struct sampler_layer
));
214 CBOX_OBJECT_HEADER_INIT(l
, sampler_layer
, doc
);
215 cbox_command_target_init(&l
->cmd_target
, sampler_layer_process_cmd
, l
);
218 l
->child_layers
= g_hash_table_new(NULL
, NULL
);
221 sampler_layer_data_clone(&l
->data
, &parent_group
->data
, FALSE
);
222 l
->parent_program
= parent_program
;
223 l
->parent_group
= parent_group
;
224 g_hash_table_replace(parent_group
->child_layers
, l
, l
);
226 CBOX_OBJECT_REGISTER(l
);
229 l
->parent_program
= parent_program
;
231 struct sampler_layer_data
*ld
= &l
->data
;
232 SAMPLER_FIXED_FIELDS(PROC_FIELDS_INITIALISER
)
234 ld
->eff_waveform
= NULL
;
235 ld
->eff_freq
= 44100;
237 ld
->velcurve
[127] = 1;
238 for (int i
= 1; i
< 127; i
++)
239 ld
->velcurve
[i
] = -1;
240 ld
->modulations
= NULL
;
244 ld
->on_cc_number
= -1;
246 ld
->eff_use_keyswitch
= 0;
250 sampler_layer_set_modulation1(l
, 74, smdest_cutoff
, 9600, 2);
251 sampler_layer_set_modulation1(l
, 71, smdest_resonance
, 12, 2);
252 sampler_layer_set_modulation(l
, smsrc_pitchlfo
, 1, smdest_pitch
, 100, 0);
255 l
->unknown_keys
= NULL
;
256 CBOX_OBJECT_REGISTER(l
);
260 #define PROC_FIELDS_CLONE(type, name, def_value) \
261 dst->name = src->name; \
262 dst->has_##name = copy_hasattr ? src->has_##name : FALSE;
263 #define PROC_FIELDS_CLONE_string(name) \
264 dst->name = src->name ? g_strdup(src->name) : NULL; \
265 dst->name##_changed = src->name##_changed; \
266 dst->has_##name = copy_hasattr ? src->has_##name : FALSE;
267 #define PROC_FIELDS_CLONE_dBamp PROC_FIELDS_CLONE
268 #define PROC_FIELDS_CLONE_enum PROC_FIELDS_CLONE
269 #define PROC_FIELDS_CLONE_dahdsr(name, parname, index) \
270 DAHDSR_FIELDS(PROC_SUBSTRUCT_CLONE, name, dst, src) \
272 DAHDSR_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, dst)
273 #define PROC_FIELDS_CLONE_lfo(name, parname, index) \
274 LFO_FIELDS(PROC_SUBSTRUCT_CLONE, name, dst, src) \
276 LFO_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, dst)
277 #define PROC_FIELDS_CLONE_eq(name, parname, index) \
278 EQ_FIELDS(PROC_SUBSTRUCT_CLONE, name, dst, src) \
280 EQ_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, dst)
281 #define PROC_FIELDS_CLONE_ccrange(name) \
282 dst->name##locc = src->name##locc; \
283 dst->name##hicc = src->name##hicc; \
284 dst->name##cc_number = src->name##cc_number; \
285 dst->has_##name##locc = copy_hasattr ? src->has_##name##locc : FALSE; \
286 dst->has_##name##hicc = copy_hasattr ? src->has_##name##hicc : FALSE;
288 void sampler_layer_data_clone(struct sampler_layer_data
*dst
, const struct sampler_layer_data
*src
, gboolean copy_hasattr
)
290 SAMPLER_FIXED_FIELDS(PROC_FIELDS_CLONE
)
291 memcpy(dst
->velcurve
, src
->velcurve
, 128 * sizeof(float));
292 dst
->modulations
= g_slist_copy(src
->modulations
);
293 for(GSList
*mod
= dst
->modulations
; mod
; mod
= mod
->next
)
295 struct sampler_modulation
*srcm
= mod
->data
;
296 struct sampler_modulation
*dstm
= g_malloc(sizeof(struct sampler_modulation
));
297 memcpy(dstm
, srcm
, sizeof(struct sampler_modulation
));
298 dstm
->has_value
= copy_hasattr
? srcm
->has_value
: FALSE
;
301 dst
->nifs
= g_slist_copy(src
->nifs
);
302 for(GSList
*nif
= dst
->nifs
; nif
; nif
= nif
->next
)
304 struct sampler_noteinitfunc
*dstn
= g_malloc(sizeof(struct sampler_noteinitfunc
));
305 struct sampler_noteinitfunc
*srcn
= nif
->data
;
306 memcpy(dstn
, srcn
, sizeof(struct sampler_noteinitfunc
));
307 dstn
->has_value
= copy_hasattr
? srcn
->has_value
: FALSE
;
310 dst
->eff_waveform
= src
->eff_waveform
;
311 if (dst
->eff_waveform
)
312 cbox_waveform_ref(dst
->eff_waveform
);
315 #define PROC_FIELDS_CLONEPARENT(type, name, def_value) \
316 if (!l->has_##name) \
317 l->name = parent ? parent->name : def_value;
318 #define PROC_FIELDS_CLONEPARENT_string(name) \
319 if (!l->has_##name && (!l->name || !parent->name || strcmp(l->name, parent->name))) { \
321 l->name = parent && parent->name ? g_strdup(parent->name) : NULL; \
322 l->name##_changed = parent && parent->name##_changed; \
324 #define PROC_FIELDS_CLONEPARENT_dBamp PROC_FIELDS_CLONEPARENT
325 #define PROC_FIELDS_CLONEPARENT_enum PROC_FIELDS_CLONEPARENT
326 #define PROC_FIELDS_CLONEPARENT_dahdsr(name, parname, index) \
327 DAHDSR_FIELDS(PROC_SUBSTRUCT_CLONEPARENT, name, l)
328 #define PROC_FIELDS_CLONEPARENT_lfo(name, parname, index) \
329 LFO_FIELDS(PROC_SUBSTRUCT_CLONEPARENT, name, l)
330 #define PROC_FIELDS_CLONEPARENT_eq(name, parname, index) \
331 EQ_FIELDS(PROC_SUBSTRUCT_CLONEPARENT, name, l)
332 #define PROC_FIELDS_CLONEPARENT_ccrange(name) \
333 if (!l->has_##name##locc) \
334 l->name##locc = parent ? parent->name##locc : 0; \
335 if (!l->has_##name##hicc) \
336 l->name##hicc = parent ? parent->name##hicc : 127; \
337 if (!l->has_##name##locc && !l->has_##name##hicc) \
338 l->name##cc_number = parent ? parent->name##cc_number : -1;
340 static void sampler_layer_data_getdefaults(struct sampler_layer_data
*l
, struct sampler_layer_data
*parent
)
342 SAMPLER_FIXED_FIELDS(PROC_FIELDS_CLONEPARENT
)
343 // XXXKF: add handling for velcurve
346 // set NIFs used by parent
347 for(GSList
*mod
= parent
->nifs
; mod
; mod
= mod
->next
)
349 struct sampler_noteinitfunc
*nif
= mod
->data
;
350 sampler_layer_data_add_nif(l
, nif
->notefunc
, nif
->variant
, nif
->param
, TRUE
);
352 // set modulations used by parent
353 for(GSList
*mod
= parent
->modulations
; mod
; mod
= mod
->next
)
355 struct sampler_modulation
*srcm
= mod
->data
;
356 sampler_layer_data_set_modulation(l
, srcm
->src
, srcm
->src2
, srcm
->dest
, srcm
->amount
, srcm
->flags
, TRUE
);
361 #define PROC_FIELDS_FINALISER(type, name, def_value)
362 #define PROC_FIELDS_FINALISER_string(name)
363 #define PROC_FIELDS_FINALISER_enum(type, name, def_value)
364 #define PROC_FIELDS_FINALISER_dBamp(type, name, def_value) \
365 l->name##_linearized = dB2gain(l->name);
366 #define PROC_FIELDS_FINALISER_dahdsr(name, parname, index) \
367 cbox_envelope_init_dahdsr(&l->name##_shape, &l->name, m->module.srate / CBOX_BLOCK_SIZE, 100.f, &l->name##_shape == &l->amp_env_shape);
368 #define PROC_FIELDS_FINALISER_lfo(name, parname, index) /* no finaliser required */
369 #define PROC_FIELDS_FINALISER_eq(name, parname, index) l->name.effective_freq = (l->name.freq ? l->name.freq : 5 * powf(10.f, 1 + (index)));
370 #define PROC_FIELDS_FINALISER_ccrange(name) /* no finaliser required */
372 void sampler_layer_data_finalize(struct sampler_layer_data
*l
, struct sampler_layer_data
*parent
, struct sampler_program
*p
)
374 struct sampler_module
*m
= p
->module
;
375 SAMPLER_FIXED_FIELDS(PROC_FIELDS_FINALISER
)
377 sampler_layer_data_getdefaults(l
, parent
);
379 // Handle change of sample in the prent group without override on region level
380 if (parent
&& (l
->sample_changed
|| parent
->sample_changed
))
382 struct cbox_waveform
*oldwf
= l
->eff_waveform
;
383 if (l
->sample
&& *l
->sample
)
385 GError
*error
= NULL
;
386 l
->eff_waveform
= cbox_wavebank_get_waveform(p
->name
, p
->tarfile
, p
->sample_dir
, l
->sample
, &error
);
387 if (!l
->eff_waveform
)
389 g_warning("Cannot load waveform %s: %s", l
->sample
, error
? error
->message
: "unknown error");
394 l
->eff_waveform
= NULL
;
396 cbox_waveform_unref(oldwf
);
397 l
->sample_changed
= FALSE
;
400 l
->eff_freq
= (l
->eff_waveform
&& l
->eff_waveform
->info
.samplerate
) ? l
->eff_waveform
->info
.samplerate
: 44100;
401 l
->eff_loop_mode
= l
->loop_mode
;
402 if (l
->loop_mode
== slm_unknown
)
404 if (l
->eff_waveform
&& l
->eff_waveform
->has_loop
)
405 l
->eff_loop_mode
= slm_loop_continuous
;
408 l
->eff_loop_mode
= l
->loop_end
== 0 ? slm_no_loop
: slm_loop_continuous
;
411 if (l
->eff_loop_mode
== slm_one_shot
|| l
->eff_loop_mode
== slm_no_loop
|| l
->eff_loop_mode
== slm_one_shot_chokeable
)
414 if ((l
->eff_loop_mode
== slm_loop_continuous
|| l
->eff_loop_mode
== slm_loop_sustain
) && l
->loop_start
== -1)
416 if ((l
->eff_loop_mode
== slm_loop_continuous
|| l
->eff_loop_mode
== slm_loop_sustain
) && l
->loop_start
== 0 && l
->eff_waveform
&& l
->eff_waveform
->has_loop
)
417 l
->loop_start
= l
->eff_waveform
->loop_start
;
418 if (l
->loop_end
== 0 && l
->eff_waveform
!= NULL
)
419 l
->loop_end
= l
->eff_waveform
->has_loop
? l
->eff_waveform
->loop_end
: l
->eff_waveform
->info
.frames
;
421 if (l
->off_mode
== som_unknown
)
422 l
->off_mode
= l
->off_by
!= 0 ? som_fast
: som_normal
;
424 // if no amp_velcurve_nnn setting, default to quadratic
425 if (l
->velcurve_quadratic
== -1)
426 l
->velcurve_quadratic
= 1;
428 if (l
->key
>= 0 && l
->key
<= 127)
429 l
->lokey
= l
->hikey
= l
->pitch_keycenter
= l
->key
;
431 // interpolate missing points in velcurve
433 for (int i
= 1; i
< 128; i
++)
435 if (l
->velcurve
[i
] == -1)
437 float sv
= l
->velcurve
[start
];
438 float ev
= l
->velcurve
[i
];
439 if (l
->velcurve_quadratic
)
441 for (int j
= start
; j
<= i
; j
++)
442 l
->eff_velcurve
[j
] = sv
+ (ev
- sv
) * (j
- start
) * (j
- start
) / ((i
- start
) * (i
- start
));
446 for (int j
= start
; j
<= i
; j
++)
447 l
->eff_velcurve
[j
] = sv
+ (ev
- sv
) * (j
- start
) / (i
- start
);
451 l
->eff_use_keyswitch
= ((l
->sw_down
!= -1) || (l
->sw_up
!= -1) || (l
->sw_last
!= -1) || (l
->sw_previous
!= -1));
453 // 'linearize' the virtual circular buffer - write 3 (or N) frames before end of the loop
454 // and 3 (N) frames at the start of the loop, and play it; in rare cases this will need to be
455 // repeated twice if output write pointer is close to CBOX_BLOCK_SIZE or playback rate is very low,
457 if (l
->eff_waveform
&& l
->eff_waveform
->preloaded_frames
== l
->eff_waveform
->info
.frames
)
459 int shift
= l
->eff_waveform
->info
.channels
== 2 ? 1 : 0;
460 uint32_t halfscratch
= MAX_INTERPOLATION_ORDER
<< shift
;
461 memcpy(&l
->scratch_loop
[0], &l
->eff_waveform
->data
[(l
->loop_end
- MAX_INTERPOLATION_ORDER
) << shift
], halfscratch
* sizeof(int16_t) );
462 memcpy(&l
->scratch_end
[0], &l
->eff_waveform
->data
[(l
->loop_end
- MAX_INTERPOLATION_ORDER
) << shift
], halfscratch
* sizeof(int16_t) );
463 memset(l
->scratch_end
+ halfscratch
, 0, halfscratch
* sizeof(int16_t));
464 if (l
->loop_start
!= (uint32_t)-1)
465 memcpy(l
->scratch_loop
+ halfscratch
, &l
->eff_waveform
->data
[l
->loop_start
<< shift
], halfscratch
* sizeof(int16_t));
467 memset(l
->scratch_loop
+ halfscratch
, 0, halfscratch
* sizeof(int16_t));
469 if (sampler_layer_data_is_4pole(l
))
470 l
->resonance_scaled
= sqrtf(l
->resonance_linearized
/ 0.707f
) * 0.5f
;
472 l
->resonance_scaled
= l
->resonance_linearized
;
476 l
->logcutoff
= 1200.0 * log(l
->cutoff
/ 440.0) / log(2) + 5700.0;
478 l
->eq_bitmask
= ((l
->eq1
.gain
!= 0 || l
->eq1
.vel2gain
!= 0) ? 1 : 0)
479 | ((l
->eq2
.gain
!= 0 || l
->eq2
.vel2gain
!= 0) ? 2 : 0)
480 | ((l
->eq3
.gain
!= 0 || l
->eq3
.vel2gain
!= 0) ? 4 : 0);
483 void sampler_layer_reset_switches(struct sampler_layer
*l
, struct sampler_module
*m
)
485 l
->last_key
= l
->data
.sw_lokey
;
486 l
->current_seq_position
= l
->data
.seq_position
;
489 struct layer_foreach_struct
491 struct sampler_layer
*layer
;
492 const char *cfg_section
;
495 static void layer_foreach_func(void *user_data
, const char *key
)
497 if (!strcmp(key
, "file"))
499 // import is handled in sampler_load_layer_overrides
500 if (!strcmp(key
, "import"))
502 // layer%d should be ignored, it's handled by sampler_program_new_from_cfg
503 if (!strncmp(key
, "layer", 5) && isdigit(key
[5]))
505 struct layer_foreach_struct
*lfs
= user_data
;
506 const char *value
= cbox_config_get_string(lfs
->cfg_section
, key
);
507 GError
*error
= NULL
;
508 if (!sampler_layer_apply_param(lfs
->layer
, key
, value
, &error
))
511 g_warning("Error '%s', context: %s in section %s", error
->message
, key
, lfs
->cfg_section
);
513 g_warning("Unknown sample layer parameter: %s in section %s", key
, lfs
->cfg_section
);
517 void sampler_layer_load_overrides(struct sampler_layer
*l
, const char *cfg_section
)
519 char *imp
= cbox_config_get_string(cfg_section
, "import");
521 sampler_layer_load_overrides(l
, imp
);
523 struct layer_foreach_struct lfs
= {
525 .cfg_section
= cfg_section
527 cbox_config_foreach_key(layer_foreach_func
, cfg_section
, &lfs
);
530 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
)
532 struct sampler_layer
*l
= sampler_layer_new(m
, parent_program
, parent_group
? parent_group
: parent_program
->default_group
);
533 sampler_layer_load_overrides(l
, cfg_section
);
534 sampler_layer_data_finalize(&l
->data
, l
->parent_group
? &l
->parent_group
->data
: NULL
, parent_program
);
535 sampler_layer_reset_switches(l
, m
);
539 static int sfz_note_from_string(const char *note
)
541 static const int semis
[] = {9, 11, 0, 2, 4, 5, 7};
543 int nn
= tolower(note
[0]);
545 if (nn
>= '0' && nn
<= '9')
547 if (nn
< 'a' || nn
> 'g')
549 nv
= semis
[nn
- 'a'];
551 for (pos
= 1; tolower(note
[pos
]) == 'b' || note
[pos
] == '#'; pos
++)
552 nv
+= (note
[pos
] != '#') ? -1 : +1;
554 if ((note
[pos
] == '-' && note
[pos
+ 1] == '1' && note
[pos
+ 2] == '\0') || (note
[pos
] >= '0' && note
[pos
] <= '9' && note
[pos
+ 1] == '\0'))
556 return nv
+ 12 * (1 + atoi(note
+ pos
));
562 static double atof_C(const char *value
)
564 return g_ascii_strtod(value
, NULL
);
567 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
)
569 static const enum sampler_modsrc srcs
[] = { smsrc_ampenv
, smsrc_filenv
, smsrc_pitchenv
};
570 static const enum sampler_moddest dests
[] = { smdest_gain
, smdest_cutoff
, smdest_pitch
};
571 enum sampler_modsrc src
= srcs
[env_type
];
572 enum sampler_moddest dest
= dests
[env_type
];
573 float fvalue
= atof_C(value
);
575 #define PROC_SET_ENV_FIELD(name, index, def_value) \
576 if (!strcmp(key, #name)) {\
577 env->name = fvalue; \
578 has_fields->name = 1; \
581 DAHDSR_FIELDS(PROC_SET_ENV_FIELD
)
582 if (!strcmp(key
, "depth"))
583 sampler_layer_set_modulation1(layer
, src
, dest
, atof_C(value
), 0);
584 else if (!strcmp(key
, "vel2delay"))
585 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 0, fvalue
);
586 else if (!strcmp(key
, "vel2attack"))
587 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 1, fvalue
);
588 else if (!strcmp(key
, "vel2hold"))
589 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 2, fvalue
);
590 else if (!strcmp(key
, "vel2decay"))
591 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 3, fvalue
);
592 else if (!strcmp(key
, "vel2sustain"))
593 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 4, fvalue
);
594 else if (!strcmp(key
, "vel2release"))
595 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 5, fvalue
);
596 else if (!strcmp(key
, "vel2depth"))
597 sampler_layer_set_modulation(layer
, src
, smsrc_vel
, dest
, atof_C(value
), 0);
598 else if (!strncmp(key
, "depthcc", 7))
600 int cc
= atoi(key
+ 7);
601 if (cc
> 0 && cc
< 120)
602 sampler_layer_set_modulation(layer
, src
, cc
, dest
, fvalue
, 0);
611 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
)
613 static const enum sampler_modsrc srcs
[] = { smsrc_amplfo
, smsrc_fillfo
, smsrc_pitchlfo
};
614 static const enum sampler_moddest dests
[] = { smdest_gain
, smdest_cutoff
, smdest_pitch
};
615 enum sampler_modsrc src
= srcs
[lfo_type
];
616 enum sampler_moddest dest
= dests
[lfo_type
];
618 #define PROC_SET_LFO_FIELD(name, index, def_value) \
619 if (!strcmp(key, #name)) {\
620 params->name = fvalue; \
621 has_fields->name = 1; \
624 float fvalue
= atof_C(value
);
625 LFO_FIELDS(PROC_SET_LFO_FIELD
)
626 if (!strcmp(key
, "depth"))
627 sampler_layer_set_modulation1(layer
, src
, dest
, fvalue
, 0);
628 else if (!strcmp(key
, "depthchanaft"))
629 sampler_layer_set_modulation(layer
, src
, smsrc_chanaft
, dest
, fvalue
, 0);
630 else if (!strcmp(key
, "depthpolyaft"))
631 sampler_layer_set_modulation(layer
, src
, smsrc_polyaft
, dest
, fvalue
, 0);
632 else if (!strncmp(key
, "depthcc", 7))
634 int cc
= atoi(key
+ 7);
635 if (cc
> 0 && cc
< 120)
636 sampler_layer_set_modulation(layer
, src
, cc
, dest
, fvalue
, 0);
645 static gboolean
parse_eq_param(struct sampler_layer
*layer
, struct sampler_eq_params
*params
, struct sampler_eq_has_fields
*has_fields
, int eq_index
, const char *key
, const char *value
)
647 #define PROC_SET_EQ_FIELD(name, index, def_value) \
648 if (!strcmp(key, #name)) {\
649 params->name = fvalue; \
650 has_fields->name = 1; \
653 float fvalue
= atof_C(value
);
654 EQ_FIELDS(PROC_SET_EQ_FIELD
)
658 #define PARSE_PARAM_midi_note_t(field, strname, valuestr) \
659 return ((l->data.field = sfz_note_from_string(value)), (l->data.has_##field = 1));
660 #define PARSE_PARAM_int(field, strname, valuestr) \
661 return ((l->data.field = atoi(value)), (l->data.has_##field = 1));
662 #define PARSE_PARAM_uint32_t(field, strname, valuestr) \
663 return ((l->data.field = (uint32_t)strtoul(value, NULL, 10)), (l->data.has_##field = 1));
664 #define PARSE_PARAM_float(field, strname, valuestr) \
665 return ((l->data.field = atof_C(value)), (l->data.has_##field = 1));
667 #define PROC_APPLY_PARAM(type, name, def_value) \
668 if (!strcmp(key, #name)) { \
669 PARSE_PARAM_##type(name, #name, value) \
671 #define PROC_APPLY_PARAM_string(name) \
672 if (!strcmp(key, #name)) { \
673 if (l->data.name && value && !strcmp(l->data.name, value)) \
674 return (l->data.has_##name = 1); \
675 g_free(l->data.name); \
676 return ((l->data.name = g_strdup(value)), (l->data.name##_changed = 1), (l->data.has_##name = 1)); \
678 #define PROC_APPLY_PARAM_dBamp(type, name, def_value) \
679 if (!strcmp(key, #name)) { \
680 return ((l->data.name = atof_C(value)), (l->data.has_##name = 1)); \
682 #define PROC_APPLY_PARAM_enum(enumtype, name, def_value) \
683 if (!strcmp(key, #name)) { \
684 if (!enumtype##_from_string(value, &l->data.name)) { \
685 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Value %s is not a correct value for %s", value, #name); \
688 l->data.has_##name = 1; \
692 // LFO and envelope need special handling now
693 #define PROC_APPLY_PARAM_dahdsr(name, parname, index) \
694 if (!strncmp(key, #parname "_", sizeof(#parname))) \
695 return parse_envelope_param(l, &l->data.name, &l->data.has_##name, index, key + sizeof(#parname), value);
696 #define PROC_APPLY_PARAM_lfo(name, parname, index) \
697 if (!strncmp(key, #parname "_", sizeof(#parname))) \
698 return parse_lfo_param(l, &l->data.name, &l->data.has_##name, index, key + sizeof(#parname), value);
699 #define PROC_APPLY_PARAM_eq(name, parname, index) \
700 if (!strncmp(key, #parname "_", sizeof(#parname))) \
701 return parse_eq_param(l, &l->data.name, &l->data.has_##name, index, key + sizeof(#parname), value);
702 #define PROC_APPLY_PARAM_ccrange(name) /* handled separately in apply_param */
704 static void sampler_layer_apply_unknown(struct sampler_layer
*l
, const char *key
, const char *value
)
706 if (!l
->unknown_keys
)
707 l
->unknown_keys
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
709 g_hash_table_insert(l
->unknown_keys
, g_strdup(key
), g_strdup(value
));
712 gboolean
sampler_layer_apply_param(struct sampler_layer
*l
, const char *key
, const char *value
, GError
**error
)
715 // XXXKF: this is seriously stupid code, this should use a hash table for
716 // fixed strings, or something else that doesn't explode O(N**2) with
717 // number of attributes. But premature optimization is a root of all evil.
719 SAMPLER_FIXED_FIELDS(PROC_APPLY_PARAM
)
721 // XXXKF: to make things worse, some attributes have names different from
722 // C field names, or have multiple names, or don't map 1:1 to internal model
724 if (!strcmp(key
, "lolev"))
725 l
->data
.lovel
= atoi(value
), l
->data
.has_lovel
= 1;
726 else if (!strcmp(key
, "hilev"))
727 l
->data
.hivel
= atoi(value
), l
->data
.has_hivel
= 1;
728 else if (!strcmp(key
, "benddown"))
729 l
->data
.bend_down
= atoi(value
), l
->data
.has_bend_down
= 1;
730 else if (!strcmp(key
, "bendup"))
731 l
->data
.bend_up
= atoi(value
), l
->data
.has_bend_up
= 1;
732 else if (!strcmp(key
, "loopstart"))
733 l
->data
.loop_start
= atoi(value
), l
->data
.has_loop_start
= 1;
734 else if (!strcmp(key
, "loopend"))
735 l
->data
.loop_end
= atoi(value
), l
->data
.has_loop_end
= 1;
736 else if (!strcmp(key
, "cutoff_chanaft"))
737 sampler_layer_set_modulation1(l
, smsrc_chanaft
, smdest_cutoff
, atof_C(value
), 0);
738 else if (!strcmp(key
, "amp_random"))
739 sampler_layer_add_nif(l
, sampler_nif_addrandom
, 0, atof_C(value
));
740 else if (!strcmp(key
, "fil_random"))
741 sampler_layer_add_nif(l
, sampler_nif_addrandom
, 1, atof_C(value
));
742 else if (!strcmp(key
, "pitch_random"))
743 sampler_layer_add_nif(l
, sampler_nif_addrandom
, 2, atof_C(value
));
744 else if (!strcmp(key
, "pitch_veltrack"))
745 sampler_layer_add_nif(l
, sampler_nif_vel2pitch
, 0, atof_C(value
));
746 else if (!strcmp(key
, "reloffset_veltrack"))
747 sampler_layer_add_nif(l
, sampler_nif_vel2reloffset
, 0, atof_C(value
));
748 else if (!strncmp(key
, "delay_cc", 8))
750 int ccno
= atoi(key
+ 8);
751 if (ccno
> 0 && ccno
< 120)
752 sampler_layer_add_nif(l
, sampler_nif_cc2delay
, ccno
, atof_C(value
));
756 else if (!strncmp(key
, "reloffset_cc", 12))
758 int ccno
= atoi(key
+ 12);
759 if (ccno
> 0 && ccno
< 120)
760 sampler_layer_add_nif(l
, sampler_nif_cc2reloffset
, ccno
, atof_C(value
));
764 else if (!strncmp(key
, "cutoff_cc", 9))
766 int ccno
= atoi(key
+ 9);
767 if (ccno
> 0 && ccno
< 120)
768 sampler_layer_set_modulation1(l
, ccno
, smdest_cutoff
, atof_C(value
), 0);
772 else if (!strncmp(key
, "pitch_cc", 8))
774 int ccno
= atoi(key
+ 8);
775 if (ccno
> 0 && ccno
< 120)
776 sampler_layer_set_modulation1(l
, ccno
, smdest_pitch
, atof_C(value
), 0);
780 else if (!strncmp(key
, "tonectl_cc", 10))
782 int ccno
= atoi(key
+ 10);
783 if (ccno
> 0 && ccno
< 120)
784 sampler_layer_set_modulation1(l
, ccno
, smdest_tonectl
, atof_C(value
), 0);
788 else if (!strncmp(key
, "gain_cc", 7))
790 int ccno
= atoi(key
+ 7);
791 if (ccno
> 0 && ccno
< 120)
792 sampler_layer_set_modulation1(l
, ccno
, smdest_gain
, atof_C(value
), 0);
796 else if (!strncmp(key
, "amp_velcurve_", 13))
798 // if not known yet, set to 0, it can always be overriden via velcurve_quadratic setting
799 if (l
->data
.velcurve_quadratic
== -1)
800 l
->data
.velcurve_quadratic
= 0;
801 int point
= atoi(key
+ 13);
802 if (point
>= 0 && point
<= 127)
804 l
->data
.velcurve
[point
] = atof_C(value
);
805 if (l
->data
.velcurve
[point
] < 0)
806 l
->data
.velcurve
[point
] = 0;
807 if (l
->data
.velcurve
[point
] > 1)
808 l
->data
.velcurve
[point
] = 1;
813 else if (!strncmp(key
, "on_locc", 7) || !strncmp(key
, "on_hicc", 7))
815 int cc
= atoi(key
+ 7);
816 if (cc
> 0 && cc
< 120)
820 l
->data
.on_cc_number
= cc
;
823 l
->data
.on_locc
= atoi(value
);
824 l
->data
.has_on_locc
= TRUE
;
828 l
->data
.on_hicc
= atoi(value
);
829 l
->data
.has_on_hicc
= TRUE
;
834 l
->data
.on_cc_number
= -1;
835 l
->data
.has_on_locc
= FALSE
;
836 l
->data
.has_on_hicc
= FALSE
;
842 else if (!strncmp(key
, "locc", 4) || !strncmp(key
, "hicc", 4))
844 int cc
= atoi(key
+ 4);
845 if (cc
> 0 && cc
< 120)
849 l
->data
.cc_number
= cc
;
852 l
->data
.locc
= atoi(value
);
853 l
->data
.has_locc
= TRUE
;
857 l
->data
.hicc
= atoi(value
);
858 l
->data
.has_hicc
= TRUE
;
863 l
->data
.cc_number
= -1;
864 l
->data
.has_locc
= FALSE
;
865 l
->data
.has_hicc
= FALSE
;
871 else if (!strcmp(key
, "loopmode"))
874 goto try_now
; // yes, goto, why not?
876 else if (!strcmp(key
, "offby"))
881 else if (!strncmp(key
, "genericmod_", 11))
883 char **tokens
= g_strsplit(key
, "_", 5);
884 sampler_layer_set_modulation(l
, atoi(tokens
[1]), atoi(tokens
[2]), atoi(tokens
[3]), atof(value
), atoi(tokens
[4]));
892 sampler_layer_apply_unknown(l
, key
, value
);
893 g_warning("Unknown SFZ property key: '%s'", key
);
897 #define TYPE_PRINTF_uint32_t(name, def_value) \
898 if (show_inherited || l->has_##name) \
899 g_string_append_printf(outstr, " %s=%u", #name, (unsigned)(l->name));
900 #define TYPE_PRINTF_int(name, def_value) \
901 if (show_inherited || l->has_##name) \
902 g_string_append_printf(outstr, " %s=%d", #name, (int)(l->name));
903 #define TYPE_PRINTF_midi_note_t(name, def_value) \
904 if (show_inherited || l->has_##name) { \
907 g_string_append_printf(outstr, " %s=-1", #name); \
909 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)); \
911 #define TYPE_PRINTF_float(name, def_value) \
912 if (show_inherited || l->has_##name) \
913 g_string_append_printf(outstr, " %s=%s", #name, g_ascii_dtostr(floatbuf, floatbufsize, l->name));
915 #define PROC_FIELDS_TO_FILEPTR(type, name, def_value) \
916 TYPE_PRINTF_##type(name, def_value)
917 #define PROC_FIELDS_TO_FILEPTR_string(name) \
918 if (show_inherited || l->has_##name) \
919 g_string_append_printf(outstr, " %s=%s", #name, l->name ? l->name : "");
920 #define PROC_FIELDS_TO_FILEPTR_dBamp(type, name, def_value) \
921 if (show_inherited || l->has_##name) \
922 g_string_append_printf(outstr, " %s=%s", #name, g_ascii_dtostr(floatbuf, floatbufsize, l->name));
923 #define PROC_FIELDS_TO_FILEPTR_enum(enumtype, name, def_value) \
924 if ((show_inherited || l->has_##name) && (tmpstr = enumtype##_to_string(l->name)) != NULL) \
925 g_string_append_printf(outstr, " %s=%s", #name, tmpstr);
927 #define ENV_PARAM_OUTPUT(param, index, def_value, env, envfield, envname) \
928 if (show_inherited || l->has_##envfield.param) \
929 g_string_append_printf(outstr, " " #envname "_" #param "=%s", g_ascii_dtostr(floatbuf, floatbufsize, env.param));
931 #define PROC_FIELDS_TO_FILEPTR_dahdsr(name, parname, index) \
932 DAHDSR_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
933 #define PROC_FIELDS_TO_FILEPTR_lfo(name, parname, index) \
934 LFO_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
935 #define PROC_FIELDS_TO_FILEPTR_eq(name, parname, index) \
936 EQ_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
937 #define PROC_FIELDS_TO_FILEPTR_ccrange(name) \
938 if (l->has_##name##locc) \
939 g_string_append_printf(outstr, " " #name "locc%d=%d", l->name##cc_number, l->name##locc); \
940 if (l->has_##name##hicc) \
941 g_string_append_printf(outstr, " " #name "hicc%d=%d", l->name##cc_number, l->name##hicc);
943 gchar
*sampler_layer_to_string(struct sampler_layer
*lr
, gboolean show_inherited
)
945 struct sampler_layer_data
*l
= &lr
->data
;
946 GString
*outstr
= g_string_sized_new(200);
948 char floatbuf
[G_ASCII_DTOSTR_BUF_SIZE
];
949 int floatbufsize
= G_ASCII_DTOSTR_BUF_SIZE
;
950 SAMPLER_FIXED_FIELDS(PROC_FIELDS_TO_FILEPTR
)
952 static const char *addrandom_variants
[] = { "amp", "fil", "pitch" };
953 static const char *modsrc_names
[] = { "chanaft", "vel", "polyaft", "pitch", "pitcheg", "fileg", "ampeg", "pitchlfo", "fillfo", "amplfo", "" };
954 static const char *moddest_names
[] = { "gain", "pitch", "cutoff", "resonance", "tonectl" };
955 for(GSList
*nif
= l
->nifs
; nif
; nif
= nif
->next
)
957 struct sampler_noteinitfunc
*nd
= nif
->data
;
958 if (!nd
->has_value
&& !show_inherited
)
960 #define PROC_ENVSTAGE_NAME(name, index, def_value) #name,
961 static const char *env_stages
[] = { DAHDSR_FIELDS(PROC_ENVSTAGE_NAME
) };
963 g_ascii_dtostr(floatbuf
, floatbufsize
, nd
->param
);
965 if (nd
->notefunc
== sampler_nif_addrandom
&& v
>= 0 && v
<= 2)
966 g_string_append_printf(outstr
, " %s_random=%s", addrandom_variants
[nd
->variant
], floatbuf
);
967 else if (nd
->notefunc
== sampler_nif_vel2pitch
)
968 g_string_append_printf(outstr
, " pitch_veltrack=%s", floatbuf
);
969 else if (nd
->notefunc
== sampler_nif_vel2reloffset
)
970 g_string_append_printf(outstr
, " reloffset_veltrack=%s", floatbuf
);
971 else if (nd
->notefunc
== sampler_nif_cc2delay
&& v
>= 0 && v
< 120)
972 g_string_append_printf(outstr
, " delay_cc%d=%s", nd
->variant
, floatbuf
);
973 else if (nd
->notefunc
== sampler_nif_cc2reloffset
&& v
>= 0 && v
< 120)
974 g_string_append_printf(outstr
, " reloffset_cc%d=%s", nd
->variant
, floatbuf
);
975 else if (nd
->notefunc
== sampler_nif_vel2env
&& v
>= 0 && (v
& 15) < 6 && (v
>> 4) < 3)
976 g_string_append_printf(outstr
, " %seg_vel2%s=%s", addrandom_variants
[nd
->variant
>> 4], env_stages
[1 + (v
& 15)], floatbuf
);
978 for(GSList
*mod
= l
->modulations
; mod
; mod
= mod
->next
)
980 struct sampler_modulation
*md
= mod
->data
;
981 if (!md
->has_value
&& !show_inherited
)
983 g_ascii_dtostr(floatbuf
, floatbufsize
, md
->amount
);
985 if (md
->src2
== smsrc_none
)
989 g_string_append_printf(outstr
, " %s_cc%d=%s", moddest_names
[md
->dest
], md
->src
, floatbuf
);
992 if (md
->src
< 120 + sizeof(modsrc_names
) / sizeof(modsrc_names
[0]))
994 if ((md
->src
== smsrc_filenv
&& md
->dest
== smdest_cutoff
) ||
995 (md
->src
== smsrc_pitchenv
&& md
->dest
== smdest_pitch
) ||
996 (md
->src
== smsrc_fillfo
&& md
->dest
== smdest_cutoff
) ||
997 (md
->src
== smsrc_pitchlfo
&& md
->dest
== smdest_pitch
))
998 g_string_append_printf(outstr
, " %s_depth=%s", modsrc_names
[md
->src
- 120], floatbuf
);
1000 g_string_append_printf(outstr
, " %s_%s=%s", moddest_names
[md
->dest
], modsrc_names
[md
->src
- 120], floatbuf
);
1004 if ((md
->src
== smsrc_amplfo
&& md
->dest
== smdest_gain
) ||
1005 (md
->src
== smsrc_fillfo
&& md
->dest
== smdest_cutoff
) ||
1006 (md
->src
== smsrc_pitchlfo
&& md
->dest
== smdest_pitch
))
1012 g_string_append_printf(outstr
, " %slfo_depth%s=%s", moddest_names
[md
->dest
], modsrc_names
[md
->src2
- 120], floatbuf
);
1017 g_string_append_printf(outstr
, " %slfo_depthcc%d=%s", moddest_names
[md
->dest
], md
->src2
, floatbuf
);
1024 if ((md
->src
== smsrc_ampenv
&& md
->dest
== smdest_gain
) ||
1025 (md
->src
== smsrc_filenv
&& md
->dest
== smdest_cutoff
) ||
1026 (md
->src
== smsrc_pitchenv
&& md
->dest
== smdest_pitch
))
1028 if (md
->src2
== smsrc_vel
)
1030 g_string_append_printf(outstr
, " %seg_vel2depth=%s", moddest_names
[md
->dest
], floatbuf
);
1035 g_string_append_printf(outstr
, " %s_depthcc%d=%s", modsrc_names
[md
->src
- 120], md
->src2
, floatbuf
);
1039 g_string_append_printf(outstr
, " genericmod_%d_%d_%d_%d=%s", md
->src
, md
->src2
, md
->dest
, md
->flags
, floatbuf
);
1042 if (lr
->unknown_keys
)
1046 g_hash_table_iter_init(&hti
, lr
->unknown_keys
);
1047 while(g_hash_table_iter_next(&hti
, (gpointer
*)&key
, (gpointer
*)&value
))
1048 g_string_append_printf(outstr
, " %s=%s", key
, value
);
1051 gchar
*res
= outstr
->str
;
1052 g_string_free(outstr
, FALSE
);
1056 void sampler_layer_dump(struct sampler_layer
*l
, FILE *f
)
1058 gchar
*str
= sampler_layer_to_string(l
, FALSE
);
1059 fprintf(f
, "%s\n", str
);
1062 void sampler_layer_data_close(struct sampler_layer_data
*l
)
1064 g_slist_free_full(l
->nifs
, g_free
);
1065 g_slist_free_full(l
->modulations
, g_free
);
1066 if (l
->eff_waveform
)
1068 cbox_waveform_unref(l
->eff_waveform
);
1069 l
->eff_waveform
= NULL
;
1074 void sampler_layer_data_destroy(struct sampler_layer_data
*l
)
1076 sampler_layer_data_close(l
);
1080 struct sampler_layer
*sampler_layer_new_clone(struct sampler_layer
*layer
,
1081 struct sampler_module
*m
, struct sampler_program
*parent_program
, struct sampler_layer
*parent_group
)
1083 struct sampler_layer
*l
= sampler_layer_new(m
, parent_program
, parent_group
);
1084 sampler_layer_data_clone(&l
->data
, &layer
->data
, TRUE
);
1085 if (layer
->unknown_keys
)
1087 GHashTableIter iter
;
1088 g_hash_table_iter_init(&iter
, layer
->unknown_keys
);
1089 gpointer key
, value
;
1090 while(g_hash_table_iter_next(&iter
, &key
, &value
))
1091 sampler_layer_apply_param(l
, (gchar
*)key
, (gchar
*)value
, NULL
);
1094 GHashTableIter iter
;
1095 g_hash_table_iter_init(&iter
, layer
->child_layers
);
1096 gpointer key
, value
;
1097 while(g_hash_table_iter_next(&iter
, &key
, &value
))
1099 sampler_layer_new_clone(key
, m
, parent_program
, l
);
1100 // g_hash_table_insert(l->child_layers, chl, NULL);
1106 void sampler_layer_destroyfunc(struct cbox_objhdr
*objhdr
)
1108 struct sampler_layer
*l
= CBOX_H2O(objhdr
);
1109 struct sampler_program
*prg
= l
->parent_program
;
1110 assert(g_hash_table_size(l
->child_layers
) == 0);
1112 if (l
->parent_group
)
1114 g_hash_table_remove(l
->parent_group
->child_layers
, l
);
1115 if (prg
&& prg
->rll
)
1117 sampler_program_delete_layer(prg
, l
);
1118 sampler_program_update_layers(l
->parent_program
);
1120 l
->parent_group
= NULL
;
1122 sampler_layer_data_close(&l
->data
);
1124 sampler_layer_data_destroy(l
->runtime
);
1125 if (l
->unknown_keys
)
1126 g_hash_table_destroy(l
->unknown_keys
);
1127 if (l
->child_layers
)
1128 g_hash_table_destroy(l
->child_layers
);
1133 //////////////////////////////////////////////////////////////////////////
1135 struct sampler_layer_update_cmd
1137 struct sampler_module
*module
;
1138 struct sampler_layer
*layer
;
1139 struct sampler_layer_data
*new_data
;
1140 struct sampler_layer_data
*old_data
;
1143 static int sampler_layer_update_cmd_prepare(void *data
)
1145 struct sampler_layer_update_cmd
*cmd
= data
;
1146 cmd
->old_data
= cmd
->layer
->runtime
;
1147 cmd
->new_data
= calloc(1, sizeof(struct sampler_layer_data
));
1149 sampler_layer_data_clone(cmd
->new_data
, &cmd
->layer
->data
, TRUE
);
1150 sampler_layer_data_finalize(cmd
->new_data
, cmd
->layer
->parent_group
? &cmd
->layer
->parent_group
->data
: NULL
, cmd
->layer
->parent_program
);
1151 if (cmd
->layer
->runtime
== NULL
)
1153 // initial update of the layer, so none of the voices need updating yet
1154 // because the layer hasn't been allocated to any voice
1155 cmd
->layer
->runtime
= cmd
->new_data
;
1161 static int sampler_layer_update_cmd_execute(void *data
)
1163 struct sampler_layer_update_cmd
*cmd
= data
;
1165 for (int i
= 0; i
< 16; i
++)
1167 FOREACH_VOICE(cmd
->module
->channels
[i
].voices_running
, v
)
1169 if (v
->layer
== cmd
->layer
->runtime
)
1171 v
->layer
= cmd
->new_data
;
1172 v
->layer_changed
= TRUE
;
1173 sampler_voice_update_params_from_layer(v
);
1177 cmd
->old_data
= cmd
->layer
->runtime
;
1178 cmd
->layer
->runtime
= cmd
->new_data
;
1182 static void sampler_layer_update_cmd_cleanup(void *data
)
1184 struct sampler_layer_update_cmd
*cmd
= data
;
1186 sampler_layer_data_destroy(cmd
->old_data
);
1190 void sampler_layer_update(struct sampler_layer
*l
)
1192 // if changing a group, update all child regions instead
1193 if (g_hash_table_size(l
->child_layers
))
1195 GHashTableIter iter
;
1196 g_hash_table_iter_init(&iter
, l
->child_layers
);
1197 gpointer key
, value
;
1198 while(g_hash_table_iter_next(&iter
, &key
, &value
))
1200 sampler_layer_data_finalize(&((struct sampler_layer
*)key
)->data
, &l
->data
, l
->parent_program
);
1201 sampler_layer_update((struct sampler_layer
*)key
);
1205 static struct cbox_rt_cmd_definition rtcmd
= {
1206 .prepare
= sampler_layer_update_cmd_prepare
,
1207 .execute
= sampler_layer_update_cmd_execute
,
1208 .cleanup
= sampler_layer_update_cmd_cleanup
,
1211 struct sampler_layer_update_cmd
*lcmd
= malloc(sizeof(struct sampler_layer_update_cmd
));
1212 lcmd
->module
= l
->module
;
1214 lcmd
->new_data
= NULL
;
1215 lcmd
->old_data
= NULL
;
1217 cbox_rt_execute_cmd_async(l
->module
->module
.rt
, &rtcmd
, lcmd
);