Attempt at fixing premature release of notes on update_playback.
[calfbox.git] / sampler_prg.c
blob695d92b2ddefb8fe7abde85cfafab492f10c188f
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 "sampler.h"
20 #include "sampler_prg.h"
21 #include "sfzloader.h"
23 #include <assert.h>
25 CBOX_CLASS_DEFINITION_ROOT(sampler_program)
27 GSList *sampler_program_get_next_layer(struct sampler_program *prg, struct sampler_channel *c, GSList *next_layer, int note, int vel, float random)
29 int ch = (c - c->module->channels) + 1;
30 for(;next_layer;next_layer = g_slist_next(next_layer))
32 struct sampler_layer *lr = next_layer->data;
33 struct sampler_layer_data *l = &lr->data;
34 if (!l->waveform)
35 continue;
36 if (l->sw_last != -1)
38 if (note >= l->sw_lokey && note <= l->sw_hikey)
39 lr->last_key = note;
41 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)
43 if (!l->eff_use_keyswitch ||
44 ((l->sw_last == -1 || l->sw_last == lr->last_key) &&
45 (l->sw_down == -1 || (c->switchmask[l->sw_down >> 5] & (1 << (l->sw_down & 31)))) &&
46 (l->sw_up == -1 || !(c->switchmask[l->sw_up >> 5] & (1 << (l->sw_up & 31)))) &&
47 (l->sw_previous == -1 || l->sw_previous == c->previous_note)))
49 gboolean play = lr->current_seq_position == 1;
50 lr->current_seq_position++;
51 if (lr->current_seq_position >= l->seq_length)
52 lr->current_seq_position = 1;
53 if (play)
54 return next_layer;
58 return NULL;
61 static gboolean return_layers(GSList *layers, const char *keyword, struct cbox_command_target *fb, GError **error)
63 for (GSList *p = layers; p; p = g_slist_next(p))
65 if (!cbox_execute_on(fb, NULL, keyword, "o", error, p->data))
66 return FALSE;
68 return TRUE;
71 static gboolean sampler_program_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
73 struct sampler_program *program = ct->user_data;
74 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
76 if (!cbox_check_fb_channel(fb, cmd->command, error))
77 return FALSE;
79 if (!(CBOX_OBJECT_DEFAULT_STATUS(program, fb, error)))
80 return FALSE;
81 return TRUE;
83 if (!strcmp(cmd->command, "/regions") && !strcmp(cmd->arg_types, ""))
85 if (!cbox_check_fb_channel(fb, cmd->command, error))
86 return FALSE;
87 return return_layers(program->all_layers, "/region", fb, error);
89 if (!strcmp(cmd->command, "/groups") && !strcmp(cmd->arg_types, ""))
91 if (!cbox_execute_on(fb, NULL, "/default_group", "o", error, program->default_group))
92 return FALSE;
93 if (!cbox_check_fb_channel(fb, cmd->command, error))
94 return FALSE;
95 return return_layers(program->groups, "/group", fb, error);
97 if (!strcmp(cmd->command, "/new_group") && !strcmp(cmd->arg_types, ""))
99 struct sampler_layer *l = sampler_layer_new(program->module, program, NULL);
100 sampler_program_add_group(program, l);
101 return cbox_execute_on(fb, NULL, "/uuid", "o", error, l);
103 return cbox_object_default_process_cmd(ct, fb, cmd, error);
107 struct sampler_program *sampler_program_new(struct sampler_module *m, int prog_no, const char *name, const char *sample_dir)
109 struct cbox_document *doc = CBOX_GET_DOCUMENT(&m->module);
110 struct sampler_program *prg = malloc(sizeof(struct sampler_program));
111 memset(prg, 0, sizeof(*prg));
112 CBOX_OBJECT_HEADER_INIT(prg, sampler_program, doc);
113 cbox_command_target_init(&prg->cmd_target, sampler_program_process_cmd, prg);
115 prg->module = m;
116 prg->prog_no = prog_no;
117 prg->name = g_strdup(name);
118 prg->sample_dir = g_strdup(sample_dir);
119 prg->source_file = NULL;
120 prg->all_layers = NULL;
121 prg->rll = NULL;
122 prg->groups = NULL;
123 prg->ctrl_init_list = NULL;
124 prg->default_group = sampler_layer_new(m, prg, NULL);
125 prg->deleting = FALSE;
126 CBOX_OBJECT_REGISTER(prg);
127 return prg;
130 struct sampler_program *sampler_program_new_from_cfg(struct sampler_module *m, const char *cfg_section, const char *name, int pgm_id, GError **error)
132 int i;
134 char *name2 = NULL, *sfz_path = NULL, *spath = NULL;
135 const char *sfz = NULL;
137 g_clear_error(error);
138 if (!strncmp(cfg_section, "spgm:!", 6))
140 sfz = cfg_section + 6;
141 name2 = strrchr(name, '/');
142 if (name2)
143 name2++;
145 else
147 if (!sfz && !cbox_config_has_section(cfg_section))
149 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);
150 return FALSE;
152 name2 = cbox_config_get_string(cfg_section, "name");
154 sfz_path = cbox_config_get_string(cfg_section, "sfz_path");
155 spath = cbox_config_get_string(cfg_section, "sample_path");
156 sfz = cbox_config_get_string(cfg_section, "sfz");
159 if (sfz && !sfz_path && !spath)
161 char *lastslash = strrchr(sfz, '/');
162 if (lastslash && !sfz_path && !spath)
164 char *tmp = g_strndup(sfz, lastslash - sfz);
165 sfz_path = cbox_config_permify(tmp);
166 g_free(tmp);
167 sfz = lastslash + 1;
171 struct sampler_program *prg = sampler_program_new(
173 pgm_id != -1 ? pgm_id : cbox_config_get_int(cfg_section, "program", 0),
174 name2 ? name2 : name,
175 spath ? spath : (sfz_path ? sfz_path : "")
178 if (sfz)
180 if (sfz_path)
181 prg->source_file = g_build_filename(sfz_path, sfz, NULL);
182 else
184 prg->source_file = g_strdup(sfz);
187 if (sampler_module_load_program_sfz(m, prg, prg->source_file, FALSE, error))
188 return prg;
189 CBOX_DELETE(prg);
190 return NULL;
193 for (i = 0; ; i++)
195 char *where = NULL;
196 gchar *s = g_strdup_printf("layer%d", 1 + i);
197 const char *layer_section = cbox_config_get_string(cfg_section, s);
198 g_free(s);
199 if (!layer_section)
200 break;
201 where = g_strdup_printf("slayer:%s", layer_section);
203 prg->source_file = g_strdup_printf("config:%s", cfg_section);
204 struct sampler_layer *l = sampler_layer_new_from_section(m, prg, where);
205 sampler_update_layer(m, l);
206 if (!l)
207 g_warning("Sample layer '%s' cannot be created - skipping", layer_section);
208 else if (!l->data.waveform)
209 g_warning("Sample layer '%s' does not have a waveform - skipping", layer_section);
210 else
211 sampler_program_add_layer(prg, l);
212 g_free(where);
214 prg->all_layers = g_slist_reverse(prg->all_layers);
215 sampler_update_program_layers(m, prg);
216 return prg;
219 void sampler_program_add_layer(struct sampler_program *prg, struct sampler_layer *l)
221 // Always call sampler_update_layer before sampler_program_add_layer.
222 assert(l->runtime);
223 prg->all_layers = g_slist_prepend(prg->all_layers, l);
226 void sampler_program_delete_layer(struct sampler_program *prg, struct sampler_layer *l)
228 prg->all_layers = g_slist_remove(prg->all_layers, l);
232 void sampler_program_add_group(struct sampler_program *prg, struct sampler_layer *l)
234 prg->groups = g_slist_prepend(prg->groups, l);
237 void sampler_program_add_controller_init(struct sampler_program *prg, uint8_t controller, uint8_t value)
239 union sampler_ctrlinit_union u;
240 u.ptr = NULL;
241 u.cinit.controller = controller;
242 u.cinit.value = value;
243 prg->ctrl_init_list = g_slist_prepend(prg->ctrl_init_list, u.ptr);
246 void sampler_program_destroyfunc(struct cbox_objhdr *hdr_ptr)
248 struct sampler_program *prg = CBOX_H2O(hdr_ptr);
249 sampler_unselect_program(prg->module, prg);
250 if (prg->rll)
252 sampler_rll_destroy(prg->rll);
253 prg->rll = NULL;
255 for (GSList *p = prg->all_layers; p; p = g_slist_next(p))
256 CBOX_DELETE((struct sampler_layer *)p->data);
257 for (GSList *p = prg->groups; p; p = g_slist_next(p))
258 CBOX_DELETE((struct sampler_layer *)p->data);
259 CBOX_DELETE(prg->default_group);
261 g_free(prg->name);
262 g_free(prg->sample_dir);
263 g_free(prg->source_file);
264 g_slist_free(prg->all_layers);
265 g_slist_free(prg->ctrl_init_list);
266 free(prg);
269 /////////////////////////////////////////////////////////////////////////////////
271 struct sampler_rll *sampler_rll_new_from_program(struct sampler_program *prg)
273 struct sampler_rll *rll = malloc(sizeof(struct sampler_rll));
274 rll->layers = NULL;
275 rll->layers_release = NULL;
276 rll->layers_oncc = NULL;
277 for (int i = 0; i < 4; i++)
278 rll->cc_trigger_bitmask[i] = 0;
280 for (GSList *p = prg->all_layers; p; p = g_slist_next(p))
282 struct sampler_layer *l = p->data;
283 int cc = l->data.on_cc_number;
284 if (cc != -1)
286 rll->layers_oncc = g_slist_prepend(rll->layers_oncc, l);
287 rll->cc_trigger_bitmask[cc >> 5] |= 1 << (cc & 31);
289 else if (l->data.trigger == stm_release)
290 rll->layers_release = g_slist_prepend(rll->layers_release, l);
291 else
292 rll->layers = g_slist_prepend(rll->layers, l);
294 return rll;
297 void sampler_rll_destroy(struct sampler_rll *rll)
299 g_slist_free(rll->layers);
300 g_slist_free(rll->layers_release);
301 g_slist_free(rll->layers_oncc);
302 free(rll);