-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.h
104 lines (88 loc) · 3.45 KB
/
midi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Copyright (C) 2018 Thomas R. Dial
* All Rights Reserved
*
* A MIDI processing library designed for use in embedded environments.
*/
#ifndef MIDI_H_INCLUDED_
#define MIDI_H_INCLUDED_
#include "status.h"
// The baud rate for MIDI data
#define MIDI_BAUD_RATE 31250
/*
#define SYS_REALTIME_TIMING_CLOCK 0xf8 // Timing clock sent 24 times/quarter.
#define SYS_REALTIME_RESERVED_F9 0xf9 // Undefined (Reserved.)
#define SYS_REALTIME_SEQ_START 0xfa // Start the current sequence.
#define SYS_REALTIME_SEQ_CONTINUE 0xfb // Continue the current sequence.
#define SYS_REALTIME_SEQ_STOP 0xfc // Stop the current sequence.
#define SYS_REALTIME_RESERVED_FD 0xfd // Undefined (Reserved.)
#define SYS_REALTIME_ACTIVE_SENSE 0xfe // Active sensing message (each 300ms.)
#define SYS_REALTIME_RESET 0xff // Reset all receivers to power-up.
*/
/*
* The midi_event_callback_t type defines a type for event callback functions
* to be invoked on the receipt of MIDI messages. The caller only needs to
* register functions for those events for which there is interest.
* Unhandled events will be dispatched to a null handler implemented within
* the library.
*/
typedef void (*midi_event_callback_t)(char chan, char data1, char data2);
typedef enum midi_errors {
E_MIDI_BAD_EVENT_HANDLER = -1,
E_MIDI_BAD_CHANNEL_STATE = -2
};
typedef enum event_type {
// System real-time messages
EVT_SYS_REALTIME_TIMING_CLOCK = 0,
EVT_SYS_REALTIME_RESERVED_F9 = 1,
EVT_SYS_REALTIME_SEQ_START = 2,
EVT_SYS_REALTIME_SEQ_CONTINUE = 3,
EVT_SYS_REALTIME_SEQ_STOP = 4,
EVT_SYS_REALTIME_RESERVED_FD = 5,
EVT_SYS_REALTIME_ACTIVE_SENSE = 6,
EVT_SYS_REALTIME_RESET = 7,
// Channel messages
EVT_CHAN_NOTE_OFF = 8,
EVT_CHAN_NOTE_ON = 9,
EVT_CHAN_POLY_AFTERTOUCH = 10,
EVT_CHAN_CONTROL_CHANGE = 11,
EVT_CHAN_PROGRAM_CHANGE = 12,
EVT_CHAN_AFTERTOUCH = 13,
EVT_CHAN_PITCH_BEND = 14,
// Not a valid event
EVT_MAX
} event_type;
/**
* Initialize the MIDI library. This routine must be called prior to using
* library functions or unpredictable behavior may result.
*
* @return Zero on success; nonzero status otherwise.
*/
status_t midi_init();
/**
* Register an event handler for the specified event. To clear an event
* handle, simply pass a NULL pointer for the callback argument.
*
* Returns 0 if a callback was registered or cleare successfully.
* Returns -1 if the event type was invalid.
*
* @param evt Event to register. See the EVT_xxx enumeration above.
* @param cb Function to invoke upon receipt of the MIDI event.
* @return Status code indicating (see above for comments.)
*/
status_t midi_register_event_handler(event_type evt, midi_event_callback_t cb);
/**
* Processes a byte arriving via the MIDI input of the device. This function
* *must* be invoked for every byte that arrives so that the internal state
* machine maintained by the library is updated properly. Calling this function
* may result in callback invocations. On success, the function returns the
* number of callbacks that resulted from the new byte's arrival. In the event
* that the received byte is invalid, a negative code is returned (see the
* section above that outlines status codes for this library.)
*
* @param byte Byte of data received from an input port.
*
* @return Number of callback invocations. Returns negative status on error.
*/
status_t midi_receive_byte(char byte);
#endif // MIDI_H_INCLUDED_