static void filter_preproc_mapkeyscale(MidiFilter* self) { int i; int identical_cfg = 1; int keymap[12]; for (i=0; i < 12; ++i) { keymap[i] = RAIL(floorf(*self->cfg[i+1]), -13, 12); if (floorf(self->lcfg[i+1]) != floorf(*self->cfg[i+1])) { identical_cfg = 0; } } if (identical_cfg) return; int c,k; uint8_t buf[3]; buf[2] = 0; for (c=0; c < 16; ++c) { for (k=0; k < 127; ++k) { int note; const int n = 1 + k%12; if (!self->memCM[c][k]) continue; if (floorf(self->lcfg[n]) == floorf(*self->cfg[n])) continue; note = k + self->memCI[c][k]; if (midi_valid(note)) { note = midi_limit_val(note); if (self->memCS[c][note] > 0) { self->memCS[c][note]--; if (self->memCS[c][note] == 0) { buf[0] = MIDI_NOTEOFF | c; buf[1] = note; buf[2] = 0; forge_midimessage(self, 0, buf, 3); } } } note = k + keymap[k%12]; if (midi_valid(note)) { note = midi_limit_val(note); buf[0] = MIDI_NOTEON | c; buf[1] = note; buf[2] = self->memCM[c][k]; self->memCI[c][k] = note - k; self->memCS[c][note]++; if (self->memCS[c][note] == 1) { forge_midimessage(self, 0, buf, 3); } } else { self->memCM[c][k] = 0; self->memCI[c][k] = -1000; } } } }
static inline void filter_midichord_noteon(MidiFilter* self, uint32_t tme, uint8_t chn, int note, uint8_t vel) { uint8_t buf[3]; if (!midi_valid(note)) return; buf[0] = MIDI_NOTEON | chn; buf[1] = note; buf[2] = vel; self->memCS[chn][note]++; if (self->memCS[chn][note] == 1) { forge_midimessage(self, tme, buf, 3); } }
static void filter_midi_enforcescale(MidiFilter* self, uint32_t tme, const uint8_t* const buffer, uint32_t size) { const int chs = midi_limit_chn(floorf(*self->cfg[0]) -1); const int scale = RAIL(floorf(*self->cfg[1]), 0, 11); const int mode = RAIL(floorf(*self->cfg[2]), 0, 2); const uint8_t chn = buffer[0] & 0x0f; const uint8_t key = buffer[1] & 0x7f; uint8_t mst = buffer[0] & 0xf0; if (midi_is_panic(buffer, size)) { filter_enforcescale_panic(self, chn, tme); } if (size != 3 || !(mst == MIDI_NOTEON || mst == MIDI_NOTEOFF || mst == MIDI_POLYKEYPRESSURE) || !(floorf(*self->cfg[0]) == 0 || chs == chn) ) { forge_midimessage(self, tme, buffer, size); return; } int transp = 0; if (!filter_enforcescale_check(scale, key)) { switch (mode) { case 1: transp = -1; break; case 2: transp = +1; break; case 0: /* discard */ default: return; } } if (!midi_valid(key + transp)) { return; } if (!filter_enforcescale_check(scale, key + transp)) { return; } int note; uint8_t buf[3]; memcpy(buf, buffer, 3); switch (mst) { case MIDI_NOTEON: note = key + transp; if (midi_valid(note)) { buf[1] = note; self->memCS[chn][note]++; if (self->memCS[chn][note] == 1) forge_midimessage(self, tme, buf, size); } self->memCI[chn][key] = transp; break; case MIDI_NOTEOFF: note = key + self->memCI[chn][key]; if (midi_valid(note)) { buf[1] = note; if (self->memCS[chn][note] > 0) { self->memCS[chn][note]--; if (self->memCS[chn][note] == 0) forge_midimessage(self, tme, buf, size); self->memCI[chn][key] = 0; } } break; case MIDI_POLYKEYPRESSURE: note = key + transp; if (midi_valid(note)) { buf[1] = note; forge_midimessage(self, tme, buf, size); } break; } }
static void filter_midi_mapkeyscale(MidiFilter* self, uint32_t tme, const uint8_t* const buffer, uint32_t size) { int i; const int chs = midi_limit_chn(floorf(*self->cfg[0]) -1); int keymap[12]; for (i=0; i < 12; ++i) { keymap[i] = RAIL(floorf(*self->cfg[i+1]), -13, 12); } const uint8_t chn = buffer[0] & 0x0f; uint8_t mst = buffer[0] & 0xf0; if (midi_is_panic(buffer, size)) { filter_mapkeyscale_panic(self, chn, tme); } if (size != 3 || !(mst == MIDI_NOTEON || mst == MIDI_NOTEOFF || mst == MIDI_POLYKEYPRESSURE) || !(floorf(*self->cfg[0]) == 0 || chs == chn) ) { forge_midimessage(self, tme, buffer, size); return; } const uint8_t key = buffer[1] & 0x7f; const uint8_t vel = buffer[2] & 0x7f; if (mst == MIDI_NOTEON && vel ==0 ) { mst = MIDI_NOTEOFF; } int note; uint8_t buf[3]; memcpy(buf, buffer, 3); switch (mst) { case MIDI_NOTEON: if (keymap[key%12] < -12) return; note = key + keymap[key%12]; // TODO keep track of dup result note-on -- see enforcescale.c if (midi_valid(note)) { buf[1] = note; self->memCS[chn][note]++; if (self->memCS[chn][note] == 1) { forge_midimessage(self, tme, buf, size); } self->memCM[chn][key] = vel; self->memCI[chn][key] = note - key; } break; case MIDI_NOTEOFF: note = key + self->memCI[chn][key]; if (midi_valid(note)) { buf[1] = note; if (self->memCS[chn][note] > 0) { self->memCS[chn][note]--; if (self->memCS[chn][note] == 0) forge_midimessage(self, tme, buf, size); } } self->memCM[chn][key] = 0; self->memCI[chn][key] = -1000; break; case MIDI_POLYKEYPRESSURE: if (keymap[key%12] < -12) return; note = key + keymap[key%12]; if (midi_valid(note)) { buf[1] = note; forge_midimessage(self, tme, buf, size); } break; } }
static void filter_midi_midichord(MidiFilter* self, uint32_t tme, const uint8_t* const buffer, uint32_t size) { int i; const int chs = midi_limit_chn(floor(*self->cfg[0]) -1); const int scale = RAIL(floor(*self->cfg[1]), 0, 11); int chord = 0; for (i=0; i < 10 ; ++i) { if ((*self->cfg[i+2]) != 0) chord |= 1<<i; } const uint8_t chn = buffer[0] & 0x0f; uint8_t mst = buffer[0] & 0xf0; if (midi_is_panic(buffer, size)) { filter_midichord_panic(self, chn, tme); } if (size != 3 || !(mst == MIDI_NOTEON || mst == MIDI_NOTEOFF || mst == MIDI_POLYKEYPRESSURE) || !(floor(*self->cfg[0]) == 0 || chs == chn) ) { forge_midimessage(self, tme, buffer, size); return; } const uint8_t key = buffer[1] & 0x7f; const uint8_t vel = buffer[2] & 0x7f; const int tonika = (key + 12 - scale) % 12; if (! filter_midichord_isonscale(tonika)) { chord = 1; } switch (mst) { case MIDI_NOTEON: self->memCI[chn][key] = chord; self->memCM[chn][key] = vel; for (i=0; i < 10 ; ++i) { if (!(chord & (1<<i))) continue; filter_midichord_noteon(self, tme, chn, key + filter_midichord_halftoneoffset(tonika, i), vel); } break; case MIDI_NOTEOFF: chord = self->memCI[chn][key]; for (i=0; i < 10 ; ++i) { if (!(chord & (1<<i))) continue; filter_midichord_noteoff(self, tme, chn, key + filter_midichord_halftoneoffset(tonika, i), vel); } self->memCI[chn][key] = -1000; self->memCM[chn][key] = 0; break; case MIDI_POLYKEYPRESSURE: for (i=0; i < 10 ; ++i) { uint8_t buf[3]; if (!(chord & (1<<i))) continue; int note = key + filter_midichord_halftoneoffset(tonika, i); if (midi_valid(note)) { buf[0] = buffer[0]; buf[1] = note; buf[2] = buffer[2]; forge_midimessage(self, tme, buf, size); } } break; } }
static void filter_midi_miditranspose(MidiFilter* self, uint32_t tme, const uint8_t* const buffer, uint32_t size) { const int chs = midi_limit_chn(floor(*self->cfg[0]) -1); const int transp = rint(*(self->cfg[1])); const uint8_t chn = buffer[0] & 0x0f; const uint8_t key = buffer[1] & 0x7f; const uint8_t vel = buffer[2] & 0x7f; uint8_t mst = buffer[0] & 0xf0; if (size != 3 || !(mst == MIDI_NOTEON || mst == MIDI_NOTEOFF || mst == MIDI_POLYKEYPRESSURE) || !(floor(*self->cfg[0]) == 0 || chs == chn) ) { forge_midimessage(self, tme, buffer, size); return; } if (mst == MIDI_NOTEON && vel ==0 ) { mst = MIDI_NOTEOFF; } int note; uint8_t buf[3]; buf[0] = buffer[0]; buf[1] = buffer[1]; buf[2] = buffer[2]; switch (mst) { case MIDI_NOTEON: note = key + transp; if (midi_valid(note)) { buf[1] = note; forge_midimessage(self, tme, buf, size); } self->memCM[chn][key] = vel; self->memCI[chn][key] = transp; break; case MIDI_NOTEOFF: note = key + self->memCI[chn][key]; if (midi_valid(note)) { buf[1] = note; forge_midimessage(self, tme, buf, size); } self->memCM[chn][key] = 0; self->memCI[chn][key] = -1000; break; case MIDI_POLYKEYPRESSURE: note = key + transp; if (midi_valid(note)) { buf[1] = note; forge_midimessage(self, tme, buf, size); } break; } }