Change MIDI velocity implementation to allow direct control of velocity value (#9940)

* Initial changes to expose "real" MIDI velocity

* Change MI_VELU and MI_VELD to fit new logic

* Apply cleanups from fauxpark's review

my bad on the errant whitespace and else {} placements

* Slight changes to MI_VELD to match values when velocity is falling to when it is rising

* Add keycode MI_VEL_0

* Update compute_velocity() to handle MI_VEL_0

* Store velocity as 7 bits to hard cap value at 127
This commit is contained in:
jakobkg 2020-09-20 03:13:45 +02:00 committed by James Young
parent 73d20754bb
commit bf84d35117
No known key found for this signature in database
GPG key ID: 8E1085BF6FCFBD74
3 changed files with 21 additions and 9 deletions

View file

@ -41,12 +41,12 @@ static int8_t midi_modulation_step;
static uint16_t midi_modulation_timer; static uint16_t midi_modulation_timer;
midi_config_t midi_config; midi_config_t midi_config;
inline uint8_t compute_velocity(uint8_t setting) { return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1)); } inline uint8_t compute_velocity(uint8_t setting) { return setting * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN)); }
void midi_init(void) { void midi_init(void) {
midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN; midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
midi_config.transpose = 0; midi_config.transpose = 0;
midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN); midi_config.velocity = 127;
midi_config.channel = 0; midi_config.channel = 0;
midi_config.modulation_interval = 8; midi_config.modulation_interval = 8;
@ -66,7 +66,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) {
case MIDI_TONE_MIN ... MIDI_TONE_MAX: { case MIDI_TONE_MIN ... MIDI_TONE_MAX: {
uint8_t channel = midi_config.channel; uint8_t channel = midi_config.channel;
uint8_t tone = keycode - MIDI_TONE_MIN; uint8_t tone = keycode - MIDI_TONE_MIN;
uint8_t velocity = compute_velocity(midi_config.velocity); uint8_t velocity = midi_config.velocity;
if (record->event.pressed) { if (record->event.pressed) {
if (tone_status[tone] == MIDI_INVALID_NOTE) { if (tone_status[tone] == MIDI_INVALID_NOTE) {
uint8_t note = midi_compute_note(keycode); uint8_t note = midi_compute_note(keycode);
@ -124,19 +124,30 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) {
return false; return false;
case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX: case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
if (record->event.pressed) { if (record->event.pressed) {
midi_config.velocity = keycode - MIDI_VELOCITY_MIN; midi_config.velocity = compute_velocity(keycode - MIDI_VELOCITY_MIN);
dprintf("midi velocity %d\n", midi_config.velocity); dprintf("midi velocity %d\n", midi_config.velocity);
} }
return false; return false;
case MI_VELD: case MI_VELD:
if (record->event.pressed && midi_config.velocity > 0) { if (record->event.pressed && midi_config.velocity > 0) {
midi_config.velocity--; if (midi_config.velocity == 127) {
midi_config.velocity -= 10;
} else if (midi_config.velocity > 12) {
midi_config.velocity -= 13;
} else {
midi_config.velocity = 0;
}
dprintf("midi velocity %d\n", midi_config.velocity); dprintf("midi velocity %d\n", midi_config.velocity);
} }
return false; return false;
case MI_VELU: case MI_VELU:
if (record->event.pressed) { if (record->event.pressed && midi_config.velocity < 127) {
midi_config.velocity++; if (midi_config.velocity < 115) {
midi_config.velocity += 13;
} else {
midi_config.velocity = 127;
}
dprintf("midi velocity %d\n", midi_config.velocity); dprintf("midi velocity %d\n", midi_config.velocity);
} }
return false; return false;

View file

@ -35,7 +35,7 @@ typedef union {
struct { struct {
uint8_t octave : 4; uint8_t octave : 4;
int8_t transpose : 4; int8_t transpose : 4;
uint8_t velocity : 4; uint8_t velocity : 7;
uint8_t channel : 4; uint8_t channel : 4;
uint8_t modulation_interval : 4; uint8_t modulation_interval : 4;
}; };

View file

@ -343,7 +343,8 @@ enum quantum_keycodes {
MI_TRNSU, // transpose up MI_TRNSU, // transpose up
MIDI_VELOCITY_MIN, MIDI_VELOCITY_MIN,
MI_VEL_1 = MIDI_VELOCITY_MIN, MI_VEL_0 = MIDI_VELOCITY_MIN,
MI_VEL_1,
MI_VEL_2, MI_VEL_2,
MI_VEL_3, MI_VEL_3,
MI_VEL_4, MI_VEL_4,