From f2683a4b707cd714b7f540ebf6482563df83d51e Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sat, 8 Apr 2017 14:21:36 +0200 Subject: Near complete rewrite. --- engine.c | 172 ++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 104 insertions(+), 68 deletions(-) (limited to 'engine.c') diff --git a/engine.c b/engine.c index 0d92b50..43892fb 100644 --- a/engine.c +++ b/engine.c @@ -2,6 +2,7 @@ #include #include "config.h" +#include "geometry.h" #include "engine.h" #include "main.h" #include "midi.h" @@ -13,91 +14,131 @@ static int g_selected_string = -1; static LPoint g_render_points[1024]; static int g_render_point_count; -static const int g_harfe_width = 1024; -static const int g_harfe_height = 768; -static int g_factor = 1<<16; +static ConfigSelect g_selected_config; +static void highlight_line(int value, int y, int max_x, uint32_t color); -static inline int scale(int coord) { - return (int)(((((int64_t)coord) << 32) / g_factor ) >> 16); -} +#define scale(X) display_scale_harfe_to_screen(X) void engine_redraw() { - int i; + char text[32]; + char *config_hints[] = { "No", "Harfe", "File", "Edit" }; + int i, MAX_X, MAX_Y, FONT_HEIGHT; + int height = g_max_y - g_min_y; display_redraw(); display_clear(); - display_text(g_harfe_connected ? "online" : "offline", 4, 4, 0xffffffff); + display_getdimensions(&MAX_X, &MAX_Y, &FONT_HEIGHT); - for (i = 0; i < g_string_count; ++i) { - LLine *l = &g_string_conf[i].line; - uint32_t color = i == g_selected_string ? 0xff00ffff : 0x00ffffffff; + snprintf( text, sizeof(text), g_harfe_connected ? "online (%s)" : "offline (%s)", config_hints[(int)g_config_source]); + display_text(text, 8, MAX_Y - 4, g_harfe_connected ? 0x00ff3fff : 0xff003fff ); - display_line_color(scale(l->x0), scale(l->y0), scale(l->x1), scale(l->y1), color); - display_circle(scale(l->x0), scale(l->y0), 4); - display_circle(scale(l->x1), scale(l->y1), 4); + if (height) { + int b = g_midi_three_octave_split_inverse; + int tos1 = g_midi_three_octave_split_1, tos2 = g_midi_three_octave_split_2; + display_text( b ? "+1" : "-1", 4, scale(g_min_y + tos1 * height / 200) + FONT_HEIGHT / 2, 0x007f7f7fff); + display_text( " 0", 4, scale(g_min_y + (tos1 + tos2) * height / 200) + FONT_HEIGHT / 2, 0x007f7f7fff); + display_text( b ? "-1" : "+1", 4, scale(g_min_y + (tos2 / 2 + 50 ) * height / 100) + FONT_HEIGHT / 2, 0x007f7f7fff); - if (g_string_conf[i].playing) - display_text(config_midi_note_to_string(g_string_conf[i].note+12*g_string_conf[i].octave), scale(l->x1) - 20, g_height - 40, color ); - else - display_text(config_midi_note_to_string(g_string_conf[i].note), scale(l->x1) - 20, g_height - 40, color ); + display_line_color(0, scale(g_min_y), MAX_X, scale(g_min_y), 0xff00ffff); + display_line_color(0, scale(g_max_y), MAX_X, scale(g_max_y), 0xff00ffff); + + display_line_color(0, scale(g_min_y + g_midi_two_octave_split * height / 100), MAX_X, scale(g_min_y + g_midi_two_octave_split * height / 100), 0xffff00ff); + + display_line_color(0, scale(g_min_y + tos1 * height / 100), MAX_X, scale(g_min_y + tos1 * height / 100), 0x00ff00ff); + display_line_color(0, scale(g_min_y + tos2 * height / 100), MAX_X, scale(g_min_y + tos2 * height / 100), 0x00ff00ff); + + + switch (g_selected_config) { + case sel_min_y: highlight_line(g_min_y, g_min_y, MAX_X, 0xff00ffff); break; + case sel_max_y: highlight_line(g_max_y, g_max_y, MAX_X, 0xff00ffff); break; + case sel_2_oct: highlight_line(g_midi_two_octave_split, g_min_y + g_midi_two_octave_split * height / 100, MAX_X, 0xffff00ff); break; + case sel_3_oct_top: highlight_line(tos2, g_min_y + tos2 * height / 100, MAX_X, 0x00ff00ff); break; + case sel_3_oct_bottom: highlight_line(tos1, g_min_y + tos1 * height / 100, MAX_X, 0x00ff00ff); break; + case sel_none: break; + } + + for (i = 0; i < g_string_count; ++i) { + LLine *l = &g_string_conf[i].line; + uint32_t color = ( ( i == g_selected_string ) ? 0xff00ffff : 0x00ffffffff ); + uint32_t text_color = ( ( i == g_selected_string ) ? 0xff00ffff : 0x007f7f7fff ); + + int center_y = g_min_y+height/2; + int x2 = l->p1.x; + + if (l->p1.x!=l->p0.x) { + double m = (double)(l->p1.y-l->p0.y) / (double)(l->p1.x-l->p0.x); + double _x2 = ((double)center_y) - (double)l->p0.y + m * (double)l->p0.x; + x2 = (int)(_x2/m); + } + + if (g_string_conf[i].playing) + display_text(config_midi_note_to_string(g_string_conf[i].note+12*g_string_conf[i].octave), scale(x2-20), scale(center_y) + FONT_HEIGHT, text_color ); + else + display_text(config_midi_note_to_string(g_string_conf[i].note), scale(x2-20), scale(center_y) + FONT_HEIGHT, text_color ); + + display_line_color(scale(l->p0.x), scale(l->p0.y), scale(l->p1.x), scale(l->p1.y), color); + display_circle(scale(l->p0.x), scale(l->p0.y), 4); + display_circle(scale(l->p1.x), scale(l->p1.y), 4); + + } } g_selected_string = -1; for (i = 0; i < g_render_point_count; ++i) display_circle_color(scale(g_render_points[i].x), scale(g_render_points[i].y), 4, 0xff0000ff); g_render_point_count = 0; +} - display_line_color(0, scale(g_min_y), g_width, scale(g_min_y), 0xff00ffff); - display_line_color(0, scale(g_max_y), g_width, scale(g_max_y), 0xff00ffff); - - if (g_min_y != g_max_y) { - int height = scale(g_max_y - g_min_y); - - display_line_color(0, scale(g_max_y) - g_midi_two_octave_split * height / 256, g_width, scale(g_max_y) - g_midi_two_octave_split * height / 256, 0xffff00ff); +static void +highlight_line(int value, int y, int max_x, uint32_t color) { + char text[32]; + display_line_color(0, scale(y-10), 10, scale(y), color); + display_line_color(0, scale(y+10), 10, scale(y), color); + display_line_color(max_x, scale(y-10), max_x-10, scale(y), color); + display_line_color(max_x, scale(y+10), max_x-10, scale(y), color); - display_line_color(0, scale(g_max_y) - g_midi_three_octave_split_1 * height / 256, g_width, scale(g_max_y) - g_midi_three_octave_split_1 * height / 256, 0x00ff00ff); - display_line_color(0, scale(g_max_y) - g_midi_three_octave_split_2 * height / 256, g_width, scale(g_max_y) - g_midi_three_octave_split_2 * height / 256, 0x00ff00ff); - } + display_rect_color(max_x/2-40, scale(y+20), 80, 40, 0xffffffff); + display_text(text, max_x/2-20, scale(y+20), 0); } -#endif - void -engine_init() { -#ifndef NO_DISPLAY - g_factor = (g_harfe_width << 16) / g_width; - if ((g_harfe_height << 16) / g_height < g_factor) - g_factor = (g_harfe_height << 16) / g_height; -#endif +engine_select_config(ConfigSelect sel) { + g_selected_config = sel; } -static int -dist_pp(int x0, int y0, int x1, int y1) +ConfigSelect +engine_change_selected(int off) { - return (y0 - y1) * (y0 - y1) + (x0 - x1) * (x0 - x1); + switch(g_selected_config) { + case sel_3_oct_top: + g_midi_three_octave_split_2 += off; + if (g_midi_three_octave_split_2<0) g_midi_three_octave_split_2 = 0; + if (g_midi_three_octave_split_2>100) g_midi_three_octave_split_2 = 100; + break; + case sel_3_oct_bottom: + g_midi_three_octave_split_1 += off; + if (g_midi_three_octave_split_1<0) g_midi_three_octave_split_1 = 0; + if (g_midi_three_octave_split_1>100) g_midi_three_octave_split_1 = 100; + break; + case sel_2_oct: + g_midi_two_octave_split += off; + if (g_midi_two_octave_split<0) g_midi_two_octave_split = 0; + if (g_midi_two_octave_split>100) g_midi_two_octave_split = 100; + break; + default: + break; + } + return g_selected_config; } -// dist is a fixed point with precission of 8 bits -// offs is where on the line segment xy0-xy1 the point's normale hits, -// range 0..65536 (but can extend, if normale hits line outside line segment) -static int -dist_lp(int x0, int y0, int x1, int y1, int xp, int yp, int *offs) -{ - int64_t r = (y1 - y0) * (y1 - y0) + (x1 - x0) * (x1 - x0); - int64_t q1 = (xp - x0) * (y1 - y0) - (yp - y0) * (x1 - x0); - int64_t q2 = (x1 - x0) * (xp - x0) + (y1 - y0) * (yp - y0); +#endif - *offs = (int)((q2 << 16) / r); - return (int)( q1 * q1 * ((y0 - y1) * (y0 - y1) + (x1 - x0) * (x1 - x0)) * 256 / (r * r)); +void +engine_init() { } -static int -dist_pl(LPoint * p, LLine * l, int *offs) -{ - return dist_lp(l->x0, l->y0, l->x1, l->y1, p->x, p->y, offs); -} void engine_handle_point(LPoint * p, uint32_t monotime) @@ -108,9 +149,6 @@ engine_handle_point(LPoint * p, uint32_t monotime) int y_viewfield, pitch_factor = 12; int dv, dt, speed, new_pitch; - // XXX should not be inverted here - p->x = 1024 - p->x; - #ifndef NO_DISPLAY /* Pass to "render thread" */ g_render_points[g_render_point_count] = *p; @@ -120,27 +158,25 @@ engine_handle_point(LPoint * p, uint32_t monotime) /* See which line is closest */ for (i = 0; i < g_string_count; ++i) { int dist = dist_pl(p, &g_string_conf[i].line, &offs); - if ((dist < 256 * 10 * 10 ) && (dist < dist_max)) { + + /* Avoid miss-fires, check if offset is in range -5% - +105% */ + if ((dist < 512 * 10 * 10 ) && (dist < dist_max) && (offs<68812) && (offs>-3276)) { dist_max = dist; saite = i; } } - /* Avoid miss-fires, check if offset is in range -5% - +105% */ - if (offs>68812 || offs<-3276) - return; - if (saite == -1) return; s = g_string_conf + saite; g_selected_string = saite; - y_viewfield = 256 * (g_max_y - p->y) / (g_max_y - g_min_y); + y_viewfield = (100 * (p->y - g_min_y)) / (g_max_y - g_min_y); if (y_viewfield < 0) y_viewfield = 0; - if (y_viewfield >= 256) - y_viewfield = 255; + if (y_viewfield >= 100) + y_viewfield = 100; // Determine octave, if configured switch (s->mode) { @@ -183,7 +219,7 @@ engine_handle_point(LPoint * p, uint32_t monotime) dv = abs(s->start_off - offs); dt = monotime - s->first_time_seen; if (!dt) ++dt; - speed = 1000 * dv / dt; // in offs_prec per second + speed = 1000 * dv / dt; // in offs_prec per second } s->last_off = offs; break; @@ -193,7 +229,7 @@ engine_handle_point(LPoint * p, uint32_t monotime) if (s->modifier == pitch_bend_up) new_pitch = (pitch_factor * (s->start_off - offs)) >> 16; else if (s->modifier == pitch_bend_down) - new_pitch = (pitch_factor * (s->start_off - offs)) >> 16; + new_pitch = (pitch_factor * (offs - s->start_off)) >> 16; else break; // avoid reporting same pitch bend over and over -- cgit v1.2.3