From a07981e1721a497a4542ac9ab63032b3ccbd1952 Mon Sep 17 00:00:00 2001
From: grm <grm@eyesin.space>
Date: Mon, 3 Mar 2025 19:51:46 +0200
Subject: ws updating webapge

---
 src/synth_engine_v2.c |  4 +--
 src/synth_gui.c       | 36 +++++++++++++++++++++++++
 src/web.c             | 74 ++++++++++++++++++++++++++-------------------------
 tmpl/index.html       | 55 ++++++++++++++++++++++++++++++--------
 4 files changed, 120 insertions(+), 49 deletions(-)

diff --git a/src/synth_engine_v2.c b/src/synth_engine_v2.c
index 4d9721c..7db9a6f 100644
--- a/src/synth_engine_v2.c
+++ b/src/synth_engine_v2.c
@@ -271,7 +271,7 @@ make_sample(synth_t * synth, unsigned int sample_rate, int frame)
   // band stop for high freqs
   //sample = bw_band_stop(synth->fff2, sample);
 
-  //if (synth->clamp) sample = clamp(sample, -1, 1);
+  if (synth->clamp) sample = clamp(sample, -1, 1);
 
   // autogain
   if (synth->autogain && (sample >= 1 || sample <= -1)) {
@@ -424,7 +424,7 @@ sound_gen(const void *inputBuffer, void *outputBuffer,
     get_frame(buffer, synth, frame);
   }
 
-  smooth_buffer(buffer, framesPerBuffer, 0.1f);
+  //smooth_buffer(buffer, framesPerBuffer, 0.1f);
 
   // output buffer
   for( unsigned long i=0; i<framesPerBuffer * 2; i += 2 ) {
diff --git a/src/synth_gui.c b/src/synth_gui.c
index 215b8f5..e1f3101 100644
--- a/src/synth_gui.c
+++ b/src/synth_gui.c
@@ -874,6 +874,7 @@ void
 rayrun(synth_t *synth){
   PaTime current_time = 0;
   PaTime prev_time = 0;
+  int mod = 0;
 
   osc_sound(0);
 
@@ -889,6 +890,41 @@ rayrun(synth_t *synth){
     keyboard(synth);
     mouse(synth);
 
+    if (true || mod++ % 10 == 0) {
+      char b[256];
+      sprintf(b, "wvt_pos:%d", synth->wvt_pos);
+      ws_send_message(b);
+      sprintf(b, "cci:%d", synth->cci);
+      ws_send_message(b);
+      sprintf(b, "autogain:%d", synth->autogain);
+      ws_send_message(b);
+      sprintf(b, "x:%f", synth->x);
+      ws_send_message(b);
+      sprintf(b, "midi_active_n:%d", synth->midi_active_n);
+      ws_send_message(b);
+      sprintf(b, "octave:%d", synth->octave);
+      ws_send_message(b);
+      sprintf(b, "delay:%d", synth->delay);
+      ws_send_message(b);
+      sprintf(b, "deli:%d", synth->deli);
+      ws_send_message(b);
+      sprintf(b, "f_adsr_enabled:%d", synth->f_adsr_enabled);
+      ws_send_message(b);
+      sprintf(b, "filter:%d", synth->filter);
+      ws_send_message(b);
+      sprintf(b, "biquad:%d", synth->biquad);
+      ws_send_message(b);
+      sprintf(b, "clamp:%d", synth->clamp);
+      ws_send_message(b);
+      sprintf(b, "modi:%d", synth->modi);
+      ws_send_message(b);
+      sprintf(b, "geni:%d", synth->geni );
+      ws_send_message(b);
+      sprintf(b, "active:%d", synth->active);
+      ws_send_message(b);
+      sprintf(b, "sound_active:%d", synth->sound_active);
+      ws_send_message(b);
+    }
     // Draw
     //----------------------------------------------------------------------------------
     if (synth->gui.screen == SCREEN_MAIN)
diff --git a/src/web.c b/src/web.c
index f3aa5f7..afd8c57 100644
--- a/src/web.c
+++ b/src/web.c
@@ -58,6 +58,38 @@ char *read_file_to_string(const char *filename) {
     return buffer;
 }
 
+char *get_from_template() {
+  int size = 1024^3 * 1000;
+  char * ret = (char *)malloc(sizeof(char) * size);
+
+  int pipefd[2];
+  if (pipe(pipefd) == -1) {
+    perror("pipe");
+    return NULL;
+  }
+
+  synth_t * synth = synthx;
+  #define OUT pipefd[1]
+  #define INT(x) dprintf(OUT, "%d", x);
+  #define FLOAT(x) dprintf(OUT, "%f", x);
+  #define STR(x) dprintf(OUT, "%s", x);
+  #define PERCENT dprintf(OUT, "%s", "%");
+  #include "index.html.h"
+  close(pipefd[1]);
+
+  // Read from the pipe into a buffer
+  ssize_t bytes_read = read(pipefd[0], ret, size - 1);
+  if (bytes_read == -1) {
+    perror("read");
+    return NULL;
+  }
+
+  // Null-terminate the ret
+  ret[bytes_read] = '\0';
+  close(pipefd[0]);
+  return ret;
+}
+
 int
 key_to_number(char key) {
   switch (key) {
@@ -164,7 +196,7 @@ static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason,
     break;
   }
   case LWS_CALLBACK_SERVER_WRITEABLE: {
-    printf("\nLWS_CALLBACK_SERVER_WRITEABLE\n\n");
+    printf("LWS_CALLBACK_SERVER_WRITEABLE\n");
     /* size_t msg_len = strlen(message_buffer); */
     /* unsigned char buffer[LWS_PRE + BUFFER_SIZE]; */
     /* memcpy(&buffer[LWS_PRE], message_buffer, msg_len); */
@@ -172,10 +204,12 @@ static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason,
     break;
   }
   case LWS_CALLBACK_HTTP: {
+    html_content = get_from_template();
     snprintf(tmp, sizeof(tmp), html_header, strlen(html_content));
     strcpy(buf, tmp);
     strcat(buf, html_content);
     lws_write(wsi, (unsigned char *)buf, strlen(buf), LWS_WRITE_HTTP);
+    free(html_content);
     break;
   }
   case LWS_CALLBACK_CLOSED: {
@@ -221,40 +255,10 @@ void *websocket_server_thread(void *arg) {
     return NULL;
 }
 
-char *get_from_template() {
-  int size = 1024^3 * 1000;
-  char * ret = (char *)malloc(sizeof(char) * size);
-
-  int pipefd[2];
-  if (pipe(pipefd) == -1) {
-    perror("pipe");
-    return NULL;
-  }
-
-  #define OUT pipefd[1]
-  #define INT(x) dprintf(OUT, "%d", x);
-  #define PERCENT dprintf(OUT, "%s", "%");
-  #include "index.html.h"
-  close(pipefd[1]);
-
-  // Read from the pipe into a buffer
-  ssize_t bytes_read = read(pipefd[0], ret, size - 1);
-  if (bytes_read == -1) {
-    perror("read");
-    return NULL;
-  }
-
-  // Null-terminate the ret
-  ret[bytes_read] = '\0';
-  close(pipefd[0]);
-  return ret;
-}
-
 void
 init_web(synth_t * synth)
 {
   //html_content = read_file_to_string("src/index.html");
-  html_content = get_from_template();
   synthx = synth;
 
   lws_set_log_level(LLL_WARN, NULL);
@@ -274,14 +278,12 @@ free_web()
 
 void ws_send_message(const char *message) {
   if (client_wsi != NULL) {
-    /* strcpy(message_buffer, message); */
-    /* lws_callback_on_writable(client_wsi); */
     size_t msg_len = strlen(message);
     unsigned char buffer[LWS_PRE + BUFFER_SIZE];
     memcpy(&buffer[LWS_PRE], message, msg_len);
     lws_write(client_wsi, &buffer[LWS_PRE], msg_len, LWS_WRITE_TEXT);
-    printf("[WS]: Sent <<%s>>\n", message);
-
-    //lws_write(client_wsi, (unsigned char *)message, strlen(message), LWS_WRITE_TEXT);
+    // printf("[WS]: Sent <<%s>>\n", message);
+    //   do I need this?
+    //lws_callback_on_writable(client_wsi); ???
   }
 }
diff --git a/tmpl/index.html b/tmpl/index.html
index 23a0714..a6ae38b 100644
--- a/tmpl/index.html
+++ b/tmpl/index.html
@@ -4,10 +4,41 @@
     <title>C SYNTH WEB!</title>
   </head>
   <body>
-    <input id='slider' style='width: 100%; height: 200px;' type='range' 
+    <input id='slider' style='width: 100%; height: 200px;' type='range'
            min='1' max='22000' />
     <button onclick='onButtonClick()'>Trigger</button>
     <button id='but'>ws</button>
+
+    <div class="struct">
+        <h4>soundcard_t</h4>
+        <span class="str">$STR(synth->soundcard.name)$</span>
+    </div>
+
+    <div class="struct">
+        <h4>midi_device_t</h4>
+        <span class="str">$STR(synth->midi_device.name)$</span>
+    </div>
+
+    <div class="struct">
+        <h4>synth_t</h4>
+        <span id="wvt_pos"        class="int">wvt_pos: $INT(        synth->wvt_pos        )$</span>
+        <span id="cci"            class="int">cci: $INT(            synth->cci            )$</span>
+        <span id="autogain"       class="int">autogain: $INT(       synth->autogain       )$</span>
+        <span id="x"              class="float">x: $FLOAT(          synth->x              )$</span>
+        <span id="midi_active_n"  class="int">midi_active_n: $INT(  synth->midi_active_n  )$</span>
+        <span id="octave"         class="int">octave: $INT(         synth->octave         )$</span>
+        <span id="delay"          class="int">delay: $INT(          synth->delay          )$</span>
+        <span id="deli"           class="int">deli: $INT(           synth->deli           )$</span>
+        <span id="f_adsr_enabled" class="int">f_adsr_enabled: $INT( synth->f_adsr_enabled )$</span>
+        <span id="filter"         class="int">filter: $INT(         synth->filter         )$</span>
+        <span id="biquad"         class="int">biquad: $INT(         synth->biquad         )$</span>
+        <span id="clamp"          class="int">clamp: $INT(          synth->clamp          )$</span>
+        <span id="modi"           class="int">modi: $INT(           synth->modi           )$</span>
+        <span id="geni"           class="int">geni: $INT(           synth->geni           )$</span>
+        <span id="active"         class="int">active: $INT(         synth->active         )$</span>
+        <span id="sound_active"   class="int">sound_active: $INT(   synth->sound_active   )$</span>
+    </div>
+
     <script>
       const ws = new WebSocket('ws://10.0.0.10:9967');
       const slider = document.getElementById('slider');
@@ -16,8 +47,10 @@
       but.onmousedown = function() { ws.send('note_on'); };
       but.onmouseup = function() { ws.send('note_off'); }
       ws.onmessage = function(event) {
-          console.log('Message from server: ' + event.data);
-          slider.value = parseInt(event.data);
+          //console.log('Message from server: ' + event.data);
+          const parts = event.data.split(":");
+          document.getElementById(parts[0]).innerText = parts[0] +": " + parts[1];
+          //slider.value = parseInt(event.data);
       };
       ws.onopen = function() {
           console.log('Connected to WebSocket server');
@@ -55,14 +88,14 @@
       document.addEventListener("keydown", (event) => sendKeyEvent("keydown", event));
       document.addEventListener("keyup", (event) => sendKeyEvent("keyup", event));
 
-      var fps = 60;
-      
-      function loop() {
-          //document.getElementById("test").innerText = ;
-
-          setTimeout(loop, 1000 / fps);
-      }
-      loop();
+      // var fps = 60;
+      // var count = 0;
+      // function loop() {
+      //     //document.getElementById("test").innerText = ;
+      //     document.getElementById("test").innerText = "Count: " + count++;
+      //     setTimeout(loop, 1000 / fps);
+      // }
+      // loop();
     </script>
   </body>
 </html>
-- 
cgit v1.2.3