From d5e9beaf5cc38c0e57bbc555a6e3054f3bf000d8 Mon Sep 17 00:00:00 2001
From: grm <grm@eyesin.space>
Date: Mon, 24 Feb 2025 20:02:32 +0200
Subject: Add audiomidisetup screen and improve audio device handling

---
 b.c                   |   1 +
 src/sound.c           |  50 ++++----
 src/sound.h           |   3 +-
 src/synth_common.h    |   2 +-
 src/synth_engine.h    |  12 +-
 src/synth_engine_v2.c |  21 ++--
 src/synth_gui.c       | 308 +++++++++++++++++++++++++++++++-------------------
 7 files changed, 245 insertions(+), 152 deletions(-)

diff --git a/b.c b/b.c
index 54e10ab..3056ebf 100644
--- a/b.c
+++ b/b.c
@@ -147,6 +147,7 @@ main(int argc, char *argv[])
     "src/synth_math.h",
     "src/types.h",
     "src/web.h",
+    "src/gui.h",
   };
 
   const char* synth_paths[] = {
diff --git a/src/sound.c b/src/sound.c
index 9cdb75e..2b2ca12 100644
--- a/src/sound.c
+++ b/src/sound.c
@@ -11,6 +11,7 @@ StreamFinished( void* synthData )
   (void)synthData;
 }
 
