shithub: m8c

Download patch

ref: 1b3b75093f137126b330b95e65709a8e725322af
parent: 6d50e07ce6724df51bf1d628acd40e1f9648403e
parent: 40dc043fdd71e70ec8af367cfb93a53aa8b5602b
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Sat May 28 17:45:30 EDT 2022

Merge pull request #69 from m-hayabusa/keyjazz

improve keyjazz feature

--- a/input.c
+++ b/input.c
@@ -27,6 +27,7 @@
 
 uint8_t keyjazz_enabled = 0;
 uint8_t keyjazz_base_octave = 2;
+uint8_t keyjazz_velocity = 0x64;
 
 static uint8_t keycode = 0; // value of the pressed key
 static int num_joysticks = 0;
@@ -91,7 +92,7 @@
 }
 
 static input_msg_s handle_keyjazz(SDL_Event *event, uint8_t keyvalue) {
-  input_msg_s key = {keyjazz, keyvalue};
+  input_msg_s key = {keyjazz, keyvalue, keyjazz_velocity, event->type};
   switch (event->key.keysym.scancode) {
   case SDL_SCANCODE_Z:
     key.value = keyjazz_base_octave * 12;
@@ -184,7 +185,7 @@
     key.type = normal;
     if (event->type == SDL_KEYDOWN && keyjazz_base_octave > 0) {
       keyjazz_base_octave--;
-      display_keyjazz_overlay(1, keyjazz_base_octave);
+      display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity);
     }
     break;
   case SDL_SCANCODE_KP_MULTIPLY:
@@ -191,9 +192,35 @@
     key.type = normal;
     if (event->type == SDL_KEYDOWN && keyjazz_base_octave < 8) {
       keyjazz_base_octave++;
-      display_keyjazz_overlay(1, keyjazz_base_octave);
+      display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity);
     }
     break;
+  case SDL_SCANCODE_KP_MINUS:
+    key.type = normal;
+    if (event->type == SDL_KEYDOWN) {
+      if ((event->key.keysym.mod & KMOD_ALT) > 0) {
+        if (keyjazz_velocity > 1)
+          keyjazz_velocity -= 1;
+      } else {
+        if (keyjazz_velocity > 0x10)
+          keyjazz_velocity -= 0x10;
+      }
+      display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity);
+    }
+    break;
+  case SDL_SCANCODE_KP_PLUS:
+    key.type = normal;
+    if (event->type == SDL_KEYDOWN) {
+      if ((event->key.keysym.mod & KMOD_ALT) > 0) {
+        if (keyjazz_velocity < 0x7F)
+          keyjazz_velocity += 1;
+      } else {
+        if (keyjazz_velocity < 0x6F)
+          keyjazz_velocity += 0x10;
+      }
+      display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity);
+    }
+    break;
   default:
     key.type = normal;
     break;
@@ -370,7 +397,7 @@
 
     // ESC = toggle keyjazz
     if (event.key.keysym.sym == SDLK_ESCAPE) {
-      display_keyjazz_overlay(toggle_input_keyjazz(), keyjazz_base_octave);
+      display_keyjazz_overlay(toggle_input_keyjazz(), keyjazz_base_octave, keyjazz_velocity);
     }
 
   // Normal keyboard inputs
--- a/input.h
+++ b/input.h
@@ -32,6 +32,8 @@
 typedef struct input_msg_s {
   input_type_t type;
   uint8_t value;
+  uint8_t value2;
+  uint32_t eventType;
 } input_msg_s;
 
 void close_game_controllers();
--- a/main.c
+++ b/main.c
@@ -72,6 +72,7 @@
   #endif
 
   uint8_t prev_input = 0;
+  uint8_t prev_note = 0;
 
   // main loop
   while (run) {
@@ -87,14 +88,15 @@
       }
       break;
     case keyjazz:
-      if (input.value != prev_input) {
-        prev_input = input.value;
-        if (input.value != 0) {
-          send_msg_keyjazz(port, input.value, 0xFF);
-        } else {
-          send_msg_keyjazz(port, 0, 0);
+      if (input.value != 0) {
+        if (input.eventType == SDL_KEYDOWN && input.value != prev_input) {
+          send_msg_keyjazz(port, input.value, input.value2);
+          prev_note = input.value;
+        } else if (input.eventType == SDL_KEYUP && input.value == prev_note) {
+          send_msg_keyjazz(port, 0xFF, 0);
         }
       }
+      prev_input = input.value;
       break;
     case special:
       if (input.value != prev_input) {
--- a/render.c
+++ b/render.c
@@ -165,7 +165,7 @@
   }
 }
 
-void display_keyjazz_overlay(uint8_t show, uint8_t base_octave) {
+void display_keyjazz_overlay(uint8_t show, uint8_t base_octave, uint8_t velocity) {
 
   if (show) {
     struct draw_rectangle_command drc;
@@ -181,19 +181,27 @@
     dcc.background = (struct color){background_color.r, background_color.g,
                                     background_color.b};
     dcc.foreground = (struct color){200, 200, 200};
-    dcc.c = base_octave + 48;
-    dcc.pos.x = 300;
+    dcc.pos.x = 296;
     dcc.pos.y = 226;
 
     draw_character(&dcc);
 
+    char buf[8];
+    sprintf(buf, "%02X %u", velocity, base_octave);
+
+    for (int i = 3; i >= 0; i--){
+      dcc.c = buf[i];
+      draw_character(&dcc);
+      dcc.pos.x -= 8;
+    }
+
   } else {
     struct draw_rectangle_command drc;
     drc.color = (struct color){background_color.r, background_color.g,
                                background_color.b};
-    drc.pos.x = 300;
+    drc.pos.x = 272;
     drc.pos.y = 226;
-    drc.size.width = 20;
+    drc.size.width = 45;
     drc.size.height = 14;
 
     draw_rectangle(&drc);
--- a/render.h
+++ b/render.h
@@ -16,6 +16,6 @@
 
 void render_screen();
 void toggle_fullscreen();
-void display_keyjazz_overlay(uint8_t show, uint8_t base_octave);
+void display_keyjazz_overlay(uint8_t show, uint8_t base_octave, uint8_t velocity);
 
 #endif
\ No newline at end of file
--- a/write.c
+++ b/write.c
@@ -71,8 +71,8 @@
 }
 
 int send_msg_keyjazz(struct sp_port *port, uint8_t note, uint8_t velocity) {
-  if (velocity > 64)
-    velocity = 64;
+  if (velocity > 0x7F)
+    velocity = 0x7F;
   char buf[3] = {'K',note,velocity};
   size_t nbytes = 3;
   int result;
--