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 // XXXKF should have a distinction between 'configured' and 'effective' loop start/end
402 if (l
->loop_mode
== slm_unknown
)
404 if (l
->eff_waveform
&& l
->eff_waveform
->has_loop
)
405 l
->loop_mode
= slm_loop_continuous
;
407 l
->loop_mode
= l
->loop_end
== 0 ? slm_no_loop
: slm_loop_continuous
;
410 if (l
->loop_mode
== slm_one_shot
|| l
->loop_mode
== slm_no_loop
|| l
->loop_mode
== slm_one_shot_chokeable
)
413 if ((l
->loop_mode
== slm_loop_continuous
|| l
->loop_mode
== slm_loop_sustain
) && l
->loop_start
== -1)
415 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
)
416 l
->loop_start
= l
->eff_waveform
->loop_start
;
417 if (l
->loop_end
== 0 && l
->eff_waveform
!= NULL
&& l
->eff_waveform
->has_loop
)
418 l
->loop_end
= l
->eff_waveform
->loop_end
;
420 if (l
->off_mode
== som_unknown
)
421 l
->off_mode
= l
->off_by
!= 0 ? som_fast
: som_normal
;
423 // if no amp_velcurve_nnn setting, default to quadratic
424 if (l
->velcurve_quadratic
== -1)
425 l
->velcurve_quadratic
= 1;
427 if (l
->key
>= 0 && l
->key
<= 127)
428 l
->lokey
= l
->hikey
= l
->pitch_keycenter
= l
->key
;
430 // interpolate missing points in velcurve
432 for (int i
= 1; i
< 128; i
++)
434 if (l
->velcurve
[i
] == -1)
436 float sv
= l
->velcurve
[start
];
437 float ev
= l
->velcurve
[i
];
438 if (l
->velcurve_quadratic
)
440 for (int j
= start
; j
<= i
; j
++)
441 l
->eff_velcurve
[j
] = sv
+ (ev
- sv
) * (j
- start
) * (j
- start
) / ((i
- start
) * (i
- start
));
445 for (int j
= start
; j
<= i
; j
++)
446 l
->eff_velcurve
[j
] = sv
+ (ev
- sv
) * (j
- start
) / (i
- start
);
450 l
->eff_use_keyswitch
= ((l
->sw_down
!= -1) || (l
->sw_up
!= -1) || (l
->sw_last
!= -1) || (l
->sw_previous
!= -1));
452 // 'linearize' the virtual circular buffer - write 3 (or N) frames before end of the loop
453 // and 3 (N) frames at the start of the loop, and play it; in rare cases this will need to be
454 // repeated twice if output write pointer is close to CBOX_BLOCK_SIZE or playback rate is very low,
456 if (l
->eff_waveform
&& l
->eff_waveform
->preloaded_frames
== l
->eff_waveform
->info
.frames
)
458 int shift
= l
->eff_waveform
->info
.channels
== 2 ? 1 : 0;
459 uint32_t halfscratch
= MAX_INTERPOLATION_ORDER
<< shift
;
460 memcpy(&l
->scratch_loop
[0], &l
->eff_waveform
->data
[(l
->loop_end
- MAX_INTERPOLATION_ORDER
) << shift
], halfscratch
* sizeof(int16_t) );
461 memcpy(&l
->scratch_end
[0], &l
->eff_waveform
->data
[(l
->loop_end
- MAX_INTERPOLATION_ORDER
) << shift
], halfscratch
* sizeof(int16_t) );
462 memset(l
->scratch_end
+ halfscratch
, 0, halfscratch
* sizeof(int16_t));
463 if (l
->loop_start
!= (uint32_t)-1)
464 memcpy(l
->scratch_loop
+ halfscratch
, &l
->eff_waveform
->data
[l
->loop_start
<< shift
], halfscratch
* sizeof(int16_t));
466 memset(l
->scratch_loop
+ halfscratch
, 0, halfscratch
* sizeof(int16_t));
468 if (sampler_layer_data_is_4pole(l
))
469 l
->resonance_scaled
= sqrtf(l
->resonance_linearized
/ 0.707f
) * 0.5f
;
471 l
->resonance_scaled
= l
->resonance_linearized
;
475 l
->logcutoff
= 1200.0 * log(l
->cutoff
/ 440.0) / log(2) + 5700.0;
477 l
->eq_bitmask
= ((l
->eq1
.gain
!= 0 || l
->eq1
.vel2gain
!= 0) ? 1 : 0)
478 | ((l
->eq2
.gain
!= 0 || l
->eq2
.vel2gain
!= 0) ? 2 : 0)
479 | ((l
->eq3
.gain
!= 0 || l
->eq3
.vel2gain
!= 0) ? 4 : 0);
482 void sampler_layer_reset_switches(struct sampler_layer
*l
, struct sampler_module
*m
)
484 l
->last_key
= l
->data
.sw_lokey
;
485 l
->current_seq_position
= l
->data
.seq_position
;
488 struct layer_foreach_struct
490 struct sampler_layer
*layer
;
491 const char *cfg_section
;
494 static void layer_foreach_func(void *user_data
, const char *key
)
496 if (!strcmp(key
, "file"))
498 // import is handled in sampler_load_layer_overrides
499 if (!strcmp(key
, "import"))
501 struct layer_foreach_struct
*lfs
= user_data
;
502 const char *value
= cbox_config_get_string(lfs
->cfg_section
, key
);
503 GError
*error
= NULL
;
504 if (!sampler_layer_apply_param(lfs
->layer
, key
, value
, &error
))
507 g_warning("Error '%s', context: %s in section %s", error
->message
, key
, lfs
->cfg_section
);
509 g_warning("Unknown sample layer parameter: %s in section %s", key
, lfs
->cfg_section
);
513 void sampler_layer_load_overrides(struct sampler_layer
*l
, const char *cfg_section
)
515 char *imp
= cbox_config_get_string(cfg_section
, "import");
517 sampler_layer_load_overrides(l
, imp
);
519 struct layer_foreach_struct lfs
= {
521 .cfg_section
= cfg_section
523 cbox_config_foreach_key(layer_foreach_func
, cfg_section
, &lfs
);
526 struct sampler_layer
*sampler_layer_new_from_section(struct sampler_module
*m
, struct sampler_program
*parent_program
, const char *cfg_section
)
528 struct sampler_layer
*l
= sampler_layer_new(m
, parent_program
, NULL
);
529 sampler_layer_load_overrides(l
, cfg_section
);
530 sampler_layer_data_finalize(&l
->data
, l
->parent_group
? &l
->parent_group
->data
: NULL
, parent_program
);
531 sampler_layer_reset_switches(l
, m
);
535 static int sfz_note_from_string(const char *note
)
537 static const int semis
[] = {9, 11, 0, 2, 4, 5, 7};
539 int nn
= tolower(note
[0]);
541 if (nn
>= '0' && nn
<= '9')
543 if (nn
< 'a' && nn
> 'g')
545 nv
= semis
[nn
- 'a'];
547 for (pos
= 1; tolower(note
[pos
]) == 'b' || note
[pos
] == '#'; pos
++)
548 nv
+= (note
[pos
] != '#') ? -1 : +1;
550 if ((note
[pos
] == '-' && note
[pos
+ 1] == '1' && note
[pos
+ 2] == '\0') || (note
[pos
] >= '0' && note
[pos
] <= '9' && note
[pos
+ 1] == '\0'))
552 return nv
+ 12 * (1 + atoi(note
+ pos
));
558 static double atof_C(const char *value
)
560 return g_ascii_strtod(value
, NULL
);
563 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
)
565 static const enum sampler_modsrc srcs
[] = { smsrc_ampenv
, smsrc_filenv
, smsrc_pitchenv
};
566 static const enum sampler_moddest dests
[] = { smdest_gain
, smdest_cutoff
, smdest_pitch
};
567 enum sampler_modsrc src
= srcs
[env_type
];
568 enum sampler_moddest dest
= dests
[env_type
];
569 float fvalue
= atof_C(value
);
571 #define PROC_SET_ENV_FIELD(name, index, def_value) \
572 if (!strcmp(key, #name)) {\
573 env->name = fvalue; \
574 has_fields->name = 1; \
577 DAHDSR_FIELDS(PROC_SET_ENV_FIELD
)
578 if (!strcmp(key
, "depth"))
579 sampler_layer_set_modulation1(layer
, src
, dest
, atof_C(value
), 0);
580 else if (!strcmp(key
, "vel2delay"))
581 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 0, fvalue
);
582 else if (!strcmp(key
, "vel2attack"))
583 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 1, fvalue
);
584 else if (!strcmp(key
, "vel2hold"))
585 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 2, fvalue
);
586 else if (!strcmp(key
, "vel2decay"))
587 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 3, fvalue
);
588 else if (!strcmp(key
, "vel2sustain"))
589 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 4, fvalue
);
590 else if (!strcmp(key
, "vel2release"))
591 sampler_layer_add_nif(layer
, sampler_nif_vel2env
, (env_type
<< 4) + 5, fvalue
);
592 else if (!strcmp(key
, "vel2depth"))
593 sampler_layer_set_modulation(layer
, src
, smsrc_vel
, dest
, atof_C(value
), 0);
594 else if (!strncmp(key
, "depthcc", 7))
596 int cc
= atoi(key
+ 7);
597 if (cc
> 0 && cc
< 120)
598 sampler_layer_set_modulation(layer
, src
, cc
, dest
, fvalue
, 0);
607 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
)
609 static const enum sampler_modsrc srcs
[] = { smsrc_amplfo
, smsrc_fillfo
, smsrc_pitchlfo
};
610 static const enum sampler_moddest dests
[] = { smdest_gain
, smdest_cutoff
, smdest_pitch
};
611 enum sampler_modsrc src
= srcs
[lfo_type
];
612 enum sampler_moddest dest
= dests
[lfo_type
];
614 #define PROC_SET_LFO_FIELD(name, index, def_value) \
615 if (!strcmp(key, #name)) {\
616 params->name = fvalue; \
617 has_fields->name = 1; \
620 float fvalue
= atof_C(value
);
621 LFO_FIELDS(PROC_SET_LFO_FIELD
)
622 if (!strcmp(key
, "depth"))
623 sampler_layer_set_modulation1(layer
, src
, dest
, fvalue
, 0);
624 else if (!strcmp(key
, "depthchanaft"))
625 sampler_layer_set_modulation(layer
, src
, smsrc_chanaft
, dest
, fvalue
, 0);
626 else if (!strcmp(key
, "depthpolyaft"))
627 sampler_layer_set_modulation(layer
, src
, smsrc_polyaft
, dest
, fvalue
, 0);
628 else if (!strncmp(key
, "depthcc", 7))
630 int cc
= atoi(key
+ 7);
631 if (cc
> 0 && cc
< 120)
632 sampler_layer_set_modulation(layer
, src
, cc
, dest
, fvalue
, 0);
641 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
)
643 #define PROC_SET_EQ_FIELD(name, index, def_value) \
644 if (!strcmp(key, #name)) {\
645 params->name = fvalue; \
646 has_fields->name = 1; \
649 float fvalue
= atof_C(value
);
650 EQ_FIELDS(PROC_SET_EQ_FIELD
)
654 #define PARSE_PARAM_midi_note_t(field, strname, valuestr) \
655 return ((l->data.field = sfz_note_from_string(value)), (l->data.has_##field = 1));
656 #define PARSE_PARAM_int(field, strname, valuestr) \
657 return ((l->data.field = atoi(value)), (l->data.has_##field = 1));
658 #define PARSE_PARAM_uint32_t(field, strname, valuestr) \
659 return ((l->data.field = (uint32_t)strtoul(value, NULL, 10)), (l->data.has_##field = 1));
660 #define PARSE_PARAM_float(field, strname, valuestr) \
661 return ((l->data.field = atof_C(value)), (l->data.has_##field = 1));
663 #define PROC_APPLY_PARAM(type, name, def_value) \
664 if (!strcmp(key, #name)) { \
665 PARSE_PARAM_##type(name, #name, value) \
667 #define PROC_APPLY_PARAM_string(name) \
668 if (!strcmp(key, #name)) { \
669 if (l->data.name && value && !strcmp(l->data.name, value)) \
670 return (l->data.has_##name = 1); \
671 g_free(l->data.name); \
672 return ((l->data.name = g_strdup(value)), (l->data.name##_changed = 1), (l->data.has_##name = 1)); \
674 #define PROC_APPLY_PARAM_dBamp(type, name, def_value) \
675 if (!strcmp(key, #name)) { \
676 return ((l->data.name = atof_C(value)), (l->data.has_##name = 1)); \
678 #define PROC_APPLY_PARAM_enum(enumtype, name, def_value) \
679 if (!strcmp(key, #name)) { \
680 if (!enumtype##_from_string(value, &l->data.name)) { \
681 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Value %s is not a correct value for %s", value, #name); \
684 l->data.has_##name = 1; \
688 // LFO and envelope need special handling now
689 #define PROC_APPLY_PARAM_dahdsr(name, parname, index) \
690 if (!strncmp(key, #parname "_", sizeof(#parname))) \
691 return parse_envelope_param(l, &l->data.name, &l->data.has_##name, index, key + sizeof(#parname), value);
692 #define PROC_APPLY_PARAM_lfo(name, parname, index) \
693 if (!strncmp(key, #parname "_", sizeof(#parname))) \
694 return parse_lfo_param(l, &l->data.name, &l->data.has_##name, index, key + sizeof(#parname), value);
695 #define PROC_APPLY_PARAM_eq(name, parname, index) \
696 if (!strncmp(key, #parname "_", sizeof(#parname))) \
697 return parse_eq_param(l, &l->data.name, &l->data.has_##name, index, key + sizeof(#parname), value);
698 #define PROC_APPLY_PARAM_ccrange(name) /* handled separately in apply_param */
700 static void sampler_layer_apply_unknown(struct sampler_layer
*l
, const char *key
, const char *value
)
702 if (!l
->unknown_keys
)
703 l
->unknown_keys
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
705 g_hash_table_insert(l
->unknown_keys
, g_strdup(key
), g_strdup(value
));
708 gboolean
sampler_layer_apply_param(struct sampler_layer
*l
, const char *key
, const char *value
, GError
**error
)
711 // XXXKF: this is seriously stupid code, this should use a hash table for
712 // fixed strings, or something else that doesn't explode O(N**2) with
713 // number of attributes. But premature optimization is a root of all evil.
715 SAMPLER_FIXED_FIELDS(PROC_APPLY_PARAM
)
717 // XXXKF: to make things worse, some attributes have names different from
718 // C field names, or have multiple names, or don't map 1:1 to internal model
720 if (!strcmp(key
, "lolev"))
721 l
->data
.lovel
= atoi(value
), l
->data
.has_lovel
= 1;
722 else if (!strcmp(key
, "hilev"))
723 l
->data
.hivel
= atoi(value
), l
->data
.has_hivel
= 1;
724 else if (!strcmp(key
, "benddown"))
725 l
->data
.bend_down
= atoi(value
), l
->data
.has_bend_down
= 1;
726 else if (!strcmp(key
, "bendup"))
727 l
->data
.bend_up
= atoi(value
), l
->data
.has_bend_up
= 1;
728 else if (!strcmp(key
, "loopstart"))
729 l
->data
.loop_start
= atoi(value
), l
->data
.has_loop_start
= 1;
730 else if (!strcmp(key
, "loopend"))
731 l
->data
.loop_end
= atoi(value
), l
->data
.has_loop_end
= 1;
732 else if (!strcmp(key
, "cutoff_chanaft"))
733 sampler_layer_set_modulation1(l
, smsrc_chanaft
, smdest_cutoff
, atof_C(value
), 0);
734 else if (!strcmp(key
, "amp_random"))
735 sampler_layer_add_nif(l
, sampler_nif_addrandom
, 0, atof_C(value
));
736 else if (!strcmp(key
, "fil_random"))
737 sampler_layer_add_nif(l
, sampler_nif_addrandom
, 1, atof_C(value
));
738 else if (!strcmp(key
, "pitch_random"))
739 sampler_layer_add_nif(l
, sampler_nif_addrandom
, 2, atof_C(value
));
740 else if (!strcmp(key
, "pitch_veltrack"))
741 sampler_layer_add_nif(l
, sampler_nif_vel2pitch
, 0, atof_C(value
));
742 else if (!strncmp(key
, "delay_cc", 8))
744 int ccno
= atoi(key
+ 8);
745 if (ccno
> 0 && ccno
< 120)
746 sampler_layer_add_nif(l
, sampler_nif_cc2delay
, ccno
, atof_C(value
));
750 else if (!strncmp(key
, "cutoff_cc", 9))
752 int ccno
= atoi(key
+ 9);
753 if (ccno
> 0 && ccno
< 120)
754 sampler_layer_set_modulation1(l
, ccno
, smdest_cutoff
, atof_C(value
), 0);
758 else if (!strncmp(key
, "gain_cc", 7))
760 int ccno
= atoi(key
+ 7);
761 if (ccno
> 0 && ccno
< 120)
762 sampler_layer_set_modulation1(l
, ccno
, smdest_gain
, atof_C(value
), 0);
766 else if (!strncmp(key
, "amp_velcurve_", 13))
768 // if not known yet, set to 0, it can always be overriden via velcurve_quadratic setting
769 if (l
->data
.velcurve_quadratic
== -1)
770 l
->data
.velcurve_quadratic
= 0;
771 int point
= atoi(key
+ 13);
772 if (point
>= 0 && point
<= 127)
774 l
->data
.velcurve
[point
] = atof_C(value
);
775 if (l
->data
.velcurve
[point
] < 0)
776 l
->data
.velcurve
[point
] = 0;
777 if (l
->data
.velcurve
[point
] > 1)
778 l
->data
.velcurve
[point
] = 1;
783 else if (!strncmp(key
, "on_locc", 7) || !strncmp(key
, "on_hicc", 7))
785 int cc
= atoi(key
+ 7);
786 if (cc
> 0 && cc
< 120)
790 l
->data
.on_cc_number
= cc
;
793 l
->data
.on_locc
= atoi(value
);
794 l
->data
.has_on_locc
= TRUE
;
798 l
->data
.on_hicc
= atoi(value
);
799 l
->data
.has_on_hicc
= TRUE
;
804 l
->data
.on_cc_number
= -1;
805 l
->data
.has_on_locc
= FALSE
;
806 l
->data
.has_on_hicc
= FALSE
;
812 else if (!strncmp(key
, "locc", 4) || !strncmp(key
, "hicc", 4))
814 int cc
= atoi(key
+ 4);
815 if (cc
> 0 && cc
< 120)
819 l
->data
.cc_number
= cc
;
822 l
->data
.locc
= atoi(value
);
823 l
->data
.has_locc
= TRUE
;
827 l
->data
.hicc
= atoi(value
);
828 l
->data
.has_hicc
= TRUE
;
833 l
->data
.cc_number
= -1;
834 l
->data
.has_locc
= FALSE
;
835 l
->data
.has_hicc
= FALSE
;
841 else if (!strcmp(key
, "loopmode"))
844 goto try_now
; // yes, goto, why not?
846 else if (!strcmp(key
, "offby"))
856 sampler_layer_apply_unknown(l
, key
, value
);
857 g_warning("Unknown SFZ property key: '%s'", key
);
861 #define TYPE_PRINTF_uint32_t(name, def_value) \
862 if (show_inherited || l->has_##name) \
863 g_string_append_printf(outstr, " %s=%u", #name, (unsigned)(l->name));
864 #define TYPE_PRINTF_int(name, def_value) \
865 if (show_inherited || l->has_##name) \
866 g_string_append_printf(outstr, " %s=%d", #name, (int)(l->name));
867 #define TYPE_PRINTF_midi_note_t(name, def_value) \
868 if (show_inherited || l->has_##name) { \
871 g_string_append_printf(outstr, " %s=-1", #name); \
873 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)); \
875 #define TYPE_PRINTF_float(name, def_value) \
876 if (show_inherited || l->has_##name) \
877 g_string_append_printf(outstr, " %s=%s", #name, g_ascii_dtostr(floatbuf, floatbufsize, l->name));
879 #define PROC_FIELDS_TO_FILEPTR(type, name, def_value) \
880 TYPE_PRINTF_##type(name, def_value)
881 #define PROC_FIELDS_TO_FILEPTR_string(name) \
882 if (show_inherited || l->has_##name) \
883 g_string_append_printf(outstr, " %s=%s", #name, l->name ? l->name : "");
884 #define PROC_FIELDS_TO_FILEPTR_dBamp(type, name, def_value) \
885 if (show_inherited || l->has_##name) \
886 g_string_append_printf(outstr, " %s=%s", #name, g_ascii_dtostr(floatbuf, floatbufsize, l->name));
887 #define PROC_FIELDS_TO_FILEPTR_enum(enumtype, name, def_value) \
888 if ((show_inherited || l->has_##name) && (tmpstr = enumtype##_to_string(l->name)) != NULL) \
889 g_string_append_printf(outstr, " %s=%s", #name, tmpstr);
891 #define ENV_PARAM_OUTPUT(param, index, def_value, env, envfield, envname) \
892 if (show_inherited || l->has_##envfield.param) \
893 g_string_append_printf(outstr, " " #envname "_" #param "=%s", g_ascii_dtostr(floatbuf, floatbufsize, env.param));
895 #define PROC_FIELDS_TO_FILEPTR_dahdsr(name, parname, index) \
896 DAHDSR_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
897 #define PROC_FIELDS_TO_FILEPTR_lfo(name, parname, index) \
898 LFO_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
899 #define PROC_FIELDS_TO_FILEPTR_eq(name, parname, index) \
900 EQ_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
901 #define PROC_FIELDS_TO_FILEPTR_ccrange(name) \
902 if (l->has_##name##locc) \
903 g_string_append_printf(outstr, " " #name "locc%d=%d", l->name##cc_number, l->name##locc); \
904 if (l->has_##name##hicc) \
905 g_string_append_printf(outstr, " " #name "hicc%d=%d", l->name##cc_number, l->name##hicc);
907 gchar
*sampler_layer_to_string(struct sampler_layer
*lr
, gboolean show_inherited
)
909 struct sampler_layer_data
*l
= &lr
->data
;
910 GString
*outstr
= g_string_sized_new(200);
912 char floatbuf
[G_ASCII_DTOSTR_BUF_SIZE
];
913 int floatbufsize
= G_ASCII_DTOSTR_BUF_SIZE
;
914 SAMPLER_FIXED_FIELDS(PROC_FIELDS_TO_FILEPTR
)
916 static const char *addrandom_variants
[] = { "amp", "fil", "pitch" };
917 static const char *modsrc_names
[] = { "chanaft", "vel", "polyaft", "pitch", "pitcheg", "fileg", "ampeg", "pitchlfo", "fillfo", "amplfo", "" };
918 static const char *moddest_names
[] = { "gain", "pitch", "cutoff", "resonance" };
919 for(GSList
*nif
= l
->nifs
; nif
; nif
= nif
->next
)
921 struct sampler_noteinitfunc
*nd
= nif
->data
;
922 if (!nd
->has_value
&& !show_inherited
)
924 #define PROC_ENVSTAGE_NAME(name, index, def_value) #name,
925 static const char *env_stages
[] = { DAHDSR_FIELDS(PROC_ENVSTAGE_NAME
) };
927 g_ascii_dtostr(floatbuf
, floatbufsize
, nd
->param
);
929 if (nd
->notefunc
== sampler_nif_addrandom
&& v
>= 0 && v
<= 2)
930 g_string_append_printf(outstr
, " %s_random=%s", addrandom_variants
[nd
->variant
], floatbuf
);
931 else if (nd
->notefunc
== sampler_nif_vel2pitch
)
932 g_string_append_printf(outstr
, " pitch_veltrack=%s", floatbuf
);
933 else if (nd
->notefunc
== sampler_nif_cc2delay
&& v
>= 0 && v
< 120)
934 g_string_append_printf(outstr
, " delay_cc%d=%s", nd
->variant
, floatbuf
);
935 else if (nd
->notefunc
== sampler_nif_vel2env
&& v
>= 0 && (v
& 15) < 6 && (v
>> 4) < 3)
936 g_string_append_printf(outstr
, " %seg_vel2%s=%s", addrandom_variants
[nd
->variant
>> 4], env_stages
[1 + (v
& 15)], floatbuf
);
938 for(GSList
*mod
= l
->modulations
; mod
; mod
= mod
->next
)
940 struct sampler_modulation
*md
= mod
->data
;
941 if (!md
->has_value
&& !show_inherited
)
943 g_ascii_dtostr(floatbuf
, floatbufsize
, md
->amount
);
945 if (md
->src2
== smsrc_none
)
949 g_string_append_printf(outstr
, " %s_cc%d=%s", moddest_names
[md
->dest
], md
->src
, floatbuf
);
952 if (md
->src
< 120 + sizeof(modsrc_names
) / sizeof(modsrc_names
[0]))
954 if ((md
->src
== smsrc_filenv
&& md
->dest
== smdest_cutoff
) ||
955 (md
->src
== smsrc_pitchenv
&& md
->dest
== smdest_pitch
))
956 g_string_append_printf(outstr
, " %s_depth=%s", modsrc_names
[md
->src
- 120], floatbuf
);
958 g_string_append_printf(outstr
, " %s_%s=%s", moddest_names
[md
->dest
], modsrc_names
[md
->src
- 120], floatbuf
);
962 if ((md
->src
== smsrc_amplfo
&& md
->dest
== smdest_gain
) ||
963 (md
->src
== smsrc_fillfo
&& md
->dest
== smdest_cutoff
) ||
964 (md
->src
== smsrc_pitchlfo
&& md
->dest
== smdest_pitch
))
970 g_string_append_printf(outstr
, " %slfo_depth%s=%s", moddest_names
[md
->dest
], modsrc_names
[md
->src2
- 120], floatbuf
);
975 g_string_append_printf(outstr
, " %slfo_depthcc%d=%s", moddest_names
[md
->dest
], md
->src2
, floatbuf
);
982 if ((md
->src
== smsrc_ampenv
&& md
->dest
== smdest_gain
) ||
983 (md
->src
== smsrc_filenv
&& md
->dest
== smdest_cutoff
) ||
984 (md
->src
== smsrc_pitchenv
&& md
->dest
== smdest_pitch
))
986 if (md
->src2
== smsrc_vel
)
988 g_string_append_printf(outstr
, " %seg_vel2depth=%s", moddest_names
[md
->dest
], floatbuf
);
993 g_string_append_printf(outstr
, " %s_depthcc%d=%s", modsrc_names
[md
->src
- 120], md
->src2
, floatbuf
);
997 g_string_append_printf(outstr
, " genericmod_from_%d_and_%d_to_%d=%s", md
->src
, md
->src2
, md
->dest
, floatbuf
);
1000 if (lr
->unknown_keys
)
1004 g_hash_table_iter_init(&hti
, lr
->unknown_keys
);
1005 while(g_hash_table_iter_next(&hti
, (gpointer
*)&key
, (gpointer
*)&value
))
1006 g_string_append_printf(outstr
, " %s=%s", key
, value
);
1009 gchar
*res
= outstr
->str
;
1010 g_string_free(outstr
, FALSE
);
1014 void sampler_layer_dump(struct sampler_layer
*l
, FILE *f
)
1016 gchar
*str
= sampler_layer_to_string(l
, FALSE
);
1017 fprintf(f
, "%s\n", str
);
1020 void sampler_layer_data_close(struct sampler_layer_data
*l
)
1022 g_slist_free_full(l
->nifs
, g_free
);
1023 g_slist_free_full(l
->modulations
, g_free
);
1024 if (l
->eff_waveform
)
1026 cbox_waveform_unref(l
->eff_waveform
);
1027 l
->eff_waveform
= NULL
;
1032 void sampler_layer_data_destroy(struct sampler_layer_data
*l
)
1034 sampler_layer_data_close(l
);
1038 void sampler_layer_destroyfunc(struct cbox_objhdr
*objhdr
)
1040 struct sampler_layer
*l
= CBOX_H2O(objhdr
);
1041 struct sampler_program
*prg
= l
->parent_program
;
1042 assert(g_hash_table_size(l
->child_layers
) == 0);
1044 if (l
->parent_group
)
1046 g_hash_table_remove(l
->parent_group
->child_layers
, l
);
1047 if (prg
&& prg
->rll
)
1049 sampler_program_delete_layer(prg
, l
);
1050 sampler_program_update_layers(l
->parent_program
);
1052 l
->parent_group
= NULL
;
1054 sampler_layer_data_close(&l
->data
);
1056 sampler_layer_data_destroy(l
->runtime
);
1057 if (l
->unknown_keys
)
1058 g_hash_table_destroy(l
->unknown_keys
);
1059 if (l
->child_layers
)
1060 g_hash_table_destroy(l
->child_layers
);
1065 //////////////////////////////////////////////////////////////////////////
1067 struct sampler_layer_update_cmd
1069 struct sampler_module
*module
;
1070 struct sampler_layer
*layer
;
1071 struct sampler_layer_data
*new_data
;
1072 struct sampler_layer_data
*old_data
;
1075 static int sampler_layer_update_cmd_prepare(void *data
)
1077 struct sampler_layer_update_cmd
*cmd
= data
;
1078 cmd
->old_data
= cmd
->layer
->runtime
;
1079 cmd
->new_data
= calloc(1, sizeof(struct sampler_layer_data
));
1081 sampler_layer_data_clone(cmd
->new_data
, &cmd
->layer
->data
, TRUE
);
1082 sampler_layer_data_finalize(cmd
->new_data
, cmd
->layer
->parent_group
? &cmd
->layer
->parent_group
->data
: NULL
, cmd
->layer
->parent_program
);
1083 if (cmd
->layer
->runtime
== NULL
)
1085 // initial update of the layer, so none of the voices need updating yet
1086 // because the layer hasn't been allocated to any voice
1087 cmd
->layer
->runtime
= cmd
->new_data
;
1093 static int sampler_layer_update_cmd_execute(void *data
)
1095 struct sampler_layer_update_cmd
*cmd
= data
;
1097 for (int i
= 0; i
< 16; i
++)
1099 FOREACH_VOICE(cmd
->module
->channels
[i
].voices_running
, v
)
1101 if (v
->layer
== cmd
->old_data
)
1103 v
->layer
= cmd
->new_data
;
1104 v
->layer_changed
= TRUE
;
1108 cmd
->layer
->runtime
= cmd
->new_data
;
1112 static void sampler_layer_update_cmd_cleanup(void *data
)
1114 struct sampler_layer_update_cmd
*cmd
= data
;
1116 sampler_layer_data_destroy(cmd
->old_data
);
1119 void sampler_layer_update(struct sampler_layer
*l
)
1121 // if changing a group, update all child regions instead
1122 if (g_hash_table_size(l
->child_layers
))
1124 GHashTableIter iter
;
1125 g_hash_table_iter_init(&iter
, l
->child_layers
);
1126 gpointer key
, value
;
1127 while(g_hash_table_iter_next(&iter
, &key
, &value
))
1129 sampler_layer_data_finalize(&((struct sampler_layer
*)key
)->data
, &l
->data
, l
->parent_program
);
1130 sampler_layer_update((struct sampler_layer
*)key
);
1134 static struct cbox_rt_cmd_definition rtcmd
= {
1135 .prepare
= sampler_layer_update_cmd_prepare
,
1136 .execute
= sampler_layer_update_cmd_execute
,
1137 .cleanup
= sampler_layer_update_cmd_cleanup
,
1140 struct sampler_layer_update_cmd lcmd
;
1141 lcmd
.module
= l
->module
;
1143 lcmd
.new_data
= NULL
;
1144 lcmd
.old_data
= NULL
;
1146 // In order to be able to use the async call, it would be necessary to
1147 // identify old data by layer pointer, not layer data pointer. For now,
1148 // it might be good enough to just use sync calls for this.
1149 cbox_rt_execute_cmd_sync(l
->module
->module
.rt
, &rtcmd
, &lcmd
);