MIDI Parser

Discussion about the Bidule SDK, post your questions, feature request and plugins here.

Moderators: davidv, seb@plogue

Breeze
Posts: 125
Joined: Mon Apr 05, 2010 5:34 pm

MIDI Parser

Postby Breeze » Mon Mar 24, 2014 10:23 am

It's been awhile since I've done any coding and I'm having fun tinkering away with the SDK... As a test, I made a MIDI message parser that sends different types to different outputs, regardless of channel. It works but if the MIDI stream gets too congested, it starts starts skipping events: I noticed it with notes (missing note-offs) but it's probably happening with other messages. It has one MIDI in and 6 MIDI outs, 5 for the Channel Voice messages and 1 for everything else:

[UPDATE! : The code below is now fixed and working.]

Code: Select all

// init each MIDI out to 0 events
for (i = 0; i < _numMIDIOuts; ++i)
{ midiOut[i].numEvents = 0; }

// For each event in the buffer...
for (i = 0; i < midiIn[0].numEvents; ++i) {
   // Reset default port within loop for other data types   
   k = 5;
   // Assign a MIDI out by data type
   if ((midiIn[0].events[i].midiData[0] & 0xF0) == 0x90) { k = 0; } // Note-on
   if ((midiIn[0].events[i].midiData[0] & 0xF0) == 0x80) { k = 0; } // Note-off
   if ((midiIn[0].events[i].midiData[0] & 0xF0) == 0xE0) { k = 1; } // Pitch Bend
   if ((midiIn[0].events[i].midiData[0] & 0xF0) == 0xB0) { k = 2; } // CCs
   if ((midiIn[0].events[i].midiData[0] & 0xF0) == 0xD0) { k = 3; } // Channel AT
   if ((midiIn[0].events[i].midiData[0] & 0xF0) == 0xA0) { k = 3; } // Poly AT
   if ((midiIn[0].events[i].midiData[0] & 0xF0) == 0xC0) { k = 4; } // PCs
   //assign Out numEvents to "j"
   j = midiOut[k].numEvents;
   // Increment the assigned MIDI OUT's event count
   ++midiOut[k].numEvents;
      midiOut[k].events[j].midiData[0] = midiIn[0].events[i].midiData[0];
      midiOut[k].events[j].midiData[1] = midiIn[0].events[i].midiData[1];
      midiOut[k].events[j].midiData[2] = midiIn[0].events[i].midiData[2];
      midiOut[k].events[j].midiData[3] = midiIn[0].events[i].midiData[3];
      midiOut[k].events[j].bufTick = midiIn[0].events[i].bufTick;
      midiOut[k].events[j].fdetune = midiIn[0].events[i].fdetune;
 }

I suspect it's a timing issue, though it doesn't seem that it would be critical for the above. I'd appreciate any hints as to what I'm doing wrong,or what I could do better... ;) Thanks!
Last edited by Breeze on Mon Mar 24, 2014 3:07 pm, edited 3 times in total.

seb@plogue
Site Admin
Posts: 9593
Joined: Tue Mar 02, 2004 7:23 pm
Location: Montreal
Contact:

Re: MIDI Event Timing?

Postby seb@plogue » Mon Mar 24, 2014 10:43 am

You can't use i to index the midiOut events!

say you have 6 events (for simplicity sake, in type order going from 0 to 5)

i = 0

//first event : this is fine
midiOut[0].numEvents = 1
midiOut[0].events[0] = ...

//second event: BAD!
midiOut[1].numEvents = 1 //this line is fine
midiOut[1].events[1] = ... //this is wrong, there's only 1 event in midiOut[1], it should go into events[0] otherwise it will never be read by the "hosting" code

etc...

Breeze
Posts: 125
Joined: Mon Apr 05, 2010 5:34 pm

Re: MIDI Event Timing?

Postby Breeze » Mon Mar 24, 2014 11:27 am

Yes, that"s a pretty big oops! :oops: This is why I opened with "it's been a while"... I was pretty sure I was doing something stupid.

Thanks, seb. I'll fix and test this, and revise the post, as soon as I get a chance.

Breeze
Posts: 125
Joined: Mon Apr 05, 2010 5:34 pm

Re: MIDI Parser

Postby Breeze » Mon Mar 24, 2014 2:59 pm

Quick fix and used j for the midiOut[k]events offset (corrected in the code above);

Code: Select all

// Added this at first
j = midiOut[k].numEvents - 1;

//But this is more elegant:
j = midiOut[k].numEvents;
++midiOut[k].numEvents;

...aaand it's solid as a rock! BTW if anyone has a use for this, just ask for the dll. But for now, I'm only doing Win x86 and x64. I'm eventually going to open a thread with the plugins I'm creating.

Thanks Seb!

And now for my next trick: creating MIDI events out of thin air!... :lol:


Return to “Bidule SDK”

Who is online

Users browsing this forum: No registered users and 1 guest