+// unused
 int
 get_soundcard_id(const char * name)
 {
@@ -19,7 +20,7 @@ get_soundcard_id(const char * name)
   const PaDeviceInfo *deviceInfo;
   for( i=0; i< Pa_GetDeviceCount(); i++ ) {
     deviceInfo = Pa_GetDeviceInfo(i);
-    if (deviceInfo->maxOutputChannels == 0) {
+    if (deviceInfo->maxOutputChannels == 0 || deviceInfo->defaultLowInputLatency < 0) {
       continue;
     }
     if (!strcmp(name, deviceInfo->name)) {
@@ -35,54 +36,54 @@ get_soundcard_id(const char * name)
 char *
 get_soundcards()
 {
-  Pa_Initialize();
   int i;
   const PaDeviceInfo *deviceInfo;
   char *ret = (char *)malloc(sizeof(char) * 4096);
-  strcpy(ret, "");
+  strcpy(ret, "Select Output Device;");
   for( i=0; i< Pa_GetDeviceCount(); i++ ) {
     deviceInfo = Pa_GetDeviceInfo(i);
-    if (deviceInfo->maxOutputChannels == 0) {
+    if (deviceInfo->maxOutputChannels == 0 || deviceInfo->defaultLowInputLatency < 0) {
       continue;
     }
     strcat(ret, deviceInfo->name);
     strcat(ret, ";");
   }
   ret[strlen(ret) - 1] = '\0';
-  Pa_Terminate();
 
   return ret;
 }
 
 void
-init_sound(synth_t * synth, PaStreamCallback *streamCallback, const int device_id)
+init_sound(synth_t * synth, PaStreamCallback *streamCallback, const char* device_name)
 {
-  printf("Before\n");
+  printf("Will try to initialized pulseaudion device [ %s ]\n", device_name);
   Pa_Initialize();
-  printf("after init\n");
 
-  int i, c=0;
+  int i=0;
   const   PaDeviceInfo *deviceInfo;
   for( i=0; i< Pa_GetDeviceCount(); i++ ) {
     deviceInfo = Pa_GetDeviceInfo(i);
-    if (deviceInfo->maxOutputChannels == 0) {
+    if (deviceInfo->maxOutputChannels == 0 ||
+        deviceInfo->defaultLowInputLatency < 0) {
       continue;
     }
-    printf("dev: %s || %f || id:%d out:%d || lil:%f lol:%f\n", deviceInfo->name,
-           deviceInfo->defaultSampleRate, deviceInfo->maxInputChannels,
-           deviceInfo->maxOutputChannels, deviceInfo->defaultLowInputLatency,
-           deviceInfo->defaultLowOutputLatency);
+    /* printf("dev: %s || %f || id:%d out:%d || lil:%f lol:%f\n", deviceInfo->name, */
+    /*        deviceInfo->defaultSampleRate, deviceInfo->maxInputChannels, */
+    /*        deviceInfo->maxOutputChannels, deviceInfo->defaultLowInputLatency, */
+    /*        deviceInfo->defaultLowOutputLatency); */
     
-    if (c == device_id) break;
-    c++;
+    if (!strcmp(deviceInfo->name, device_name)) break;
   }
 
   PaStreamParameters outputParameters;
-  outputParameters.device = i; Pa_GetDefaultOutputDevice(); /* default output device */
-  printf("-------\nSelected device: %s\n-------\n", Pa_GetDeviceInfo(outputParameters.device)->name);
+  outputParameters.device = i;// Pa_GetDefaultOutputDevice(); /* default output device */
+  printf("-------\nSelected device: dev: %s || %f || id:%d out:%d || lil:%f lol:%f\n-------\n", deviceInfo->name,
+           deviceInfo->defaultSampleRate, deviceInfo->maxInputChannels,
+           deviceInfo->maxOutputChannels, deviceInfo->defaultLowInputLatency,
+           deviceInfo->defaultLowOutputLatency);
   outputParameters.channelCount = 2;       /* stereo output */
   outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
-  outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
+  outputParameters.suggestedLatency = deviceInfo->defaultLowOutputLatency;
   outputParameters.hostApiSpecificStreamInfo = NULL;
 
   PaError err;
@@ -96,18 +97,27 @@ init_sound(synth_t * synth, PaStreamCallback *streamCallback, const int device_i
                       synth );
 
   if (err != paNoError) {
-    printf("Error opening stream with %s!!!!!", Pa_GetDeviceInfo(outputParameters.device)->name);
+    printf("Error opening stream with %s!!!!!\n",
+           deviceInfo->name);
+    if (strcmp(device_name, "default")) {
+      printf("Trying to use default device\n");
+      init_sound(synth, streamCallback, "default");
+    }
+    return;
   }
 
   Pa_SetStreamFinishedCallback(synth->stream, &StreamFinished);
   Pa_StartStream(synth->stream);
 
+  printf("<3 <3 <3 <3 Portaudio stream started <3 <3 <3 <3\n");
   synth->sound_active = 1;
 }
 
 void
 destroy_sound(synth_t * synth)
 {
+  printf(":( :( :( stopping stream </3 </3 </3\n");
+  synth->sound_active = 0;
   Pa_StopStream( synth->stream );
   Pa_CloseStream( synth->stream );
   Pa_Terminate();
diff --git a/src/sound.h b/src/sound.h
index 29ec561..db8d581 100644
--- a/src/sound.h
+++ b/src/sound.h
@@ -2,13 +2,12 @@
 #define SOUND_H
 
 #include <portaudio.h>
-
 #include "synth_engine.h"
 
 /* #define SAMPLE_RATE       (44100) */
 /* #define FRAMES_PER_BUFFER (256) */
 
-void init_sound(synth_t * synth, PaStreamCallback *streamCallback, const int device_id);
+void init_sound(synth_t * synth, PaStreamCallback *streamCallback, const char * device_name);
 
 char *get_soundcards();
 int get_soundcard_id(const char * name);
diff --git a/src/synth_common.h b/src/synth_common.h
index 0110597..b9d2b90 100644
--- a/src/synth_common.h
+++ b/src/synth_common.h
@@ -8,7 +8,7 @@
 #define VIZ_BUF 1024
 
 #define WIDTH 1024
-#define HEIGHT 1200
+#define HEIGHT 600
 
 #define MIDI_NOTES 128
 
diff --git a/src/synth_engine.h b/src/synth_engine.h
index 16c2135..21e427c 100644
--- a/src/synth_engine.h
+++ b/src/synth_engine.h
@@ -9,6 +9,7 @@
 #include "adsr.h"
 #include "control.h"
 #include "pa_ringbuffer.h"
+#include "gui.h"
 
 #ifndef M_PI
 #define M_PI (3.14159265)
@@ -16,6 +17,11 @@
 
 #define RING_SIZE 65536
 
+typedef struct soundcard_t {
+  char name[2048];
+  int id;
+} soundcard_t;
+
 typedef struct lfo_t {
   float freq;
   float amp;
@@ -129,12 +135,14 @@ typedef struct {
   int active;
   int sound_active;
 
-  int soundcard_id;
+  soundcard_t soundcard;
   int midi_device_id;
 
   synth_viz viz;
 
-  struct midi_t * midi;
+  struct midi_t *midi;
+
+  synth_gui gui;
 } synth_t;
 
 synth_t * init_synth();
diff --git a/src/synth_engine_v2.c b/src/synth_engine_v2.c
index ad1be72..d4b7509 100644
--- a/src/synth_engine_v2.c
+++ b/src/synth_engine_v2.c
@@ -349,11 +349,11 @@ get_frame(void *outputBuffer, synth_t *synth, int frame)
   *out++ = sample;
   *out++ = sample;
 
-  if (sample > 1.0f || sample < -1.0f)
-    printf("%f\n", sample);
-  if (prev != 0.0f && fabs(prev - sample) > 0.5f) {
-    printf("%.2f  -->  %.2f\n", prev, sample);
-  }
+  /* if (sample > 1.0f || sample < -1.0f) */
+  /*   printf("%f\n", sample); */
+  /* if (prev != 0.0f && fabs(prev - sample) > 0.5f) { */
+  /*   printf("%.2f  -->  %.2f\n", prev, sample); */
+  /* } */
   prev = sample;
 
   // move time
@@ -564,16 +564,18 @@ init_synth(void)
 
   synth->wvt_pos = 0;
 
-
   
   synth->sound_active = 0;
-  synth->soundcard_id = get_soundcard_id("default");
-  init_sound(synth, sound_gen, synth->soundcard_id);
+  strcpy(synth->soundcard.name, "default");
+  init_sound(synth, sound_gen, synth->soundcard.name);
 
   synth->midi = (midi_t *)malloc(sizeof(midi_t));
   synth->midi_device_id = get_midi_device_id("Midi Through Port-0");
   init_midi(synth->midi, synth);
 
+  synth->gui.screen = SCREEN_MAIN;
+  synth->gui.audiomidi_initialized = 0;
+
   return synth;
 }
 
@@ -604,8 +606,7 @@ void
 change_soundcard(synth_t *synth)
 {
   destroy_sound(synth);
-  synth->sound_active = 0;
-  init_sound(synth, sound_gen, synth->soundcard_id);
+  init_sound(synth, sound_gen, synth->soundcard.name);
 }
 
 void
diff --git a/src/synth_gui.c b/src/synth_gui.c
index cb899db..43cfda1 100644
--- a/src/synth_gui.c
+++ b/src/synth_gui.c
@@ -23,6 +23,9 @@ draw_text(synth_t * synth, int x, int y)
   DrawText(buf, x, y + offset * count++, text_size, GRAY);
   snprintf(buf, sizeof buf, "extra pitch    %.4f", synth->cc_pitch.value);
   DrawText(buf, x, y + offset * count++, text_size, GRAY);
+  count++;
+  snprintf(buf, sizeof buf, "%s", synth->soundcard.name);
+  DrawText(buf, x, y + offset * count++, text_size, GRAY);
 
   DrawRectangleLines(x - 6, y - 3, 120, 3 + (count * offset) + 3, GRAY);
 }
@@ -684,6 +687,184 @@ draw_bars(synth_t * synth, int x, int y, int width, int height, int offset)
 #include "sound.h"
 #include "midi.h"
 
+void get_nth_entry(const char *str, int n, char *result) {
+  char temp[strlen(str) + 1]; // Create a modifiable copy
+  strcpy(temp, str);
+
+  char *token = strtok(temp, ";");
+  int count = 0;
+
+  while (token) {
+    if (count == n) {
+      strcpy(result, token); // Replace result content
+      return;
+    }
+    token = strtok(NULL, ";");
+    count++;
+  }
+
+  // If nth entry isn't found, clear result
+  result[0] = '\0';
+}
+
+char *soundcards = NULL;
+void
+draw_audiomidisetup(synth_t *synth, const char *midi_devices)
+{
+  static int edit_midi = 0;
+  static int edit_sound = 0;
+
+  static int pick = 0;
+
+  if (synth->gui.audiomidi_initialized == 0) {
+    soundcards = get_soundcards();
+    pick = 0;
+    synth->gui.audiomidi_initialized = 1;
+    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")) {
+     synth->gui.screen = SCREEN_MAIN;
+  }
+  if (GuiButton((Rectangle){WIDTH - 100 - 50,
+                            50 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6, 100, 24},
+                "apply")) {
+    if (pick != 0) {
+      get_nth_entry(soundcards, pick, synth->soundcard.name);
+      printf("CHANGING TO %s\n", synth->soundcard.name);
+      change_soundcard(synth);
+    }
+    synth->gui.screen = SCREEN_MAIN;
+  }
+
+
+  // audio dev
+  if (GuiDropdownBox((Rectangle){WIDTH - 300 - 50,
+                                 12 + 24 + 6, 300,
+                                 24},
+      soundcards, &pick, edit_sound)) {
+    edit_sound = !edit_sound;
+  }
+
+  /* if (old_soundcard_id != synth->soundcard_id) { */
+  /*   old_soundcard_id = synth->soundcard_id; */
+  /*   get_nth_entry(soundcards, pick, synth->soundcard_name); */
+  /* } */
+  // midi dev
+  if (GuiDropdownBox((Rectangle){WIDTH - 300 - 50,
+                                 12, 200,
+                                 24},
+      midi_devices, &synth->midi_device_id, edit_midi)) {
+    edit_midi = !edit_midi;
+  }
+
+  /* if (old_midi_device_id != synth->midi_device_id) { */
+  /*   old_midi_device_id = synth->midi_device_id; */
+  /*   change_midi_device(synth); */
+  /* } */
+
+
+
+  EndDrawing();
+}
+
+void
+draw_main(synth_t *synth)
+{
+  if (synth->gui.audiomidi_initialized != 0) {
+    if (soundcards) free(soundcards);
+    synth->gui.audiomidi_initialized = 0;
+  }
+  BeginDrawing();
+  ClearBackground(BLACK);
+
+  int fb = 0;
+  int foffset = 9;
+  draw_cc_circle(&synth->cc_adsr_a, 30 + (30 + foffset) * fb++, 180, 30, 30);
+  draw_cc_circle(&synth->cc_adsr_peak, 30 + (30 + foffset) * fb++, 180, 30, 30);
+  draw_cc_circle(&synth->cc_adsr_d, 30 + (30 + foffset) * fb++, 180, 30, 30);
+  draw_cc_circle(&synth->cc_adsr_s, 30 + (30 + foffset) * fb++, 180, 30, 30);
+  draw_cc_circle(&synth->cc_adsr_r, 30 + (30 + foffset) * fb++, 180, 30, 30);
+
+  draw_cc_circle(&synth->cc_pitch, 30, 220, 30, 30);
+
+  fb = 0;
+  draw_cc_circle(&synth->cc_f_adsr_a, 30 + (30 + foffset) * fb++, 260, 30, 30);
+  draw_cc_circle(&synth->cc_f_adsr_peak, 30 + (30 + foffset) * fb++, 260, 30, 30);
+  draw_cc_circle(&synth->cc_f_adsr_d, 30 + (30 + foffset) * fb++, 260, 30, 30);
+  draw_cc_circle(&synth->cc_f_adsr_s, 30 + (30 + foffset) * fb++, 260, 30, 30);
+  draw_cc_circle(&synth->cc_f_adsr_r, 30 + (30 + foffset) * fb++, 260, 30, 30);
+
+  draw_cc_hbar(&synth->cc_cutoff, 30, 300, 256, 24);
+  draw_cc_vbar(&synth->cc_resonance, 330, 20, 24, 256);
+  //draw_cc_vbar(&synth->cc_adsr_s, 30, 250, 30, 30);
+
+
+  // GUI
+  char buf[64];
+  snprintf(buf, sizeof buf, "%d", synth->wvt_pos);
+  synth->wvt_pos = GuiSlider((Rectangle){WIDTH / 2.0 - 108, 150 + 42 + 42, 216, 24 }, "", buf, synth->wvt_pos , 0, 127);
+  set_sound_start(synth->wvt_pos*2048);
+  set_sound_len(synth->wvt_pos*2048 + 2048);
+
+  draw_bars(synth, 20, 20, 200, 16, 3);
+  draw_text(synth, WIDTH / 2 - 108, 20);
+
+  if ( GuiButton((Rectangle){ WIDTH / 2.0 - 108, 150 - 6 - 6 + 42, 216, 6 }, "")) {
+    synth->x = 1;
+  }
+  snprintf(buf, sizeof buf, "%.1f", synth->x);
+  synth->x = GuiSlider((Rectangle){ WIDTH / 2.0 - 108, 150 + 42, 216, 24 }, "x", buf, synth->x , 0.0f, 2.0f);
+
+  synth->filter = GuiToggle((Rectangle){ WIDTH - 100 - 50 - 100 - 50 , 50 , 46, 24 }, "LP", synth->filter);
+  synth->biquad = GuiToggle((Rectangle){ WIDTH - 100 - 50 - 100 - 50 + 46 + 8 , 50 , 46, 24 }, "bq", synth->biquad);
+  if (synth->biquad) {
+    if (GuiToggle((Rectangle){WIDTH - 100 - 50 - 100 - 50 , 50 + 24 + 6 + 24 + 6, 24, 24}, "l", synth->biquad_type == 'l')) {
+      synth->biquad_type = 'l';
+    }
+    if (GuiToggle((Rectangle){WIDTH - 100 - 50 - 100 - 50 + 24 + 8, 50 + 24 + 6 + 24 + 6, 24, 24}, "b", synth->biquad_type == 'b')) {
+      synth->biquad_type = 'b';
+    }
+    if (GuiToggle((Rectangle){WIDTH - 100 - 50 - 100 - 50 + 24 + 8 + 24 + 8 , 50 + 24 + 6 + 24 + 6, 24, 24}, "h", synth->biquad_type == 'h')) {
+      synth->biquad_type = 'h';
+    }
+  }
+
+  GuiSpinner((Rectangle){ WIDTH - 100 - 50 , 50, 100, 24 }, "oct: ", &(synth->octave), 0, 7, 0);
+  snprintf(buf, sizeof buf, "generator %d  --> ", synth->geni);
+  GuiSpinner((Rectangle){ WIDTH - 100 - 50 , 50 + 24 + 6, 100, 24 }, buf, &(synth->geni), 0, 6, 0);
+
+  synth->clamp = GuiToggle((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6, 100, 24 }, "clamp", synth->clamp);
+  synth->delay = GuiToggle((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6 + 24 + 6, 100, 24 }, "delay", synth->delay);
+
+  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 + 24 + 6, 100, 24 }, "LOAD!")) {
+    load_synth(synth, "asdas");
+  }
+
+
+  // signals
+  draw_signals(synth, 20, 390, WIDTH - 2*20, 200);
+  //draw_signals(synth, 300, 390, WIDTH - 2*300, 200);
+
+  //DrawText("THE SYNTH!!!!!!!!!!!!!!!!!!1", WIDTH / 2 - 100, 50, 20, LIGHTGRAY);
+  /* 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
 rayrun(synth_t *synth){
   PaTime current_time = 0;
@@ -691,11 +872,10 @@ rayrun(synth_t *synth){
 
   osc_sound(0);
 
+  /* //char *midi_devices = "test1;test2;test3"; */
   char *midi_devices = get_midi_devices();
-  //char *midi_devices = "test1;test2;test3";
-  char *soundcards = get_soundcards();
-  int old_soundcard_id = synth->soundcard_id;
-  int old_midi_device_id = synth->midi_device_id;
+  /* int old_soundcard_id = synth->soundcard_id; */
+  /* int old_midi_device_id = synth->midi_device_id; */
 
   InitWindow(WIDTH, HEIGHT, "Raylib synth");
   SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
@@ -705,126 +885,20 @@ rayrun(synth_t *synth){
 
     // Draw
     //----------------------------------------------------------------------------------
-    BeginDrawing();
-    ClearBackground(BLACK);
-
-    int fb = 0;
-    int foffset = 9;
-    draw_cc_circle(&synth->cc_adsr_a, 30 + (30 + foffset) * fb++, 180, 30, 30);
-    draw_cc_circle(&synth->cc_adsr_peak, 30 + (30 + foffset) * fb++, 180, 30, 30);
-    draw_cc_circle(&synth->cc_adsr_d, 30 + (30 + foffset) * fb++, 180, 30, 30);
-    draw_cc_circle(&synth->cc_adsr_s, 30 + (30 + foffset) * fb++, 180, 30, 30);
-    draw_cc_circle(&synth->cc_adsr_r, 30 + (30 + foffset) * fb++, 180, 30, 30);
-
-    draw_cc_circle(&synth->cc_pitch, 30, 220, 30, 30);
-
-    fb = 0;
-    draw_cc_circle(&synth->cc_f_adsr_a, 30 + (30 + foffset) * fb++, 260, 30, 30);
-    draw_cc_circle(&synth->cc_f_adsr_peak, 30 + (30 + foffset) * fb++, 260, 30, 30);
-    draw_cc_circle(&synth->cc_f_adsr_d, 30 + (30 + foffset) * fb++, 260, 30, 30);
-    draw_cc_circle(&synth->cc_f_adsr_s, 30 + (30 + foffset) * fb++, 260, 30, 30);
-    draw_cc_circle(&synth->cc_f_adsr_r, 30 + (30 + foffset) * fb++, 260, 30, 30);
-
-    draw_cc_hbar(&synth->cc_cutoff, 30, 300, 256, 24);
-    draw_cc_vbar(&synth->cc_resonance, 330, 20, 24, 256);
-    //draw_cc_vbar(&synth->cc_adsr_s, 30, 250, 30, 30);
-
-
-    // GUI
-    char buf[64];
-    snprintf(buf, sizeof buf, "%d", synth->wvt_pos);
-    synth->wvt_pos = GuiSlider((Rectangle){WIDTH / 2.0 - 108, 150 + 42 + 42, 216, 24 }, "", buf, synth->wvt_pos , 0, 127);
-    set_sound_start(synth->wvt_pos*2048);
-    set_sound_len(synth->wvt_pos*2048 + 2048);
-
-    draw_bars(synth, 20, 20, 200, 16, 3);
-    draw_text(synth, WIDTH / 2 - 108, 20);
-
-    if ( GuiButton((Rectangle){ WIDTH / 2.0 - 108, 150 - 6 - 6 + 42, 216, 6 }, "")) {
-      synth->x = 1;
-    }
-    snprintf(buf, sizeof buf, "%.1f", synth->x);
-    synth->x = GuiSlider((Rectangle){ WIDTH / 2.0 - 108, 150 + 42, 216, 24 }, "x", buf, synth->x , 0.0f, 2.0f);
-
-    synth->filter = GuiToggle((Rectangle){ WIDTH - 100 - 50 - 100 - 50 , 50 , 46, 24 }, "LP", synth->filter);
-    synth->biquad = GuiToggle((Rectangle){ WIDTH - 100 - 50 - 100 - 50 + 46 + 8 , 50 , 46, 24 }, "bq", synth->biquad);
-    if (synth->biquad) {
-      if (GuiToggle((Rectangle){WIDTH - 100 - 50 - 100 - 50 , 50 + 24 + 6 + 24 + 6, 24, 24}, "l", synth->biquad_type == 'l')) {
-	synth->biquad_type = 'l';
-      }
-      if (GuiToggle((Rectangle){WIDTH - 100 - 50 - 100 - 50 + 24 + 8, 50 + 24 + 6 + 24 + 6, 24, 24}, "b", synth->biquad_type == 'b')) {
-	synth->biquad_type = 'b';
-      }
-      if (GuiToggle((Rectangle){WIDTH - 100 - 50 - 100 - 50 + 24 + 8 + 24 + 8 , 50 + 24 + 6 + 24 + 6, 24, 24}, "h", synth->biquad_type == 'h')) {
-	synth->biquad_type = 'h';
-      }
-    }
-
-    GuiSpinner((Rectangle){ WIDTH - 100 - 50 , 50, 100, 24 }, "oct: ", &(synth->octave), 0, 7, 0);
-    snprintf(buf, sizeof buf, "generator %d  --> ", synth->geni);
-    GuiSpinner((Rectangle){ WIDTH - 100 - 50 , 50 + 24 + 6, 100, 24 }, buf, &(synth->geni), 0, 6, 0);
-
-    synth->clamp = GuiToggle((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6, 100, 24 }, "clamp", synth->clamp);
-    synth->delay = GuiToggle((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6 + 24 + 6, 100, 24 }, "delay", synth->delay);
-
-    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 + 24 + 6, 100, 24 }, "LOAD!")) {
-      load_synth(synth, "asdas");
-    }
-
-    static int edit_midi = 0;
-    static int edit_sound = 0;
-
-    // signals
-    draw_signals(synth, 20, 390, WIDTH - 2*20, 200);
-    //draw_signals(synth, 300, 390, WIDTH - 2*300, 200);
-
-    //DrawText("THE SYNTH!!!!!!!!!!!!!!!!!!1", WIDTH / 2 - 100, 50, 20, LIGHTGRAY);
-    /* 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); */
-
-    // midi dev
-    if (GuiDropdownBox(
-            (Rectangle){WIDTH - 200 - 50,
-                        50 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6 , 200,
-                        24},
-                       midi_devices, &synth->midi_device_id, edit_midi)) {
-      edit_midi = !edit_midi;
-    }
-
-    if (old_midi_device_id != synth->midi_device_id) {
-      old_midi_device_id = synth->midi_device_id;
-      change_midi_device(synth);
-    }
-
-    // audio dev
-    if (GuiDropdownBox(
-            (Rectangle){WIDTH - 300 - 50,
-                        12, 300,
-                        24},
-                       soundcards, &synth->soundcard_id, edit_sound)) {
-      edit_sound = !edit_sound;
-    }
-
-    if (old_soundcard_id != synth->soundcard_id) {
-      old_soundcard_id = synth->soundcard_id;
-      change_soundcard(synth);
-    }
-
-    EndDrawing();
+    if (synth->gui.screen == SCREEN_MAIN)
+      draw_main(synth);
+    else if (synth->gui.screen == SCREEN_AUDIOMIDISETUP)
+      draw_audiomidisetup(synth, midi_devices);
     //----------------------------------------------------------------------------------
 
+
     current_time = Pa_GetStreamTime(synth->stream);
     //printf("%f :: %ld\n", current_time - prev_time, phase);
 
     prev_time = current_time;
     (void)prev_time;
   }
-  free(soundcards);
-  free(midi_devices);
+  /* free(soundcards); */
+  /* free(midi_devices); */
   CloseWindow();
 }
-- 
cgit v1.2.3