Skip to content

Commit

Permalink
add Velocity to keyjazz
Browse files Browse the repository at this point in the history
  • Loading branch information
m-hayabusa committed May 28, 2022
1 parent cdc9ef1 commit 40dc043
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
35 changes: 31 additions & 4 deletions input.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum keycodes {

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;
Expand Down Expand Up @@ -91,7 +92,7 @@ void close_game_controllers() {
}

static input_msg_s handle_keyjazz(SDL_Event *event, uint8_t keyvalue) {
input_msg_s key = {keyjazz, keyvalue, event->type};
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;
Expand Down Expand Up @@ -184,14 +185,40 @@ static input_msg_s handle_keyjazz(SDL_Event *event, uint8_t keyvalue) {
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:
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:
Expand Down Expand Up @@ -370,7 +397,7 @@ void handle_sdl_events(config_params_s *conf) {

// 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
Expand Down
1 change: 1 addition & 0 deletions input.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ typedef enum special_messages_t {
typedef struct input_msg_s {
input_type_t type;
uint8_t value;
uint8_t value2;
uint32_t eventType;
} input_msg_s;

Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int main(int argc, char *argv[]) {
case keyjazz:
if (input.value != 0) {
if (input.eventType == SDL_KEYDOWN && input.value != prev_input) {
send_msg_keyjazz(port, input.value, 0xFF);
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);
Expand Down
18 changes: 13 additions & 5 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void draw_waveform(struct draw_oscilloscope_waveform_command *command) {
}
}

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;
Expand All @@ -181,19 +181,27 @@ void display_keyjazz_overlay(uint8_t show, uint8_t base_octave) {
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);
Expand Down
2 changes: 1 addition & 1 deletion render.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ int draw_character(struct draw_character_command *command);

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
4 changes: 2 additions & 2 deletions write.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ int send_msg_controller(struct sp_port *port, uint8_t input) {
}

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;
Expand Down

0 comments on commit 40dc043

Please sign in to comment.