summaryrefslogtreecommitdiff
path: root/config.c
blob: 8465dc7dfa3df10095bf8b42497400776a08d694 (plain)
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
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "main.h"
#include "config.h"

int     g_midi_main_control = -1;
int     g_midi_main_channel = 0;
int     g_midi_two_octave_split = 256 / 2;
int     g_midi_three_octave_split_1 = 256 / 3;
int     g_midi_three_octave_split_2 = 512 / 3;
int     g_midi_three_octave_split_inverse = 0;
int     g_settled_dist = 5;
int     g_timetosilence = 30;

int     g_min_y = 0, g_max_y;

static char *midi_note[] = {
        "C-1", "C#-1", "D-1", "D#-1", "E-1", "F-1", "F#-1", "G-1", "G#-1", "A-1", "A#-1", "B-1",
        "C0", "C#0", "D0", "D#0", "E0", "F0", "F#0", "G0", "G#0", "A0", "A#0", "B0",
        "C1", "C#1", "D1", "D#1", "E1", "F1", "F#1", "G1", "G#1", "A1", "A#1", "B1",
        "C2", "C#2", "D2", "D#2", "E2", "F2", "F#2", "G2", "G#2", "A2", "A#2", "B2",
        "C3", "C#3", "D3", "D#3", "E3", "F3", "F#3", "G3", "G#3", "A3", "A#3", "B3",
        "C4", "C#4", "D4", "D#4", "E4", "F4", "F#4", "G4", "G#4", "A4", "A#4", "B4",
        "C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", "G#5", "A5", "A#5", "B5",
        "C6", "C#6", "D6", "D#6", "E6", "F6", "F#6", "G6", "G#6", "A6", "A#6", "B6",
        "C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7",
        "C8", "C#8", "D8", "D#8", "E8", "F8", "F#8", "G8", "G#8", "A8", "A#8", "B8",
        "C9", "C#9", "D9", "D#9", "E9", "F9", "F#9", "G9", "G#9", "A9", "A#9", "B9"
};

static uint8_t
config_midi_note_from_string(char *string)
{
        int i;

        for (i = 0; i < sizeof(midi_note) / sizeof(*midi_note); ++i)
                if (!strncasecmp(string, midi_note[i], strlen(midi_note[i])))
                        return i;
        return 0xff;
}

char *config_midi_note_to_string(int string) {
        return midi_note[string];
}

