Got seek_ppqn/seek_samples the other way around :-)
[calfbox.git] / scene.c
blobad93e35775ded7afaf527d850b9ed0d2ad55c2d4
1 /*
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 "auxbus.h"
20 #include "config-api.h"
21 #include "engine.h"
22 #include "errors.h"
23 #include "instr.h"
24 #include "io.h"
25 #include "layer.h"
26 #include "master.h"
27 #include "midi.h"
28 #include "module.h"
29 #include "rt.h"
30 #include "scene.h"
31 #include "seq.h"
32 #include <assert.h>
33 #include <glib.h>
35 CBOX_CLASS_DEFINITION_ROOT(cbox_scene)
37 static gboolean cbox_scene_addlayercmd(struct cbox_scene *s, struct cbox_command_target *fb, struct cbox_osc_command *cmd, int cmd_type, GError **error)
39 int pos = CBOX_ARG_I(cmd, 0);
40 if (pos < 0 || pos > 1 + s->layer_count)
42 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Invalid position %d (valid are 1..%d or 0 for append)", pos, 1 + s->layer_count);
43 return FALSE;
45 if (pos == 0)
46 pos = s->layer_count;
47 else
48 pos--;
49 struct cbox_layer *layer = NULL;
51 switch(cmd_type)
53 case 1:
54 layer = cbox_layer_new_from_config(s, CBOX_ARG_S(cmd, 1), error);
55 break;
56 case 2:
57 layer = cbox_layer_new_with_instrument(s, CBOX_ARG_S(cmd, 1), error);
58 break;
59 case 3:
61 struct cbox_instrument *instr = cbox_scene_create_instrument(s, CBOX_ARG_S(cmd, 1), CBOX_ARG_S(cmd, 2), error);
62 if (!instr)
63 return FALSE;
64 layer = cbox_layer_new_with_instrument(s, CBOX_ARG_S(cmd, 1), error);
65 break;
67 default:
68 assert(0);
69 break;
71 if (!layer)
72 return FALSE;
73 if (!cbox_scene_insert_layer(s, layer, pos, error))
75 CBOX_DELETE(layer);
76 return FALSE;
78 if (fb)
80 if (!cbox_execute_on(fb, NULL, "/uuid", "o", error, layer))
81 return FALSE;
83 return TRUE;
86 static gboolean cbox_scene_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
88 struct cbox_scene *s = ct->user_data;
89 const char *subcommand = NULL;
90 char *subobj = NULL;
91 int index = 0;
93 if (!strcmp(cmd->command, "/transpose") && !strcmp(cmd->arg_types, "i"))
95 s->transpose = CBOX_ARG_I(cmd, 0);
96 return TRUE;
98 else if (!strcmp(cmd->command, "/load") && !strcmp(cmd->arg_types, "s"))
100 if (!cbox_scene_load(s, CBOX_ARG_S(cmd, 0), error))
101 return FALSE;
102 return TRUE;
104 else if (!strcmp(cmd->command, "/clear") && !strcmp(cmd->arg_types, ""))
106 cbox_scene_clear(s);
107 return TRUE;
109 else if (!strcmp(cmd->command, "/add_layer") && !strcmp(cmd->arg_types, "is"))
111 return cbox_scene_addlayercmd(s, fb, cmd, 1, error);
113 else if (!strcmp(cmd->command, "/add_instrument_layer") && !strcmp(cmd->arg_types, "is"))
115 return cbox_scene_addlayercmd(s, fb, cmd, 2, error);
117 else if (!strcmp(cmd->command, "/add_new_instrument_layer") && !strcmp(cmd->arg_types, "iss"))
119 return cbox_scene_addlayercmd(s, fb, cmd, 3, error);
121 else if (!strcmp(cmd->command, "/delete_layer") && !strcmp(cmd->arg_types, "i"))
123 int pos = CBOX_ARG_I(cmd, 0);
124 if (pos < 0 || pos > s->layer_count)
126 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Invalid position %d (valid are 1..%d or 0 for last)", pos, s->layer_count);
127 return FALSE;
129 if (pos == 0)
130 pos = s->layer_count - 1;
131 else
132 pos--;
133 struct cbox_layer *layer = cbox_scene_remove_layer(s, pos);
134 CBOX_DELETE(layer);
135 return TRUE;
137 else if (!strcmp(cmd->command, "/move_layer") && !strcmp(cmd->arg_types, "ii"))
139 int oldpos = CBOX_ARG_I(cmd, 0);
140 if (oldpos < 1 || oldpos > s->layer_count)
142 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Invalid position %d (valid are 1..%d)", oldpos, s->layer_count);
143 return FALSE;
145 int newpos = CBOX_ARG_I(cmd, 1);
146 if (newpos < 1 || newpos > s->layer_count)
148 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Invalid position %d (valid are 1..%d)", newpos, s->layer_count);
149 return FALSE;
151 cbox_scene_move_layer(s, oldpos - 1, newpos - 1);
152 return TRUE;
154 else if (cbox_parse_path_part_int(cmd, "/layer/", &subcommand, &index, 1, s->layer_count, error))
156 if (!subcommand)
157 return FALSE;
158 return cbox_execute_sub(&s->layers[index - 1]->cmd_target, fb, cmd, subcommand, error);
160 else if (cbox_parse_path_part_str(cmd, "/aux/", &subcommand, &subobj, error))
162 if (!subcommand)
163 return FALSE;
164 struct cbox_aux_bus *aux = cbox_scene_get_aux_bus(s, subobj, FALSE, error);
165 g_free(subobj);
166 if (!aux)
167 return FALSE;
168 return cbox_execute_sub(&aux->cmd_target, fb, cmd, subcommand, error);
170 else if (!strncmp(cmd->command, "/instr/", 7))
172 const char *obj = &cmd->command[1];
173 const char *pos = strchr(obj, '/');
174 obj = &pos[1];
175 pos = strchr(obj, '/');
176 if (!pos)
178 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Invalid instrument path '%s'", cmd->command);
179 return FALSE;
181 int len = pos - obj;
183 gchar *name = g_strndup(obj, len);
184 struct cbox_instrument *instr = cbox_scene_get_instrument_by_name(s, name, FALSE, error);
185 if (instr)
187 g_free(name);
189 return cbox_execute_sub(&instr->cmd_target, fb, cmd, pos, error);
191 else
193 cbox_force_error(error);
194 g_prefix_error(error, "Cannot access instrument '%s': ", name);
195 g_free(name);
196 return FALSE;
198 return TRUE;
200 else if (!strcmp(cmd->command, "/load_aux") && !strcmp(cmd->arg_types, "s"))
202 struct cbox_aux_bus *bus = cbox_scene_get_aux_bus(s, CBOX_ARG_S(cmd, 0), TRUE, error);
203 if (!bus)
204 return FALSE;
205 if (fb)
207 if (!cbox_execute_on(fb, NULL, "/uuid", "o", error, bus))
208 return FALSE;
210 return TRUE;
212 else if (!strcmp(cmd->command, "/delete_aux") && !strcmp(cmd->arg_types, "s"))
214 const char *name = CBOX_ARG_S(cmd, 0);
215 struct cbox_aux_bus *aux = cbox_scene_get_aux_bus(s, name, FALSE, error);
216 if (!aux)
217 return FALSE;
218 CBOX_DELETE(aux);
219 return TRUE;
221 else if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
223 if (!cbox_check_fb_channel(fb, cmd->command, error))
224 return FALSE;
226 if (!cbox_execute_on(fb, NULL, "/name", "s", error, s->name) ||
227 !cbox_execute_on(fb, NULL, "/title", "s", error, s->title) ||
228 !cbox_execute_on(fb, NULL, "/transpose", "i", error, s->transpose) ||
229 !cbox_execute_on(fb, NULL, "/enable_default_song_input", "i", error, s->enable_default_song_input) ||
230 !cbox_execute_on(fb, NULL, "/enable_default_external_input", "i", error, s->enable_default_external_input) ||
231 !CBOX_OBJECT_DEFAULT_STATUS(s, fb, error))
232 return FALSE;
234 for (int i = 0; i < s->layer_count; i++)
236 if (!cbox_execute_on(fb, NULL, "/layer", "o", error, s->layers[i]))
237 return FALSE;
239 for (int i = 0; i < s->instrument_count; i++)
241 if (!cbox_execute_on(fb, NULL, "/instrument", "sso", error, s->instruments[i]->module->instance_name, s->instruments[i]->module->engine_name, s->instruments[i]))
242 return FALSE;
244 for (int i = 0; i < s->aux_bus_count; i++)
246 if (!cbox_execute_on(fb, NULL, "/aux", "so", error, s->aux_buses[i]->name, s->aux_buses[i]))
247 return FALSE;
249 return TRUE;
251 else
252 if (!strcmp(cmd->command, "/send_event") && (!strcmp(cmd->arg_types, "iii") || !strcmp(cmd->arg_types, "ii") || !strcmp(cmd->arg_types, "i")))
254 int mcmd = CBOX_ARG_I(cmd, 0);
255 int arg1 = 0, arg2 = 0;
256 if (cmd->arg_types[1] == 'i')
258 arg1 = CBOX_ARG_I(cmd, 1);
259 if (cmd->arg_types[2] == 'i')
260 arg2 = CBOX_ARG_I(cmd, 2);
262 struct cbox_midi_buffer buf;
263 cbox_midi_buffer_init(&buf);
264 cbox_midi_buffer_write_inline(&buf, 0, mcmd, arg1, arg2);
265 cbox_midi_merger_push(&s->scene_input_merger, &buf, s->rt);
266 return TRUE;
268 else
269 if (!strcmp(cmd->command, "/play_note") && !strcmp(cmd->arg_types, "iii"))
271 int channel = CBOX_ARG_I(cmd, 0);
272 int note = CBOX_ARG_I(cmd, 1);
273 int velocity = CBOX_ARG_I(cmd, 2);
274 struct cbox_midi_buffer buf;
275 cbox_midi_buffer_init(&buf);
276 cbox_midi_buffer_write_inline(&buf, 0, 0x90 + ((channel - 1) & 15), note & 127, velocity & 127);
277 cbox_midi_buffer_write_inline(&buf, 1, 0x80 + ((channel - 1) & 15), note & 127, velocity & 127);
278 cbox_midi_merger_push(&s->scene_input_merger, &buf, s->rt);
279 return TRUE;
281 else
282 if (!strcmp(cmd->command, "/enable_default_song_input") && !strcmp(cmd->arg_types, "i"))
284 s->enable_default_song_input = CBOX_ARG_I(cmd, 0);
285 return TRUE;
287 else
288 if (!strcmp(cmd->command, "/enable_default_external_input") && !strcmp(cmd->arg_types, "i"))
290 s->enable_default_external_input = CBOX_ARG_I(cmd, 0);
291 return TRUE;
293 else
294 return cbox_object_default_process_cmd(ct, fb, cmd, error);
297 gboolean cbox_scene_load(struct cbox_scene *s, const char *name, GError **error)
299 const char *cv = NULL;
300 int i;
301 gchar *section = g_strdup_printf("scene:%s", name);
303 if (!cbox_config_has_section(section))
305 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "No config section for scene '%s'", name);
306 goto error;
309 cbox_scene_clear(s);
311 assert(s->layers == NULL);
312 assert(s->instruments == NULL);
313 assert(s->aux_buses == NULL);
314 assert(s->layer_count == 0);
315 assert(s->instrument_count == 0);
316 assert(s->aux_bus_count == 0);
318 for (i = 1; ; i++)
320 struct cbox_layer *l = NULL;
322 gchar *sn = g_strdup_printf("layer%d", i);
323 cv = cbox_config_get_string(section, sn);
324 g_free(sn);
326 if (!cv)
327 break;
329 l = cbox_layer_new_from_config(s, cv, error);
330 if (!l)
331 goto error;
333 if (!cbox_scene_add_layer(s, l, error))
334 goto error;
337 s->transpose = cbox_config_get_int(section, "transpose", 0);
338 s->title = g_strdup(cbox_config_get_string_with_default(section, "title", ""));
339 g_free(section);
340 cbox_command_target_init(&s->cmd_target, cbox_scene_process_cmd, s);
341 s->name = g_strdup(name);
342 return TRUE;
344 error:
345 g_free(section);
346 return FALSE;
349 gboolean cbox_scene_insert_layer(struct cbox_scene *scene, struct cbox_layer *layer, int pos, GError **error)
351 int i;
353 struct cbox_instrument *instrument = layer->instrument;
354 for (i = 0; i < instrument->aux_output_count; i++)
356 assert(!instrument->aux_outputs[i]);
357 if (instrument->aux_output_names[i])
359 instrument->aux_outputs[i] = cbox_scene_get_aux_bus(scene, instrument->aux_output_names[i], TRUE, error);
360 if (!instrument->aux_outputs[i])
361 return FALSE;
362 cbox_aux_bus_ref(instrument->aux_outputs[i]);
365 for (i = 0; i < scene->layer_count; i++)
367 if (scene->layers[i]->instrument == layer->instrument)
368 break;
370 if (i == scene->layer_count)
372 layer->instrument->scene = scene;
373 cbox_rt_array_insert(scene->rt, (void ***)&scene->instruments, &scene->instrument_count, -1, layer->instrument);
375 cbox_rt_array_insert(scene->rt, (void ***)&scene->layers, &scene->layer_count, pos, layer);
377 return TRUE;
380 gboolean cbox_scene_add_layer(struct cbox_scene *scene, struct cbox_layer *layer, GError **error)
382 return cbox_scene_insert_layer(scene, layer, scene->layer_count, error);
385 struct cbox_layer *cbox_scene_remove_layer(struct cbox_scene *scene, int pos)
387 struct cbox_layer *removed = scene->layers[pos];
388 cbox_rt_array_remove(scene->rt, (void ***)&scene->layers, &scene->layer_count, pos);
389 cbox_instrument_unref_aux_buses(removed->instrument);
391 return removed;
394 void cbox_scene_move_layer(struct cbox_scene *scene, int oldpos, int newpos)
396 if (oldpos == newpos)
397 return;
398 struct cbox_layer **layers = malloc(sizeof(struct cbox_layer *) * scene->layer_count);
399 for (int i = 0; i < scene->layer_count; i++)
401 int s;
402 if (i == newpos)
403 s = oldpos;
404 else
406 if (oldpos < newpos)
407 s = (i < oldpos || i > newpos) ? i : i + 1;
408 else
409 s = (i < newpos || i > oldpos) ? i : i - 1;
411 layers[i] = scene->layers[s];
413 free(cbox_rt_swap_pointers(scene->rt, (void **)&scene->layers, layers));
416 gboolean cbox_scene_remove_instrument(struct cbox_scene *scene, struct cbox_instrument *instrument)
418 assert(instrument->scene == scene);
419 int pos;
420 for (pos = 0; pos < scene->instrument_count; pos++)
422 if (scene->instruments[pos] == instrument)
424 cbox_rt_array_remove(scene->rt, (void ***)&scene->instruments, &scene->instrument_count, pos);
425 g_hash_table_remove(scene->instrument_hash, instrument->module->instance_name);
426 instrument->scene = NULL;
427 return TRUE;
430 return FALSE;
433 gboolean cbox_scene_insert_aux_bus(struct cbox_scene *scene, struct cbox_aux_bus *aux_bus)
435 struct cbox_aux_bus **aux_buses = malloc(sizeof(struct cbox_aux_bus *) * (scene->aux_bus_count + 1));
436 memcpy(aux_buses, scene->aux_buses, sizeof(struct cbox_aux_bus *) * (scene->aux_bus_count));
437 aux_buses[scene->aux_bus_count] = aux_bus;
438 free(cbox_rt_swap_pointers_and_update_count(scene->rt, (void **)&scene->aux_buses, aux_buses, &scene->aux_bus_count, scene->aux_bus_count + 1));
439 return TRUE;
442 void cbox_scene_remove_aux_bus(struct cbox_scene *scene, struct cbox_aux_bus *removed)
444 int pos = -1;
445 for (int i = 0; i < scene->aux_bus_count; i++)
447 if (scene->aux_buses[i] == removed)
449 pos = i;
450 break;
453 assert(pos != -1);
454 for (int i = 0; i < scene->instrument_count; i++)
455 cbox_instrument_disconnect_aux_bus(scene->instruments[i], removed);
457 struct cbox_aux_bus **aux_buses = malloc(sizeof(struct cbox_aux_bus *) * (scene->aux_bus_count - 1));
458 memcpy(aux_buses, scene->aux_buses, sizeof(struct cbox_aux_bus *) * pos);
459 memcpy(aux_buses + pos, scene->aux_buses + pos + 1, sizeof(struct cbox_aux_bus *) * (scene->aux_bus_count - pos - 1));
460 free(cbox_rt_swap_pointers_and_update_count(scene->rt, (void **)&scene->aux_buses, aux_buses, &scene->aux_bus_count, scene->aux_bus_count - 1));
463 struct cbox_aux_bus *cbox_scene_get_aux_bus(struct cbox_scene *scene, const char *name, int allow_load, GError **error)
465 for (int i = 0; i < scene->aux_bus_count; i++)
467 if (!strcmp(scene->aux_buses[i]->name, name))
469 return scene->aux_buses[i];
472 if (!allow_load)
474 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Aux bus not found: %s", name);
475 return FALSE;
477 struct cbox_aux_bus *bus = cbox_aux_bus_load(scene, name, scene->rt, error);
478 if (!bus)
479 return NULL;
480 return bus;
483 static int write_events_to_instrument_ports(struct cbox_scene *scene, struct cbox_midi_buffer *source)
485 uint32_t i;
487 for (i = 0; i < scene->instrument_count; i++)
488 cbox_midi_buffer_clear(&scene->instruments[i]->module->midi_input);
490 if (!source)
491 return 0;
493 uint32_t event_count = cbox_midi_buffer_get_count(source);
494 for (i = 0; i < event_count; i++)
496 const struct cbox_midi_event *event = cbox_midi_buffer_get_event(source, i);
498 // XXXKF ignore sysex for now
499 if (event->size >= 4)
500 continue;
502 for (int l = 0; l < scene->layer_count; l++)
504 struct cbox_layer *lp = scene->layers[l];
505 if (!lp->enabled)
506 continue;
507 uint8_t data[4] = {0, 0, 0, 0};
508 memcpy(data, event->data_inline, event->size);
509 if (data[0] < 0xF0) // per-channel messages
511 int cmd = data[0] >> 4;
512 // filter on MIDI channel
513 if (lp->in_channel >= 0 && lp->in_channel != (data[0] & 0x0F))
514 continue;
515 // force output channel
516 if (lp->out_channel >= 0)
517 data[0] = (data[0] & 0xF0) + (lp->out_channel & 0x0F);
518 if (cmd >= 8 && cmd <= 10)
520 if (cmd == 10 && lp->disable_aftertouch)
521 continue;
522 // note filter
523 if (data[1] < lp->low_note || data[1] > lp->high_note)
524 continue;
525 // transpose
526 int transpose = lp->transpose + (lp->ignore_scene_transpose ? 0 : scene->transpose);
527 if (transpose)
529 int note = data[1] + transpose;
530 if (note < 0 || note > 127)
531 continue;
532 data[1] = (uint8_t)note;
534 // fixed note
535 if (lp->fixed_note != -1)
537 data[1] = (uint8_t)lp->fixed_note;
540 else if (cmd == 11 && data[1] == 64 && lp->invert_sustain)
542 data[2] = 127 - data[2];
544 else if (lp->ignore_program_changes && cmd == 11 && (data[1] == 0 || data[1] == 32))
545 continue;
546 else if (cmd == 13 && lp->disable_aftertouch)
547 continue;
548 else if (cmd == 12 && lp->ignore_program_changes)
549 continue;
551 if (!cbox_midi_buffer_write_event(&lp->instrument->module->midi_input, event->time, data, event->size))
552 return -i;
553 if (lp->consume)
554 break;
558 return event_count;
561 void cbox_scene_render(struct cbox_scene *scene, uint32_t nframes, float *output_buffers[])
563 int n, i, j;
565 if (scene->rt && scene->rt->io)
567 struct cbox_io *io = scene->rt->io;
568 for (i = 0; i < io->io_env.input_count; i++)
570 if (IS_RECORDING_SOURCE_CONNECTED(scene->rec_mono_inputs[i]))
571 cbox_recording_source_push(&scene->rec_mono_inputs[i], (const float **)&io->input_buffers[i], nframes);
573 for (i = 0; i < io->io_env.input_count / 2; i++)
575 if (IS_RECORDING_SOURCE_CONNECTED(scene->rec_stereo_inputs[i]))
577 const float *buf[2] = { io->input_buffers[i * 2], io->input_buffers[i * 2 + 1] };
578 cbox_recording_source_push(&scene->rec_stereo_inputs[i], buf, nframes);
583 cbox_midi_buffer_clear(&scene->midibuf_total);
584 cbox_midi_merger_render(&scene->scene_input_merger);
586 write_events_to_instrument_ports(scene, &scene->midibuf_total);
588 for (n = 0; n < scene->aux_bus_count; n++)
590 for (i = 0; i < nframes; i ++)
592 scene->aux_buses[n]->input_bufs[0][i] = 0.f;
593 scene->aux_buses[n]->input_bufs[1][i] = 0.f;
597 for (n = 0; n < scene->instrument_count; n++)
599 struct cbox_instrument *instr = scene->instruments[n];
600 struct cbox_module *module = instr->module;
601 int event_count = instr->module->midi_input.count;
602 int cur_event = 0;
603 uint32_t highwatermark = 0;
604 cbox_sample_t channels[CBOX_MAX_AUDIO_PORTS][CBOX_BLOCK_SIZE];
605 cbox_sample_t *outputs[CBOX_MAX_AUDIO_PORTS];
606 for (i = 0; i < module->outputs; i++)
607 outputs[i] = channels[i];
609 for (i = 0; i < nframes; i += CBOX_BLOCK_SIZE)
611 if (i >= highwatermark)
613 while(cur_event < event_count)
615 const struct cbox_midi_event *event = cbox_midi_buffer_get_event(&module->midi_input, cur_event);
616 if (event)
618 if (event->time <= i)
619 (*module->process_event)(module, cbox_midi_event_get_data(event), event->size);
620 else
622 highwatermark = event->time;
623 break;
626 else
627 break;
629 cur_event++;
632 (*module->process_block)(module, NULL, outputs);
633 for (int o = 0; o < module->outputs / 2; o++)
635 struct cbox_instrument_output *oobj = &instr->outputs[o];
636 struct cbox_module *insert = oobj->insert;
637 float gain = oobj->gain;
638 if (IS_RECORDING_SOURCE_CONNECTED(oobj->rec_dry))
639 cbox_recording_source_push(&oobj->rec_dry, (const float **)(outputs + 2 * o), CBOX_BLOCK_SIZE);
640 if (insert && !insert->bypass)
641 (*insert->process_block)(insert, outputs + 2 * o, outputs + 2 * o);
642 if (IS_RECORDING_SOURCE_CONNECTED(oobj->rec_wet))
643 cbox_recording_source_push(&oobj->rec_wet, (const float **)(outputs + 2 * o), CBOX_BLOCK_SIZE);
644 float *leftbuf, *rightbuf;
645 if (o < module->aux_offset / 2)
647 int leftch = oobj->output_bus * 2;
648 int rightch = leftch + 1;
649 leftbuf = output_buffers[leftch];
650 rightbuf = output_buffers[rightch];
652 else
654 int bus = o - module->aux_offset / 2;
655 struct cbox_aux_bus *busobj = instr->aux_outputs[bus];
656 if (busobj == NULL)
657 continue;
658 leftbuf = busobj->input_bufs[0];
659 rightbuf = busobj->input_bufs[1];
661 for (j = 0; j < CBOX_BLOCK_SIZE; j++)
663 leftbuf[i + j] += gain * channels[2 * o][j];
664 rightbuf[i + j] += gain * channels[2 * o + 1][j];
668 while(cur_event < event_count)
670 const struct cbox_midi_event *event = cbox_midi_buffer_get_event(&module->midi_input, cur_event);
671 if (event)
673 (*module->process_event)(module, cbox_midi_event_get_data(event), event->size);
675 else
676 break;
678 cur_event++;
682 for (n = 0; n < scene->aux_bus_count; n++)
684 struct cbox_aux_bus *bus = scene->aux_buses[n];
685 float left[CBOX_BLOCK_SIZE], right[CBOX_BLOCK_SIZE];
686 float *outputs[2] = {left, right};
687 for (i = 0; i < nframes; i += CBOX_BLOCK_SIZE)
689 float *inputs[2];
690 inputs[0] = &bus->input_bufs[0][i];
691 inputs[1] = &bus->input_bufs[1][i];
692 bus->module->process_block(bus->module, inputs, outputs);
693 for (int j = 0; j < CBOX_BLOCK_SIZE; j++)
695 output_buffers[0][i + j] += left[j];
696 output_buffers[1][i + j] += right[j];
701 int output_count = scene->engine->io_env.output_count;
702 // XXXKF this assumes that the buffers are zeroed on start - which isn't true if there are multiple scenes
703 for (i = 0; i < output_count; i++)
705 if (IS_RECORDING_SOURCE_CONNECTED(scene->rec_mono_outputs[i]))
706 cbox_recording_source_push(&scene->rec_mono_outputs[i], (const float **)&output_buffers[i], nframes);
708 for (i = 0; i < output_count / 2; i++)
710 if (IS_RECORDING_SOURCE_CONNECTED(scene->rec_stereo_outputs[i]))
712 const float *buf[2] = { output_buffers[i * 2], output_buffers[i * 2 + 1] };
713 cbox_recording_source_push(&scene->rec_stereo_outputs[i], buf, nframes);
718 void cbox_scene_clear(struct cbox_scene *scene)
720 g_free(scene->name);
721 g_free(scene->title);
722 scene->name = g_strdup("");
723 scene->title = g_strdup("");
724 while(scene->layer_count > 0)
726 struct cbox_layer *layer = cbox_scene_remove_layer(scene, 0);
727 CBOX_DELETE(layer);
730 while(scene->aux_bus_count > 0)
731 CBOX_DELETE(scene->aux_buses[scene->aux_bus_count - 1]);
734 static struct cbox_instrument *create_instrument(struct cbox_scene *scene, struct cbox_module *module)
736 int auxes = (module->outputs - module->aux_offset) / 2;
738 struct cbox_instrument *instr = malloc(sizeof(struct cbox_instrument));
739 CBOX_OBJECT_HEADER_INIT(instr, cbox_instrument, CBOX_GET_DOCUMENT(scene));
740 instr->scene = scene;
741 instr->module = module;
742 instr->outputs = calloc(module->outputs / 2, sizeof(struct cbox_instrument_output));
743 instr->refcount = 0;
744 instr->aux_outputs = calloc(auxes, sizeof(struct cbox_aux_bus *));
745 instr->aux_output_names = calloc(auxes, sizeof(char *));
746 instr->aux_output_count = auxes;
748 for (int i = 0; i < module->outputs / 2; i ++)
749 cbox_instrument_output_init(&instr->outputs[i], scene, module->engine->io_env.buffer_size);
751 return instr;
754 struct cbox_instrument *cbox_scene_create_instrument(struct cbox_scene *scene, const char *instrument_name, const char *engine_name, GError **error)
756 gpointer value = g_hash_table_lookup(scene->instrument_hash, instrument_name);
757 if (value)
759 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Instrument already exists: '%s'", instrument_name);
760 return NULL;
763 struct cbox_document *doc = CBOX_GET_DOCUMENT(scene);
764 struct cbox_module_manifest *mptr = NULL;
765 struct cbox_instrument *instr = NULL;
766 struct cbox_module *module = NULL;
768 mptr = cbox_module_manifest_get_by_name(engine_name);
769 if (!mptr)
771 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "No engine called '%s'", engine_name);
772 return NULL;
775 module = cbox_module_manifest_create_module(mptr, NULL, doc, scene->rt, scene->engine, instrument_name, error);
776 if (!module)
778 cbox_force_error(error);
779 g_prefix_error(error, "Cannot create engine '%s' for instrument '%s': ", engine_name, instrument_name);
780 return NULL;
783 instr = create_instrument(scene, module);
785 cbox_command_target_init(&instr->cmd_target, cbox_instrument_process_cmd, instr);
786 g_hash_table_insert(scene->instrument_hash, g_strdup(instrument_name), instr);
787 CBOX_OBJECT_REGISTER(instr);
789 return instr;
792 struct cbox_instrument *cbox_scene_get_instrument_by_name(struct cbox_scene *scene, const char *name, gboolean load, GError **error)
794 struct cbox_module_manifest *mptr = NULL;
795 struct cbox_instrument *instr = NULL;
796 struct cbox_module *module = NULL;
797 gchar *instr_section = NULL;
798 gpointer value = g_hash_table_lookup(scene->instrument_hash, name);
799 const char *cv, *instr_engine;
800 struct cbox_document *doc = CBOX_GET_DOCUMENT(scene);
801 assert(scene);
803 if (value)
804 return value;
805 if (!load)
806 return NULL;
808 instr_section = g_strdup_printf("instrument:%s", name);
810 if (!cbox_config_has_section(instr_section))
812 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "No config section for instrument '%s'", name);
813 goto error;
816 instr_engine = cbox_config_get_string(instr_section, "engine");
817 if (!instr_engine)
819 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Engine not specified in instrument '%s'", name);
820 goto error;
823 mptr = cbox_module_manifest_get_by_name(instr_engine);
824 if (!mptr)
826 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "No engine called '%s'", instr_engine);
827 goto error;
830 // cbox_module_manifest_dump(mptr);
832 module = cbox_module_manifest_create_module(mptr, instr_section, doc, scene->rt, scene->engine, name, error);
833 if (!module)
835 cbox_force_error(error);
836 g_prefix_error(error, "Cannot create engine '%s' for instrument '%s': ", instr_engine, name);
837 goto error;
840 instr = create_instrument(scene, module);
842 for (int i = 0; i < module->outputs / 2; i ++)
844 struct cbox_instrument_output *oobj = instr->outputs + i;
846 gchar *key = i == 0 ? g_strdup("output_bus") : g_strdup_printf("output%d_bus", 1 + i);
847 oobj->output_bus = cbox_config_get_int(instr_section, key, 1) - 1;
848 g_free(key);
849 key = i == 0 ? g_strdup("gain") : g_strdup_printf("gain%d", 1 + i);
850 oobj->gain = cbox_config_get_gain_db(instr_section, key, 0);
851 g_free(key);
853 key = i == 0 ? g_strdup("insert") : g_strdup_printf("insert%d", 1 + i);
854 cv = cbox_config_get_string(instr_section, key);
855 g_free(key);
857 if (cv)
859 oobj->insert = cbox_module_new_from_fx_preset(cv, CBOX_GET_DOCUMENT(scene), module->rt, scene->engine, error);
860 if (!oobj->insert)
862 cbox_force_error(error);
863 g_prefix_error(error, "Cannot instantiate effect preset '%s' for instrument '%s': ", cv, name);
868 for (int i = 0; i < instr->aux_output_count; i++)
870 instr->aux_outputs[i] = NULL;
872 gchar *key = g_strdup_printf("aux%d", 1 + i);
873 gchar *value = cbox_config_get_string(instr_section, key);
874 instr->aux_output_names[i] = value ? g_strdup(value) : NULL;
875 g_free(key);
878 cbox_command_target_init(&instr->cmd_target, cbox_instrument_process_cmd, instr);
880 free(instr_section);
882 g_hash_table_insert(scene->instrument_hash, g_strdup(name), instr);
883 CBOX_OBJECT_REGISTER(instr);
885 // cbox_recording_source_attach(&instr->outputs[0].rec_dry, cbox_recorder_new_stream("output.wav"));
887 return instr;
889 error:
890 free(instr_section);
891 return NULL;
894 static struct cbox_recording_source *create_rec_sources(struct cbox_scene *scene, int buffer_size, int count, int channels)
896 struct cbox_recording_source *s = malloc(sizeof(struct cbox_recording_source) * count);
897 for (int i = 0; i < count; i++)
898 cbox_recording_source_init(&s[i], scene, buffer_size, channels);
899 return s;
902 static void destroy_rec_sources(struct cbox_recording_source *s, int count)
904 for (int i = 0; i < count; i++)
905 cbox_recording_source_uninit(&s[i]);
906 free(s);
909 struct cbox_scene *cbox_scene_new(struct cbox_document *document, struct cbox_engine *engine)
911 struct cbox_scene *s = malloc(sizeof(struct cbox_scene));
912 if (!s)
913 return NULL;
915 CBOX_OBJECT_HEADER_INIT(s, cbox_scene, document);
916 s->engine = engine;
917 s->rt = engine ? engine->rt : NULL;
918 s->instrument_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
919 s->name = g_strdup("");
920 s->title = g_strdup("");
921 s->layers = NULL;
922 s->aux_buses = NULL;
923 s->instruments = NULL;
924 s->layer_count = 0;
925 s->instrument_count = 0;
926 s->aux_bus_count = 0;
927 cbox_command_target_init(&s->cmd_target, cbox_scene_process_cmd, s);
928 s->transpose = 0;
929 s->connected_inputs = NULL;
930 s->connected_input_count = 0;
931 s->enable_default_song_input = TRUE;
932 s->enable_default_external_input = TRUE;
934 cbox_midi_buffer_init(&s->midibuf_total);
935 cbox_midi_merger_init(&s->scene_input_merger, &s->midibuf_total);
937 int buffer_size = engine->io_env.buffer_size;
938 s->rec_mono_inputs = create_rec_sources(s, buffer_size, engine->io_env.input_count, 1);
939 s->rec_stereo_inputs = create_rec_sources(s, buffer_size, engine->io_env.input_count / 2, 2);
940 s->rec_mono_outputs = create_rec_sources(s, buffer_size, engine->io_env.output_count, 1);
941 s->rec_stereo_outputs = create_rec_sources(s, buffer_size, engine->io_env.output_count / 2, 2);
943 CBOX_OBJECT_REGISTER(s);
945 cbox_engine_add_scene(s->engine, s);
946 cbox_scene_update_connected_inputs(s);
947 return s;
950 void cbox_scene_update_connected_inputs(struct cbox_scene *scene)
952 if (!scene->rt || !scene->rt->io)
953 return;
955 // This is called when a MIDI Input port has been created, connected/disconnected
956 // or is about to be removed (and then the removing flag will be set)
957 for (int i = 0; i < scene->connected_input_count; )
959 struct cbox_midi_input *input = scene->connected_inputs[i];
960 if (input->removing || !cbox_uuid_equal(&input->output, &scene->_obj_hdr.instance_uuid))
962 cbox_midi_merger_disconnect(&scene->scene_input_merger, &input->buffer, scene->rt);
963 cbox_rt_array_remove(scene->rt, (void ***)&scene->connected_inputs, &scene->connected_input_count, i);
965 else
966 i++;
968 for (GSList *p = scene->rt->io->midi_inputs; p; p = p->next)
970 struct cbox_midi_input *input = p->data;
971 if (cbox_uuid_equal(&input->output, &scene->_obj_hdr.instance_uuid))
973 gboolean found = FALSE;
974 for (int i = 0; i < scene->connected_input_count; i++)
976 if (scene->connected_inputs[i] == input)
978 found = TRUE;
979 break;
982 if (!found)
984 cbox_midi_merger_connect(&scene->scene_input_merger, &input->buffer, scene->rt);
985 cbox_rt_array_insert(scene->rt, (void ***)&scene->connected_inputs, &scene->connected_input_count, -1, input);
989 if (scene->enable_default_song_input)
991 cbox_midi_merger_connect(&scene->scene_input_merger, &scene->engine->midibuf_aux, scene->rt);
992 cbox_midi_merger_connect(&scene->scene_input_merger, &scene->engine->midibuf_song, scene->rt);
994 else
996 cbox_midi_merger_disconnect(&scene->scene_input_merger, &scene->engine->midibuf_aux, scene->rt);
997 cbox_midi_merger_disconnect(&scene->scene_input_merger, &scene->engine->midibuf_song, scene->rt);
1000 if (scene->enable_default_external_input)
1001 cbox_midi_merger_connect(&scene->scene_input_merger, &scene->engine->midibuf_jack, scene->rt);
1002 else
1003 cbox_midi_merger_disconnect(&scene->scene_input_merger, &scene->engine->midibuf_jack, scene->rt);
1007 static void cbox_scene_destroyfunc(struct cbox_objhdr *objhdr)
1009 struct cbox_scene *scene = CBOX_H2O(objhdr);
1010 cbox_midi_merger_disconnect(&scene->scene_input_merger, &scene->engine->midibuf_aux, scene->rt);
1011 cbox_midi_merger_disconnect(&scene->scene_input_merger, &scene->engine->midibuf_jack, scene->rt);
1012 cbox_midi_merger_disconnect(&scene->scene_input_merger, &scene->engine->midibuf_song, scene->rt);
1013 cbox_engine_remove_scene(scene->engine, scene);
1014 cbox_scene_clear(scene);
1015 g_free(scene->name);
1016 g_free(scene->title);
1017 assert(scene->instrument_count == 0);
1018 free(scene->layers);
1019 free(scene->aux_buses);
1020 free(scene->instruments);
1021 g_hash_table_destroy(scene->instrument_hash);
1022 free(scene->connected_inputs);
1024 destroy_rec_sources(scene->rec_mono_inputs, scene->engine->io_env.input_count);
1025 destroy_rec_sources(scene->rec_stereo_inputs, scene->engine->io_env.input_count / 2);
1026 destroy_rec_sources(scene->rec_mono_outputs, scene->engine->io_env.output_count);
1027 destroy_rec_sources(scene->rec_stereo_outputs, scene->engine->io_env.output_count / 2);
1029 cbox_midi_merger_close(&scene->scene_input_merger);
1030 free(scene);