-
Notifications
You must be signed in to change notification settings - Fork 2
/
modulation_matrix.c
397 lines (344 loc) · 9.43 KB
/
modulation_matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Pithesiser - a software synthesiser for Raspberry Pi
// Copyright (C) 2015 Nicholas Tuckett
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
* modulation_matrix.c
*
* Created on: 31 Dec 2013
* Author: ntuckett
*/
#include "modulation_matrix.h"
#include <memory.h>
#include "system_constants.h"
#include "logging.h"
#define MOD_MATRIX_MAX_CONNECTIONS (MOD_MATRIX_MAX_SOURCES * MOD_MATRIX_MAX_SINKS)
#define MAX_MOD_MATRIX_CALLBACKS 4
static int source_count = 0;
static int sink_count = 0;
mod_matrix_source_t* sources[MOD_MATRIX_MAX_SOURCES];
mod_matrix_sink_t* sinks[MOD_MATRIX_MAX_SINKS];
typedef struct mod_matrix_connection_t
{
mod_matrix_source_t* source;
mod_matrix_sink_t* sink;
} mod_matrix_connection_t;
mod_matrix_connection_t connections[MOD_MATRIX_MAX_CONNECTIONS];
typedef struct mod_matrix_callback_info_t mod_matrix_callback_info_t;
typedef struct mod_matrix_callback_info_t
{
mod_matrix_callback_t callback;
void* data;
mod_matrix_callback_info_t* next_callback;
} mod_matrix_callback_info_t;
static mod_matrix_callback_info_t* mod_matrix_callback_head = NULL;
static mod_matrix_callback_info_t* mod_matrix_callback_free = NULL;
static mod_matrix_callback_info_t mod_matrix_callback_info[MAX_MOD_MATRIX_CALLBACKS];
void mod_matrix_initialise()
{
source_count = 0;
sink_count = 0;
memset(connections, 0, sizeof(connections));
for (int i = 0; i < MAX_MOD_MATRIX_CALLBACKS; i++)
{
mod_matrix_callback_info[i].next_callback = mod_matrix_callback_free;
mod_matrix_callback_free = mod_matrix_callback_info + i;
}
}
void mod_matrix_init_source(const char* name, generate_mod_matrix_value_t generate_value, get_mod_matrix_value_t get_value, mod_matrix_source_t* source)
{
strncpy(source->name, name, MOD_MATRIX_MAX_NAME_LEN);
source->name[MOD_MATRIX_MAX_NAME_LEN] = 0;
source->generate_value = generate_value;
source->get_value = get_value;
}
void mod_matrix_init_sink(const char* name, base_update_t base_update, model_update_t model_update, mod_matrix_sink_t* sink)
{
strncpy(sink->name, name, MOD_MATRIX_MAX_NAME_LEN);
sink->name[MOD_MATRIX_MAX_NAME_LEN] = 0;
sink->base_update = base_update;
sink->model_update = model_update;
}
int mod_matrix_add_source(mod_matrix_source_t* source)
{
if (source_count < MOD_MATRIX_MAX_SOURCES)
{
sources[source_count++] = source;
return RESULT_OK;
}
else
{
LOG_ERROR("Mod matrix source limit of %d reached", MOD_MATRIX_MAX_SOURCES);
return RESULT_ERROR;
}
}
int mod_matrix_add_sink(mod_matrix_sink_t* sink)
{
if (sink_count < MOD_MATRIX_MAX_SINKS)
{
sinks[sink_count++] = sink;
return RESULT_OK;
}
else
{
LOG_ERROR("Mod matrix sink limit of %d reached", MOD_MATRIX_MAX_SINKS);
return RESULT_ERROR;
}
}
void mod_matrix_add_callback(mod_matrix_callback_t callback, void* callback_data)
{
if (mod_matrix_callback_free != NULL)
{
mod_matrix_callback_info_t* callback_info = mod_matrix_callback_free;
mod_matrix_callback_free = mod_matrix_callback_free->next_callback;
callback_info->callback = callback;
callback_info->data = callback_data;
callback_info->next_callback = mod_matrix_callback_head;
mod_matrix_callback_head = callback_info;
}
else
{
LOG_ERROR("No voice callbacks available");
}
}
void mod_matrix_remove_callback(mod_matrix_callback_t callback)
{
mod_matrix_callback_info_t* callback_info = mod_matrix_callback_head;
mod_matrix_callback_info_t** last_link = &mod_matrix_callback_head;
while (callback_info != NULL)
{
if (callback_info->callback == callback)
{
*last_link = callback_info->next_callback;
callback_info->callback = NULL;
callback_info->data = NULL;
callback_info->next_callback = mod_matrix_callback_free;
mod_matrix_callback_free = callback_info;
break;
}
else
{
last_link = &callback_info->next_callback;
callback_info = callback_info->next_callback;
}
}
}
void mod_matrix_make_callback(mod_matrix_event_t event, mod_matrix_source_t* source, mod_matrix_sink_t* sink)
{
mod_matrix_callback_info_t* callback_info = mod_matrix_callback_head;
while (callback_info != NULL)
{
callback_info->callback(event, source, sink, callback_info->data);
callback_info = callback_info->next_callback;
}
}
mod_matrix_source_t* find_source(const char* source_name)
{
for (int i = 0; i < source_count; i++)
{
if (strcmp(source_name, sources[i]->name) == 0)
{
return sources[i];
}
}
return NULL;
}
mod_matrix_sink_t* find_sink(const char* sink_name)
{
for (int i = 0; i < sink_count; i++)
{
if (strcmp(sink_name, sinks[i]->name) == 0)
{
return sinks[i];
}
}
return NULL;
}
mod_matrix_connection_t* find_free_connection()
{
for (int i = 0; i < MOD_MATRIX_MAX_CONNECTIONS; i++)
{
if (connections[i].source == NULL && connections[i].sink == NULL)
{
return connections + i;
}
}
return NULL;
}
mod_matrix_connection_t* find_connection(mod_matrix_source_t* source, mod_matrix_sink_t* sink)
{
for (int i = 0; i < MOD_MATRIX_MAX_CONNECTIONS; i++)
{
if (connections[i].source == source && connections[i].sink == sink)
{
return connections + i;
}
}
return NULL;
}
mod_matrix_connection_t* connect(mod_matrix_source_t* source, mod_matrix_sink_t* sink)
{
mod_matrix_connection_t* connection = find_free_connection();
if (connection != NULL)
{
connection->source = source;
connection->sink = sink;
mod_matrix_make_callback(MOD_MATRIX_EVENT_CONNECTION, source, sink);
}
return connection;
}
void disconnect(mod_matrix_connection_t* connection)
{
mod_matrix_make_callback(MOD_MATRIX_EVENT_DISCONNECTION, connection->source, connection->sink);
connection->source = NULL;
connection->sink = NULL;
}
int mod_matrix_connect(const char* source_name, const char* sink_name)
{
mod_matrix_source_t* source = find_source(source_name);
if (source == NULL)
{
LOG_ERROR("Unknown source %s for connection", source_name);
return RESULT_ERROR;
}
mod_matrix_sink_t* sink = find_sink(sink_name);
if (sink == NULL)
{
LOG_ERROR("Unknown sink %s for connection", sink_name);
return RESULT_ERROR;
}
if (find_connection(source, sink) == NULL)
{
if (connect(source, sink) == NULL)
{
LOG_ERROR("No free connections for %s-%s", source_name, sink_name);
return RESULT_ERROR;
}
else
{
return RESULT_OK;
}
}
else
{
LOG_WARN("Connection exists for %s-%s", source_name, sink_name);
return RESULT_ERROR;
}
}
int mod_matrix_disconnect(const char* source_name, const char* sink_name)
{
mod_matrix_source_t* source = find_source(source_name);
if (source == NULL)
{
LOG_ERROR("Unknown source %s for disconnection", source_name);
return RESULT_ERROR;
}
mod_matrix_sink_t* sink = find_sink(sink_name);
if (sink == NULL)
{
LOG_ERROR("Unknown sink %s for disconnection", sink_name);
return RESULT_ERROR;
}
mod_matrix_connection_t* connection = find_connection(source, sink);
if (connection != NULL)
{
disconnect(connection);
return RESULT_OK;
}
else
{
LOG_WARN("No connection exists for %s-%s", source_name, sink_name);
return RESULT_ERROR;
}
}
void mod_matrix_disconnect_source(const char* source_name)
{
mod_matrix_source_t* source = find_source(source_name);
if (source == NULL)
{
LOG_ERROR("Unknown source %s for disconnection", source_name);
}
else
{
for (int i = 0; i < MOD_MATRIX_MAX_CONNECTIONS; i++)
{
if (connections[i].source == source)
{
disconnect(connections + i);
}
}
}
}
int mod_matrix_toggle_connection(const char* source_name, const char* sink_name)
{
mod_matrix_source_t* source = find_source(source_name);
if (source == NULL)
{
LOG_ERROR("Unknown source %s for connection", source_name);
return MOD_MATRIX_DISCONNECTED;
}
mod_matrix_sink_t* sink = find_sink(sink_name);
if (sink == NULL)
{
LOG_ERROR("Unknown sink %s for connection", sink_name);
return MOD_MATRIX_DISCONNECTED;
}
mod_matrix_connection_t* connection = find_connection(source, sink);
if (connection != NULL)
{
disconnect(connection);
return MOD_MATRIX_DISCONNECTED;
}
else
{
connect(source, sink);
return MOD_MATRIX_CONNECTED;
}
}
void mod_matrix_update(void* data)
{
for (int i = 0; i < source_count; i++)
{
if (sources[i]->generate_value != NULL)
{
sources[i]->generate_value(sources[i], data);
}
}
for (int i = 0; i < sink_count; i++)
{
if (sinks[i]->base_update != NULL)
{
sinks[i]->base_update(sinks[i], data);
}
}
mod_matrix_connection_t* connection = connections;
for (int i = 0; i < MOD_MATRIX_MAX_CONNECTIONS; i++, connection++)
{
if (connection->source != NULL && connection->sink != NULL)
{
connection->sink->model_update(connection->source, connection->sink);
}
}
}
void mod_matrix_iterate_connections(void* data, connection_callback_t callback)
{
mod_matrix_connection_t* connection = connections;
for (int i = 0; i < MOD_MATRIX_MAX_CONNECTIONS; i++, connection++)
{
if (connection->source != NULL && connection->sink != NULL)
{
callback(data, connection->source, connection->sink);
}
}
}