#define LINEBUFFER 512
static char *config_file;
void
config_parse(char *config_file_in)
{
        FILE *fh;
        char line_in[LINEBUFFER];
        int current_string = -1;

        if (!config_file_in && !config_file)
                return;
        if (config_file_in)
                config_file = config_file_in;
        fh = fopen(config_file, "r");

        if (!fh) {
                fprintf(stderr, "Couldn't open config file %s, exiting.\n", config_file);
                exit(1);
        }

        //Reinitialise string config array
        memset(g_string_conf, 0, sizeof(g_string_conf));
        g_max_y = 1024;

        while (!feof(fh)) {
                char *line = fgets(line_in, LINEBUFFER, fh);

                if (!line)
                        break;

                //Skip leading spaces
                while (isspace(*line))
                        ++line;
                if (*line == 0 || *line == '#' || *line == '\n')
                        continue;

                if (!strncasecmp(line, "Strings", 7) && isspace(line[7])) {
                        line += 7;
                        while (isspace(*line))
                                ++line;
                        g_string_count = atol(line);
                        if (!g_string_count || g_string_count > MAX_LINECOUNT) {
                                fprintf(stderr, "Incorrect number of strings: %s\n", line);
                                exit(1);
                        }
                        printf("GLOBAL: Configuring expected lines %d\n", g_string_count);
                } else if (!strncasecmp(line, "String", 6) && isspace(line[6])) {
                        line += 6;
                        while (isspace(*line))
                                ++line;
                        current_string = atol(line);
                        printf("Switching to string: %d\n", current_string);
                        if (current_string < 0 || current_string > g_string_count) {
                                fprintf(stderr, "Incorrect string selected: %s\n", line);
                                exit(1);
                        }
                } else if (!strncasecmp(line, "Line", 4) && isspace(line[4])) {
                        LLine *l = &g_string_conf[current_string - 1].line;

                        if (current_string == -1) {
                                fprintf(stderr, "No string selected yet.\n");
                                exit(1);
                        }
                        line += 4;
                        while (isspace(*line))
                                ++line;
                        if (sscanf(line, "%d %d %d %d", &l->x0, &l->y0, &l->x1, &l->y1) != 4) {
                                fprintf(stderr, "Incorrect Line statement for string\n");
                                exit(1);
                        }
                        if (l->y0 > l->y1) {
                                l->y0 ^= l->y1;
                                l->y1 ^= l->y0;
                                l->y0 ^= l->y1;
                                l->x0 ^= l->x1;
                                l->x1 ^= l->x0;
                                l->x0 ^= l->x1;

                        }
                        if (l->y0 > g_min_y)
                                g_min_y = l->y0;
                        if (l->y1 < g_max_y)
                                g_max_y = l->y1;
                } else if (!strncasecmp(line, "Mode", 4) && isspace(line[4])) {
                        if (current_string == -1) {
                                fprintf(stderr, "No string selected yet.\n");
                                exit(1);
                        }
                        line += 4;
                        while (isspace(*line))
                                ++line;
                        if (!strncasecmp(line, "midi_one_octave", 15) && (!line[15] || isspace(line[15]))) {
                                g_string_conf[current_string - 1].mode = midi_one_octave;
                                printf("String %d is midi_one_octave\n", current_string);
                        } else if (!strncasecmp(line, "midi_two_octaves", 16) && (!line[16] || isspace(line[16]))) {
                                g_string_conf[current_string - 1].mode = midi_two_octaves;
                                printf("String %d is midi_two_octaves\n", current_string);
                        } else if (!strncasecmp(line, "midi_three_octaves", 18) && (!line[18] || isspace(line[18]))) {
                                g_string_conf[current_string - 1].mode = midi_three_octaves;
                                printf("String %d is midi_three_octaves\n", current_string);
                        } else if (!strncasecmp(line, "midi_control", 12) && (!line[12] || isspace(line[12]))) {
                                g_string_conf[current_string - 1].mode = midi_control;
                                printf("String %d is midi_control\n", current_string);
                        } else if (!strncasecmp(line, "midi_control_inverse", 20) && (!line[20] || isspace(line[20]))) {
                                g_string_conf[current_string - 1].mode = midi_control_inv;
                                printf("String %d is midi_control_inverse\n", current_string);
                        } else {
                                fprintf(stderr, "Illegal Mode for string: %s\n", line);
                                exit(1);
                        }
                } else if (!strncasecmp(line, "Channel", 7) && isspace(line[7])) {
                        if (current_string == -1) {
                                fprintf(stderr, "No string selected yet.\n");
                                exit(1);
                        }
                        line += 7;
                        while (isspace(*line))
                                ++line;
                        g_string_conf[current_string - 1].channel = atol(line);
                        if (g_string_conf[current_string - 1].channel > 16) {
                                fprintf(stderr, "Incorrect channel specified: %s.\n", line);
                                exit(1);
                        }
                        printf("String %d is on channel %d\n", current_string, g_string_conf[current_string - 1].channel);
                } else if (!strncasecmp(line, "Note", 4) && isspace(line[4])) {
                        if (current_string == -1) {
                                fprintf(stderr, "No string selected yet.\n");
                                exit(1);
                        }
                        line += 4;
                        while (isspace(*line))
                                ++line;
                        g_string_conf[current_string - 1].note = config_midi_note_from_string(line);
                        if (g_string_conf[current_string - 1].note == 0xff) {
                                fprintf(stderr, "Unknown midi note specified: %s.\n", line);
                                exit(1);
                        }
                        printf("String %d is midi note %d\n", current_string, g_string_conf[current_string - 1].note);
                } else if (!strncasecmp(line, "AfterTouch", 10) && isspace(line[10])) {
                        if (current_string == -1) {
                                fprintf(stderr, "No string selected yet.\n");
                                exit(1);
                        }
                        line += 10;
                        while (isspace(*line))
                                ++line;
                        if (!strncasecmp(line, "none", 4) && (!line[4] || isspace(line[4]))) {
                                g_string_conf[current_string - 1].modifier = none;
                                printf("String %d does not act aftertouch\n", current_string);
                        } else if (!strncasecmp(line, "pitch_bend_up", 13) && (!line[13] || isspace(line[13]))) {
                                g_string_conf[current_string - 1].modifier = pitch_bend_up;
                                printf("String %d acts aftertouch as pitch_bend_up\n", current_string);
                        } else if (!strncasecmp(line, "pitch_bend_down", 15) && (!line[15] || isspace(line[15]))) {
                                g_string_conf[current_string - 1].modifier = pitch_bend_down;
                                printf("String %d acts aftertouch as pitch_bend_down\n", current_string);
                        } else if (!strncasecmp(line, "midi_control", 12) && (!line[12] || isspace(line[12]))) {
                                g_string_conf[current_string - 1].modifier = midi_controller;
                                printf("String %d acts aftertouch as midi_controller\n", current_string);
                        } else if (!strncasecmp(line, "midi_control_inverse", 20) && (!line[20] || isspace(line[20]))) {
                                g_string_conf[current_string - 1].modifier = midi_controller_inv;
                                printf("String %d acts aftertouch as midi_controller_inverse\n", current_string);
                        } else {
                                fprintf(stderr, "Illegal Modifier for string: %s\n", line);
                                exit(1);
                        }
                } else if (!strncasecmp(line, "Controller", 10) && isspace(line[10])) {
                        if (current_string == -1) {
                                fprintf(stderr, "No string selected yet.\n");
                                exit(1);
                        }
                        line += 10;
                        while (isspace(*line))
                                ++line;
                        g_string_conf[current_string - 1].controller = atol(line);
                        printf("String %d is on midi_controller line %d\n", current_string, g_string_conf[current_string - 1].controller);
                } else if (!strncasecmp(line, "TimeToSilence", 13) && isspace(line[13])) {
                        if (current_string == -1) {
                                fprintf(stderr, "No string selected yet.\n");
                                exit(1);
                        }
                        line += 13;
                        while (isspace(*line))
                                ++line;
                        g_string_conf[current_string - 1].timetosilence = atol(line);
                        printf("String %d has a timetosilence of %d\n", current_string, g_string_conf[current_string - 1].timetosilence);
                } else if (!strncasecmp(line, "midi_two_octave_split", 21) && isspace(line[21])) {
                        line += 21;
                        while (isspace(*line))
                                ++line;
                        g_midi_two_octave_split = atol(line);
                        printf("Splitting TWO octaves at %d%%\n", g_midi_two_octave_split);
                        if (g_midi_two_octave_split < 0 || g_midi_two_octave_split > 100) {
                                fprintf(stderr, "Invalid percentage in line: %s\n", line);
                                exit(1);
                        }
                        g_midi_two_octave_split = (256 * g_midi_two_octave_split) / 100;
                } else if (!strncasecmp(line, "midi_three_octave_split_1", 25) && isspace(line[25])) {
                        line += 25;
                        while (isspace(*line))
                                ++line;
                        g_midi_three_octave_split_1 = atol(line);
                        printf("Splitting THREE octaves top above %d%%\n", g_midi_three_octave_split_1);
                        if (g_midi_three_octave_split_1 < 0 || g_midi_three_octave_split_1 > 100) {
                                fprintf(stderr, "Invalid percentage in line: %s\n", line);
                                exit(1);
                        }
                        g_midi_three_octave_split_1 = (256 * g_midi_three_octave_split_1) / 100;
                } else if (!strncasecmp(line, "midi_three_octave_split_2", 25) && isspace(line[25])) {
                        line += 25;
                        while (isspace(*line))
                                ++line;
                        g_midi_three_octave_split_2 = atol(line);
                        printf("Splitting THREE octaves bottom below %d%%\n", g_midi_three_octave_split_2);
                        if (g_midi_three_octave_split_2 < 0 || g_midi_three_octave_split_2 > 100) {
                                fprintf(stderr, "Invalid percentage in line: %s\n", line);
                                exit(1);
                        }
                        g_midi_three_octave_split_2 = (256 * g_midi_three_octave_split_2) / 100;
                } else if (!strncasecmp(line, "midi_main_control", 17) && isspace(line[17])) {
                        line += 17;
                        while (isspace(*line))
                                ++line;
                        g_midi_main_control = atol(line);
                        printf("All Strings modify controller %d\n", g_midi_main_control);
                        if (g_midi_main_control > 127) {
                                fprintf(stderr, "Invalid controller number %d in line: %s\n", g_midi_main_control, line);
                                exit(1);
                        }
                } else if (!strncasecmp(line, "midi_main_channel", 17) && isspace(line[17])) {
                        line += 17;
                        while (isspace(*line))
                                ++line;
                        g_midi_main_channel = atol(line);
                        printf("All Strings modify controller %d on channel %d\n", g_midi_main_control, g_midi_main_channel);
                        if (g_midi_main_channel < 1 || g_midi_main_channel > 16) {
                                fprintf(stderr, "Invalid channel number %d in line: %s\n", g_midi_main_channel, line);
                                exit(1);
                        }
                } else {
                        fprintf(stderr, "Unhandled config line: %s\n", line);
                        exit(1);
                }

                // split also true for two octaves
                if (g_midi_three_octave_split_2 < g_midi_three_octave_split_1) {
                        g_midi_three_octave_split_inverse = g_midi_three_octave_split_1;
                        g_midi_three_octave_split_1 = g_midi_three_octave_split_2;
                        g_midi_three_octave_split_2 = g_midi_three_octave_split_inverse;
                        g_midi_three_octave_split_inverse = 1;
                } else
                        g_midi_three_octave_split_inverse = 0;
        }
        fclose(fh);
}

void
config_write()
{

}