Reset streaming buffer pointer when starting a new voice.
[calfbox.git] / sampler_prg.c
blobdba42a28b8e883735925c164094db9c3e6b9d134
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 "rt.h"
20 #include "sampler.h"
21 #include "sampler_prg.h"
22 #include "sfzloader.h"
24 #include <assert.h>
26 CBOX_CLASS_DEFINITION_ROOT(sampler_program)
28 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)
30 int ch = (c - c->module->channels) + 1;
31 for(;next_layer;next_layer = g_slist_next(next_layer))
33 struct sampler_layer *lr = next_layer->data;
34 struct sampler_layer_data *l = lr->runtime;
35 if (!l->eff_waveform)
36 continue;
37 if ((l->trigger == stm_first && !is_first) ||
38 (l->trigger == stm_legato && is_first))
39 continue;
40 if (l->sw_last != -1)
42 if (note >= l->sw_lokey && note <= l->sw_hikey)
43 lr->last_key = note;
45 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 &&
46 (l->cc_number == -1 || (c->cc[l->cc_number] >= l->locc && c->cc[l->cc_number] <= l->hicc)))
48 if (!l->eff_use_keyswitch ||
49 ((l->sw_last == -1 || l->sw_last == lr->last_key) &&
50 (l->sw_down == -1 || (c->switchmask[l->sw_down >> 5] & (1 << (l->sw_down & 31)))) &&
51 (l->sw_up == -1 || !(c->switchmask[l->sw_up >> 5] & (1 << (l->sw_up & 31)))) &&
52 (l->sw_previous == -1 || l->sw_previous == c->previous_note)))
54 gboolean play = lr->current_seq_position == 1;
55 lr->current_seq_position++;
56 if (lr->current_seq_position >= l->seq_length)
57 lr->current_seq_position = 1;
58 if (play)
59 return next_layer;
63 return NULL;
66 static gboolean return_layers(GSList *layers, const char *keyword, struct cbox_command_target *fb, GError **error)
68 for (GSList *p = layers; p; p = g_slist_next(p))
70 if (!cbox_execute_on(fb, NULL, keyword, "o", error, p->data))
71 return FALSE;
73 return TRUE;
76 static gboolean sampler_program_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
78 struct sampler_program *program = ct->user_data;
79 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
81 if (!cbox_check_fb_channel(fb, cmd->command, error))
82 return FALSE;
84 if (!((!program->name || cbox_execute_on(fb, NULL, "/name", "s", error, program->name)) &&
85 cbox_execute_on(fb, NULL, "/sample_dir", "s", error, program->sample_dir) &&
86 cbox_execute_on(fb, NULL, "/source_file", "s", error, program->source_file) &&
87 cbox_execute_on(fb, NULL, "/program_no", "i", error, program->prog_no) &&
88 cbox_execute_on(fb, NULL, "/in_use", "i", error, program->in_use) &&
89 CBOX_OBJECT_DEFAULT_STATUS(program, fb, error)))
90 return FALSE;
91 return TRUE;
93 if (!strcmp(cmd->command, "/regions") && !strcmp(cmd->arg_types, ""))
95 if (!cbox_check_fb_channel(fb, cmd->command, error))
96 return FALSE;
97 return return_layers(program->all_layers, "/region", fb, error);
99 if (!strcmp(cmd->command, "/groups") && !strcmp(cmd->arg_types, ""))
101 if (!cbox_check_fb_channel(fb, cmd->command, error))
102 return FALSE;
103 if (!cbox_execute_on(fb, NULL, "/default_group", "o", error, program->default_group))
104 return FALSE;
105 return return_layers(program->groups, "/group", fb, error);
107 if (!strcmp(cmd->command, "/control_inits") && !strcmp(cmd->arg_types, ""))
109 if (!cbox_check_fb_channel(fb, cmd->command, error))
110 return FALSE;
111 for (GSList *p = program->ctrl_init_list; p; p = p->next)
113 const struct sampler_ctrlinit *cin = (const struct sampler_ctrlinit *)&p->data;
114 if (!cbox_execute_on(fb, NULL, "/control_init", "ii", error, (int)cin->controller, (int)cin->value))
115 return FALSE;
117 return TRUE;
119 if (!strcmp(cmd->command, "/add_control_init") && !strcmp(cmd->arg_types, "ii"))
121 sampler_program_add_controller_init(program, CBOX_ARG_I(cmd, 0), CBOX_ARG_I(cmd, 1));
122 return TRUE;
124 if (!strcmp(cmd->command, "/delete_control_init") && !strcmp(cmd->arg_types, "ii"))
126 sampler_program_remove_controller_init(program, CBOX_ARG_I(cmd, 0), CBOX_ARG_I(cmd, 1));
127 return TRUE;
129 if (!strcmp(cmd->command, "/new_group") && !strcmp(cmd->arg_types, ""))
131 struct sampler_layer *l = sampler_layer_new(program->module, program, NULL);
132 sampler_program_add_group(program, l);
133 return cbox_execute_on(fb, NULL, "/uuid", "o", error, l);
135 return cbox_object_default_process_cmd(ct, fb, cmd, error);
139 struct sampler_program *sampler_program_new(struct sampler_module *m, int prog_no, const char *name, const char *sample_dir)
141 struct cbox_document *doc = CBOX_GET_DOCUMENT(&m->module);
142 struct sampler_program *prg = malloc(sizeof(struct sampler_program));
143 memset(prg, 0, sizeof(*prg));
144 CBOX_OBJECT_HEADER_INIT(prg, sampler_program, doc);
145 cbox_command_target_init(&prg->cmd_target, sampler_program_process_cmd, prg);
147 prg->module = m;
148 prg->prog_no = prog_no;
149 prg->name = g_strdup(name);
150 prg->sample_dir = g_strdup(sample_dir);
151 prg->source_file = NULL;
152 prg->all_layers = NULL;
153 prg->rll = NULL;
154 prg->groups = NULL;
155 prg->ctrl_init_list = NULL;
156 prg->default_group = sampler_layer_new(m, prg, NULL);
157 prg->deleting = FALSE;
158 prg->in_use = 0;
159 CBOX_OBJECT_REGISTER(prg);
160 return prg;
163 struct sampler_program *sampler_program_new_from_cfg(struct sampler_module *m, const char *cfg_section, const char *name, int pgm_id, GError **error)
165 int i;
167 char *name2 = NULL, *sfz_path = NULL, *spath = NULL;
168 const char *sfz = NULL;
170 g_clear_error(error);
171 if (!strncmp(cfg_section, "spgm:!", 6))
173 sfz = cfg_section + 6;
174 name2 = strrchr(name, '/');
175 if (name2)
176 name2++;
178 else
180 if (!sfz && !cbox_config_has_section(cfg_section))
182 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);
183 return FALSE;
185 name2 = cbox_config_get_string(cfg_section, "name");
187 sfz_path = cbox_config_get_string(cfg_section, "sfz_path");
188 spath = cbox_config_get_string(cfg_section, "sample_path");
189 sfz = cbox_config_get_string(cfg_section, "sfz");
192 if (sfz && !sfz_path && !spath)
194 char *lastslash = strrchr(sfz, '/');
195 if (lastslash && !sfz_path && !spath)
197 char *tmp = g_strndup(sfz, lastslash - sfz);
198 sfz_path = cbox_config_permify(tmp);
199 g_free(tmp);
200 sfz = lastslash + 1;
204 struct sampler_program *prg = sampler_program_new(
206 pgm_id != -1 ? pgm_id : cbox_config_get_int(cfg_section, "program", 0),
207 name2 ? name2 : name,
208 spath ? spath : (sfz_path ? sfz_path : "")
211 if (sfz)
213 if (sfz_path)
214 prg->source_file = g_build_filename(sfz_path, sfz, NULL);
215 else
217 prg->source_file = g_strdup(sfz);
220 if (sampler_module_load_program_sfz(m, prg, prg->source_file, FALSE, error))
221 return prg;
222 CBOX_DELETE(prg);
223 return NULL;
226 for (i = 0; ; i++)
228 char *where = NULL;
229 gchar *s = g_strdup_printf("layer%d", 1 + i);
230 const char *layer_section = cbox_config_get_string(cfg_section, s);
231 g_free(s);
232 if (!layer_section)
233 break;
234 where = g_strdup_printf("slayer:%s", layer_section);
236 prg->source_file = g_strdup_printf("config:%s", cfg_section);
237 struct sampler_layer *l = sampler_layer_new_from_section(m, prg, where);
238 if (!l)
239 g_warning("Sample layer '%s' cannot be created - skipping", layer_section);
240 else
242 sampler_layer_update(l);
243 if (!l->data.eff_waveform)
244 g_warning("Sample layer '%s' does not have a waveform - skipping", layer_section);
245 else
246 sampler_program_add_layer(prg, l);
248 g_free(where);
250 prg->all_layers = g_slist_reverse(prg->all_layers);
251 sampler_program_update_layers(prg);
252 return prg;
255 void sampler_program_add_layer(struct sampler_program *prg, struct sampler_layer *l)
257 // Always call sampler_update_layer before sampler_program_add_layer.
258 assert(l->runtime);
259 prg->all_layers = g_slist_prepend(prg->all_layers, l);
262 void sampler_program_delete_layer(struct sampler_program *prg, struct sampler_layer *l)
264 prg->all_layers = g_slist_remove(prg->all_layers, l);
268 void sampler_program_add_group(struct sampler_program *prg, struct sampler_layer *l)
270 prg->groups = g_slist_prepend(prg->groups, l);
273 void sampler_program_add_controller_init(struct sampler_program *prg, uint8_t controller, uint8_t value)
275 union sampler_ctrlinit_union u;
276 u.ptr = NULL;
277 u.cinit.controller = controller;
278 u.cinit.value = value;
279 prg->ctrl_init_list = g_slist_append(prg->ctrl_init_list, u.ptr);
282 void sampler_program_remove_controller_init(struct sampler_program *prg, uint8_t controller, int which)
284 for (GSList **p = &prg->ctrl_init_list; *p; )
286 const struct sampler_ctrlinit *cin = (const struct sampler_ctrlinit *)&(*p)->data;
287 if (cin->controller != controller)
289 p = &((*p)->next);
290 continue;
292 if (which > 0)
293 which--;
294 GSList *q = (GSList *)cbox_rt_swap_pointers(prg->module->module.rt, (void **)p, (*p)->next);
295 g_slist_free1(q);
296 if (which == 0)
297 break;
301 void sampler_program_destroyfunc(struct cbox_objhdr *hdr_ptr)
303 struct sampler_program *prg = CBOX_H2O(hdr_ptr);
304 sampler_unselect_program(prg->module, prg);
305 if (prg->rll)
307 sampler_rll_destroy(prg->rll);
308 prg->rll = NULL;
310 for (GSList *p = prg->all_layers; p; p = g_slist_next(p))
311 CBOX_DELETE((struct sampler_layer *)p->data);
312 for (GSList *p = prg->groups; p; p = g_slist_next(p))
313 CBOX_DELETE((struct sampler_layer *)p->data);
314 CBOX_DELETE(prg->default_group);
316 g_free(prg->name);
317 g_free(prg->sample_dir);
318 g_free(prg->source_file);
319 g_slist_free(prg->all_layers);
320 g_slist_free(prg->ctrl_init_list);
321 free(prg);
324 void sampler_program_update_layers(struct sampler_program *prg)
326 struct sampler_module *m = prg->module;
327 struct sampler_rll *new_rll = sampler_rll_new_from_program(prg);
328 struct sampler_rll *old_rll = cbox_rt_swap_pointers(m->module.rt, (void **)&prg->rll, new_rll);
329 if (old_rll)
330 sampler_rll_destroy(old_rll);
333 /////////////////////////////////////////////////////////////////////////////////
335 struct sampler_rll *sampler_rll_new_from_program(struct sampler_program *prg)
337 struct sampler_rll *rll = malloc(sizeof(struct sampler_rll));
338 rll->layers = NULL;
339 rll->layers_release = NULL;
340 rll->layers_oncc = NULL;
341 for (int i = 0; i < 4; i++)
342 rll->cc_trigger_bitmask[i] = 0;
344 for (GSList *p = prg->all_layers; p; p = g_slist_next(p))
346 struct sampler_layer *l = p->data;
347 int cc = l->data.on_cc_number;
348 if (cc != -1)
350 rll->layers_oncc = g_slist_prepend(rll->layers_oncc, l);
351 rll->cc_trigger_bitmask[cc >> 5] |= 1 << (cc & 31);
353 else if (l->data.trigger == stm_release)
354 rll->layers_release = g_slist_prepend(rll->layers_release, l);
355 else
356 rll->layers = g_slist_prepend(rll->layers, l);
358 return rll;
361 void sampler_rll_destroy(struct sampler_rll *rll)
363 g_slist_free(rll->layers);
364 g_slist_free(rll->layers_release);
365 g_slist_free(rll->layers_oncc);
366 free(rll);