Merge pull request #1 from atsampson/master
[calfbox.git] / sampler_prg.c
blob3d0ae486409fffef3a49480af5b4522ffeceb35f
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2013 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 "app.h"
20 #include "blob.h"
21 #include "instr.h"
22 #include "rt.h"
23 #include "sampler.h"
24 #include "sampler_prg.h"
25 #include "sfzloader.h"
26 #include "tarfile.h"
28 #include <assert.h>
30 CBOX_CLASS_DEFINITION_ROOT(sampler_program)
32 GSList *sampler_program_get_next_layer(struct sampler_program *prg, struct sampler_channel *c, GSList *next_layer, int note, int vel, float random, gboolean is_first)
34 int ch = (c - c->module->channels) + 1;
35 for(;next_layer;next_layer = g_slist_next(next_layer))
37 struct sampler_layer *lr = next_layer->data;
38 struct sampler_layer_data *l = lr->runtime;
39 if (!l->eff_waveform)
40 continue;
41 if ((l->trigger == stm_first && !is_first) ||
42 (l->trigger == stm_legato && is_first))
43 continue;
44 if (l->sw_last != -1)
46 if (note >= l->sw_lokey && note <= l->sw_hikey)
47 lr->last_key = note;
49 if (note >= l->lokey && note <= l->hikey && vel >= l->lovel && vel <= l->hivel && ch >= l->lochan && ch <= l->hichan && random >= l->lorand && random < l->hirand &&
50 (l->cc_number == -1 || (c->cc[l->cc_number] >= l->locc && c->cc[l->cc_number] <= l->hicc)))
52 if (!l->eff_use_keyswitch ||
53 ((l->sw_last == -1 || l->sw_last == lr->last_key) &&
54 (l->sw_down == -1 || (c->switchmask[l->sw_down >> 5] & (1 << (l->sw_down & 31)))) &&
55 (l->sw_up == -1 || !(c->switchmask[l->sw_up >> 5] & (1 << (l->sw_up & 31)))) &&
56 (l->sw_previous == -1 || l->sw_previous == c->previous_note)))
58 gboolean play = lr->current_seq_position == 1;
59 lr->current_seq_position++;
60 if (lr->current_seq_position >= l->seq_length)
61 lr->current_seq_position = 1;
62 if (play)
63 return next_layer;
67 return NULL;
70 static gboolean return_layers(GSList *layers, const char *keyword, struct cbox_command_target *fb, GError **error)
72 for (GSList *p = layers; p; p = g_slist_next(p))
74 if (!cbox_execute_on(fb, NULL, keyword, "o", error, p->data))
75 return FALSE;
77 return TRUE;
80 static gboolean sampler_program_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
82 struct sampler_program *program = ct->user_data;
83 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
85 if (!cbox_check_fb_channel(fb, cmd->command, error))
86 return FALSE;
88 if (!((!program->name || cbox_execute_on(fb, NULL, "/name", "s", error, program->name)) &&
89 cbox_execute_on(fb, NULL, "/sample_dir", "s", error, program->sample_dir) &&
90 cbox_execute_on(fb, NULL, "/source_file", "s", error, program->source_file) &&
91 cbox_execute_on(fb, NULL, "/program_no", "i", error, program->prog_no) &&
92 cbox_execute_on(fb, NULL, "/in_use", "i", error, program->in_use) &&
93 CBOX_OBJECT_DEFAULT_STATUS(program, fb, error)))
94 return FALSE;
95 return TRUE;
97 if (!strcmp(cmd->command, "/regions") && !strcmp(cmd->arg_types, ""))
99 if (!cbox_check_fb_channel(fb, cmd->command, error))
100 return FALSE;
101 return return_layers(program->all_layers, "/region", fb, error);
103 if (!strcmp(cmd->command, "/groups") && !strcmp(cmd->arg_types, ""))
105 if (!cbox_check_fb_channel(fb, cmd->command, error))
106 return FALSE;
107 if (!cbox_execute_on(fb, NULL, "/default_group", "o", error, program->default_group))
108 return FALSE;
109 return return_layers(program->groups, "/group", fb, error);
111 if (!strcmp(cmd->command, "/control_inits") && !strcmp(cmd->arg_types, ""))
113 if (!cbox_check_fb_channel(fb, cmd->command, error))
114 return FALSE;
115 for (GSList *p = program->ctrl_init_list; p; p = p->next)
117 const struct sampler_ctrlinit *cin = (const struct sampler_ctrlinit *)&p->data;
118 if (!cbox_execute_on(fb, NULL, "/control_init", "ii", error, (int)cin->controller, (int)cin->value))
119 return FALSE;
121 return TRUE;
123 if (!strcmp(cmd->command, "/add_control_init") && !strcmp(cmd->arg_types, "ii"))
125 sampler_program_add_controller_init(program, CBOX_ARG_I(cmd, 0), CBOX_ARG_I(cmd, 1));
126 return TRUE;
128 if (!strcmp(cmd->command, "/delete_control_init") && !strcmp(cmd->arg_types, "ii"))
130 sampler_program_remove_controller_init(program, CBOX_ARG_I(cmd, 0), CBOX_ARG_I(cmd, 1));
131 return TRUE;
133 if (!strcmp(cmd->command, "/new_group") && !strcmp(cmd->arg_types, ""))
135 struct sampler_layer *l = sampler_layer_new(program->module, program, NULL);
136 sampler_program_add_group(program, l);
137 return cbox_execute_on(fb, NULL, "/uuid", "o", error, l);
139 if (!strcmp(cmd->command, "/clone_to") && !strcmp(cmd->arg_types, "si"))
141 struct cbox_instrument *instrument = (struct cbox_instrument *)CBOX_ARG_O(cmd, 0, program, cbox_instrument, error);
142 if (!instrument)
143 return FALSE;
144 struct cbox_module *module = instrument->module;
145 if (strcmp(module->engine_name, "sampler"))
147 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Cannot copy sampler program to module '%s' of type '%s'", module->instance_name, module->engine_name);
148 return FALSE;
150 struct sampler_program *prg = sampler_program_clone(program, (struct sampler_module *)module, CBOX_ARG_I(cmd, 1), error);
151 if (!prg)
152 return FALSE;
153 sampler_register_program((struct sampler_module *)module, prg);
154 return cbox_execute_on(fb, NULL, "/uuid", "o", error, prg);
156 if (!strcmp(cmd->command, "/load_file") && !strcmp(cmd->arg_types, "si"))
158 if (!cbox_check_fb_channel(fb, cmd->command, error))
159 return FALSE;
160 struct cbox_blob *blob = cbox_blob_new_from_file(program->name, program->tarfile, program->sample_dir, CBOX_ARG_S(cmd, 0), CBOX_ARG_I(cmd, 1), error);
161 if (!blob)
162 return FALSE;
163 return cbox_execute_on(fb, NULL, "/data", "b", error, blob);
165 return cbox_object_default_process_cmd(ct, fb, cmd, error);
168 struct sampler_program *sampler_program_new(struct sampler_module *m, int prog_no, const char *name, struct cbox_tarfile *tarfile, const char *sample_dir, GError **error)
170 gchar *perm_sample_dir = g_strdup(sample_dir);
171 if (!perm_sample_dir)
172 return NULL;
174 struct cbox_document *doc = CBOX_GET_DOCUMENT(&m->module);
175 struct sampler_program *prg = malloc(sizeof(struct sampler_program));
176 if (!prg)
178 g_free(perm_sample_dir);
179 return NULL;
181 memset(prg, 0, sizeof(*prg));
182 CBOX_OBJECT_HEADER_INIT(prg, sampler_program, doc);
183 cbox_command_target_init(&prg->cmd_target, sampler_program_process_cmd, prg);
185 prg->module = m;
186 prg->prog_no = prog_no;
187 prg->name = g_strdup(name);
188 prg->tarfile = tarfile;
189 prg->source_file = NULL;
190 prg->sample_dir = perm_sample_dir;
191 prg->all_layers = NULL;
192 prg->rll = NULL;
193 prg->groups = NULL;
194 prg->ctrl_init_list = NULL;
195 prg->default_group = sampler_layer_new(m, prg, NULL);
196 prg->deleting = FALSE;
197 prg->in_use = 0;
198 CBOX_OBJECT_REGISTER(prg);
199 return prg;
202 struct sampler_program *sampler_program_new_from_cfg(struct sampler_module *m, const char *cfg_section, const char *name, int pgm_id, GError **error)
204 int i;
206 char *name2 = NULL, *sfz_path = NULL, *spath = NULL, *tar_name = NULL;
207 const char *sfz = NULL;
208 struct cbox_tarfile *tarfile = NULL;
210 g_clear_error(error);
211 tar_name = cbox_config_get_string(cfg_section, "tar");
212 if (!strncmp(cfg_section, "spgm:!", 6))
214 sfz = cfg_section + 6;
215 if (!strncmp(sfz, "sbtar:", 6))
217 sfz_path = ".";
218 gchar *p = strchr(sfz + 6, ';');
219 if (p)
221 char *tmp = g_strndup(sfz + 6, p - sfz - 6);
222 tarfile = cbox_tarpool_get_tarfile(app.tarpool, tmp, error);
223 g_free(tmp);
224 if (!tarfile)
225 return NULL;
226 sfz = p + 1;
227 name2 = p + 1;
229 else
231 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Cannot load sampler program '%s' from section '%s': missing name of a file inside a tar archive", name, cfg_section);
232 return NULL;
235 else
237 name2 = strrchr(name, '/');
238 if (name2)
239 name2++;
242 else
244 if (tar_name)
246 tarfile = cbox_tarpool_get_tarfile(app.tarpool, tar_name, error);
247 if (!tarfile)
248 return NULL;
250 if (!sfz && !cbox_config_has_section(cfg_section))
252 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Cannot load sampler program '%s' from section '%s': section not found", name, cfg_section);
253 return FALSE;
255 name2 = cbox_config_get_string(cfg_section, "name");
257 sfz_path = cbox_config_get_string(cfg_section, "sfz_path");
258 spath = cbox_config_get_string(cfg_section, "sample_path");
259 sfz = cbox_config_get_string(cfg_section, "sfz");
260 if (tarfile && !sfz_path)
261 sfz_path = ".";
264 if (sfz && !sfz_path && !spath && !tarfile)
266 char *lastslash = strrchr(sfz, '/');
267 if (lastslash && !sfz_path && !spath)
269 char *tmp = g_strndup(sfz, lastslash - sfz);
270 sfz_path = cbox_config_permify(tmp);
271 g_free(tmp);
272 sfz = lastslash + 1;
276 struct sampler_program *prg = sampler_program_new(
278 pgm_id != -1 ? pgm_id : cbox_config_get_int(cfg_section, "program", 0),
279 name2 ? name2 : name,
280 tarfile,
281 spath ? spath : (sfz_path ? sfz_path : ""),
282 error
284 if (!prg)
285 return NULL;
287 if (sfz)
289 if (sfz_path)
290 prg->source_file = g_build_filename(sfz_path, sfz, NULL);
291 else
293 prg->source_file = g_strdup(sfz);
296 if (sampler_module_load_program_sfz(m, prg, prg->source_file, FALSE, error))
297 return prg;
298 CBOX_DELETE(prg);
299 return NULL;
300 } else {
301 prg->source_file = g_strdup_printf("config:%s", cfg_section);
304 for (i = 0; ; i++)
306 gchar *s = g_strdup_printf("group%d", 1 + i);
307 const char *group_section = cbox_config_get_string(cfg_section, s);
308 g_free(s);
309 if (!group_section)
310 break;
312 gchar *swhere = g_strdup_printf("sgroup:%s", group_section);
313 struct sampler_layer *g = sampler_layer_new_from_section(m, prg, NULL, swhere);
314 if (!g)
315 g_warning("Sample layer '%s' cannot be created - skipping", group_section);
316 else
318 sampler_program_add_group(prg, g);
319 for (int j = 0; ; j++)
321 char *where = NULL;
322 gchar *s = g_strdup_printf("layer%d", 1 + j);
323 const char *layer_section = cbox_config_get_string(swhere, s);
324 g_free(s);
325 if (!layer_section)
326 break;
327 where = g_strdup_printf("slayer:%s", layer_section);
328 struct sampler_layer *l = sampler_layer_new_from_section(m, prg, g, where);
329 if (!l)
330 g_warning("Sample layer '%s' cannot be created - skipping", layer_section);
331 else
333 sampler_layer_update(l);
334 if (!l->data.eff_waveform)
336 g_warning("Sample layer '%s' does not have a waveform - skipping", layer_section);
337 CBOX_DELETE((struct sampler_layer *)l);
339 else
340 sampler_program_add_layer(prg, l);
342 g_free(where);
345 g_free(swhere);
347 for (i = 0; ; i++)
349 char *where = NULL;
350 gchar *s = g_strdup_printf("layer%d", 1 + i);
351 const char *layer_section = cbox_config_get_string(cfg_section, s);
352 g_free(s);
353 if (!layer_section)
354 break;
355 where = g_strdup_printf("slayer:%s", layer_section);
357 struct sampler_layer *l = sampler_layer_new_from_section(m, prg, NULL, where);
358 if (!l)
359 g_warning("Sample layer '%s' cannot be created - skipping", layer_section);
360 else
362 sampler_layer_update(l);
363 if (!l->data.eff_waveform)
364 g_warning("Sample layer '%s' does not have a waveform - skipping", layer_section);
365 else
366 sampler_program_add_layer(prg, l);
368 g_free(where);
370 prg->all_layers = g_slist_reverse(prg->all_layers);
371 sampler_program_update_layers(prg);
372 return prg;
375 void sampler_program_add_layer(struct sampler_program *prg, struct sampler_layer *l)
377 // Always call sampler_update_layer before sampler_program_add_layer.
378 assert(l->runtime);
379 prg->all_layers = g_slist_prepend(prg->all_layers, l);
382 void sampler_program_delete_layer(struct sampler_program *prg, struct sampler_layer *l)
384 prg->all_layers = g_slist_remove(prg->all_layers, l);
388 void sampler_program_add_group(struct sampler_program *prg, struct sampler_layer *l)
390 prg->groups = g_slist_prepend(prg->groups, l);
393 void sampler_program_add_controller_init(struct sampler_program *prg, uint8_t controller, uint8_t value)
395 union sampler_ctrlinit_union u;
396 u.ptr = NULL;
397 u.cinit.controller = controller;
398 u.cinit.value = value;
399 prg->ctrl_init_list = g_slist_append(prg->ctrl_init_list, u.ptr);
402 void sampler_program_remove_controller_init(struct sampler_program *prg, uint8_t controller, int which)
404 for (GSList **p = &prg->ctrl_init_list; *p; )
406 const struct sampler_ctrlinit *cin = (const struct sampler_ctrlinit *)&(*p)->data;
407 if (cin->controller != controller)
409 p = &((*p)->next);
410 continue;
412 if (which > 0)
413 which--;
414 GSList *q = (GSList *)cbox_rt_swap_pointers(prg->module->module.rt, (void **)p, (*p)->next);
415 g_slist_free1(q);
416 if (which == 0)
417 break;
421 void sampler_program_destroyfunc(struct cbox_objhdr *hdr_ptr)
423 struct sampler_program *prg = CBOX_H2O(hdr_ptr);
424 sampler_unselect_program(prg->module, prg);
425 if (prg->rll)
427 sampler_rll_destroy(prg->rll);
428 prg->rll = NULL;
430 for (GSList *p = prg->all_layers; p; p = g_slist_next(p))
431 CBOX_DELETE((struct sampler_layer *)p->data);
432 for (GSList *p = prg->groups; p; p = g_slist_next(p))
433 CBOX_DELETE((struct sampler_layer *)p->data);
434 CBOX_DELETE(prg->default_group);
436 g_free(prg->name);
437 g_free(prg->sample_dir);
438 g_free(prg->source_file);
439 g_slist_free(prg->all_layers);
440 g_slist_free(prg->ctrl_init_list);
441 if (prg->tarfile)
442 cbox_tarpool_release_tarfile(app.tarpool, prg->tarfile);
443 free(prg);
446 void sampler_program_update_layers(struct sampler_program *prg)
448 struct sampler_module *m = prg->module;
449 struct sampler_rll *new_rll = sampler_rll_new_from_program(prg);
450 struct sampler_rll *old_rll = cbox_rt_swap_pointers(m->module.rt, (void **)&prg->rll, new_rll);
451 if (old_rll)
452 sampler_rll_destroy(old_rll);
455 static void add_child_layers_of_group(struct sampler_program *newprg, struct sampler_layer *group)
457 sampler_layer_update(group);
459 GHashTableIter iter;
460 g_hash_table_iter_init(&iter, group->child_layers);
461 gpointer key, value;
462 while(g_hash_table_iter_next(&iter, &key, &value))
464 sampler_layer_reset_switches((struct sampler_layer *)key, newprg->module);
465 sampler_program_add_layer(newprg, (struct sampler_layer *)key);
469 struct sampler_program *sampler_program_clone(struct sampler_program *prg, struct sampler_module *m, int prog_no, GError **error)
471 struct sampler_program *newprg = sampler_program_new(m, prog_no, prg->name, prg->tarfile, prg->sample_dir, error);
472 if (!newprg)
473 return NULL;
474 if (prg->source_file)
475 newprg->source_file = g_strdup(prg->source_file);
476 // The values are stored as a union aliased with the data pointer, so no need to deep-copy
477 newprg->ctrl_init_list = g_slist_copy(prg->ctrl_init_list);
478 newprg->rll = NULL;
479 if (prg->default_group)
481 // XXXKF remove the original default group
482 newprg->default_group = sampler_layer_new_clone(prg->default_group, m, newprg, NULL);
483 add_child_layers_of_group(newprg, newprg->default_group);
485 newprg->groups = g_slist_copy(prg->groups);
486 for (GSList *p = newprg->groups; p; p = g_slist_next(p))
488 struct sampler_layer *l = p->data;
489 l = sampler_layer_new_clone(l, m, newprg, NULL);
490 p->data = l;
491 add_child_layers_of_group(newprg, l);
493 sampler_program_update_layers(newprg);
494 if (newprg->tarfile)
495 newprg->tarfile->refs++;
497 return newprg;
500 /////////////////////////////////////////////////////////////////////////////////
502 struct sampler_rll *sampler_rll_new_from_program(struct sampler_program *prg)
504 struct sampler_rll *rll = malloc(sizeof(struct sampler_rll));
505 rll->layers = NULL;
506 rll->layers_release = NULL;
507 rll->layers_oncc = NULL;
508 for (int i = 0; i < 4; i++)
509 rll->cc_trigger_bitmask[i] = 0;
511 for (GSList *p = prg->all_layers; p; p = g_slist_next(p))
513 struct sampler_layer *l = p->data;
514 int cc = l->data.on_cc_number;
515 if (cc != -1)
517 rll->layers_oncc = g_slist_prepend(rll->layers_oncc, l);
518 rll->cc_trigger_bitmask[cc >> 5] |= 1 << (cc & 31);
520 else if (l->data.trigger == stm_release)
521 rll->layers_release = g_slist_prepend(rll->layers_release, l);
522 else
523 rll->layers = g_slist_prepend(rll->layers, l);
525 return rll;
528 void sampler_rll_destroy(struct sampler_rll *rll)
530 g_slist_free(rll->layers);
531 g_slist_free(rll->layers_release);
532 g_slist_free(rll->layers_oncc);
533 free(rll);