commit 31e257497383ffad3b12c26d0b8c78f0260e2b92
parent f9390d3c9c4c113527febed2f3b9a2a5196092ec
Author: krasjet
Date: 2020-10-14 22:33Z

add keybinds to change velocity

Diffstat:
Mconfig.def.h | 56+++++++++++++++++++++++++++++---------------------------
Mjack.c | 27+++++++++++----------------
Mjack.h | 12++++++++----
Mkb.c | 17+++++++++++++++--
4 files changed, 63 insertions(+), 49 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -1,4 +1,4 @@ -/* config file for kb */ +/* config file for kb, it will be pasted to kb.c during compilation */ /* * The keybindings for kb. @@ -7,13 +7,13 @@ * * KeyCode -> opcode_t * - * You need to fill in this array in `init_keybinds`. + * You need to fill in this array in `keybinds_init`. */ static opcode_t keybinds[MAX_KEYCODE + 1]; /* - * The following struct and array are defined only for convienience. - * They are not required. + * The following struct and array are defined only for easier configuration. + * They are not used anywhere in kb.c. */ /* KeySym -> opcode_t */ struct SymBind { @@ -21,38 +21,40 @@ struct SymBind { opcode_t opcode; }; +/* Change keybindings here */ static struct SymBind symbinds[] = { /* key opcode */ - { XK_A, OP_C }, - { XK_S, OP_D }, - { XK_D, OP_E }, - { XK_F, OP_F }, - { XK_G, OP_G }, - { XK_H, OP_A }, - { XK_J, OP_B }, - { XK_W, OP_CSHARP }, - { XK_E, OP_DSHARP }, - { XK_T, OP_FSHARP }, - { XK_Y, OP_GSHARP }, - { XK_U, OP_ASHARP }, - { XK_K, OP_C + 12 }, /* next octave */ - { XK_L, OP_D + 12 }, - { XK_comma, OP_E + 12 }, - { XK_apostrophe, OP_F + 12 }, - { XK_O, OP_CSHARP + 12 }, - { XK_P, OP_DSHARP + 12 }, - { XK_bracketright, OP_FSHARP + 12 }, - { XK_X, OP_INC_OCTAVE }, - { XK_Z, OP_DEC_OCTAVE }, + { XK_A, OP_C }, + { XK_S, OP_D }, + { XK_D, OP_E }, + { XK_F, OP_F }, + { XK_G, OP_G }, + { XK_H, OP_A }, + { XK_J, OP_B }, + { XK_W, OP_CSHARP }, + { XK_E, OP_DSHARP }, + { XK_T, OP_FSHARP }, + { XK_Y, OP_GSHARP }, + { XK_U, OP_ASHARP }, + { XK_K, OP_C + 12 }, /* next octave */ + { XK_L, OP_D + 12 }, + { XK_comma, OP_E + 12 }, + { XK_apostrophe, OP_F + 12 }, + { XK_O, OP_CSHARP + 12 }, + { XK_P, OP_DSHARP + 12 }, + { XK_bracketright, OP_FSHARP + 12 }, + { XK_X, OP_INC_OCTAVE }, + { XK_Z, OP_DEC_OCTAVE }, + { XK_V, OP_INC_VELOCITY }, + { XK_C, OP_DEC_VELOCITY }, }; /* * This function will be called in main to initialize keybindings. - * * You need to initialize the `keybinds` variable in this function. */ void -init_keybinds(Display *d) { +keybinds_init(Display *d) { size_t i; for (i = 0; i < sizeof(keybinds)/sizeof(keybinds[0]); ++i) { diff --git a/jack.c b/jack.c @@ -47,15 +47,15 @@ shutdown_cb(void *arg) die("jack server is down, exiting..."); } -int -write_note_on(char channel, char pitch) { +static int +write_midi(char *msg, size_t size) +{ size_t avail_write = jack_ringbuffer_write_space(buffer); - char msg[] = { 0x90 | channel, pitch, 0x40 }; /* for now, we fix vel at 64 */ - if (avail_write < sizeof(msg)) { + if (avail_write < size) { return 0; /* no space left */ } - if (jack_ringbuffer_write(buffer, msg, sizeof(msg)) < sizeof(msg)) { + if (jack_ringbuffer_write(buffer, msg, size) < size) { return 0; /* write failed */ } @@ -63,18 +63,13 @@ write_note_on(char channel, char pitch) { } int -write_note_off(char channel, char pitch) { - size_t avail_write = jack_ringbuffer_write_space(buffer); - char msg[] = { 0x80 | channel, pitch, 0x40 }; /* for now, we fix vel at 64 */ - - if (avail_write < sizeof(msg)) { - return 0; /* no space left */ - } - if (jack_ringbuffer_write(buffer, msg, sizeof(msg)) < sizeof(msg)) { - return 0; /* write failed */ - } +write_note_on(char channel, char pitch, char vel) { + return write_midi((char[]) {0x90 | channel, pitch, vel}, MSG_SIZE); +} - return 1; +int +write_note_off(char channel, char pitch, char vel) { + return write_midi((char[]) {0x80 | channel, pitch, vel}, MSG_SIZE); } void diff --git a/jack.h b/jack.h @@ -7,7 +7,9 @@ enum { /* valid MIDI notes 0-127 */ MAX_MIDI_NOTE = 127, MAX_OCTAVE = 10, - MIN_OCTAVE = 0 + MIN_OCTAVE = 0, + MAX_VELOCITY = 127, + MIN_VELOCITY = 0 }; /* @@ -17,9 +19,11 @@ enum { * < 0 values represent operations */ typedef enum { - OP_INVALID = -3, + OP_INVALID = -5, OP_INC_OCTAVE, OP_DEC_OCTAVE, + OP_INC_VELOCITY, + OP_DEC_VELOCITY, OP_C = 0, OP_CSHARP, OP_D, @@ -38,12 +42,12 @@ typedef enum { * writes NoteOn midi message to buffer. * return: 1 on success, 0 on failure. */ -int write_note_on(char channel, char pitch); +int write_note_on(char channel, char pitch, char vel); /* * writes NoteOff midi message to buffer. * return: 1 on success, 0 on failure. */ -int write_note_off(char channel, char pitch); +int write_note_off(char channel, char pitch, char vel); /* init jack */ void jack_init(void); diff --git a/kb.c b/kb.c @@ -37,6 +37,7 @@ main(int argc, char *argv[]) int channel = 0; char pressed[MAX_KEYCODE + 1] = { 0 }; int octave = 5; /* counting from midi 0, 0 <= octave <=10 */ + int velocity = 64; int c; while ((c = getopt(argc, argv, "c:")) != -1) { @@ -81,7 +82,7 @@ main(int argc, char *argv[]) /* play note */ note = octave * 12 + keybinds[keycode]; if (note <= MAX_MIDI_NOTE) - write_note_on(channel, note); + write_note_on(channel, note, velocity); } else { /* opeartions */ switch (keybinds[keycode]) { @@ -93,6 +94,18 @@ main(int argc, char *argv[]) if (octave > MIN_OCTAVE) octave--; break; + case OP_INC_VELOCITY: + if (velocity + 10 <= MAX_VELOCITY) + velocity += 10; + else + velocity = MAX_VELOCITY; + break; + case OP_DEC_VELOCITY: + if (velocity - 10 >= MIN_VELOCITY) + velocity -= 10; + else + velocity = MIN_VELOCITY; + break; default: break; } } @@ -110,7 +123,7 @@ main(int argc, char *argv[]) note = octave * 12 + keybinds[keycode]; if (keybinds[keycode] >= 0 && note <= MAX_MIDI_NOTE) { - write_note_off(channel, note); + write_note_off(channel, note, velocity); } } break;