Commit 8f02e62d authored by Knut Aldrin's avatar Knut Aldrin

Ignore unsupported status bytes and messages to other channels

parent c7dd7830
......@@ -3,7 +3,7 @@
*
* Created: 19.08.2014 23:32:07
* Author: Snorre
*/
*/
#include <inttypes.h>
......@@ -28,45 +28,61 @@ 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)
{
midi_notes = notes_struct;
midi_channel = channel_struct;
midi_channel->active_notes = 0;
midi_channel->channel = channel;
midi_current_byte = 0;
for(uint8_t i = 0; i << TESLA_MAX_CHANNELS; i++){
midi_notes[i].age = 0;
midi_notes[i].change = 0;
midi_notes[i].update = 0;
midi_notes[i].on = 0;
midi_notes[i].tesla_channel = i;
}
}
uint8_t parse_status( char input ) {
if( ( input & MIDI_CHANNEL_MASK ) == midi_channel->channel ) {
switch ( input & MIDI_STATUS_MASK ) {
case MIDI_note_off_gc:
midi_state = NOTE_OFF;
midi_current_byte = 0;
return 1;
case MIDI_note_on_gc:
midi_state = NOTE_ON;
midi_current_byte = 0;
return 1;
}
// No status
// Status byte?
if( input & MIDI_STATUS_bm ) {
// Check that they want to speak to us
if( ( input & MIDI_CHANNEL_MASK ) == midi_channel->channel ) {
switch ( input & MIDI_STATUS_MASK ) {
case MIDI_note_off_gc:
midi_state = NOTE_OFF;
midi_current_byte = 0;
break;
case MIDI_note_on_gc:
midi_state = NOTE_ON;
midi_current_byte = 0;
break;
default:
// Not supported yet
midi_state = IGNORE;
midi_current_byte = 0;
break;
}
}
// Return 1 cause we intercepted a status byte
return 1;
}
else {
// Not a status byte
return 0;
}
}
......@@ -77,25 +93,26 @@ 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
midi_buffer[midi_current_byte++] = 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
......@@ -104,7 +121,7 @@ void parse_midi(char input)
//find free channel & update
midi_add_note(midi_buffer[0],midi_buffer[1]);
}
// Get ready for next note
midi_current_byte = 0;
} else {
......@@ -113,25 +130,25 @@ void parse_midi(char input)
} else {
//evaluate if single-byte-interrupting package, else abort
}
break;
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
midi_buffer[midi_current_byte++] = input;
} else if (midi_current_byte == 2) {
//store velocity
midi_buffer[midi_current_byte++] = input;
midi_clear_note(midi_buffer[0],midi_buffer[1]);
// Get ready for next note
midi_current_byte = 0;
} else {
......@@ -139,20 +156,23 @@ void parse_midi(char input)
}
} else {
//check for interrupting frame, panic otherwise
}
break;
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;
//check if note already is on
for (n = 0; n < TESLA_MAX_CHANNELS; n++){
if ((midi_notes[n].note == note) && (midi_notes[n].on)){
......@@ -161,7 +181,7 @@ void midi_add_note(uint8_t note, uint8_t velocity){
return;
}
}
//find free channel
for (n = 0; n < TESLA_MAX_CHANNELS; n++){
if (!(midi_notes[n].on)){
......@@ -172,24 +192,24 @@ void midi_add_note(uint8_t note, uint8_t velocity){
return;
}
}
//if no free channels, find oldest note and overwrite
for (n = 0; n < TESLA_MAX_CHANNELS; n++){
if (midi_notes[n].age > max_age){
result = n;
max_age = midi_notes[n].age;
}
}
}
midi_notes[result].on = 1;
midi_notes[result].note = note;
midi_notes[result].velocity = velocity;
midi_notes[result].change = 1;
}
void midi_clear_note(uint8_t note, uint8_t velocity){
uint8_t n;
//find note
for (n = 0; n < TESLA_MAX_CHANNELS; n++){
if ((midi_notes[n].note == note) && (midi_notes[n].on)){
......@@ -199,5 +219,5 @@ void midi_clear_note(uint8_t note, uint8_t velocity){
return;
}
}
}
\ No newline at end of file
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment