-
Notifications
You must be signed in to change notification settings - Fork 5
/
midi.c
141 lines (132 loc) · 3.46 KB
/
midi.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* GZX - George's ZX Spectrum Emulator
* ZX Spectrum 128K MIDI port emulation
*
* Copyright (c) 1999-2017 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include "midi.h"
/** Initialize MIDI port.
*
* @param mp MIDI port
*/
void midi_port_init(midi_port_t *mp)
{
mp->ms = ms_statusb;
/* No status byte seen yet */
mp->sb = 0x00;
}
/** Return number of data bytes for a message type.
*
* @param statusb Status byte
* @return Number of data bytes
*/
static uint8_t midi_msg_ndata_bytes(uint8_t statusb)
{
switch (statusb & 0xf0) {
case midi_sb_note_on:
case midi_sb_note_off:
case midi_sb_poly_kp:
case midi_sb_ctl_change:
case midi_sb_pitch_bend:
return 2;
case midi_sb_pgm_change:
case midi_sb_chan_press:
return 1;
default:
return 0;
}
}
/** Message was sent through MIDI port.
*
* @param mp MIDI port
* @param sb Status byte
* @param db1 Data byte 1
* @param db2 Data byte 2
*/
static void midi_port_msg(midi_port_t *mp, uint8_t sb, uint8_t db1,
uint8_t db2)
{
midi_msg_t msg;
msg.sb = sb;
msg.db1 = db1;
msg.db2 = db2;
if (mp->midi_msg != NULL)
mp->midi_msg(mp->midi_msg_arg, &msg);
}
/** Write byte to MIDI port.
*
* @param mp MIDI port
* @param val Value to write
*/
void midi_port_write(midi_port_t *mp, uint8_t val)
{
uint8_t stype;
again:
switch (mp->ms) {
case ms_statusb:
stype = val & 0xf0;
if (stype == midi_sb_system) {
if (val == midi_sb_sysex_start)
mp->ms = ms_sysexb;
} else if (val >= midi_sb_min) {
mp->sb = val;
mp->ms = ms_datab1;
} else {
mp->ms = ms_datab1;
goto again;
}
break;
case ms_datab1:
if (val >= midi_sb_min) {
mp->ms = ms_statusb;
goto again;
}
if (midi_msg_ndata_bytes(mp->sb) > 1) {
mp->db1 = val;
mp->ms = ms_datab2;
} else {
midi_port_msg(mp, mp->sb, val, 0);
}
break;
case ms_datab2:
if (val >= midi_sb_min) {
mp->ms = ms_statusb;
goto again;
}
midi_port_msg(mp, mp->sb, mp->db1, val);
break;
case ms_sysexb:
if (val == midi_sb_sysex_end)
mp->ms = ms_statusb;
if (val >= midi_sb_min) {
mp->ms = ms_statusb;
goto again;
}
break;
}
}