peacock/plugin/assigner.cpp

113 lines
3.6 KiB
C++
Raw Normal View History

2024-10-09 21:10:51 +00:00
/*
Peacock-8 VA polysynth
Copyright 2024 Gordon JC Pearce <gordonjcp@gjcp.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
2024-10-19 22:11:28 +00:00
#include "assigner.hpp"
2024-10-09 21:10:51 +00:00
2024-10-19 22:11:28 +00:00
#include "voiceboard.hpp"
2024-10-09 21:10:51 +00:00
#include "peacock.hpp"
2024-10-13 22:27:54 +00:00
Assigner ic1;
2024-10-10 09:52:04 +00:00
void Assigner::printMap() {
printf("note memory:\n");
for (uint8_t i = 0; i < NUM_VOICES; i++) printf("%02x ", voicemap[i]);
printf("\n");
for (uint8_t i = 0; i < NUM_VOICES; i++) printf("%02x ", keymap[i]);
printf("\n");
}
2024-10-09 21:10:51 +00:00
Assigner::Assigner() {
// set up the assigner
for (uint8_t i = 0; i < NUM_VOICES; i++) {
voicemap[i] = 0x80 + i;
keymap[i] = 0x3c | 0x80;
}
}
void Assigner::handleMidi(const MidiEvent *ev) {
//
uint8_t status = ev->data[0];
switch (status & 0xf0) {
case 0x80:
noteOff(ev->data[1]);
break;
case 0x90:
noteOn(ev->data[1]);
break;
case 0xb0:
2024-10-18 22:57:14 +00:00
switch (ev->data[1]) {
case 0x01:
ic29.modWheel = ev->data[2];
break;
default:
break;
}
break; // nothing to do here except in special cases where we don't expect the host to pass on controls
2024-10-18 22:57:14 +00:00
case 0xe0: // pitch bend;
ic29.pitchBend = ev->data[2] << 7 | ev->data[1];
break;
2024-10-09 21:10:51 +00:00
default:
2024-10-13 20:07:01 +00:00
d_debug("unhandled MIDI event, status %02x value %02x\n", ev->data[0], ev->data[1]);
2024-10-09 21:10:51 +00:00
break;
}
}
void Assigner::noteOff(uint8_t note) {
// Poly 1 note off
for (uint8_t i = 0; i < NUM_VOICES; i++) {
if (keymap[i] == note) {
uint8_t voice = voicemap[i]; // save the voice
memmove(voicemap + i, voicemap + i + 1, NUM_VOICES - i - 1);
memmove(keymap + i, keymap + i + 1, NUM_VOICES - i - 1);
voicemap[NUM_VOICES - 1] = voice;
keymap[NUM_VOICES - 1] = note | 0x80;
2024-10-13 22:27:54 +00:00
ic29.voiceOff(voice);
2024-10-09 21:10:51 +00:00
}
}
}
2024-10-10 09:52:04 +00:00
2024-10-09 21:10:51 +00:00
void Assigner::noteOn(uint8_t note) {
// Poly 1 assigner
// scan for the same note
for (uint8_t i = 0; i < NUM_VOICES; i++) {
if ((keymap[i] & 0x7f) == note) {
2024-10-10 09:52:04 +00:00
// note found, move it to the front of the queue
uint8_t voice = voicemap[i];
memmove(voicemap + 1, voicemap, i);
memmove(keymap + 1, keymap, i);
keymap[0] = note; // show note as on
voicemap[0] = voice;
2024-10-13 22:27:54 +00:00
ic29.voiceOn(voice, note);
2024-10-09 21:10:51 +00:00
return;
}
}
// note is not in the queue, assign a new voice
for (uint8_t i = 0; i < NUM_VOICES; i++) {
if (keymap[i] & 0x80) {
// voice is free
2024-10-10 09:52:04 +00:00
uint8_t voice = voicemap[i];
memmove(voicemap + 1, voicemap, i);
memmove(keymap + 1, keymap, i);
keymap[0] = note; // show note as on
voicemap[0] = voice;
2024-10-13 22:27:54 +00:00
ic29.voiceOn(voice, note);
2024-10-09 21:10:51 +00:00
return;
}
}
}