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"
36 CBOX_CLASS_DEFINITION_ROOT(cbox_scene
)
38 static gboolean
cbox_scene_addlayercmd(struct cbox_scene
*s
, struct cbox_command_target
*fb
, struct cbox_osc_command
*cmd
, int cmd_type
, GError
**error
)
40 int pos
= CBOX_ARG_I(cmd
, 0);
41 if (pos
< 0 || pos
> 1 + s
->layer_count
)
43 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
);
50 struct cbox_layer
*layer
= NULL
;
55 layer
= cbox_layer_new_from_config(s
, CBOX_ARG_S(cmd
, 1), error
);
58 layer
= cbox_layer_new_with_instrument(s
, CBOX_ARG_S(cmd
, 1), error
);
62 struct cbox_instrument
*instr
= cbox_scene_create_instrument(s
, CBOX_ARG_S(cmd
, 1), CBOX_ARG_S(cmd
, 2), error
);
65 layer
= cbox_layer_new_with_instrument(s
, CBOX_ARG_S(cmd
, 1), error
);
74 if (!cbox_scene_insert_layer(s
, layer
, pos
, error
))
81 if (!cbox_execute_on(fb
, NULL
, "/uuid", "o", error
, layer
))
87 static gboolean
cbox_scene_process_cmd(struct cbox_command_target
*ct
, struct cbox_command_target
*fb
, struct cbox_osc_command
*cmd
, GError
**error
)
89 struct cbox_scene
*s
= ct
->user_data
;
90 const char *subcommand
= NULL
;
94 if (!strcmp(cmd
->command
, "/transpose") && !strcmp(cmd
->arg_types
, "i"))
96 s
->transpose
= CBOX_ARG_I(cmd
, 0);
99 else if (!strcmp(cmd
->command
, "/load") && !strcmp(cmd
->arg_types
, "s"))
101 if (!cbox_scene_load(s
, CBOX_ARG_S(cmd
, 0), error
))
105 else if (!strcmp(cmd
->command
, "/clear") && !strcmp(cmd
->arg_types
, ""))
110 else if (!strcmp(cmd
->command
, "/add_layer") && !strcmp(cmd
->arg_types
, "is"))
112 return cbox_scene_addlayercmd(s
, fb
, cmd
, 1, error
);
114 else if (!strcmp(cmd
->command
, "/add_instrument_layer") && !strcmp(cmd
->arg_types
, "is"))
116 return cbox_scene_addlayercmd(s
, fb
, cmd
, 2, error
);
118 else if (!strcmp(cmd
->command
, "/add_new_instrument_layer") && !strcmp(cmd
->arg_types
, "iss"))
120 return cbox_scene_addlayercmd(s
, fb
, cmd
, 3, error
);
122 else if (!strcmp(cmd
->command
, "/delete_layer") && !strcmp(cmd
->arg_types
, "i"))
124 int pos
= CBOX_ARG_I(cmd
, 0);
125 if (pos
< 0 || pos
> s
->layer_count
)
127 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
);
131 pos
= s
->layer_count
- 1;
134 struct cbox_layer
*layer
= cbox_scene_remove_layer(s
, pos
);
138 else if (!strcmp(cmd
->command
, "/move_layer") && !strcmp(cmd
->arg_types
, "ii"))
140 int oldpos
= CBOX_ARG_I(cmd
, 0);
141 if (oldpos
< 1 || oldpos
> s
->layer_count
)
143 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Invalid position %d (valid are 1..%d)", oldpos
, s
->layer_count
);
146 int newpos
= CBOX_ARG_I(cmd
, 1);
147 if (newpos
< 1 || newpos
> s
->layer_count
)
149 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Invalid position %d (valid are 1..%d)", newpos
, s
->layer_count
);
152 cbox_scene_move_layer(s
, oldpos
- 1, newpos
- 1);
155 else if (cbox_parse_path_part_int(cmd
, "/layer/", &subcommand
, &index
, 1, s
->layer_count
, error
))
159 return cbox_execute_sub(&s
->layers
[index
- 1]->cmd_target
, fb
, cmd
, subcommand
, error
);
161 else if (cbox_parse_path_part_str(cmd
, "/aux/", &subcommand
, &subobj
, error
))
165 struct cbox_aux_bus
*aux
= cbox_scene_get_aux_bus(s
, subobj
, FALSE
, error
);
169 return cbox_execute_sub(&aux
->cmd_target
, fb
, cmd
, subcommand
, error
);
171 else if (!strncmp(cmd
->command
, "/instr/", 7))
173 const char *obj
= &cmd
->command
[1];
174 const char *pos
= strchr(obj
, '/');
176 pos
= strchr(obj
, '/');
179 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Invalid instrument path '%s'", cmd
->command
);
184 gchar
*name
= g_strndup(obj
, len
);
185 struct cbox_instrument
*instr
= cbox_scene_get_instrument_by_name(s
, name
, FALSE
, error
);
190 return cbox_execute_sub(&instr
->cmd_target
, fb
, cmd
, pos
, error
);
194 cbox_force_error(error
);
195 g_prefix_error(error
, "Cannot access instrument '%s': ", name
);
201 else if (!strcmp(cmd
->command
, "/load_aux") && !strcmp(cmd
->arg_types
, "s"))
203 struct cbox_aux_bus
*bus
= cbox_scene_get_aux_bus(s
, CBOX_ARG_S(cmd
, 0), TRUE
, error
);
208 if (!cbox_execute_on(fb
, NULL
, "/uuid", "o", error
, bus
))
213 else if (!strcmp(cmd
->command
, "/delete_aux") && !strcmp(cmd
->arg_types
, "s"))
215 const char *name
= CBOX_ARG_S(cmd
, 0);
216 struct cbox_aux_bus
*aux
= cbox_scene_get_aux_bus(s
, name
, FALSE
, error
);
222 else if (!strcmp(cmd
->command
, "/status") && !strcmp(cmd
->arg_types
, ""))
224 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
227 if (!cbox_execute_on(fb
, NULL
, "/name", "s", error
, s
->name
) ||
228 !cbox_execute_on(fb
, NULL
, "/title", "s", error
, s
->title
) ||
229 !cbox_execute_on(fb
, NULL
, "/transpose", "i", error
, s
->transpose
) ||
230 !cbox_execute_on(fb
, NULL
, "/enable_default_song_input", "i", error
, s
->enable_default_song_input
) ||
231 !cbox_execute_on(fb
, NULL
, "/enable_default_external_input", "i", error
, s
->enable_default_external_input
) ||
232 !CBOX_OBJECT_DEFAULT_STATUS(s
, fb
, error
))
235 for (int i
= 0; i
< s
->layer_count
; i
++)
237 if (!cbox_execute_on(fb
, NULL
, "/layer", "o", error
, s
->layers
[i
]))
240 for (int i
= 0; i
< s
->instrument_count
; i
++)
242 if (!cbox_execute_on(fb
, NULL
, "/instrument", "sso", error
, s
->instruments
[i
]->module
->instance_name
, s
->instruments
[i
]->module
->engine_name
, s
->instruments
[i
]))
245 for (int i
= 0; i
< s
->aux_bus_count
; i
++)
247 if (!cbox_execute_on(fb
, NULL
, "/aux", "so", error
, s
->aux_buses
[i
]->name
, s
->aux_buses
[i
]))
253 if (!strcmp(cmd
->command
, "/send_event") && (!strcmp(cmd
->arg_types
, "iii") || !strcmp(cmd
->arg_types
, "ii") || !strcmp(cmd
->arg_types
, "i")))
255 int mcmd
= CBOX_ARG_I(cmd
, 0);
256 int arg1
= 0, arg2
= 0;
257 if (cmd
->arg_types
[1] == 'i')
259 arg1
= CBOX_ARG_I(cmd
, 1);
260 if (cmd
->arg_types
[2] == 'i')
261 arg2
= CBOX_ARG_I(cmd
, 2);
263 struct cbox_midi_buffer buf
;
264 cbox_midi_buffer_init(&buf
);
265 cbox_midi_buffer_write_inline(&buf
, 0, mcmd
, arg1
, arg2
);
266 cbox_midi_merger_push(&s
->scene_input_merger
, &buf
, s
->rt
);
270 if (!strcmp(cmd
->command
, "/play_note") && !strcmp(cmd
->arg_types
, "iii"))
272 int channel
= CBOX_ARG_I(cmd
, 0);
273 int note
= CBOX_ARG_I(cmd
, 1);
274 int velocity
= CBOX_ARG_I(cmd
, 2);
275 struct cbox_midi_buffer buf
;
276 cbox_midi_buffer_init(&buf
);
277 cbox_midi_buffer_write_inline(&buf
, 0, 0x90 + ((channel
- 1) & 15), note
& 127, velocity
& 127);
278 cbox_midi_buffer_write_inline(&buf
, 1, 0x80 + ((channel
- 1) & 15), note
& 127, velocity
& 127);
279 cbox_midi_merger_push(&s
->scene_input_merger
, &buf
, s
->rt
);
283 if (!strcmp(cmd
->command
, "/play_pattern") && !strcmp(cmd
->arg_types
, "sfi"))
285 struct cbox_midi_pattern
*pattern
= (struct cbox_midi_pattern
*)CBOX_ARG_O(cmd
, 0, s
, cbox_midi_pattern
, error
);
289 struct cbox_adhoc_pattern
*ap
= cbox_adhoc_pattern_new(s
->engine
, CBOX_ARG_I(cmd
, 2), pattern
);
290 ap
->master
->tempo
= ap
->master
->new_tempo
= CBOX_ARG_F(cmd
, 1);
291 cbox_scene_play_adhoc_pattern(s
, ap
);
295 if (!strcmp(cmd
->command
, "/enable_default_song_input") && !strcmp(cmd
->arg_types
, "i"))
297 s
->enable_default_song_input
= CBOX_ARG_I(cmd
, 0);
301 if (!strcmp(cmd
->command
, "/enable_default_external_input") && !strcmp(cmd
->arg_types
, "i"))
303 s
->enable_default_external_input
= CBOX_ARG_I(cmd
, 0);
307 return cbox_object_default_process_cmd(ct
, fb
, cmd
, error
);
310 gboolean
cbox_scene_load(struct cbox_scene
*s
, const char *name
, GError
**error
)
312 const char *cv
= NULL
;
314 gchar
*section
= g_strdup_printf("scene:%s", name
);
316 if (!cbox_config_has_section(section
))
318 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "No config section for scene '%s'", name
);
324 assert(s
->layers
== NULL
);
325 assert(s
->instruments
== NULL
);
326 assert(s
->aux_buses
== NULL
);
327 assert(s
->layer_count
== 0);
328 assert(s
->instrument_count
== 0);
329 assert(s
->aux_bus_count
== 0);
333 struct cbox_layer
*l
= NULL
;
335 gchar
*sn
= g_strdup_printf("layer%d", i
);
336 cv
= cbox_config_get_string(section
, sn
);
342 l
= cbox_layer_new_from_config(s
, cv
, error
);
346 if (!cbox_scene_add_layer(s
, l
, error
))
350 s
->transpose
= cbox_config_get_int(section
, "transpose", 0);
351 s
->title
= g_strdup(cbox_config_get_string_with_default(section
, "title", ""));
353 cbox_command_target_init(&s
->cmd_target
, cbox_scene_process_cmd
, s
);
354 s
->name
= g_strdup(name
);
362 gboolean
cbox_scene_insert_layer(struct cbox_scene
*scene
, struct cbox_layer
*layer
, int pos
, GError
**error
)
366 struct cbox_instrument
*instrument
= layer
->instrument
;
367 for (i
= 0; i
< instrument
->aux_output_count
; i
++)
369 assert(!instrument
->aux_outputs
[i
]);
370 if (instrument
->aux_output_names
[i
])
372 instrument
->aux_outputs
[i
] = cbox_scene_get_aux_bus(scene
, instrument
->aux_output_names
[i
], TRUE
, error
);
373 if (!instrument
->aux_outputs
[i
])
375 cbox_aux_bus_ref(instrument
->aux_outputs
[i
]);
378 for (i
= 0; i
< scene
->layer_count
; i
++)
380 if (scene
->layers
[i
]->instrument
== layer
->instrument
)
383 if (i
== scene
->layer_count
)
385 layer
->instrument
->scene
= scene
;
386 cbox_rt_array_insert(scene
->rt
, (void ***)&scene
->instruments
, &scene
->instrument_count
, -1, layer
->instrument
);
388 cbox_rt_array_insert(scene
->rt
, (void ***)&scene
->layers
, &scene
->layer_count
, pos
, layer
);
393 gboolean
cbox_scene_add_layer(struct cbox_scene
*scene
, struct cbox_layer
*layer
, GError
**error
)
395 return cbox_scene_insert_layer(scene
, layer
, scene
->layer_count
, error
);
398 struct cbox_layer
*cbox_scene_remove_layer(struct cbox_scene
*scene
, int pos
)
400 struct cbox_layer
*removed
= scene
->layers
[pos
];
401 cbox_rt_array_remove(scene
->rt
, (void ***)&scene
->layers
, &scene
->layer_count
, pos
);
402 cbox_instrument_unref_aux_buses(removed
->instrument
);
407 void cbox_scene_move_layer(struct cbox_scene
*scene
, int oldpos
, int newpos
)
409 if (oldpos
== newpos
)
411 struct cbox_layer
**layers
= malloc(sizeof(struct cbox_layer
*) * scene
->layer_count
);
412 for (int i
= 0; i
< scene
->layer_count
; i
++)
420 s
= (i
< oldpos
|| i
> newpos
) ? i
: i
+ 1;
422 s
= (i
< newpos
|| i
> oldpos
) ? i
: i
- 1;
424 layers
[i
] = scene
->layers
[s
];
426 free(cbox_rt_swap_pointers(scene
->rt
, (void **)&scene
->layers
, layers
));
429 gboolean
cbox_scene_remove_instrument(struct cbox_scene
*scene
, struct cbox_instrument
*instrument
)
431 assert(instrument
->scene
== scene
);
433 for (pos
= 0; pos
< scene
->instrument_count
; pos
++)
435 if (scene
->instruments
[pos
] == instrument
)
437 cbox_rt_array_remove(scene
->rt
, (void ***)&scene
->instruments
, &scene
->instrument_count
, pos
);
438 g_hash_table_remove(scene
->instrument_hash
, instrument
->module
->instance_name
);
439 instrument
->scene
= NULL
;
446 gboolean
cbox_scene_insert_aux_bus(struct cbox_scene
*scene
, struct cbox_aux_bus
*aux_bus
)
448 struct cbox_aux_bus
**aux_buses
= malloc(sizeof(struct cbox_aux_bus
*) * (scene
->aux_bus_count
+ 1));
449 memcpy(aux_buses
, scene
->aux_buses
, sizeof(struct cbox_aux_bus
*) * (scene
->aux_bus_count
));
450 aux_buses
[scene
->aux_bus_count
] = aux_bus
;
451 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));
455 void cbox_scene_remove_aux_bus(struct cbox_scene
*scene
, struct cbox_aux_bus
*removed
)
458 for (int i
= 0; i
< scene
->aux_bus_count
; i
++)
460 if (scene
->aux_buses
[i
] == removed
)
467 for (int i
= 0; i
< scene
->instrument_count
; i
++)
468 cbox_instrument_disconnect_aux_bus(scene
->instruments
[i
], removed
);
470 struct cbox_aux_bus
**aux_buses
= malloc(sizeof(struct cbox_aux_bus
*) * (scene
->aux_bus_count
- 1));
471 memcpy(aux_buses
, scene
->aux_buses
, sizeof(struct cbox_aux_bus
*) * pos
);
472 memcpy(aux_buses
+ pos
, scene
->aux_buses
+ pos
+ 1, sizeof(struct cbox_aux_bus
*) * (scene
->aux_bus_count
- pos
- 1));
473 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));
476 struct cbox_aux_bus
*cbox_scene_get_aux_bus(struct cbox_scene
*scene
, const char *name
, int allow_load
, GError
**error
)
478 for (int i
= 0; i
< scene
->aux_bus_count
; i
++)
480 if (!strcmp(scene
->aux_buses
[i
]->name
, name
))
482 return scene
->aux_buses
[i
];
487 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Aux bus not found: %s", name
);
490 struct cbox_aux_bus
*bus
= cbox_aux_bus_load(scene
, name
, scene
->rt
, error
);
496 static int write_events_to_instrument_ports(struct cbox_scene
*scene
, struct cbox_midi_buffer
*source
)
500 for (i
= 0; i
< scene
->instrument_count
; i
++)
501 cbox_midi_buffer_clear(&scene
->instruments
[i
]->module
->midi_input
);
506 uint32_t event_count
= cbox_midi_buffer_get_count(source
);
507 for (i
= 0; i
< event_count
; i
++)
509 const struct cbox_midi_event
*event
= cbox_midi_buffer_get_event(source
, i
);
511 // XXXKF ignore sysex for now
512 if (event
->size
>= 4)
515 for (int l
= 0; l
< scene
->layer_count
; l
++)
517 struct cbox_layer
*lp
= scene
->layers
[l
];
520 uint8_t data
[4] = {0, 0, 0, 0};
521 memcpy(data
, event
->data_inline
, event
->size
);
522 if (data
[0] < 0xF0) // per-channel messages
524 int cmd
= data
[0] >> 4;
525 // filter on MIDI channel
526 if (lp
->in_channel
>= 0 && lp
->in_channel
!= (data
[0] & 0x0F))
528 // force output channel
529 if (lp
->out_channel
>= 0)
530 data
[0] = (data
[0] & 0xF0) + (lp
->out_channel
& 0x0F);
531 if (cmd
>= 8 && cmd
<= 10)
533 if (cmd
== 10 && lp
->disable_aftertouch
)
536 if (data
[1] < lp
->low_note
|| data
[1] > lp
->high_note
)
539 int transpose
= lp
->transpose
+ (lp
->ignore_scene_transpose
? 0 : scene
->transpose
);
542 int note
= data
[1] + transpose
;
543 if (note
< 0 || note
> 127)
545 data
[1] = (uint8_t)note
;
548 if (lp
->fixed_note
!= -1)
550 data
[1] = (uint8_t)lp
->fixed_note
;
553 else if (cmd
== 11 && data
[1] == 64 && lp
->invert_sustain
)
555 data
[2] = 127 - data
[2];
557 else if (lp
->ignore_program_changes
&& cmd
== 11 && (data
[1] == 0 || data
[1] == 32))
559 else if (cmd
== 13 && lp
->disable_aftertouch
)
561 else if (cmd
== 12 && lp
->ignore_program_changes
)
564 if (!cbox_midi_buffer_write_event(&lp
->instrument
->module
->midi_input
, event
->time
, data
, event
->size
))
574 void cbox_scene_render(struct cbox_scene
*scene
, uint32_t nframes
, float *output_buffers
[])
578 if (scene
->rt
&& scene
->rt
->io
)
580 struct cbox_io
*io
= scene
->rt
->io
;
581 for (i
= 0; i
< io
->io_env
.input_count
; i
++)
583 if (IS_RECORDING_SOURCE_CONNECTED(scene
->rec_mono_inputs
[i
]))
584 cbox_recording_source_push(&scene
->rec_mono_inputs
[i
], (const float **)&io
->input_buffers
[i
], nframes
);
586 for (i
= 0; i
< io
->io_env
.input_count
/ 2; i
++)
588 if (IS_RECORDING_SOURCE_CONNECTED(scene
->rec_stereo_inputs
[i
]))
590 const float *buf
[2] = { io
->input_buffers
[i
* 2], io
->input_buffers
[i
* 2 + 1] };
591 cbox_recording_source_push(&scene
->rec_stereo_inputs
[i
], buf
, nframes
);
596 for(struct cbox_adhoc_pattern
**ppat
= &scene
->adhoc_patterns
; *ppat
; )
598 cbox_midi_buffer_clear(&(*ppat
)->output_buffer
);
599 if ((*ppat
)->completed
)
601 struct cbox_adhoc_pattern
*retired
= *ppat
;
602 *ppat
= retired
->next
;
603 retired
->next
= scene
->retired_adhoc_patterns
;
604 scene
->retired_adhoc_patterns
= retired
;
608 cbox_adhoc_pattern_render((*ppat
), 0, nframes
);
609 ppat
= &((*ppat
)->next
);
613 // XXXKF implement full cleanup, not only the front of the queue
614 if(scene
->adhoc_patterns
&& scene
->adhoc_patterns
->completed
)
616 struct cbox_adhoc_pattern
*top
= scene
->adhoc_patterns
;
617 cbox_midi_buffer_clear(&top
->output_buffer
);
618 scene
->adhoc_patterns
= top
->next
;
622 cbox_midi_buffer_clear(&scene
->midibuf_total
);
623 cbox_midi_merger_render(&scene
->scene_input_merger
);
625 write_events_to_instrument_ports(scene
, &scene
->midibuf_total
);
627 for (n
= 0; n
< scene
->aux_bus_count
; n
++)
629 for (i
= 0; i
< nframes
; i
++)
631 scene
->aux_buses
[n
]->input_bufs
[0][i
] = 0.f
;
632 scene
->aux_buses
[n
]->input_bufs
[1][i
] = 0.f
;
636 for (n
= 0; n
< scene
->instrument_count
; n
++)
638 struct cbox_instrument
*instr
= scene
->instruments
[n
];
639 struct cbox_module
*module
= instr
->module
;
640 int event_count
= instr
->module
->midi_input
.count
;
642 uint32_t highwatermark
= 0;
643 cbox_sample_t channels
[CBOX_MAX_AUDIO_PORTS
][CBOX_BLOCK_SIZE
];
644 cbox_sample_t
*outputs
[CBOX_MAX_AUDIO_PORTS
];
645 for (i
= 0; i
< module
->outputs
; i
++)
646 outputs
[i
] = channels
[i
];
648 for (i
= 0; i
< nframes
; i
+= CBOX_BLOCK_SIZE
)
650 if (i
>= highwatermark
)
652 while(cur_event
< event_count
)
654 const struct cbox_midi_event
*event
= cbox_midi_buffer_get_event(&module
->midi_input
, cur_event
);
657 if (event
->time
<= i
)
658 (*module
->process_event
)(module
, cbox_midi_event_get_data(event
), event
->size
);
661 highwatermark
= event
->time
;
671 (*module
->process_block
)(module
, NULL
, outputs
);
672 for (int o
= 0; o
< module
->outputs
/ 2; o
++)
674 struct cbox_instrument_output
*oobj
= &instr
->outputs
[o
];
675 struct cbox_module
*insert
= oobj
->insert
;
676 float gain
= oobj
->gain
;
677 if (IS_RECORDING_SOURCE_CONNECTED(oobj
->rec_dry
))
678 cbox_recording_source_push(&oobj
->rec_dry
, (const float **)(outputs
+ 2 * o
), CBOX_BLOCK_SIZE
);
679 if (insert
&& !insert
->bypass
)
680 (*insert
->process_block
)(insert
, outputs
+ 2 * o
, outputs
+ 2 * o
);
681 if (IS_RECORDING_SOURCE_CONNECTED(oobj
->rec_wet
))
682 cbox_recording_source_push(&oobj
->rec_wet
, (const float **)(outputs
+ 2 * o
), CBOX_BLOCK_SIZE
);
683 float *leftbuf
, *rightbuf
;
684 if (o
< module
->aux_offset
/ 2)
686 int leftch
= oobj
->output_bus
* 2;
687 int rightch
= leftch
+ 1;
688 leftbuf
= output_buffers
[leftch
];
689 rightbuf
= output_buffers
[rightch
];
693 int bus
= o
- module
->aux_offset
/ 2;
694 struct cbox_aux_bus
*busobj
= instr
->aux_outputs
[bus
];
697 leftbuf
= busobj
->input_bufs
[0];
698 rightbuf
= busobj
->input_bufs
[1];
700 if (leftbuf
&& rightbuf
)
702 for (j
= 0; j
< CBOX_BLOCK_SIZE
; j
++)
704 leftbuf
[i
+ j
] += gain
* channels
[2 * o
][j
];
705 rightbuf
[i
+ j
] += gain
* channels
[2 * o
+ 1][j
];
710 for (j
= 0; j
< CBOX_BLOCK_SIZE
; j
++)
713 leftbuf
[i
+ j
] += gain
* channels
[2 * o
][j
];
715 rightbuf
[i
+ j
] += gain
* channels
[2 * o
+ 1][j
];
720 while(cur_event
< event_count
)
722 const struct cbox_midi_event
*event
= cbox_midi_buffer_get_event(&module
->midi_input
, cur_event
);
725 (*module
->process_event
)(module
, cbox_midi_event_get_data(event
), event
->size
);
734 for (n
= 0; n
< scene
->aux_bus_count
; n
++)
736 struct cbox_aux_bus
*bus
= scene
->aux_buses
[n
];
737 float left
[CBOX_BLOCK_SIZE
], right
[CBOX_BLOCK_SIZE
];
738 float *outputs
[2] = {left
, right
};
739 for (i
= 0; i
< nframes
; i
+= CBOX_BLOCK_SIZE
)
742 inputs
[0] = &bus
->input_bufs
[0][i
];
743 inputs
[1] = &bus
->input_bufs
[1][i
];
744 bus
->module
->process_block(bus
->module
, inputs
, outputs
);
745 for (int j
= 0; j
< CBOX_BLOCK_SIZE
; j
++)
747 output_buffers
[0][i
+ j
] += left
[j
];
748 output_buffers
[1][i
+ j
] += right
[j
];
753 int output_count
= scene
->engine
->io_env
.output_count
;
754 // XXXKF this assumes that the buffers are zeroed on start - which isn't true if there are multiple scenes
755 for (i
= 0; i
< output_count
; i
++)
757 if (IS_RECORDING_SOURCE_CONNECTED(scene
->rec_mono_outputs
[i
]))
758 cbox_recording_source_push(&scene
->rec_mono_outputs
[i
], (const float **)&output_buffers
[i
], nframes
);
760 for (i
= 0; i
< output_count
/ 2; i
++)
762 if (IS_RECORDING_SOURCE_CONNECTED(scene
->rec_stereo_outputs
[i
]))
764 const float *buf
[2] = { output_buffers
[i
* 2], output_buffers
[i
* 2 + 1] };
765 cbox_recording_source_push(&scene
->rec_stereo_outputs
[i
], buf
, nframes
);
770 void cbox_scene_clear(struct cbox_scene
*scene
)
773 g_free(scene
->title
);
774 scene
->name
= g_strdup("");
775 scene
->title
= g_strdup("");
776 while(scene
->layer_count
> 0)
778 struct cbox_layer
*layer
= cbox_scene_remove_layer(scene
, 0);
782 while(scene
->aux_bus_count
> 0)
783 CBOX_DELETE(scene
->aux_buses
[scene
->aux_bus_count
- 1]);
786 static struct cbox_instrument
*create_instrument(struct cbox_scene
*scene
, struct cbox_module
*module
)
788 int auxes
= (module
->outputs
- module
->aux_offset
) / 2;
790 struct cbox_instrument
*instr
= malloc(sizeof(struct cbox_instrument
));
791 CBOX_OBJECT_HEADER_INIT(instr
, cbox_instrument
, CBOX_GET_DOCUMENT(scene
));
792 instr
->scene
= scene
;
793 instr
->module
= module
;
794 instr
->outputs
= calloc(module
->outputs
/ 2, sizeof(struct cbox_instrument_output
));
796 instr
->aux_outputs
= calloc(auxes
, sizeof(struct cbox_aux_bus
*));
797 instr
->aux_output_names
= calloc(auxes
, sizeof(char *));
798 instr
->aux_output_count
= auxes
;
800 for (int i
= 0; i
< module
->outputs
/ 2; i
++)
801 cbox_instrument_output_init(&instr
->outputs
[i
], scene
, module
->engine
->io_env
.buffer_size
);
806 struct cbox_instrument
*cbox_scene_create_instrument(struct cbox_scene
*scene
, const char *instrument_name
, const char *engine_name
, GError
**error
)
808 gpointer value
= g_hash_table_lookup(scene
->instrument_hash
, instrument_name
);
811 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Instrument already exists: '%s'", instrument_name
);
815 struct cbox_document
*doc
= CBOX_GET_DOCUMENT(scene
);
816 struct cbox_module_manifest
*mptr
= NULL
;
817 struct cbox_instrument
*instr
= NULL
;
818 struct cbox_module
*module
= NULL
;
820 mptr
= cbox_module_manifest_get_by_name(engine_name
);
823 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "No engine called '%s'", engine_name
);
827 module
= cbox_module_manifest_create_module(mptr
, NULL
, doc
, scene
->rt
, scene
->engine
, instrument_name
, error
);
830 cbox_force_error(error
);
831 g_prefix_error(error
, "Cannot create engine '%s' for instrument '%s': ", engine_name
, instrument_name
);
835 instr
= create_instrument(scene
, module
);
837 cbox_command_target_init(&instr
->cmd_target
, cbox_instrument_process_cmd
, instr
);
838 g_hash_table_insert(scene
->instrument_hash
, g_strdup(instrument_name
), instr
);
839 CBOX_OBJECT_REGISTER(instr
);
844 struct cbox_instrument
*cbox_scene_get_instrument_by_name(struct cbox_scene
*scene
, const char *name
, gboolean load
, GError
**error
)
846 struct cbox_module_manifest
*mptr
= NULL
;
847 struct cbox_instrument
*instr
= NULL
;
848 struct cbox_module
*module
= NULL
;
849 gchar
*instr_section
= NULL
;
850 gpointer value
= g_hash_table_lookup(scene
->instrument_hash
, name
);
851 const char *cv
, *instr_engine
;
852 struct cbox_document
*doc
= CBOX_GET_DOCUMENT(scene
);
860 instr_section
= g_strdup_printf("instrument:%s", name
);
862 if (!cbox_config_has_section(instr_section
))
864 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "No config section for instrument '%s'", name
);
868 instr_engine
= cbox_config_get_string(instr_section
, "engine");
871 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Engine not specified in instrument '%s'", name
);
875 mptr
= cbox_module_manifest_get_by_name(instr_engine
);
878 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "No engine called '%s'", instr_engine
);
882 // cbox_module_manifest_dump(mptr);
884 module
= cbox_module_manifest_create_module(mptr
, instr_section
, doc
, scene
->rt
, scene
->engine
, name
, error
);
887 cbox_force_error(error
);
888 g_prefix_error(error
, "Cannot create engine '%s' for instrument '%s': ", instr_engine
, name
);
892 instr
= create_instrument(scene
, module
);
894 for (int i
= 0; i
< module
->outputs
/ 2; i
++)
896 struct cbox_instrument_output
*oobj
= instr
->outputs
+ i
;
898 gchar
*key
= i
== 0 ? g_strdup("output_bus") : g_strdup_printf("output%d_bus", 1 + i
);
899 oobj
->output_bus
= cbox_config_get_int(instr_section
, key
, 1) - 1;
901 key
= i
== 0 ? g_strdup("gain") : g_strdup_printf("gain%d", 1 + i
);
902 oobj
->gain
= cbox_config_get_gain_db(instr_section
, key
, 0);
905 key
= i
== 0 ? g_strdup("insert") : g_strdup_printf("insert%d", 1 + i
);
906 cv
= cbox_config_get_string(instr_section
, key
);
911 oobj
->insert
= cbox_module_new_from_fx_preset(cv
, CBOX_GET_DOCUMENT(scene
), module
->rt
, scene
->engine
, error
);
914 cbox_force_error(error
);
915 g_prefix_error(error
, "Cannot instantiate effect preset '%s' for instrument '%s': ", cv
, name
);
920 for (int i
= 0; i
< instr
->aux_output_count
; i
++)
922 instr
->aux_outputs
[i
] = NULL
;
924 gchar
*key
= g_strdup_printf("aux%d", 1 + i
);
925 gchar
*value
= cbox_config_get_string(instr_section
, key
);
926 instr
->aux_output_names
[i
] = value
? g_strdup(value
) : NULL
;
930 cbox_command_target_init(&instr
->cmd_target
, cbox_instrument_process_cmd
, instr
);
934 g_hash_table_insert(scene
->instrument_hash
, g_strdup(name
), instr
);
935 CBOX_OBJECT_REGISTER(instr
);
937 // cbox_recording_source_attach(&instr->outputs[0].rec_dry, cbox_recorder_new_stream("output.wav"));
946 static struct cbox_recording_source
*create_rec_sources(struct cbox_scene
*scene
, int buffer_size
, int count
, int channels
)
948 struct cbox_recording_source
*s
= malloc(sizeof(struct cbox_recording_source
) * count
);
949 for (int i
= 0; i
< count
; i
++)
950 cbox_recording_source_init(&s
[i
], scene
, buffer_size
, channels
);
954 static void destroy_rec_sources(struct cbox_recording_source
*s
, int count
)
956 for (int i
= 0; i
< count
; i
++)
957 cbox_recording_source_uninit(&s
[i
]);
961 struct cbox_scene
*cbox_scene_new(struct cbox_document
*document
, struct cbox_engine
*engine
)
963 struct cbox_scene
*s
= malloc(sizeof(struct cbox_scene
));
967 CBOX_OBJECT_HEADER_INIT(s
, cbox_scene
, document
);
969 s
->rt
= engine
? engine
->rt
: NULL
;
970 s
->instrument_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, NULL
);
971 s
->name
= g_strdup("");
972 s
->title
= g_strdup("");
975 s
->instruments
= NULL
;
977 s
->instrument_count
= 0;
978 s
->aux_bus_count
= 0;
979 cbox_command_target_init(&s
->cmd_target
, cbox_scene_process_cmd
, s
);
981 s
->connected_inputs
= NULL
;
982 s
->connected_input_count
= 0;
983 s
->enable_default_song_input
= TRUE
;
984 s
->enable_default_external_input
= TRUE
;
986 cbox_midi_buffer_init(&s
->midibuf_total
);
987 cbox_midi_merger_init(&s
->scene_input_merger
, &s
->midibuf_total
);
989 int buffer_size
= engine
->io_env
.buffer_size
;
990 s
->rec_mono_inputs
= create_rec_sources(s
, buffer_size
, engine
->io_env
.input_count
, 1);
991 s
->rec_stereo_inputs
= create_rec_sources(s
, buffer_size
, engine
->io_env
.input_count
/ 2, 2);
992 s
->rec_mono_outputs
= create_rec_sources(s
, buffer_size
, engine
->io_env
.output_count
, 1);
993 s
->rec_stereo_outputs
= create_rec_sources(s
, buffer_size
, engine
->io_env
.output_count
/ 2, 2);
994 s
->adhoc_patterns
= NULL
;
995 s
->retired_adhoc_patterns
= NULL
;
997 CBOX_OBJECT_REGISTER(s
);
999 cbox_engine_add_scene(s
->engine
, s
);
1000 cbox_scene_update_connected_inputs(s
);
1004 void cbox_scene_update_connected_inputs(struct cbox_scene
*scene
)
1006 if (!scene
->rt
|| !scene
->rt
->io
)
1009 // This is called when a MIDI Input port has been created, connected/disconnected
1010 // or is about to be removed (and then the removing flag will be set)
1011 for (int i
= 0; i
< scene
->connected_input_count
; )
1013 struct cbox_midi_input
*input
= scene
->connected_inputs
[i
];
1014 if (input
->removing
|| !cbox_uuid_equal(&input
->output
, &scene
->_obj_hdr
.instance_uuid
))
1016 cbox_midi_merger_disconnect(&scene
->scene_input_merger
, &input
->buffer
, scene
->rt
);
1017 cbox_rt_array_remove(scene
->rt
, (void ***)&scene
->connected_inputs
, &scene
->connected_input_count
, i
);
1022 for (GSList
*p
= scene
->rt
->io
->midi_inputs
; p
; p
= p
->next
)
1024 struct cbox_midi_input
*input
= p
->data
;
1025 if (cbox_uuid_equal(&input
->output
, &scene
->_obj_hdr
.instance_uuid
))
1027 gboolean found
= FALSE
;
1028 for (int i
= 0; i
< scene
->connected_input_count
; i
++)
1030 if (scene
->connected_inputs
[i
] == input
)
1038 cbox_midi_merger_connect(&scene
->scene_input_merger
, &input
->buffer
, scene
->rt
);
1039 cbox_rt_array_insert(scene
->rt
, (void ***)&scene
->connected_inputs
, &scene
->connected_input_count
, -1, input
);
1043 if (scene
->enable_default_song_input
)
1045 cbox_midi_merger_connect(&scene
->scene_input_merger
, &scene
->engine
->midibuf_aux
, scene
->rt
);
1046 cbox_midi_merger_connect(&scene
->scene_input_merger
, &scene
->engine
->midibuf_song
, scene
->rt
);
1050 cbox_midi_merger_disconnect(&scene
->scene_input_merger
, &scene
->engine
->midibuf_aux
, scene
->rt
);
1051 cbox_midi_merger_disconnect(&scene
->scene_input_merger
, &scene
->engine
->midibuf_song
, scene
->rt
);
1054 if (scene
->enable_default_external_input
)
1055 cbox_midi_merger_connect(&scene
->scene_input_merger
, &scene
->engine
->midibuf_jack
, scene
->rt
);
1057 cbox_midi_merger_disconnect(&scene
->scene_input_merger
, &scene
->engine
->midibuf_jack
, scene
->rt
);
1061 static void free_adhoc_pattern_list(struct cbox_scene
*scene
, struct cbox_adhoc_pattern
*ap
)
1065 struct cbox_adhoc_pattern
*tmp
= ap
;
1068 cbox_midi_merger_disconnect(&scene
->scene_input_merger
, &ap
->output_buffer
, scene
->rt
);
1069 cbox_adhoc_pattern_destroy(tmp
);
1073 struct play_adhoc_pattern_arg
1075 struct cbox_scene
*scene
;
1076 struct cbox_adhoc_pattern
*ap
;
1077 struct cbox_adhoc_pattern
*retired
;
1080 static int play_adhoc_pattern_execute(void *arg_
)
1082 struct play_adhoc_pattern_arg
*arg
= arg_
;
1086 struct cbox_adhoc_pattern
*ap
= arg
->scene
->adhoc_patterns
;
1088 // If there is already an adhoc pattern with a given non-zero id, stop it
1089 // and release all the pending notes. Retry until all the notes are
1093 while(ap
&& ap
->id
!= arg
->ap
->id
)
1097 ap
->completed
= TRUE
;
1098 if (ap
->active_notes
.channels_active
)
1103 arg
->ap
->next
= arg
->scene
->adhoc_patterns
;
1104 arg
->scene
->adhoc_patterns
= arg
->ap
;
1106 arg
->retired
= arg
->scene
->retired_adhoc_patterns
;
1107 arg
->scene
->retired_adhoc_patterns
= NULL
;
1108 // XXXKF should convert pattern length into sample position instead of assuming 0x7FFFFFFF (though it likely doesn't matter)
1109 cbox_midi_clip_playback_set_pattern(&arg
->ap
->playback
, arg
->ap
->pattern_playback
, 0, 0x7FFFFFFF, 0, 0);
1113 void cbox_scene_play_adhoc_pattern(struct cbox_scene
*scene
, struct cbox_adhoc_pattern
*ap
)
1115 static struct cbox_rt_cmd_definition cmd
= { NULL
, play_adhoc_pattern_execute
, NULL
};
1116 struct play_adhoc_pattern_arg arg
= { scene
, ap
, NULL
};
1117 cbox_midi_merger_connect(&scene
->scene_input_merger
, &ap
->output_buffer
, scene
->rt
);
1118 cbox_rt_execute_cmd_sync(scene
->rt
, &cmd
, &arg
);
1120 free_adhoc_pattern_list(scene
, arg
.retired
);
1123 gboolean
cbox_scene_move_instrument_to(struct cbox_scene
*scene
, struct cbox_instrument
*instrument
, struct cbox_scene
*new_scene
, int dstpos
, GError
**error
)
1127 dstpos
= new_scene
->layer_count
;
1128 for (int i
= 0; i
< scene
->layer_count
; i
++)
1130 if (scene
->layers
[i
]->instrument
== instrument
)
1135 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Instrument '%s' not found in source scene", instrument
->module
->instance_name
);
1138 if (cbox_scene_get_instrument_by_name(new_scene
, instrument
->module
->instance_name
, FALSE
, NULL
))
1140 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Instrument '%s' already exists in target scene", instrument
->module
->instance_name
);
1144 struct cbox_layer
**new_src_layers
= malloc(sizeof(struct cbox_layer
*) * (scene
->layer_count
- lcount
));
1145 struct cbox_layer
**new_dst_layers
= malloc(sizeof(struct cbox_layer
*) * (new_scene
->layer_count
+ lcount
));
1146 int srcidx
= 0, dstidx
= 0;
1147 memcpy(&new_dst_layers
[dstidx
], new_scene
->layers
, dstpos
* sizeof(struct cbox_layer
**));
1149 for (int i
= 0; i
< scene
->layer_count
; i
++)
1151 if (scene
->layers
[i
]->instrument
!= instrument
)
1152 new_src_layers
[srcidx
++] = scene
->layers
[i
];
1154 new_dst_layers
[dstidx
++] = scene
->layers
[i
];
1156 memcpy(&new_dst_layers
[dstidx
], new_scene
->layers
, (new_scene
->layer_count
- dstpos
) * sizeof(struct cbox_layer
**));
1157 dstidx
+= new_scene
->layer_count
;
1159 free(cbox_rt_swap_pointers_and_update_count(scene
->rt
, (void **)&scene
->layers
, new_src_layers
, &scene
->layer_count
, srcidx
));
1160 cbox_rt_array_remove_by_value(scene
->rt
, (void ***)&scene
->instruments
, &scene
->instrument_count
, instrument
);
1162 cbox_rt_array_insert(scene
->rt
, (void ***)&new_scene
->instruments
, &new_scene
->instrument_count
, -1, instrument
);
1163 free(cbox_rt_swap_pointers_and_update_count(new_scene
->rt
, (void **)&new_scene
->layers
, new_dst_layers
, &new_scene
->layer_count
, dstidx
));
1168 static void cbox_scene_destroyfunc(struct cbox_objhdr
*objhdr
)
1170 struct cbox_scene
*scene
= CBOX_H2O(objhdr
);
1171 cbox_midi_merger_disconnect(&scene
->scene_input_merger
, &scene
->engine
->midibuf_aux
, scene
->rt
);
1172 cbox_midi_merger_disconnect(&scene
->scene_input_merger
, &scene
->engine
->midibuf_jack
, scene
->rt
);
1173 cbox_midi_merger_disconnect(&scene
->scene_input_merger
, &scene
->engine
->midibuf_song
, scene
->rt
);
1174 cbox_engine_remove_scene(scene
->engine
, scene
);
1175 cbox_scene_clear(scene
);
1176 g_free(scene
->name
);
1177 g_free(scene
->title
);
1178 assert(scene
->instrument_count
== 0);
1179 free(scene
->layers
);
1180 free(scene
->aux_buses
);
1181 free(scene
->instruments
);
1182 g_hash_table_destroy(scene
->instrument_hash
);
1183 free(scene
->connected_inputs
);
1185 destroy_rec_sources(scene
->rec_mono_inputs
, scene
->engine
->io_env
.input_count
);
1186 destroy_rec_sources(scene
->rec_stereo_inputs
, scene
->engine
->io_env
.input_count
/ 2);
1187 destroy_rec_sources(scene
->rec_mono_outputs
, scene
->engine
->io_env
.output_count
);
1188 destroy_rec_sources(scene
->rec_stereo_outputs
, scene
->engine
->io_env
.output_count
/ 2);
1190 free_adhoc_pattern_list(scene
, scene
->retired_adhoc_patterns
);
1191 free_adhoc_pattern_list(scene
, scene
->adhoc_patterns
);
1192 cbox_midi_merger_close(&scene
->scene_input_merger
);