-
Notifications
You must be signed in to change notification settings - Fork 2
/
modulation_matrix_controller.c
274 lines (242 loc) · 7.41 KB
/
modulation_matrix_controller.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
// 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_controller.c
*
* Created on: 31 Dec 2013
* Author: ntuckett
*/
#include "modulation_matrix_controller.h"
#include <libconfig.h>
#include <string.h>
#include "system_constants.h"
#include "logging.h"
#include "modulation_matrix.h"
#include "error_handler.h"
#include "midi.h"
#define MOD_MATRIX_CONTROL_NOTE_BASE 0
#define MOD_MATRIX_CONTROL_NOTE_STRIDE 16
static const char* column_names[MOD_MATRIX_MAX_SOURCES];
static const char* row_names[MOD_MATRIX_MAX_SINKS];
static int column_count = 0;
static int row_count = 0;
static int midi_channel = 0;
static const char* CFG_MOD_MATRIX_CHANNEL = "channel";
static const char* CFG_MOD_MATRIX_COLUMNS = "columns";
static const char* CFG_MOD_MATRIX_ROWS = "rows";
static const char* CFG_MOD_MATRIX_CONNECTIONS = "connections";
static unsigned char get_matrix_note_number(const char* source, const char* sink)
{
int column;
int row;
for (column = 0; column < column_count; column++)
{
if (strcmp(source, column_names[column]) == 0)
{
break;
}
}
if (column == column_count)
{
return 0xff;
}
for (row = 0; row < row_count; row++)
{
if (strcmp(sink, row_names[row]) == 0)
{
break;
}
}
if (row == row_count)
{
return 0xff;
}
return (unsigned char)(MOD_MATRIX_CONTROL_NOTE_BASE + column + row * MOD_MATRIX_CONTROL_NOTE_STRIDE);
}
static int parse_matrix_controller_settings(config_setting_t *config)
{
int result = RESULT_OK;
if (config == NULL)
{
return result;
}
if (config_setting_lookup_int(config, CFG_MOD_MATRIX_CHANNEL, &midi_channel) != CONFIG_TRUE)
{
setting_error_report(config, "Missing or invalid MIDI channel for modulation matrix");
result = RESULT_ERROR;
}
config_setting_t* row_settings = config_setting_get_member(config, CFG_MOD_MATRIX_ROWS);
if (row_settings == NULL)
{
setting_error_report(config, "Missing or invalid row list for modulation matrix");
result = RESULT_ERROR;
}
else
{
row_count = config_setting_length(row_settings);
if (row_count >= MOD_MATRIX_MAX_SINKS)
{
setting_error_report(row_settings, "Too many rows (%d), limit is %d", row_count, MOD_MATRIX_MAX_SINKS);
result = RESULT_ERROR;
}
else
{
for (int i = 0; i < row_count; i++)
{
row_names[i] = config_setting_get_string_elem(row_settings, i);
if (row_names[i] == NULL)
{
setting_error_report(row_settings, "Missing or invalid row name %d", i);
result = RESULT_ERROR;
}
}
}
}
config_setting_t* column_settings = config_setting_get_member(config, CFG_MOD_MATRIX_COLUMNS);
if (column_settings == NULL)
{
setting_error_report(config, "Missing or invalid column list for modulation matrix");
result = RESULT_ERROR;
}
else
{
column_count = config_setting_length(column_settings);
if (column_count >= MOD_MATRIX_MAX_SOURCES)
{
setting_error_report(row_settings, "Too many columns (%d), limit is %d", column_count, MOD_MATRIX_MAX_SOURCES);
result = RESULT_ERROR;
}
else
{
for (int i = 0; i < column_count; i++)
{
column_names[i] = config_setting_get_string_elem(column_settings, i);
if (column_names[i] == NULL)
{
setting_error_report(column_settings, "Missing or invalid column name %d", i);
result = RESULT_ERROR;
}
}
}
}
return result;
}
void mod_matrix_controller_load(config_setting_t* patch)
{
config_setting_t* connections = config_setting_get_member(patch, CFG_MOD_MATRIX_CONNECTIONS);
if (connections != NULL)
{
for (int i = 0; i < config_setting_length(connections); i++)
{
config_setting_t* connection = config_setting_get_elem(connections, i);
if (connection != NULL)
{
const char* source = config_setting_get_string_elem(connection, 0);
const char* sink = config_setting_get_string_elem(connection, 1);
if (source != NULL && sink != NULL)
{
mod_matrix_connect(source, sink);
unsigned char midi_note = get_matrix_note_number(source, sink);
if (midi_note != 0xff)
{
midi_send(MIDI_ALL_DEVICES, 0x90, midi_channel, midi_note, 0x3c);
}
}
else if (source == NULL)
{
setting_error_report(connection, "Modulation matrix patch data missing source");
}
else if (source == NULL)
{
setting_error_report(connection, "Modulation matrix patch data missing source");
}
}
}
}
else
{
setting_error_report(patch, "Patch contains no modulation matrix data");
}
}
static void mod_matrix_save_callback(void* data, mod_matrix_source_t* source, mod_matrix_sink_t* sink)
{
config_setting_t* connections = (config_setting_t*)data;
config_setting_t* connection = config_setting_add(connections, NULL, CONFIG_TYPE_ARRAY);
if (connection != NULL)
{
config_setting_add(connection, NULL, CONFIG_TYPE_STRING);
config_setting_add(connection, NULL, CONFIG_TYPE_STRING);
if (config_setting_set_string_elem(connection, 0, source->name) == NULL || config_setting_set_string_elem(connection, 1, sink->name) == NULL)
{
LOG_ERROR("Could not set modulation matrix entry in patch for %s-%s", source->name, sink->name);
config_setting_remove_elem(connections, config_setting_index(connection));
}
}
else
{
LOG_ERROR("Could not create modulation matrix entry in patch for %s-%s", source->name, sink->name);
}
}
void mod_matrix_controller_save(config_setting_t* patch)
{
config_setting_t* connections = config_setting_add(patch, CFG_MOD_MATRIX_CONNECTIONS, CONFIG_TYPE_LIST);
if (connections != NULL)
{
mod_matrix_iterate_connections(connections, mod_matrix_save_callback);
}
else
{
LOG_ERROR("Could not create modulation matrix entry in patch");
}
}
int mod_matrix_controller_initialise(config_setting_t *config, config_setting_t *patch)
{
if (parse_matrix_controller_settings(config) == RESULT_ERROR)
{
return RESULT_ERROR;
}
// Turn off all Launchpad LEDs.
midi_send(MIDI_ALL_DEVICES, 0xb0, midi_channel, 0, 0);
if (patch != NULL)
{
mod_matrix_controller_load(patch);
}
return RESULT_OK;
}
void mod_matrix_controller_process_midi(int device_handle, int channel, unsigned char event_type, unsigned char data0, unsigned char data1)
{
if (channel == midi_channel && event_type == 0x90 && data1 == 0x7f)
{
int row = (data0 - MOD_MATRIX_CONTROL_NOTE_BASE) / MOD_MATRIX_CONTROL_NOTE_STRIDE;
int column = (data0 - MOD_MATRIX_CONTROL_NOTE_BASE) % MOD_MATRIX_CONTROL_NOTE_STRIDE;
if (row < row_count && column < column_count)
{
if (mod_matrix_toggle_connection(column_names[column], row_names[row]) == MOD_MATRIX_CONNECTED)
{
midi_send(device_handle, 0x90, midi_channel, data0, 0x3c);
}
else
{
midi_send(device_handle, 0x90, midi_channel, data0, 0x0c);
}
}
}
}
void mod_matrix_controller_deinitialise()
{
// Turn off all Launchpad LEDs.
midi_send(MIDI_ALL_DEVICES, 0xb0, midi_channel, 0, 0);
}