Fix broken bounds check.
[calfbox.git] / module.h
blob28389b8da7d7f8594d4a178cfeac1c366e373eeb
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 #ifndef CBOX_MODULE_H
20 #define CBOX_MODULE_H
22 #include "dom.h"
23 #include "dspmath.h"
24 #include "errors.h"
25 #include "midi.h"
27 #include <stdint.h>
29 CBOX_EXTERN_CLASS(cbox_module)
31 #define CBOX_MAX_AUDIO_PORTS 32
33 struct cbox_engine;
34 struct cbox_rt;
36 struct cbox_module_keyrange_metadata
38 uint8_t channel; // 0 = omni
39 uint8_t low_key;
40 uint8_t high_key;
41 const char *name;
44 enum cbox_module_livecontroller_class
46 cmlc_onoffcc,
47 cmlc_continuouscc,
48 cmlc_continuous,
49 cmlc_discrete,
50 cmlc_enum
53 struct cbox_module_livecontroller_metadata
55 uint8_t channel;
56 enum cbox_module_livecontroller_class controller_class:8;
57 uint16_t controller;
58 const char *name;
59 void *extra_info;
62 struct cbox_module_voicingparam_metadata
66 struct cbox_module
68 CBOX_OBJECT_HEADER()
69 void *user_data;
70 struct cbox_rt *rt;
71 struct cbox_engine *engine;
72 const char *engine_name;
73 gchar *instance_name;
74 cbox_sample_t *input_samples;
75 cbox_sample_t *output_samples;
76 struct cbox_midi_buffer midi_input;
77 int inputs, outputs, aux_offset;
78 int bypass;
79 int srate;
80 double srate_inv;
82 struct cbox_command_target cmd_target;
84 void (*process_event)(struct cbox_module *module, const uint8_t *data, uint32_t len);
85 void (*process_block)(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs);
86 void (*destroy)(struct cbox_module *module);
89 struct cbox_module_manifest
91 void *user_data;
92 const char *name;
93 int min_inputs;
94 int min_outputs;
96 struct cbox_module_keyrange_metadata *keyranges;
97 int num_keyranges;
99 struct cbox_module_livecontroller_metadata *live_controllers;
100 int num_live_controllers;
102 struct cbox_module_voicingparam_metadata *voicing_params;
103 int num_voicing_params;
105 struct cbox_module *(*create)(void *user_data, const char *cfg_section, struct cbox_document *doc, struct cbox_rt *rt, struct cbox_engine *engine, GError **error);
108 #define DEFINE_MODULE(modname, ninputs, noutputs) \
109 struct cbox_module_manifest modname##_module = { \
110 NULL, \
111 .name = #modname, \
112 .min_inputs = ninputs, \
113 .min_outputs = noutputs, \
114 .keyranges = modname##_keyranges, \
115 .num_keyranges = sizeof(modname##_keyranges)/sizeof(modname##_keyranges[0]), \
116 .live_controllers = modname##_controllers, \
117 .num_live_controllers = sizeof(modname##_controllers)/sizeof(modname##_controllers[0]), \
118 .create = modname##_create \
121 extern struct cbox_module_manifest *cbox_module_list[];
123 extern void cbox_module_manifest_dump(struct cbox_module_manifest *manifest);
124 extern struct cbox_module_manifest *cbox_module_manifest_get_by_name(const char *name);
125 extern struct cbox_module *cbox_module_manifest_create_module(struct cbox_module_manifest *manifest, const char *cfg_section, struct cbox_document *doc, struct cbox_rt *rt, struct cbox_engine *engine, const char *instance_name, GError **error);
127 extern struct cbox_module *cbox_module_new_from_fx_preset(const char *name, struct cbox_document *doc, struct cbox_rt *rt, struct cbox_engine *engine, GError **error);
129 extern void cbox_module_init(struct cbox_module *module, struct cbox_document *doc, struct cbox_rt *rt, struct cbox_engine *engine, void *user_data, int inputs, int outputs, cbox_process_cmd cmd_handler, void (*destroy)(struct cbox_module *module));
130 extern void cbox_module_swap_pointers_and_free(struct cbox_module *sm, void **pptr, void *value);
132 extern gboolean cbox_module_slot_process_cmd(struct cbox_module **psm, struct cbox_command_target *fb, struct cbox_osc_command *cmd, const char *subcmd, struct cbox_document *doc, struct cbox_rt *rt, struct cbox_engine *engine, GError **error);
134 #define EFFECT_PARAM_CLONE(res) \
135 struct MODULE_PARAMS *res = malloc(sizeof(struct MODULE_PARAMS)); \
136 memcpy(res, m->params, sizeof(struct MODULE_PARAMS)); \
138 #define EFFECT_PARAM(path, type, field, ctype, expr, minv, maxv) \
139 if (!strcmp(cmd->command, path) && !strcmp(cmd->arg_types, type)) \
141 ctype value = *(ctype *)cmd->arg_values[0]; \
142 if (value < minv || value > maxv) \
143 return cbox_set_range_error(error, path, minv, maxv);\
144 EFFECT_PARAM_CLONE(pp); \
145 pp->field = expr(value); \
146 cbox_module_swap_pointers_and_free(&m->module, (void **)&m->params, pp); \
149 #define EFFECT_PARAM_ARRAY(path, type, array, field, ctype, expr, minv, maxv) \
150 if (!strcmp(cmd->command, path) && !strcmp(cmd->arg_types, "i" type)) \
152 int pos = *(int *)cmd->arg_values[0]; \
153 ctype value = *(ctype *)cmd->arg_values[1]; \
154 if (value < minv || value > maxv) \
155 return cbox_set_range_error(error, path, minv, maxv);\
156 EFFECT_PARAM_CLONE(pp); \
157 pp->array[pos].field = expr(value); \
158 cbox_module_swap_pointers_and_free(&m->module, (void **)&m->params, pp); \
161 #define MODULE_CREATE_FUNCTION(module) \
162 struct cbox_module *module##_create(void *user_data, const char *cfg_section, struct cbox_document *doc, struct cbox_rt *rt, struct cbox_engine *engine, GError **error)
164 #define MODULE_PROCESSCMD_FUNCTION(module) \
165 gboolean module##_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
167 #define MODULE_SIMPLE_DESTROY_FUNCTION(module) \
168 static void module##_destroyfunc(struct cbox_module *module_) \
170 struct module##_module *m = (struct module##_module *)module_; \
171 free(m->params); \
174 #define CALL_MODULE_INIT(m, inputs, outputs, name) \
175 cbox_module_init(&(m)->module, doc, rt, engine, (m), inputs, outputs, name##_process_cmd, name##_destroyfunc);
177 #define CALL_MODULE_INIT_SIMPLE(m, inputs, outputs) \
178 cbox_module_init(&(m)->module, doc, rt, engine, (m), inputs, outputs, NULL, NULL);
181 #endif