From a8be0d3d20f07d4561826b01f566ca307eb23526 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Fri, 12 Aug 2016 14:46:51 +0200 Subject: commit as a backup --- config.c | 309 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 config.c (limited to 'config.c') diff --git a/config.c b/config.c new file mode 100644 index 0000000..8465dc7 --- /dev/null +++ b/config.c @@ -0,0 +1,309 @@ +#include +#include +#include +#include +#include + +#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() +{ + +} -- cgit v1.2.3