Skip to content

Commit

Permalink
Tracks notes for random midi chan
Browse files Browse the repository at this point in the history
  • Loading branch information
Quixotic7 committed Mar 2, 2024
1 parent f645604 commit d9cdb9b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 8 deletions.
88 changes: 80 additions & 8 deletions OMX-27-firmware/src/midifx/midifx_randomizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace midifx
return;
}

int8_t origNote = note.noteNumber;
// int8_t origNote = note.noteNumber;

int8_t octaveMax = octMinus_ + octPlus_ + 1;
int8_t octave = random(0, octaveMax) - octMinus_;
Expand All @@ -107,20 +107,92 @@ namespace midifx
note.velocity = getRand(note.velocity, velMinus_, velPlus_);
note.stepLength = note.stepLength * map(random(lengthPerc_), 0, 100, 1, 16);

if(midiChan_ != 0)
{
// note.channel = constrain(random(note.channel, note.channel + midiChan_), 1, 16);
}
// if(midiChan_ != 0)
// {
// note.channel = constrain(random(note.channel, note.channel + midiChan_), 1, 16);
// }

if(delayMin_ > 0 || delayMax_ > 0)
{
processDelayedNote(&note);
}
else
{
processNoteOn(origNote, note);
processNoteOn(note);
}
}

void MidiFXRandomizer::processNoteOff(MidiNoteGroup note)
{
bool foundNote = false;
if (trackedNotes.size() > 0)
{
auto it = trackedNotes.begin();
while (it != trackedNotes.end())
{
if (it->prevNoteNumber == note.prevNoteNumber && it->origChannel == note.channel)
{
foundNote = true;
// sendNoteOut(note);

note.channel = it->channel;
// note.noteNumber = it->noteNumber;

sendNoteOut(note);

it = trackedNotes.erase(it);
}
else
{
++it;
}
}
}

if(!foundNote)
{
sendNoteOut(note);
}
}

void MidiFXRandomizer::processNoteOn(MidiNoteGroup note)
{
// Tracking not needed if not randomizing midi chan
if(midiChan_ == 0)
{
sendNoteOut(note);
return;
}

if (trackedNotes.size() > 0)
{
for(auto tNt : trackedNotes)
{
if (tNt.noteNumber == note.noteNumber && tNt.origChannel == note.channel)
{
// same note is being tracked already, kill note
return;
}
}
}

if (trackedNotes.size() < queueSize)
{
RandTrackedNote trackedNote;
trackedNote.setFromNoteGroup(&note);

// Keep track of original channel
trackedNote.origChannel = note.channel;

note.channel = constrain(random(note.channel, note.channel + midiChan_), 1, 16);

trackedNote.channel = note.channel;

trackedNotes.push_back(trackedNote);

sendNoteOut(note);
}
// Kill if queue is full
}

void MidiFXRandomizer::removeFromDelayQueue(MidiNoteGroup *note)
Expand Down Expand Up @@ -186,7 +258,7 @@ namespace midifx
else
{
// queue is filled, send note out
sendNoteOut(*note);
processNoteOn(*note);
}
}

Expand All @@ -212,7 +284,7 @@ namespace midifx
if (stepmicros >= it->noteonMicros)
{
// Send out and remove
sendNoteOut(*it);
processNoteOn(*it);
it = delayedNoteQueue.erase(it);
}
else
Expand Down
28 changes: 28 additions & 0 deletions OMX-27-firmware/src/midifx/midifx_randomizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,30 @@ namespace midifx

void onEncoderChangedEditParam(Encoder::Update enc) override;

void processNoteOff(MidiNoteGroup note) override;


private:
struct RandTrackedNote
{
uint8_t prevNoteNumber = 0;
uint8_t channel = 1;
uint8_t origChannel = 1;
uint8_t noteNumber = 0;

RandTrackedNote()
{
}

void setFromNoteGroup(MidiNoteGroup *noteGroup)
{
prevNoteNumber = noteGroup->prevNoteNumber;
channel = noteGroup->channel;
origChannel = noteGroup->channel;
noteNumber = noteGroup->noteNumber;
}
};

// std::vector<MidiNoteGroup> triggeredNotes;
struct RandomSave
{
Expand Down Expand Up @@ -65,11 +88,16 @@ namespace midifx
static const int queueSize = 16;
std::vector<MidiNoteGroup> delayedNoteQueue; // notes pending for quantization

std::vector<RandTrackedNote> trackedNotes; // notes that are tracked because midi chan changed


static uint8_t getDelayLength(uint8_t delayIndex);

static uint8_t getRand(uint8_t v, uint8_t minus, uint8_t plus);

void removeFromDelayQueue(MidiNoteGroup *note);
void processDelayedNote(MidiNoteGroup *note);

void processNoteOn(MidiNoteGroup note);
};
}

0 comments on commit d9cdb9b

Please sign in to comment.