diff options
| author | grm <grm@eyesin.space> | 2025-10-19 12:44:29 +0300 |
|---|---|---|
| committer | grm <grm@eyesin.space> | 2025-10-19 12:44:29 +0300 |
| commit | bb13b4a409578cf1e9fbca727196f88249fc4b0b (patch) | |
| tree | a1a309981bdfeab29f5cce57a58edb3aab765362 | |
| parent | f0a57d10dd93580ef0b553b8781185fdc09b2da0 (diff) | |
| download | synth-project-bb13b4a409578cf1e9fbca727196f88249fc4b0b.tar.gz synth-project-bb13b4a409578cf1e9fbca727196f88249fc4b0b.tar.bz2 synth-project-bb13b4a409578cf1e9fbca727196f88249fc4b0b.zip | |
Add y-divider, generic vbar and fix fill math
| -rw-r--r-- | src/synth_engine.h | 1 | ||||
| -rw-r--r-- | src/synth_engine_v2.c | 1 | ||||
| -rw-r--r-- | src/synth_gui.c | 114 |
3 files changed, 99 insertions, 17 deletions
diff --git a/src/synth_engine.h b/src/synth_engine.h index 8330daa..d9932d6 100644 --- a/src/synth_engine.h +++ b/src/synth_engine.h @@ -56,6 +56,7 @@ typedef struct synth_viz { int osc_enabled; int freeze; int rate_divider; + float y_divider; float *wave_buffer_data; PaUtilRingBuffer wave_buffer; diff --git a/src/synth_engine_v2.c b/src/synth_engine_v2.c index 14d3e7e..ac248b3 100644 --- a/src/synth_engine_v2.c +++ b/src/synth_engine_v2.c @@ -598,6 +598,7 @@ init_synth(void) synth->fff2 = create_bw_band_stop_filter(8, SAMPLE_RATE, 15000, 22000); synth->viz.rate_divider = 15; + synth->viz.y_divider = 1; // for (int i = 0; i < RING_SIZE; i++) synth->viz.wave_buffer_data[i] = 0; synth->viz.wave_buffer_data = (float *)calloc(sizeof(float), RING_SIZE); PaUtil_InitializeRingBuffer(&synth->viz.wave_buffer, sizeof(float), RING_SIZE, synth->viz.wave_buffer_data); diff --git a/src/synth_gui.c b/src/synth_gui.c index 3fa2536..f96cccb 100644 --- a/src/synth_gui.c +++ b/src/synth_gui.c @@ -285,7 +285,7 @@ draw_wave(synth_t *synth, int x, int y, int width, int height) col = MAGENTA; else col = WHITE; - DrawPixel(i + x , y + height / 2 + (int)floor(50.0 * synth->viz.wave_viz_buffer[ii + j]), col); + DrawPixel(i + x , y + height / 2 + (int)floor(50.0 * synth->viz.wave_viz_buffer[ii + j] * synth->viz.y_divider), col); } } /* for (int j = 0; j < 100; j++) { */ @@ -551,13 +551,105 @@ draw_filter_adsr_graph(synth_t * synth, int x, int y, int width, int height) } } +float +map(float value, float start1, float end1, float start2, float end2) { + return start2 + (end2 - start2) * ((value - start1) / (end1 - start1)); +} + + +/** + * This uses the FNV-1a hash algorithm, which provides better distribution and + * handles any integer values without collisions from the combining operation + * itself. + */ +uint +rect_checksum(int x, int y, int width, int height) { + unsigned int hash = 2166136261u; // FNV offset basis + hash = (hash ^ x) * 16777619u; + hash = (hash ^ y) * 16777619u; + hash = (hash ^ width) * 16777619u; + hash = (hash ^ height) * 16777619u; + return hash; +} + +uint rect_id_pressed = 0; + +float +generic_vbar(float val, float def, float min, float max, int x, int y, int width, int height) { + + Vector2 p = GetMousePosition(); + uint checksum = rect_checksum(x, y, width, height); + + if (CheckCollisionPointRec(p, (Rectangle){x, y, width, height})) { + if (IsMouseButtonPressed(0)) { + rect_id_pressed = checksum; + } + if (IsMouseButtonPressed(1)) { + val = def; + } + } + if (IsMouseButtonDown(0) && rect_id_pressed == checksum) { + if (p.y < y) { + val = max; + } else if (p.y >= y + height) { + val = min; + } else { + val = min + (max - min) * (1.0f - ((p.y - y) / (float)height)); + } + } + if (IsMouseButtonReleased(0) && rect_id_pressed == checksum) { + rect_id_pressed = 0; + } + + int fill_height = map(val, min, max, 0, height - 2); + + DrawRectangleLines(x, y, width, height, WHITE); + DrawRectangle(x + 1, y + height - fill_height - 1, width - 2, fill_height, Fade(MAROON, 0.7f)); + + return val; +} + void draw_signals(synth_t * synth, int x, int y, int width, int height) { DrawRectangleLines(x, y, width, height, WHITE); - if (synth->viz.wave_enabled || synth->viz.spectrum_enabled) - synth->viz.rate_divider = GuiSlider((Rectangle){ x + (width / 2.0) / 2, y - 12, width / 2.0, 12 }, "", NULL, synth->viz.rate_divider , 1, 150); + if (synth->viz.wave_enabled || synth->viz.spectrum_enabled) { + synth->viz.rate_divider = + GuiSlider((Rectangle){x + (width / 2.0) / 2, y - 12, width / 2.0, 12}, + "", NULL, synth->viz.rate_divider, 1, 150); + synth->viz.y_divider = generic_vbar(synth->viz.y_divider, 1.0, 0.5, 3.0, x - 10, y, 10, height); + /* /\* GuiSlider((Rectangle){x - 10, y, 10, height}, "", NULL, *\/ */ + /* /\* synth->viz.y_divider, 0.5, 3); *\/ */ + + /* Vector2 p = GetMousePosition(); */ + + /* if (CheckCollisionPointRec(p, (Rectangle){x - 10, y, 10, height})) { */ + /* if (IsMouseButtonPressed(0)) { */ + /* is_y_divider_pressed = 1; */ + /* } */ + /* } */ + /* float min = 0.5; */ + /* float max = 3; */ + /* if (IsMouseButtonDown(0) && is_y_divider_pressed) { */ + /* if (p.y < y) { */ + /* synth->viz.y_divider = max; */ + /* } else if (p.y >= y + height) { */ + /* synth->viz.y_divider = min; */ + /* } else { */ + /* synth->viz.y_divider = min + max - (min + (max - min)) * ((((p.y - y) * (float)1/height) - 0) / (1 - 0)); */ + /* } */ + /* } */ + /* if (IsMouseButtonReleased(0) && is_y_divider_pressed) { */ + /* is_y_divider_pressed = 0; */ + /* } */ + + /* int fill_height = map(synth->viz.y_divider, min, max, 0, height - 2); */ + + /* DrawRectangleLines(x - 10, y, 10, height, WHITE); */ + /* DrawRectangle(x - 10 + 1, y + height - fill_height - 1, 10 - 2, fill_height, Fade(MAROON, 0.7f)); */ + + } int viz_size = width * synth->viz.rate_divider; @@ -645,11 +737,6 @@ draw_cc_circle(cc_t * cc, int x, int y, int width, int height) { } -float -map(float value, float start1, float end1, float start2, float end2) { - return start2 + (end2 - start2) * ((value - start1) / (end1 - start1)); -} - #define CC_SET(cc, value) cc->mod = value - cc->target #include "web.h" void @@ -665,14 +752,11 @@ draw_cc_hbar(cc_t * cc, int x, int y, int width, int height) { } if (IsMouseButtonDown(0) && flag == cc->name) { if (p.x < x) { - //cc->target = cc->min; cc_set(cc, cc->min); } else if (p.x >= x + width) { - //cc->target = cc->max; cc_set(cc, cc->max); } else { - //cc->target = (cc->min + (cc->max - cc->min) * ((((p.x - x) * (float)1/width) - 0) / (1 - 0))); - cc_set(cc, (cc->min + (cc->max - cc->min) * ((((p.x - x) * (float)1/width) - 0) / (1 - 0)))); + cc_set(cc, cc->min + (cc->max - cc->min) * ((p.x - x) / (float)width)); } char tmp[128] = ""; sprintf(tmp, "%f", cc->value); @@ -699,7 +783,6 @@ draw_cc_hbar(cc_t * cc, int x, int y, int width, int height) { void draw_cc_vbar(cc_t * cc, int x, int y, int width, int height) { char buf1[32], buf2[32]; - //int current = height * (cc->target - cc->min) / (cc->max - cc->min) + cc->min - 1; Vector2 p = GetMousePosition(); @@ -710,14 +793,11 @@ draw_cc_vbar(cc_t * cc, int x, int y, int width, int height) { } if (IsMouseButtonDown(0) && flag == cc->name) { if (p.y < y) { - //cc->target = cc->max; cc_set(cc, cc->max); } else if (p.y >= y + height) { - //cc->target = cc->min; cc_set(cc, cc->min); } else { - //cc->target = cc->min + cc->max - (cc->min + (cc->max - cc->min) * ((((p.y - y) * (float)1/height) - 0) / (1 - 0))); - cc_set(cc, (cc->min + cc->max - (cc->min + (cc->max - cc->min) * ((((p.y - y) * (float)1/height) - 0) / (1 - 0))))); + cc_set(cc, cc->min + (cc->max - cc->min) * (1.0f - ((p.y - y) / (float)height))); } } if (IsMouseButtonReleased(0) && flag == cc->name) { |
