Logo Search packages:      
Sourcecode: aubio version File versions  Download package

aubio_midi_event_t* aubio_midi_parser_parse ( aubio_midi_parser_t parser,
unsigned char  c 
)

aubio_midi_parser_parse

The MIDI byte stream is fed into the parser, one byte at a time. As soon as the parser has recognized an event, it will return it. Otherwise it returns NULL.

Definition at line 70 of file midi_parser.c.

References aubio_midi_event_length(), _aubio_midi_parser_t::channel, _aubio_midi_parser_t::event, _aubio_midi_parser_t::nr_bytes, _aubio_midi_parser_t::nr_bytes_total, _aubio_midi_parser_t::p, and _aubio_midi_parser_t::status.

{
  /*********************************************************************/
  /* 'Process' system real-time messages                               */
  /*********************************************************************/
  /* There are not too many real-time messages that are of interest here.
   * They can occur anywhere, even in the middle of a noteon message! 
   * Real-time range: 0xF8 .. 0xFF
   * Note: Real-time does not affect (running) status.
   */  
  if (c >= 0xF8){
    if (c == MIDI_SYSTEM_RESET){
      parser->event.type = c;
      parser->status = 0; /* clear the status */
      return &parser->event;
    };
    return NULL;
  };

  /*********************************************************************/
  /* 'Process' system common messages (again, just skip them)          */
  /*********************************************************************/
  /* There are no system common messages that are of interest here.
   * System common range: 0xF0 .. 0xF7 
   */

  if (c > 0xF0){
    /* MIDI specs say: To ignore a non-real-time message, just discard all data
     * up to the next status byte.  And our parser will ignore data that is
     * received without a valid status.  
     * Note: system common cancels running status. */
    parser->status = 0;
    return NULL;
  };

  /*********************************************************************/
  /* Process voice category messages:                                  */
  /*********************************************************************/
  /* Now that we have handled realtime and system common messages, only
   * voice messages are left.
   * Only a status byte has bit # 7 set.
   * So no matter the status of the parser (in case we have lost sync),
   * as soon as a byte >= 0x80 comes in, we are dealing with a status byte
   * and start a new event.
   */

  if (c & 0x80){
    parser->channel = c & 0x0F;
    parser->status = c & 0xF0;
    /* The event consumes x bytes of data... (subtract 1 for the status 
     * byte) */
    parser->nr_bytes_total=aubio_midi_event_length(parser->status)-1;
    /* of which we have read 0 at this time. */
    parser->nr_bytes = 0;
    return NULL;
  };

  /*********************************************************************/
  /* Process data                                                      */
  /*********************************************************************/
  /* If we made it this far, then the received char belongs to the data
   * of the last event. */
  if (parser->status == 0){
    /* We are not interested in the event currently received.
     * Discard the data. */
    return NULL;
  };

  /* Store the first couple of bytes */
  if (parser->nr_bytes < AUBIO_MIDI_PARSER_MAX_PAR){
    parser->p[parser->nr_bytes]=c;
  };
  parser->nr_bytes++;

  /* Do we still need more data to get this event complete? */
  if (parser->nr_bytes < parser->nr_bytes_total){
    return NULL;
  };

  /*********************************************************************/
  /* Send the event                                                    */
  /*********************************************************************/
  /* The event is ready-to-go.  About 'running status': 
   * 
   * The MIDI protocol has a built-in compression mechanism. If several similar
   * events are sent in-a-row, for example note-ons, then the event type is
   * only sent once. For this case, the last event type (status) is remembered.
   * We simply keep the status as it is, just reset the parameter counter. If
   * another status byte comes in, it will overwrite the status. 
   */
  parser->event.type = parser->status;
  parser->event.channel = parser->channel;
  parser->nr_bytes = 0; /* Related to running status! */
  switch (parser->status){
    case NOTE_OFF:
    case NOTE_ON:
    case KEY_PRESSURE:
    case CONTROL_CHANGE:
    case PROGRAM_CHANGE:
    case CHANNEL_PRESSURE:
      parser->event.param1 = parser->p[0]; /* For example key number */
      parser->event.param2 = parser->p[1]; /* For example velocity */
      break;
    case PITCH_BEND:
      /* Pitch-bend is transmitted with 14-bit precision. */
      /* Note: '|' does here the same as '+' (no common bits), 
       * but might be faster */
      parser->event.param1 = ((parser->p[1] << 7) | parser->p[0]); 
      break;
    default: 
      /* Unlikely */
      return NULL;
  };
  return &parser->event;
};


Generated by  Doxygen 1.6.0   Back to index