summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2023-06-28 13:51:53 +0200
committerBad Diode <bd@badd10de.dev>2023-06-28 13:51:53 +0200
commit05f3c922d703666e0539f27f6cebfbf67d8cea53 (patch)
tree31bf5b17ac8563d7910c4ad12e24da51c31daa13
parent5d70f83bd9d5e8047a1dc7d00f885d586fe6196c (diff)
downloadlaunchpad-polymaker-05f3c922d703666e0539f27f6cebfbf67d8cea53.tar.gz
launchpad-polymaker-05f3c922d703666e0539f27f6cebfbf67d8cea53.zip
Fix bug with channel selection and add poly to kbd
-rw-r--r--src/app.c56
1 files changed, 27 insertions, 29 deletions
diff --git a/src/app.c b/src/app.c
index b1009da..06f43a9 100644
--- a/src/app.c
+++ b/src/app.c
@@ -507,13 +507,9 @@ app_surface_event(u8 type, u8 index, u8 value) {
507 } 507 }
508 508
509 if (value) { 509 if (value) {
510 hal_send_midi(USBSTANDALONE, NOTEON | channel, note, value); 510 app_midi_event(USBSTANDALONE, NOTEON | channel, note, value);
511 hal_send_midi(USBMIDI, NOTEON | channel, note, value);
512 hal_send_midi(DINMIDI, NOTEON | channel, note, value);
513 } else { 511 } else {
514 hal_send_midi(USBSTANDALONE, NOTEOFF | channel, note, value); 512 app_midi_event(USBSTANDALONE, NOTEOFF | channel, note, value);
515 hal_send_midi(USBMIDI, NOTEOFF | channel, note, value);
516 hal_send_midi(DINMIDI, NOTEOFF | channel, note, value);
517 } 513 }
518 } break; 514 } break;
519 } 515 }
@@ -539,16 +535,21 @@ app_surface_event(u8 type, u8 index, u8 value) {
539void 535void
540app_midi_event(u8 port, u8 status, u8 d1, u8 d2) { 536app_midi_event(u8 port, u8 status, u8 d1, u8 d2) {
541 if (!state.active) { 537 if (!state.active) {
542 hal_send_midi(port, status, d1, d2); 538 hal_send_midi(USBSTANDALONE, status, d1, d2);
539 hal_send_midi(USBMIDI, status, d1, d2);
540 hal_send_midi(DINMIDI, status, d1, d2);
543 return; 541 return;
544 } 542 }
545 u8 channel = status & 0x0F; 543 u8 channel = status & 0x0F;
546 u8 max_voices = (state.ch_max - state.ch_min + 1);
547 // If the channel isn't set to listen, just passthrough the message. 544 // If the channel isn't set to listen, just passthrough the message.
548 if (!state.ch_listen[channel]) { 545 if (!state.ch_listen[channel]) {
549 hal_send_midi(port, status, d1, d2); 546 hal_send_midi(USBSTANDALONE, status, d1, d2);
547 hal_send_midi(USBMIDI, status, d1, d2);
548 hal_send_midi(DINMIDI, status, d1, d2);
550 return; 549 return;
551 } 550 }
551
552 u8 max_voices = (state.ch_max - state.ch_min + 1);
552 if ((status & 0xF0) == NOTEON) { 553 if ((status & 0xF0) == NOTEON) {
553 if (n_voices == max_voices) { 554 if (n_voices == max_voices) {
554 u8 selected = 0; 555 u8 selected = 0;
@@ -590,10 +591,9 @@ app_midi_event(u8 port, u8 status, u8 d1, u8 d2) {
590 591
591 Voice *voice = &voices[selected]; 592 Voice *voice = &voices[selected];
592 voice->active = 0; 593 voice->active = 0;
593 hal_send_midi(voice->port, 594 hal_send_midi(USBSTANDALONE, NOTEOFF | voice->channel, voice->d1, voice->d2);
594 NOTEOFF | voice->channel, 595 hal_send_midi(USBMIDI, NOTEOFF | voice->channel, voice->d1, voice->d2);
595 voice->d1, 596 hal_send_midi(DINMIDI, NOTEOFF | voice->channel, voice->d1, voice->d2);
596 voice->d2);
597 n_voices--; 597 n_voices--;
598 } 598 }
599 599
@@ -603,28 +603,26 @@ app_midi_event(u8 port, u8 status, u8 d1, u8 d2) {
603 // Stop voice if needed. 603 // Stop voice if needed.
604 if (voice->active && voice->d1 == d1) { 604 if (voice->active && voice->d1 == d1) {
605 voice->active = 0; 605 voice->active = 0;
606 hal_send_midi(voice->port, 606 hal_send_midi(USBSTANDALONE, NOTEOFF | voice->channel, voice->d1, voice->d2);
607 NOTEOFF | voice->channel, 607 hal_send_midi(USBMIDI, NOTEOFF | voice->channel, voice->d1, voice->d2);
608 voice->d1, 608 hal_send_midi(DINMIDI, NOTEOFF | voice->channel, voice->d1, voice->d2);
609 voice->d2);
610 break; 609 break;
611 } 610 }
612 } 611 }
613 612
614 // Find next free voice slot. 613 // Find next free voice slot.
615 for (u8 i = state.ch_min; i <= state.ch_max; i++, next_voice++) { 614 for (u8 i = state.ch_min; i <= state.ch_max; i++, next_voice++) {
616 Voice *voice = &voices[(next_voice % max_voices)]; 615 Voice *voice = &voices[state.ch_min + (next_voice % max_voices)];
617 // Register voice. 616 // Register voice.
618 if (!voice->active) { 617 if (!voice->active) {
619 voice->active = 1; 618 voice->active = 1;
620 voice->port = port; 619 voice->port = port;
621 voice->channel = (next_voice % max_voices); 620 voice->channel = state.ch_min + (next_voice % max_voices);
622 voice->d1 = d1; 621 voice->d1 = d1;
623 voice->d2 = d2; 622 voice->d2 = d2;
624 hal_send_midi(voice->port, 623 hal_send_midi(USBSTANDALONE, NOTEON | voice->channel, voice->d1, voice->d2);
625 NOTEON | voice->channel, 624 hal_send_midi(USBMIDI, NOTEON | voice->channel, voice->d1, voice->d2);
626 voice->d1, 625 hal_send_midi(DINMIDI, NOTEON | voice->channel, voice->d1, voice->d2);
627 voice->d2);
628 voice->age = note_counter++; 626 voice->age = note_counter++;
629 next_voice++; 627 next_voice++;
630 break; 628 break;
@@ -636,16 +634,17 @@ app_midi_event(u8 port, u8 status, u8 d1, u8 d2) {
636 Voice *voice = &voices[i]; 634 Voice *voice = &voices[i];
637 if (voice->active && voice->d1 == d1) { 635 if (voice->active && voice->d1 == d1) {
638 voice->active = 0; 636 voice->active = 0;
639 hal_send_midi(voice->port, 637 hal_send_midi(USBSTANDALONE, NOTEOFF | voice->channel, voice->d1, voice->d2);
640 NOTEOFF | voice->channel, 638 hal_send_midi(USBMIDI, NOTEOFF | voice->channel, voice->d1, voice->d2);
641 voice->d1, 639 hal_send_midi(DINMIDI, NOTEOFF | voice->channel, voice->d1, voice->d2);
642 voice->d2);
643 n_voices--; 640 n_voices--;
644 break; 641 break;
645 } 642 }
646 } 643 }
647 } else { 644 } else {
648 hal_send_midi(port, status, d1, d2); 645 hal_send_midi(USBSTANDALONE, status, d1, d2);
646 hal_send_midi(USBMIDI, status, d1, d2);
647 hal_send_midi(DINMIDI, status, d1, d2);
649 } 648 }
650} 649}
651 650
@@ -898,7 +897,6 @@ app_timer_event() {
898 897
899void 898void
900app_init(const u16 *adc_raw) { 899app_init(const u16 *adc_raw) {
901 // example - load button states from flash
902 hal_read_flash(0, (u8*)&state, sizeof(State)); 900 hal_read_flash(0, (u8*)&state, sizeof(State));
903 if (state.magic != 0xbadd10de) { 901 if (state.magic != 0xbadd10de) {
904 state = (State){ 902 state = (State){