diff options
-rw-r--r-- | src/gui.h | 8 | ||||
-rw-r--r-- | src/synth_engine_v2.c | 29 | ||||
-rw-r--r-- | src/synth_gui.c | 108 |
3 files changed, 109 insertions, 36 deletions
@@ -7,8 +7,16 @@ typedef enum screen_e { SCREEN_ERR } screen_e; +typedef enum popup_e { + POPUP_NONE = 0, + POPUP_SAVE_PATCH, + POPUP_LOAD_PATCH, + POPUP_ERR +} popup_e; + typedef struct synth_gui { screen_e screen; + popup_e popup; int audiomidi_initialized; } synth_gui; diff --git a/src/synth_engine_v2.c b/src/synth_engine_v2.c index 7db9a6f..8740fe0 100644 --- a/src/synth_engine_v2.c +++ b/src/synth_engine_v2.c @@ -149,11 +149,13 @@ do_fliter(synth_t *synth, float *sample, unsigned int sample_rate, int frame) } } - cutoff = 50 + cutoff * fix_adsr(&synth->f_adsr, - note->noteOn, - note->noteOff, - note->elapsed, - note->noteOffSample); + float adsr = fix_adsr(&synth->f_adsr, + note->noteOn, + note->noteOff, + note->elapsed, + note->noteOffSample); + if (adsr < 0.1) adsr = 0.1; + cutoff = cutoff * adsr; } } @@ -274,8 +276,10 @@ make_sample(synth_t * synth, unsigned int sample_rate, int frame) if (synth->clamp) sample = clamp(sample, -1, 1); // autogain - if (synth->autogain && (sample >= 1 || sample <= -1)) { - synth->cc_gain.target *= 0.999; + if (synth->autogain) { + if (sample >= 1 || sample <= -1) { + synth->cc_gain.target *= 0.999; + } } //printf("CLICK! %f\n", fabsf(prev_sample) - fabsf(sample)); @@ -630,7 +634,7 @@ load_synth(synth_t *synth, const char *path) config_init(&cfg); /* Read the file. If there is an error, report it and exit. */ - if(! config_read_file(&cfg, "TEST.cfg")) + if(! config_read_file(&cfg, path)) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); @@ -663,6 +667,7 @@ load_synth(synth_t *synth, const char *path) config_lookup_float(&cfg, "synth.delay.feedback", &FLOAT); synth->cc_del_feedback.target = FLOAT; + config_lookup_int(&cfg, "synth.biquad.enable", &synth->filter); config_lookup_int(&cfg, "synth.filter.enable", &synth->filter); config_lookup_float(&cfg, "synth.filter.cutoff", &FLOAT); synth->cc_cutoff.target = FLOAT; @@ -687,7 +692,7 @@ save_synth(synth_t *synth, const char *path) { (void)path; - static const char *output_file = "TEST.cfg"; + const char *output_file = path;//"TEST.cfg"; config_t cfg; config_setting_t *root, *setting, *group, *adsr, *delay, *lfo, *filter; @@ -699,7 +704,7 @@ save_synth(synth_t *synth, const char *path) group = config_setting_add(root, "synth", CONFIG_TYPE_GROUP); setting = config_setting_add(group, "name", CONFIG_TYPE_STRING); - config_setting_set_string(setting, "example synth name"); + config_setting_set_string(setting, path); setting = config_setting_add(group, "generator", CONFIG_TYPE_INT); config_setting_set_int(setting, synth->geni); @@ -724,6 +729,10 @@ save_synth(synth_t *synth, const char *path) setting = config_setting_add(delay, "feedback", CONFIG_TYPE_FLOAT); config_setting_set_float(setting, synth->cc_del_feedback.target); + filter = config_setting_add(group, "biquad", CONFIG_TYPE_GROUP); + setting = config_setting_add(filter, "enable", CONFIG_TYPE_INT); + config_setting_set_int(setting, synth->biquad); + filter = config_setting_add(group, "filter", CONFIG_TYPE_GROUP); setting = config_setting_add(filter, "enable", CONFIG_TYPE_INT); config_setting_set_int(setting, synth->filter); diff --git a/src/synth_gui.c b/src/synth_gui.c index e1f3101..169d057 100644 --- a/src/synth_gui.c +++ b/src/synth_gui.c @@ -707,10 +707,70 @@ void get_nth_entry(const char *str, int n, char *result) { result[0] = '\0'; } +void draw_load_patch(synth_t *synth) { + static bool save_name_edit = false; + static char save_text[1024] = ""; // TODO default to current patch name + + GuiPanel((Rectangle){ WIDTH / 2.0 - 200, HEIGHT / 2.0 - 140, 400, 280 }, "Save patch"); + if (GuiButton( + (Rectangle){WIDTH / 2.0 - 50 - 50 - 3, HEIGHT / 2.0 - 12, 100, 24}, + "load")) { + if (strlen(save_text) > 0) { + if (load_synth(synth, save_text)) { + fprintf(stderr, "Can't load %s\n", save_text); + return; + } + synth->gui.popup = POPUP_NONE; + } + } + if (GuiButton((Rectangle){WIDTH / 2.0 + 3, HEIGHT / 2.0 - 12, 100, 24}, + "cancel")) { + synth->gui.popup = POPUP_NONE; + } + + + if (GuiTextBox( + (Rectangle){WIDTH / 2.0 - 125, HEIGHT / 2.0 - 18 - 24 - 6, 250, 36}, + save_text, 20, save_name_edit)) { + save_name_edit = true; + } +} + +void draw_save_patch(synth_t *synth) +{ + static bool save_name_edit = false; + static char save_text[1024] = ""; // TODO default to current patch name + + GuiPanel((Rectangle){ WIDTH / 2.0 - 200, HEIGHT / 2.0 - 140, 400, 280 }, "Save patch"); + if (GuiButton( + (Rectangle){WIDTH / 2.0 - 50 - 50 - 3, HEIGHT / 2.0 - 12, 100, 24}, + "save")) { + if (strlen(save_text) > 0) { + if (save_synth(synth, save_text)) { + fprintf(stderr, "Can't save at %s\n", save_text); + return; + } + synth->gui.popup = POPUP_NONE; + } + } + if (GuiButton((Rectangle){WIDTH / 2.0 + 3, HEIGHT / 2.0 - 12, 100, 24}, + "cancel")) { + synth->gui.popup = POPUP_NONE; + } + + + if (GuiTextBox( + (Rectangle){WIDTH / 2.0 - 125, HEIGHT / 2.0 - 18 - 24 - 6, 250, 36}, + save_text, 20, save_name_edit)) { + save_name_edit = true; + } +} + + char *soundcards = NULL; char *midi_devices = NULL; void -draw_audiomidisetup(synth_t *synth, const char *midi_devices) +draw_audiomidisetup(synth_t *synth) { static int edit_midi = 0; static int edit_sound = 0; @@ -727,9 +787,6 @@ draw_audiomidisetup(synth_t *synth, const char *midi_devices) printf("================ AUDIOMIDISETUP GUI INITIALIZED!\n"); } - BeginDrawing(); - ClearBackground(BLACK); - if (GuiButton((Rectangle){WIDTH - 100 - 50 - 6 - 100, 50 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6, 100, 24}, "cancel")) { @@ -767,14 +824,6 @@ draw_audiomidisetup(synth_t *synth, const char *midi_devices) soundcards, &soundcard_pick, edit_sound)) { edit_sound = !edit_sound; } - - /* if (old_midi_device_id != synth->midi_device_id) { */ - /* old_midi_device_id = synth->midi_device_id; */ - /* } */ - - - - EndDrawing(); } void @@ -785,8 +834,6 @@ draw_main(synth_t *synth) if (midi_devices) free(midi_devices); synth->gui.audiomidi_initialized = 0; } - BeginDrawing(); - ClearBackground(BLACK); int fb = 0; int foffset = 9; @@ -850,12 +897,15 @@ draw_main(synth_t *synth) if ( GuiButton((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6, 100, 24 }, "audiomidi")) { synth->gui.screen = SCREEN_AUDIOMIDISETUP; } - if ( GuiButton((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6, 100, 24 }, "SAVE!")) { - save_synth(synth, "asdas"); + if (GuiButton((Rectangle){WIDTH - 100 - 50, + 50 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6, + 100, 24}, + "SAVE!")) { + synth->gui.popup = POPUP_SAVE_PATCH; } if ( GuiButton((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6, 100, 24 }, "LOAD!")) { - load_synth(synth, "asdas"); + synth->gui.popup = POPUP_LOAD_PATCH; } @@ -867,7 +917,6 @@ draw_main(synth_t *synth) /* DrawText("KEYBOARD: Q .. ]", WIDTH / 2 -300, HEIGHT - 300 - 50, 20, LIGHTGRAY); */ /* snprintf(buf, sizeof buf, "stream time: %f", Pa_GetStreamTime(synth->stream)); */ /* DrawText(buf, WIDTH / 2 -300, HEIGHT - 300, 11, LIGHTGRAY); */ - EndDrawing(); } void @@ -878,17 +927,14 @@ rayrun(synth_t *synth){ osc_sound(0); - /* //char *midi_devices = "test1;test2;test3"; */ - char *midi_devices = get_midi_devices(); - /* int old_soundcard_id = synth->soundcard_id; */ - /* int old_midi_device_id = synth->midi_device_id; */ - SetTraceLogLevel(LOG_ERROR); InitWindow(WIDTH, HEIGHT, "Raylib synth"); SetTargetFPS(60); // Set our game to run at 60 frames-per-second while (!WindowShouldClose()) { - keyboard(synth); - mouse(synth); + if (synth->gui.popup == POPUP_NONE) { + keyboard(synth); + mouse(synth); + } if (true || mod++ % 10 == 0) { char b[256]; @@ -927,10 +973,20 @@ rayrun(synth_t *synth){ } // Draw //---------------------------------------------------------------------------------- + BeginDrawing(); + ClearBackground(BLACK); + if (synth->gui.screen == SCREEN_MAIN) draw_main(synth); else if (synth->gui.screen == SCREEN_AUDIOMIDISETUP) - draw_audiomidisetup(synth, midi_devices); + draw_audiomidisetup(synth); + + if (synth->gui.popup == POPUP_SAVE_PATCH) + draw_save_patch(synth); + else if (synth->gui.popup == POPUP_LOAD_PATCH) + draw_load_patch(synth); + + EndDrawing(); //---------------------------------------------------------------------------------- current_time = Pa_GetStreamTime(synth->stream); |