Skip to content

Commit

Permalink
Merge pull request #766 from hathach/enhance-midi
Browse files Browse the repository at this point in the history
Enhance midi
  • Loading branch information
hathach authored Apr 2, 2021
2 parents e4f5a5d + 7582528 commit eb92986
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 162 deletions.
8 changes: 5 additions & 3 deletions examples/device/dynamic_configuration/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void midi_task(void)
// regardless of these being used or not. Therefore incoming traffic should be read
// (possibly just discarded) to avoid the sender blocking in IO
uint8_t packet[4];
while(tud_midi_available()) tud_midi_receive(packet);
while( tud_midi_available() ) tud_midi_packet_read(packet);

// send note every 1000 ms
if (board_millis() - start_ms < 286) return; // not enough time
Expand All @@ -186,10 +186,12 @@ void midi_task(void)
if (previous < 0) previous = sizeof(note_sequence) - 1;

// Send Note On for current position at full velocity (127) on channel 1.
tud_midi_write24(cable_num, 0x90 | channel, note_sequence[note_pos], 127);
uint8_t note_on[3] = { 0x90 | channel, note_sequence[note_pos], 127 };
tud_midi_stream_write(cable_num, note_on, 3);

// Send Note Off for previous note.
tud_midi_write24(cable_num, 0x80 | channel, note_sequence[previous], 0);
uint8_t note_off[3] = { 0x80 | channel, note_sequence[previous], 0};
tud_midi_stream_write(cable_num, note_off, 3);

// Increment position
note_pos++;
Expand Down
8 changes: 5 additions & 3 deletions examples/device/midi_test/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void midi_task(void)
// regardless of these being used or not. Therefore incoming traffic should be read
// (possibly just discarded) to avoid the sender blocking in IO
uint8_t packet[4];
while(tud_midi_available()) tud_midi_receive(packet);
while ( tud_midi_available() ) tud_midi_packet_read(packet);

// send note every 1000 ms
if (board_millis() - start_ms < 286) return; // not enough time
Expand All @@ -146,10 +146,12 @@ void midi_task(void)
if (previous < 0) previous = sizeof(note_sequence) - 1;

// Send Note On for current position at full velocity (127) on channel 1.
tud_midi_write24(cable_num, 0x90 | channel, note_sequence[note_pos], 127);
uint8_t note_on[3] = { 0x90 | channel, note_sequence[note_pos], 127 };
tud_midi_stream_write(cable_num, note_on, 3);

// Send Note Off for previous note.
tud_midi_write24(cable_num, 0x80 | channel, note_sequence[previous], 0);
uint8_t note_off[3] = { 0x80 | channel, note_sequence[previous], 0};
tud_midi_stream_write(cable_num, note_off, 3);

// Increment position
note_pos++;
Expand Down
45 changes: 45 additions & 0 deletions src/class/midi/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,51 @@ typedef enum
MIDI_JACK_EXTERNAL = 0x02
} midi_jack_type_t;

typedef enum
{
MIDI_CIN_MISC = 0,
MIDI_CIN_CABLE_EVENT = 1,
MIDI_CIN_SYSCOM_2BYTE = 2, // 2 byte system common message e.g MTC, SongSelect
MIDI_CIN_SYSCOM_3BYTE = 3, // 3 byte system common message e.g SPP
MIDI_CIN_SYSEX_START = 4, // SysEx starts or continue
MIDI_CIN_SYSEX_END_1BYTE = 5, // SysEx ends with 1 data, or 1 byte system common message
MIDI_CIN_SYSEX_END_2BYTE = 6, // SysEx ends with 2 data
MIDI_CIN_SYSEX_END_3BYTE = 7, // SysEx ends with 3 data
MIDI_CIN_NOTE_ON = 8,
MIDI_CIN_NOTE_OFF = 9,
MIDI_CIN_POLY_KEYPRESS = 10,
MIDI_CIN_CONTROL_CHANGE = 11,
MIDI_CIN_PROGRAM_CHANGE = 12,
MIDI_CIN_CHANNEL_PRESSURE = 13,
MIDI_CIN_PITCH_BEND_CHANGE = 14,
MIDI_CIN_1BYTE_DATA = 15
} midi_code_index_number_t;

// MIDI 1.0 status byte
enum
{
//------------- System Exclusive -------------//
MIDI_STATUS_SYSEX_START = 0xF0,
MIDI_STATUS_SYSEX_END = 0xF7,

//------------- System Common -------------//
MIDI_STATUS_SYSCOM_TIME_CODE_QUARTER_FRAME = 0xF1,
MIDI_STATUS_SYSCOM_SONG_POSITION_POINTER = 0xF2,
MIDI_STATUS_SYSCOM_SONG_SELECT = 0xF3,
// F4, F5 is undefined
MIDI_STATUS_SYSCOM_TUNE_REQUEST = 0xF6,

//------------- System RealTime -------------//
MIDI_STATUS_SYSREAL_TIMING_CLOCK = 0xF8,
// 0xF9 is undefined
MIDI_STATUS_SYSREAL_START = 0xFA,
MIDI_STATUS_SYSREAL_CONTINUE = 0xFB,
MIDI_STATUS_SYSREAL_STOP = 0xFC,
// 0xFD is undefined
MIDI_STATUS_SYSREAL_ACTIVE_SENSING = 0xFE,
MIDI_STATUS_SYSREAL_SYSTEM_RESET = 0xFF,
};

/// MIDI Interface Header Descriptor
typedef struct TU_ATTR_PACKED
{
Expand Down
Loading

0 comments on commit eb92986

Please sign in to comment.