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/>.
20 #include "config-api.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
);
49 struct cbox_layer
*layer
= NULL
;
54 layer
= cbox_layer_new_from_config(s
, CBOX_ARG_S(cmd
, 1), error
);
57 layer
= cbox_layer_new_with_instrument(s
, CBOX_ARG_S(cmd
, 1), error
);
61 struct cbox_instrument
*instr
= cbox_scene_create_instrument(s
, CBOX_ARG_S(cmd
, 1), CBOX_ARG_S(cmd
, 2), error
);
64 layer
= cbox_layer_new_with_instrument(s
, CBOX_ARG_S(cmd
, 1), error
);
73 if (!cbox_scene_insert_layer(s
, layer
, pos
, error
))
80 if (!cbox_execute_on(fb
, NULL
, "/uuid", "o", error
, layer
))
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
;
93 if (!strcmp(cmd
->command
, "/transpose") && !strcmp(cmd
->arg_types
, "i"))
95 s
->transpose
= CBOX_ARG_I(cmd
, 0);
98 else if (!strcmp(cmd
->command
, "/load") && !strcmp(cmd
->arg_types
, "s"))
100 if (!cbox_scene_load(s
, CBOX_ARG_S(cmd
, 0), error
))
104 else if (!strcmp(cmd
->command
, "/clear") && !strcmp(cmd
->arg_types
, ""))
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
);
130 pos
= s
->layer_count
- 1;
133 struct cbox_layer
*layer
= cbox_scene_remove_layer(s
, pos
);
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
);
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
);
151 cbox_scene_move_layer(s
, oldpos
- 1, newpos
- 1);
154 else if (cbox_parse_path_part_int(cmd
, "/layer/", &subcommand
, &index
, 1, s
->layer_count
, error
))
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
))
164 struct cbox_aux_bus
*aux
= cbox_scene_get_aux_bus(s
, subobj
, FALSE
, error
);
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
, '/');
175 pos
= strchr(obj
, '/');
178 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Invalid instrument path '%s'", cmd
->command
);
183 gchar
*name
= g_strndup(obj
, len
);
184 struct cbox_instrument
*instr
= cbox_scene_get_instrument_by_name(s
, name
, FALSE
, error
);
189 return cbox_execute_sub(&instr
->cmd_target
, fb
, cmd
, pos
, error
);
193 cbox_force_error(error
);
194 g_prefix_error(error
, "Cannot access instrument '%s': ", name
);
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
);
207 if (!cbox_execute_on(fb
, NULL
, "/uuid", "o", error
, bus
))
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
);
221 else if (!strcmp(cmd
->command
, "/status") && !strcmp(cmd
->arg_types
, ""))
223 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
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
))
234 for (int i
= 0; i
< s
->layer_count
; i
++)
236 if (!cbox_execute_on(fb
, NULL
, "/layer", "o", error
, s
->layers
[i
]))
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
]))
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
]))
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
);
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
);
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);
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);
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
;
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
);
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);
320 struct cbox_layer
*l
= NULL
;
322 gchar
*sn
= g_strdup_printf("layer%d", i
);
323 cv
= cbox_config_get_string(section
, sn
);
329 l
= cbox_layer_new_from_config(s
, cv
, error
);
333 if (!cbox_scene_add_layer(s
, l
, error
))
337 s
->transpose
= cbox_config_get_int(section
, "transpose", 0);
338 s
->title
= g_strdup(cbox_config_get_string_with_default(section
, "title", ""));
340 cbox_command_target_init(&s
->cmd_target
, cbox_scene_process_cmd
, s
);
341 s
->name
= g_strdup(name
);
349 gboolean
cbox_scene_insert_layer(struct cbox_scene
*scene
, struct cbox_layer
*layer
, int pos
, GError
**error
)
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
])
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
)
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
);
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
);
394 void cbox_scene_move_layer(struct cbox_scene
*scene
, int oldpos
, int newpos
)
396 if (oldpos
== newpos
)
398 struct cbox_layer
**layers
= malloc(sizeof(struct cbox_layer
*) * scene
->layer_count
);
399 for (int i
= 0; i
< scene
->layer_count
; i
++)
407 s
= (i
< oldpos
|| i
> newpos
) ? i
: i
+ 1;
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
);
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
;
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));
442 void cbox_scene_remove_aux_bus(struct cbox_scene
*scene
, struct cbox_aux_bus
*removed
)
445 for (int i
= 0; i
< scene
->aux_bus_count
; i
++)
447 if (scene
->aux_buses
[i
] == removed
)
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
];
474 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Aux bus not found: %s", name
);
477 struct cbox_aux_bus
*bus
= cbox_aux_bus_load(scene
, name
, scene
->rt
, error
);
483 static int write_events_to_instrument_ports(struct cbox_scene
*scene
, struct cbox_midi_buffer
*source
)
487 for (i
= 0; i
< scene
->instrument_count
; i
++)
488 cbox_midi_buffer_clear(&scene
->instruments
[i
]->module
->midi_input
);
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)
502 for (int l
= 0; l
< scene
->layer_count
; l
++)
504 struct cbox_layer
*lp
= scene
->layers
[l
];
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))
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
)
523 if (data
[1] < lp
->low_note
|| data
[1] > lp
->high_note
)
526 int transpose
= lp
->transpose
+ (lp
->ignore_scene_transpose
? 0 : scene
->transpose
);
529 int note
= data
[1] + transpose
;
530 if (note
< 0 || note
> 127)
532 data
[1] = (uint8_t)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))
546 else if (cmd
== 13 && lp
->disable_aftertouch
)
548 else if (cmd
== 12 && lp
->ignore_program_changes
)
551 if (!cbox_midi_buffer_write_event(&lp
->instrument
->module
->midi_input
, event
->time
, data
, event
->size
))
561 void cbox_scene_render(struct cbox_scene
*scene
, uint32_t nframes
, float *output_buffers
[])
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
;
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
);
618 if (event
->time
<= i
)
619 (*module
->process_event
)(module
, cbox_midi_event_get_data(event
), event
->size
);
622 highwatermark
= event
->time
;
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
];
654 int bus
= o
- module
->aux_offset
/ 2;
655 struct cbox_aux_bus
*busobj
= instr
->aux_outputs
[bus
];
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
);
673 (*module
->process_event
)(module
, cbox_midi_event_get_data(event
), event
->size
);
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
)
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
)
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);
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
));
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
);
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
);
759 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Instrument already exists: '%s'", instrument_name
);
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
);
771 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "No engine called '%s'", engine_name
);
775 module
= cbox_module_manifest_create_module(mptr
, NULL
, doc
, scene
->rt
, scene
->engine
, instrument_name
, error
);
778 cbox_force_error(error
);
779 g_prefix_error(error
, "Cannot create engine '%s' for instrument '%s': ", engine_name
, instrument_name
);
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
);
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
);
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
);
816 instr_engine
= cbox_config_get_string(instr_section
, "engine");
819 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Engine not specified in instrument '%s'", name
);
823 mptr
= cbox_module_manifest_get_by_name(instr_engine
);
826 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "No engine called '%s'", instr_engine
);
830 // cbox_module_manifest_dump(mptr);
832 module
= cbox_module_manifest_create_module(mptr
, instr_section
, doc
, scene
->rt
, scene
->engine
, name
, error
);
835 cbox_force_error(error
);
836 g_prefix_error(error
, "Cannot create engine '%s' for instrument '%s': ", instr_engine
, name
);
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;
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);
853 key
= i
== 0 ? g_strdup("insert") : g_strdup_printf("insert%d", 1 + i
);
854 cv
= cbox_config_get_string(instr_section
, key
);
859 oobj
->insert
= cbox_module_new_from_fx_preset(cv
, CBOX_GET_DOCUMENT(scene
), module
->rt
, scene
->engine
, error
);
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
;
878 cbox_command_target_init(&instr
->cmd_target
, cbox_instrument_process_cmd
, instr
);
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"));
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
);
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
]);
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
));
915 CBOX_OBJECT_HEADER_INIT(s
, cbox_scene
, document
);
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("");
923 s
->instruments
= NULL
;
925 s
->instrument_count
= 0;
926 s
->aux_bus_count
= 0;
927 cbox_command_target_init(&s
->cmd_target
, cbox_scene_process_cmd
, s
);
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
);
950 void cbox_scene_update_connected_inputs(struct cbox_scene
*scene
)
952 if (!scene
->rt
|| !scene
->rt
->io
)
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
);
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
)
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
);
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
);
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
);