Skip to content

Instantly share code, notes, and snippets.

@Jacajack
Created October 31, 2017 23:49
Show Gist options
  • Save Jacajack/ce8ca1892451c84237d873ef32b323ab to your computer and use it in GitHub Desktop.
Save Jacajack/ce8ca1892451c84237d873ef32b323ab to your computer and use it in GitHub Desktop.
Simple C MIDI interpreter
//Toporny, lecz skuteczny interpreter midi
void midi( uint8_t byte )
{
//Odpowiednio: numer kanału, 3 bity statusu, limit danych w bajtach, liczba odczytanych bajtów
static uint8_t channel = 0, status = 0, dlim = 0, dread = 0;
static uint8_t dbuf[16] = {0}; //Bufor na dane
if ( byte & ( 1 << 7 ) )
{
//Bajt stanu
status = byte & 0x70;
dread = dlim = 0;
channel = byte & 0x0f;
//Przygotowanie do interpretacji komend
switch ( status )
{
//Note on
case 0x10:
dlim = 2;
break;
//Note off
case 0x00:
dlim = 2;
break;
}
}
else if ( MIDI_CHAN == channel )
{
//Bajt danych
dbuf[dread++] = byte;
//Interpretacja komendy
if ( dread == dlim )
{
switch ( status )
{
//Note on
case 0x10:
setnote( dbuf[0] );
noteon = 1;
break;
//Note off
case 0x00:
if ( notenum == dbuf[0] )
noteon = 0;
break;
}
dread = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment