Commit a5cd5b98 authored by Snorre Nilssen Vestli's avatar Snorre Nilssen Vestli
Browse files

Merge branch 'master' of git.omegav.no:tesla/tesla-midi

parents ac9837b3 c6d2b24d
Loading
Loading
Loading
Loading
+187 −133
Original line number Diff line number Diff line
@@ -28,7 +28,8 @@ uint8_t midi_current_byte;
enum {
  FREE,
  NOTE_ON,
	NOTE_OFF
  NOTE_OFF,
  IGNORE
} midi_state;

void midi_init(uint8_t channel, note_t* notes_struct, midi_channel_t* channel_struct)
@@ -52,14 +53,15 @@ void midi_init(uint8_t channel, note_t* notes_struct, midi_channel_t* channel_st

}

uint8_t parse_status( char input ) {
  // Status byte?
  if( input & MIDI_STATUS_bm ) {

void parse_midi(char input)
{
	switch (midi_state)
	{
		case FREE:
			//check channel
			if ((input & MIDI_CHANNEL_MASK) == (midi_channel->channel)){
    if( input == MIDI_clock_bm ) {
      // TODO: Implement midi clock handling
    }
    // Check that they want to speak to us
    else if( ( input & MIDI_CHANNEL_MASK ) == midi_channel->channel ) {
      switch ( input & MIDI_STATUS_MASK ) {
        case MIDI_note_off_gc:
        	midi_state = NOTE_OFF;
@@ -70,11 +72,46 @@ void parse_midi(char input)
      		midi_state = NOTE_ON;
      		midi_current_byte = 0;
      		break;

        default:
          // Not supported yet
          midi_state = IGNORE;
          midi_current_byte = 0;
          break;
      }
    }
    else {
      // Not to us, ignore
      midi_state = IGNORE;
      midi_current_byte = 0;
    }

    // Return 1 cause we intercepted a status byte
    return 1;
  }
  else {
    // Not a status byte
    return 0;
  }
}


void parse_midi(char input)
{
  switch (midi_state)
  {
    case FREE:
    case IGNORE:
      //check channel
      parse_status( input );
      break;

    case NOTE_ON:
      // Check for status bytes
      if( parse_status( input ) ) {
        break;
      }

      if (!(input & MIDI_STATUS_bm)){
        if (midi_current_byte == 1){
          //store note
@@ -83,9 +120,18 @@ void parse_midi(char input)
        } else if (midi_current_byte == 2) {
          //store velocity
          midi_buffer[midi_current_byte++] = input;

          // Zero-velocity input == NOTE_OFF
          if( input == 0 ) {
            midi_clear_note( midi_buffer[0], 127 ); // Release with full force
          }
          else {
            //find free channel & update
            midi_add_note(midi_buffer[0],midi_buffer[1]);
					midi_state = FREE;
          }

          // Get ready for next note
          midi_current_byte = 0;
        } else {
          tesla_panic();
        }
@@ -95,6 +141,11 @@ void parse_midi(char input)
      break;

    case NOTE_OFF:
      // Check for status bytes
      if( parse_status( input ) ) {
        break;
      }

      if (!(input & MIDI_STATUS_bm)){
        if (midi_current_byte == 1){
          //store note
@@ -105,8 +156,9 @@ void parse_midi(char input)
          midi_buffer[midi_current_byte++] = input;

          midi_clear_note(midi_buffer[0],midi_buffer[1]);
					midi_state = FREE;

          // Get ready for next note
          midi_current_byte = 0;
        } else {
          tesla_panic();
        }
@@ -117,12 +169,14 @@ void parse_midi(char input)

      break;

    default:
      // WTF? Unknown state
      tesla_panic();
  }

}



void midi_add_note(uint8_t note, uint8_t velocity){
  uint8_t n, result = 0, max_age = 0;

+2 −0
Original line number Diff line number Diff line
@@ -31,4 +31,6 @@ void midi_init(uint8_t channel, note_t* notes_struct, midi_channel_t* channel_st

void parse_midi(char input);

uint8_t parse_status( char input );

#endif /* MIDI_H_ */
 No newline at end of file
+4 −2
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#define MIDI_note_on_gc (0b1001 << 4)
#define MIDI_note_off_gc (0b1000 << 4)
#define MIDI_aftertouch_gc ( 0b1010 << 4 )
#define MIDI_clock_gc (0xf8)