void MidiSession::read()
{
    while(active)
    {
        auto available = Pm_Poll(midiStream);
        
        if(available)
        {
            numEvents = Pm_Read(midiStream, events, 1);
            buffer.events.resize(numEvents);
            
            for(auto i = 0; i < numEvents; i++)
            {
                write(&events[i]);
                MidiEvent event;
                event.status = Pm_MessageStatus(events[i].message);
                event.note = Pm_MessageData1(events[i].message);
                event.velocity = Pm_MessageData2(events[i].message);
                event.timeStamp = events[i].timestamp;
                
                if(event.isNoteOff())
                {
                    event.channel = event.status - 127;
                }
                
                if(event.isNoteOn())
                    event.channel = event.status - 143;
                
                if (event.isPolyAftertouch())
                    event.channel = event.status = 159;
                
                buffer.events[i] = event;
            }
            
            for(auto& listener: listeners)
                listener->addBuffer(buffer);
        }
    }
}
Example #2
0
void processNote(MidiEvent message, int seqLength, int direction) {
   static Array<char>         notes;
   static Array<char>         velocities;
   static Array<int>          durations;
   static Array<int>          iois;
   static Array<int>          ontimes;
   static CircularBuffer<int> attacktimes;
   static int                 init = 0;
   static TumbleParameters    temparam;
   char vel;

   if (!init) {
      attacktimes.setSize(256);
      attacktimes.reset();
      notes.setSize(0);
      velocities.setSize(0);
      durations.setSize(0);
      iois.setSize(0);
      ontimes.setSize(128);
      ontimes.zero();
      init = 1;
   }

   char note;
   int deltatime;
   int ioi0;
   int ioix;
   if (message.isNoteOn()) {
      attacktimes.insert(message.tick);

      // check to see if the ioi is in the correct range
      if (notes.getSize() == 0) {
         // no notes yet, so don't know the first ioi
      } else {
         deltatime = attacktimes[0] - attacktimes[1];
         iois.append(deltatime);
      }
      if (iois.getSize() > 1) {
         ioi0 = iois[0];
         ioix = iois[iois.getSize()-1];
         if ((ioix < ioi0 * tolerance) || (ioix > ioi0 / tolerance)) {
            goto resettrigger;
         }
      }

      // at this point the note can be added to the sequence
      if (notes.getSize() + 1 >= seqLength) {
         // time to trigger an algorithm
         if (durations.getSize() < notes.getSize()) {
            // if the last note has not yet been turned off, approximate dur.
            deltatime = iois[iois.getSize()-1];
            durations.append(deltatime);
         }

         int i;
         for (i=0; i<seqLength; i++) {
            temparam.v[i] = velocities[i];
            temparam.i[i] = iois[i];
            temparam.d[i] = durations[i];
            temparam.n[i] = notes[i] - notes[0];
         }
         temparam.n[0]    = message.getP1() - notes[0];
         temparam.current = message.getP1();
         temparam.pos     = 1;
         temparam.max     = seqLength;
         temparam.active  = 1;
         
         startAlgorithm(temparam);
         goto resettrigger;
      } else {
         // add the note info to the algorithm pile
         note = message.getP1();
         notes.append(note);
         vel = message.getP2();
         velocities.append(vel);
         attacktimes[message.getP1()] = message.tick;
      }
   } else if (message.isNoteOff()) {
      if (notes.getSize() > 0) {
         if (notes[notes.getSize()-1] == message.getP1()) {
         deltatime = message.tick - ontimes[message.getP1()];
         durations.append(deltatime);
      } else {
         cout << "A funny error ocurred" << endl;
      }
   }

   return;

resettrigger:
   attacktimes.setSize(0);
   notes.setSize(0);
   velocities.setSize(0);
   durations.setSize(0);
   iois.setSize(0);

   if (message.isNoteOn()) {
      note = message.getP1();
      notes.append(note);
      ontimes[message.getP1()] = message.tick;
      vel = message.getP2();
      velocities.append(vel);
   }
}



//////////////////////////////
//
// startAlgorithm -- start playing the tumble algorithm.  Inserts a
//     FunctionEvent into the eventBuffer which plays the tumble
//     algorithm sequence.  The algorithm will die after the notes
//     fall off of the 88-note keyboard.
//

}