Problem midi filter RX

(deleted)

(deleted)

So, if I understand correctly, you want to allow only note on/off and control change events?

Try this:

void setup() {
  Serial.begin(31250); // open the serial port for MIDI
}

const uint8_t NoteOn = 0x90;
const uint8_t NoteOff = 0x80;
const uint8_t ControlChange = 0xB0;

bool isAllowedHeader(uint8_t header) {
  header &= 0xF0; // allow all channels
  return header == NoteOn
         || header == NoteOff
         || header == ControlChange;
}

bool isHeader(uint8_t midiByte) {
  return midiByte & 0x80;
}

void loop() {
  static bool lastHeaderAllowed = false;
  if (Serial.available()) {
    uint8_t midiByte = Serial.read();
    if (isHeader(midiByte)) {
      // header byte
      if (midiByte >= 0xF8) {
        // real-time
      } else {
        // normal header
        lastHeaderAllowed = isAllowedHeader(midiByte);
        if (lastHeaderAllowed) {
          Serial.write(midiByte);
        }
      }
    } else {
      // data byte
      if (lastHeaderAllowed) {
        Serial.write(midiByte);
      }
    }
  }
}

Pieter

(deleted)

You can use the same approach. Just remember whether it's a note number or controller number byte or a velocity or value byte. Don't forget to allow for running status.

(deleted)

Is there a question anywhere in your reply?
If you want us to help, post what you’ve tried, and tell us exactly at what point it goes wrong, instead of just stating that it doesn’t work.

(deleted)

void setup() {
  Serial.begin(31250); // open the serial port for MIDI
}

const uint8_t NoteOn = 0x90;
const uint8_t NoteOff = 0x80;
const uint8_t ControlChange = 0xB0;

bool isAllowedHeader(uint8_t header) {
  header &= 0xF0; // allow all channels
  return header == NoteOn
         || header == NoteOff
         || header == ControlChange;
}

bool isHeader(uint8_t midiByte) {
  return midiByte & 0x80;
}

bool isAllowedMessage(uint8_t header, uint8_t address) {
  header &= 0xF0;
  if (header == NoteOn || header == NoteOff)
    return address >= 12 && address <= 24; // Whatever
  else if (header == ControlChange)
    return address >= 16 && address < 20; // Whatever
  return false;
}

void loop() {
  static uint8_t lastHeader = 0;
  static bool sentHeader = true;
  static bool thirdByte = false;
  static bool allowMessage = false;
  if (Serial.available()) {
    uint8_t midiByte = Serial.read();
    if (isHeader(midiByte)) {
      // header byte
      if (midiByte >= 0xF8) {
        // real-time
      } else {
        // normal header
        lastHeader = isAllowedHeader(midiByte) ? midiByte : 0;
        sentHeader = false;
        thirdByte = false;
      }
    } else {
      // data byte
      if (thirdByte == false) {
        // note number or controller number
        allowMessage = isAllowedMessage(lastHeader, midiByte);
        if (allowMessage) {
          if (sentHeader == false) {
            Serial.write(lastHeader);
            sentHeader = true;
          }
          Serial.write(midiByte);
        }
        thirdByte = true;
      } else { // thirdByte
        // velocity or controller value
        if (allowMessage) {
          Serial.write(midiByte);
        }
        thirdByte = false;
      }
    }
  }
}

(deleted)

What messages did you try? It only allows notes from 12 until 24 and CCs from 16 until 19.

(deleted)

Change the isAllowedMessage functions to whatever you need, you can add the transpose there. The "address" variable is the note number if header == NoteOn or NoteOff.

(deleted)

Do you want to filter only, or do you want to change the notes?

(deleted)

(deleted)