Skip to content

Instantly share code, notes, and snippets.

@ryrun
Last active April 23, 2023 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryrun/f03d6e9cc340747165969b9d2feeaeaf to your computer and use it in GitHub Desktop.
Save ryrun/f03d6e9cc340747165969b9d2feeaeaf to your computer and use it in GitHub Desktop.
Bluecat's Plug'N'Script - Midi Channel 1 Exclusive only, when playing
/** \file
* MIDI channel filter.
* Filter MIDI events (keeps only MIDI events for the selected channel).
*/
#include "lib/Midi.hxx"
// metadata
string name="MIDI Channel Special Filter";
string description="Filters events of other channels (when channel 1 is used)";
array<string> inputParametersNames={"Mode"};
array<string> inputParametersEnums={"Ch1 Exkl;Ch1 Only;Ch1++;Off"};
array<double> inputParameters(inputParametersNames.length,0);
array<double> inputParametersMin={0};
array<double> inputParametersMax={3};
array<double> inputParametersDefault={0};
array<double> inputParametersSteps={1};
array<MidiEvent> lateMidiIn = {};
int channelOneInUse = 0;
int channelOtherInUse = 0;
uint8 notePlus = 255;
uint8 lastNotePlus = 255;
uint64 pblock = 0;
int8 mode = 0;
void reset() {
channelOneInUse = 0;
channelOtherInUse = 0;
notePlus = 255;
lastNotePlus = 255;
pblock = 0;
}
void processBlock(BlockData& data) {
array<MidiEvent> midiIn = {};
string temp, temp2, temp3, temp4, temp5;
bool skip = false;
pblock++;
//just 1 midi event and 16 samples delay event per one process block, process it on the next process block (delayed for 16 samples)
if(mode == 2 && data.inputMidiEvents.length > 0 && data.inputMidiEvents.length < 3 && data.samples.length <= 16) {
//fill buffer
for(uint i=0;i<data.inputMidiEvents.length;i++) {
lateMidiIn.insertLast(data.inputMidiEvents[i]);
}
return;
}
//if late midi data, add this to buffer
if(lateMidiIn.length>0) {
midiIn = lateMidiIn;
lateMidiIn = {};
}
//fill buffer with new events
for(uint i=0;i<data.inputMidiEvents.length;i++) {
if(MidiEventUtils::getType(data.inputMidiEvents[i]) == kMidiNoteOff || MidiEventUtils::getType(data.inputMidiEvents[i]) == kMidiNoteOn) {
midiIn.insertLast(data.inputMidiEvents[i]);
}
}
//preprocess for bass Mode
for(uint i=0;i<midiIn.length;i++) {
if(MidiEventUtils::getChannel(midiIn[i]) != 1) {
switch(MidiEventUtils::getType(midiIn[i])) {
case kMidiNoteOn:
notePlus = MidiEventUtils::getNote(midiIn[i]);
break;
case kMidiNoteOff:
notePlus = 255;
break;
}
}
}
//debugdata
intToString(mode, temp2);
intToString(pblock, temp3);
intToString(data.samples.length, temp4);
intToString(midiIn.length, temp5);
//process midi events
for(uint i=0;i<midiIn.length;i++) {
skip = false;
intToString(MidiEventUtils::getNote(midiIn[i]), temp);
switch (MidiEventUtils::getChannel(midiIn[i])) {
case 1:
switch(MidiEventUtils::getType(midiIn[i])) {
case kMidiNoteOn:
channelOneInUse ++;
break;
case kMidiNoteOff:
channelOneInUse --;
break;
}
data.outputMidiEvents.push(midiIn[i]);
if(mode == 2 && notePlus != 255) {
MidiEvent tempEv = midiIn[i];
if(MidiEventUtils::getType(midiIn[i]) == kMidiNoteOn) {
//normal out
MidiEventUtils::setNote(tempEv, notePlus);
lastNotePlus = notePlus;
} else {
MidiEventUtils::setNote(tempEv, lastNotePlus);
}
data.outputMidiEvents.push(tempEv);
}
break;
default:
switch(MidiEventUtils::getType(midiIn[i])) {
case kMidiNoteOn:
channelOtherInUse ++;
break;
case kMidiNoteOff:
channelOtherInUse --;
break;
}
if(
(channelOneInUse==0 || mode == 3) && (mode != 1 && mode != 2)
) {
data.outputMidiEvents.push(midiIn[i]);
} else {
skip = true;
}
break;
}
/*
print(
(MidiEventUtils::getChannel(midiIn[i]) != 1 ? " ;" : "" ) +
(MidiEventUtils::getType(midiIn[i]) == kMidiNoteOn ? "Note On" : "Note Off") +
" " + temp + ";" +
(skip ? "- Skipped -":"") +
"Mode: " + temp2 + " --- #" + temp3 + " (" + temp4 + " samples, " + temp5 + " midi data)"
);
*/
}
//switch mode only, hwn nothing is in process
if(channelOneInUse == 0 && channelOtherInUse == 0 && lateMidiIn.length == 0) {
mode = int8(inputParameters[0]+0.5);
}
}
int getLatency() {
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment