/* Peacock-8 VA polysynth Copyright 2024 Gordon JC Pearce 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. */ #include "ic1.hpp" #include "peacock.hpp" 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; default: printf("handle event, status %02x value %02x\n", ev->data[0], ev->data[1]); break; } } void Assigner::noteOff(uint8_t note) { // Poly 1 note off d_debug("Note Off %02x", note); for (uint8_t i = 0; i < NUM_VOICES; i++) { if (keymap[i] == note) { d_debug("Found note at slot %02x, moving to last", i); 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; } } /* 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"); */ } 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) { keymap[i] &= 0x7f; // show note as on d_debug("voice %02x on in slot %02x, existing note %02x", voicemap[i], i, keymap[i]); 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 keymap[i] = note; d_debug("voice %02x on in slot %02x, setting note %02x", voicemap[i], i, keymap[i]); return; } } d_debug("unable to allocate a voice for note %02x\n", note); }