summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorerdgeist <erdgeist@bauklotz.fritz.box>2016-08-12 14:46:51 +0200
committererdgeist <erdgeist@bauklotz.fritz.box>2016-08-12 14:46:51 +0200
commita8be0d3d20f07d4561826b01f566ca307eb23526 (patch)
treeb2c5c6d513ae3a84aba8e4eea94ec32e46d352fa /config.c
commit as a backup
Diffstat (limited to 'config.c')
-rw-r--r--config.c309
1 files changed, 309 insertions, 0 deletions
diff --git a/config.c b/config.c
new file mode 100644
index 0000000..8465dc7
--- /dev/null
+++ b/config.c
@@ -0,0 +1,309 @@
1#include <stdlib.h>
2#include <fcntl.h>
3#include <stdio.h>
4#include <string.h>
5#include <ctype.h>
6
7#include "main.h"
8#include "config.h"
9
10int g_midi_main_control = -1;
11int g_midi_main_channel = 0;
12int g_midi_two_octave_split = 256 / 2;
13int g_midi_three_octave_split_1 = 256 / 3;
14int g_midi_three_octave_split_2 = 512 / 3;
15int g_midi_three_octave_split_inverse = 0;
16int g_settled_dist = 5;
17int g_timetosilence = 30;
18
19int g_min_y = 0, g_max_y;
20
21static char *midi_note[] = {
22 "C-1", "C#-1", "D-1", "D#-1", "E-1", "F-1", "F#-1", "G-1", "G#-1", "A-1", "A#-1", "B-1",
23 "C0", "C#0", "D0", "D#0", "E0", "F0", "F#0", "G0", "G#0", "A0", "A#0", "B0",
24 "C1", "C#1", "D1", "D#1", "E1", "F1", "F#1", "G1", "G#1", "A1", "A#1", "B1",
25 "C2", "C#2", "D2", "D#2", "E2", "F2", "F#2", "G2", "G#2", "A2", "A#2", "B2",
26 "C3", "C#3", "D3", "D#3", "E3", "F3", "F#3", "G3", "G#3", "A3", "A#3", "B3",
27 "C4", "C#4", "D4", "D#4", "E4", "F4", "F#4", "G4", "G#4", "A4", "A#4", "B4",
28 "C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", "G#5", "A5", "A#5", "B5",
29 "C6", "C#6", "D6", "D#6", "E6", "F6", "F#6", "G6", "G#6", "A6", "A#6", "B6",
30 "C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7",
31 "C8", "C#8", "D8", "D#8", "E8", "F8", "F#8", "G8", "G#8", "A8", "A#8", "B8",
32 "C9", "C#9", "D9", "D#9", "E9", "F9", "F#9", "G9", "G#9", "A9", "A#9", "B9"
33};
34
35static uint8_t
36config_midi_note_from_string(char *string)
37{
38 int i;
39
40 for (i = 0; i < sizeof(midi_note) / sizeof(*midi_note); ++i)
41 if (!strncasecmp(string, midi_note[i], strlen(midi_note[i])))
42 return i;
43 return 0xff;
44}
45
46char *config_midi_note_to_string(int string) {
47 return midi_note[string];
48}
49
50#define LINEBUFFER 512
51static char *config_file;
52void
53config_parse(char *config_file_in)
54{
55 FILE *fh;
56 char line_in[LINEBUFFER];
57 int current_string = -1;
58
59 if (!config_file_in && !config_file)
60 return;
61 if (config_file_in)
62 config_file = config_file_in;
63 fh = fopen(config_file, "r");
64
65 if (!fh) {
66 fprintf(stderr, "Couldn't open config file %s, exiting.\n", config_file);
67 exit(1);
68 }
69
70 //Reinitialise string config array
71 memset(g_string_conf, 0, sizeof(g_string_conf));
72 g_max_y = 1024;
73
74 while (!feof(fh)) {
75 char *line = fgets(line_in, LINEBUFFER, fh);
76
77 if (!line)
78 break;
79
80 //Skip leading spaces
81 while (isspace(*line))
82 ++line;
83 if (*line == 0 || *line == '#' || *line == '\n')
84 continue;
85
86 if (!strncasecmp(line, "Strings", 7) && isspace(line[7])) {
87 line += 7;
88 while (isspace(*line))
89 ++line;
90 g_string_count = atol(line);
91 if (!g_string_count || g_string_count > MAX_LINECOUNT) {
92 fprintf(stderr, "Incorrect number of strings: %s\n", line);
93 exit(1);
94 }
95 printf("GLOBAL: Configuring expected lines %d\n", g_string_count);
96 } else if (!strncasecmp(line, "String", 6) && isspace(line[6])) {
97 line += 6;
98 while (isspace(*line))
99 ++line;
100 current_string = atol(line);
101 printf("Switching to string: %d\n", current_string);
102 if (current_string < 0 || current_string > g_string_count) {
103 fprintf(stderr, "Incorrect string selected: %s\n", line);
104 exit(1);
105 }
106 } else if (!strncasecmp(line, "Line", 4) && isspace(line[4])) {
107 LLine *l = &g_string_conf[current_string - 1].line;
108
109 if (current_string == -1) {
110 fprintf(stderr, "No string selected yet.\n");
111 exit(1);
112 }
113 line += 4;
114 while (isspace(*line))
115 ++line;
116 if (sscanf(line, "%d %d %d %d", &l->x0, &l->y0, &l->x1, &l->y1) != 4) {
117 fprintf(stderr, "Incorrect Line statement for string\n");
118 exit(1);
119 }
120 if (l->y0 > l->y1) {
121 l->y0 ^= l->y1;
122 l->y1 ^= l->y0;
123 l->y0 ^= l->y1;
124 l->x0 ^= l->x1;
125 l->x1 ^= l->x0;
126 l->x0 ^= l->x1;
127
128 }
129 if (l->y0 > g_min_y)
130 g_min_y = l->y0;
131 if (l->y1 < g_max_y)
132 g_max_y = l->y1;
133 } else if (!strncasecmp(line, "Mode", 4) && isspace(line[4])) {
134 if (current_string == -1) {
135 fprintf(stderr, "No string selected yet.\n");
136 exit(1);
137 }
138 line += 4;
139 while (isspace(*line))
140 ++line;
141 if (!strncasecmp(line, "midi_one_octave", 15) && (!line[15] || isspace(line[15]))) {
142 g_string_conf[current_string - 1].mode = midi_one_octave;
143 printf("String %d is midi_one_octave\n", current_string);
144 } else if (!strncasecmp(line, "midi_two_octaves", 16) && (!line[16] || isspace(line[16]))) {
145 g_string_conf[current_string - 1].mode = midi_two_octaves;
146 printf("String %d is midi_two_octaves\n", current_string);
147 } else if (!strncasecmp(line, "midi_three_octaves", 18) && (!line[18] || isspace(line[18]))) {
148 g_string_conf[current_string - 1].mode = midi_three_octaves;
149 printf("String %d is midi_three_octaves\n", current_string);
150 } else if (!strncasecmp(line, "midi_control", 12) && (!line[12] || isspace(line[12]))) {
151 g_string_conf[current_string - 1].mode = midi_control;
152 printf("String %d is midi_control\n", current_string);
153 } else if (!strncasecmp(line, "midi_control_inverse", 20) && (!line[20] || isspace(line[20]))) {
154 g_string_conf[current_string - 1].mode = midi_control_inv;
155 printf("String %d is midi_control_inverse\n", current_string);
156 } else {
157 fprintf(stderr, "Illegal Mode for string: %s\n", line);
158 exit(1);
159 }
160 } else if (!strncasecmp(line, "Channel", 7) && isspace(line[7])) {
161 if (current_string == -1) {
162 fprintf(stderr, "No string selected yet.\n");
163 exit(1);
164 }
165 line += 7;
166 while (isspace(*line))
167 ++line;
168 g_string_conf[current_string - 1].channel = atol(line);
169 if (g_string_conf[current_string - 1].channel > 16) {
170 fprintf(stderr, "Incorrect channel specified: %s.\n", line);
171 exit(1);
172 }
173 printf("String %d is on channel %d\n", current_string, g_string_conf[current_string - 1].channel);
174 } else if (!strncasecmp(line, "Note", 4) && isspace(line[4])) {
175 if (current_string == -1) {
176 fprintf(stderr, "No string selected yet.\n");
177 exit(1);
178 }
179 line += 4;
180 while (isspace(*line))
181 ++line;
182 g_string_conf[current_string - 1].note = config_midi_note_from_string(line);
183 if (g_string_conf[current_string - 1].note == 0xff) {
184 fprintf(stderr, "Unknown midi note specified: %s.\n", line);
185 exit(1);
186 }
187 printf("String %d is midi note %d\n", current_string, g_string_conf[current_string - 1].note);
188 } else if (!strncasecmp(line, "AfterTouch", 10) && isspace(line[10])) {
189 if (current_string == -1) {
190 fprintf(stderr, "No string selected yet.\n");
191 exit(1);
192 }
193 line += 10;
194 while (isspace(*line))
195 ++line;
196 if (!strncasecmp(line, "none", 4) && (!line[4] || isspace(line[4]))) {
197 g_string_conf[current_string - 1].modifier = none;
198 printf("String %d does not act aftertouch\n", current_string);
199 } else if (!strncasecmp(line, "pitch_bend_up", 13) && (!line[13] || isspace(line[13]))) {
200 g_string_conf[current_string - 1].modifier = pitch_bend_up;
201 printf("String %d acts aftertouch as pitch_bend_up\n", current_string);
202 } else if (!strncasecmp(line, "pitch_bend_down", 15) && (!line[15] || isspace(line[15]))) {
203 g_string_conf[current_string - 1].modifier = pitch_bend_down;
204 printf("String %d acts aftertouch as pitch_bend_down\n", current_string);
205 } else if (!strncasecmp(line, "midi_control", 12) && (!line[12] || isspace(line[12]))) {
206 g_string_conf[current_string - 1].modifier = midi_controller;
207 printf("String %d acts aftertouch as midi_controller\n", current_string);
208 } else if (!strncasecmp(line, "midi_control_inverse", 20) && (!line[20] || isspace(line[20]))) {
209 g_string_conf[current_string - 1].modifier = midi_controller_inv;
210 printf("String %d acts aftertouch as midi_controller_inverse\n", current_string);
211 } else {
212 fprintf(stderr, "Illegal Modifier for string: %s\n", line);
213 exit(1);
214 }
215 } else if (!strncasecmp(line, "Controller", 10) && isspace(line[10])) {
216 if (current_string == -1) {
217 fprintf(stderr, "No string selected yet.\n");
218 exit(1);
219 }
220 line += 10;
221 while (isspace(*line))
222 ++line;
223 g_string_conf[current_string - 1].controller = atol(line);
224 printf("String %d is on midi_controller line %d\n", current_string, g_string_conf[current_string - 1].controller);
225 } else if (!strncasecmp(line, "TimeToSilence", 13) && isspace(line[13])) {
226 if (current_string == -1) {
227 fprintf(stderr, "No string selected yet.\n");
228 exit(1);
229 }
230 line += 13;
231 while (isspace(*line))
232 ++line;
233 g_string_conf[current_string - 1].timetosilence = atol(line);
234 printf("String %d has a timetosilence of %d\n", current_string, g_string_conf[current_string - 1].timetosilence);
235 } else if (!strncasecmp(line, "midi_two_octave_split", 21) && isspace(line[21])) {
236 line += 21;
237 while (isspace(*line))
238 ++line;
239 g_midi_two_octave_split = atol(line);
240 printf("Splitting TWO octaves at %d%%\n", g_midi_two_octave_split);
241 if (g_midi_two_octave_split < 0 || g_midi_two_octave_split > 100) {
242 fprintf(stderr, "Invalid percentage in line: %s\n", line);
243 exit(1);
244 }
245 g_midi_two_octave_split = (256 * g_midi_two_octave_split) / 100;
246 } else if (!strncasecmp(line, "midi_three_octave_split_1", 25) && isspace(line[25])) {
247 line += 25;
248 while (isspace(*line))
249 ++line;
250 g_midi_three_octave_split_1 = atol(line);
251 printf("Splitting THREE octaves top above %d%%\n", g_midi_three_octave_split_1);
252 if (g_midi_three_octave_split_1 < 0 || g_midi_three_octave_split_1 > 100) {
253 fprintf(stderr, "Invalid percentage in line: %s\n", line);
254 exit(1);
255 }
256 g_midi_three_octave_split_1 = (256 * g_midi_three_octave_split_1) / 100;
257 } else if (!strncasecmp(line, "midi_three_octave_split_2", 25) && isspace(line[25])) {
258 line += 25;
259 while (isspace(*line))
260 ++line;
261 g_midi_three_octave_split_2 = atol(line);
262 printf("Splitting THREE octaves bottom below %d%%\n", g_midi_three_octave_split_2);
263 if (g_midi_three_octave_split_2 < 0 || g_midi_three_octave_split_2 > 100) {
264 fprintf(stderr, "Invalid percentage in line: %s\n", line);
265 exit(1);
266 }
267 g_midi_three_octave_split_2 = (256 * g_midi_three_octave_split_2) / 100;
268 } else if (!strncasecmp(line, "midi_main_control", 17) && isspace(line[17])) {
269 line += 17;
270 while (isspace(*line))
271 ++line;
272 g_midi_main_control = atol(line);
273 printf("All Strings modify controller %d\n", g_midi_main_control);
274 if (g_midi_main_control > 127) {
275 fprintf(stderr, "Invalid controller number %d in line: %s\n", g_midi_main_control, line);
276 exit(1);
277 }
278 } else if (!strncasecmp(line, "midi_main_channel", 17) && isspace(line[17])) {
279 line += 17;
280 while (isspace(*line))
281 ++line;
282 g_midi_main_channel = atol(line);
283 printf("All Strings modify controller %d on channel %d\n", g_midi_main_control, g_midi_main_channel);
284 if (g_midi_main_channel < 1 || g_midi_main_channel > 16) {
285 fprintf(stderr, "Invalid channel number %d in line: %s\n", g_midi_main_channel, line);
286 exit(1);
287 }
288 } else {
289 fprintf(stderr, "Unhandled config line: %s\n", line);
290 exit(1);
291 }
292
293 // split also true for two octaves
294 if (g_midi_three_octave_split_2 < g_midi_three_octave_split_1) {
295 g_midi_three_octave_split_inverse = g_midi_three_octave_split_1;
296 g_midi_three_octave_split_1 = g_midi_three_octave_split_2;
297 g_midi_three_octave_split_2 = g_midi_three_octave_split_inverse;
298 g_midi_three_octave_split_inverse = 1;
299 } else
300 g_midi_three_octave_split_inverse = 0;
301 }
302 fclose(fh);
303}
304
305void
306config_write()
307{
308
309}