source: buchla-emu/emu/mid.c@ 0529a19

Last change on this file since 0529a19 was 0529a19, checked in by Alexander Heinrich <alex.heinrich@…>, 7 years ago

Add basic handling of midi input.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (C) 2017 The Contributors
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or (at
7 * your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * A copy of the GNU General Public License can be found in the file
15 * "gpl.txt" in the top directory of this repository.
16 */
17
18#include <all.h>
19#include <rtmidi/rtmidi_c.h>
20
21#define ver(...) _ver(mid_verbose, 0, __VA_ARGS__)
22#define ver2(...) _ver(mid_verbose, 1, __VA_ARGS__)
23#define ver3(...) _ver(mid_verbose, 2, __VA_ARGS__)
24
25#define REG_IER_ISR 0
26#define REG_CFR_SR 1
27#define REG_CDR_TBR 2
28#define REG_TDR_RDR 3
29
30#define BUF_SZ 128
31
32typedef struct {
33 int32_t buf_hd;
34 int32_t buf_tl;
35 uint8_t buf[BUF_SZ];
36 bool irq_r;
37 bool irq_t;
38 bool rdr_ok;
39 uint8_t rdr;
40} state_t;
41
42static state_t state[] = {
43 { .buf_hd = 0, .buf_tl = 0, .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 },
44 { .buf_hd = 0, .buf_tl = 0, .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 }
45};
46
47int32_t mid_verbose = 0;
48
49struct RtMidiWrapper* midiin;
50
51static void xmit(int32_t un)
52{
53 int32_t i = state[un].buf_tl;
54 ver2("mid xmit %d %d", i, state[un].buf_hd);
55
56 if (i >= state[un].buf_hd) {
57 return;
58 }
59
60 uint8_t byte = state[un].buf[i % BUF_SZ];
61 ver2("mid xmit 0x%02x", byte);
62
63 state[un].rdr = byte;
64 state[un].rdr_ok = true;
65 state[un].irq_r = true;
66
67 state[un].buf_tl = i + 1;
68
69 if (state[un].buf_tl >= BUF_SZ) {
70 state[un].buf_hd -= BUF_SZ;
71 state[un].buf_tl -= BUF_SZ;
72 ver2("mid adj %d %d", state[un].buf_tl, state[un].buf_hd);
73 }
74}
75
76static void out_lk(int32_t un, uint8_t c)
77{
78 int32_t i = state[un].buf_hd;
79 ver2("mid out %d %d 0x%02x", state[un].buf_tl, i, c);
80
81 if (i >= state[un].buf_tl + BUF_SZ) {
82 err("midi port %d losing data", un);
83 return;
84 }
85
86 state[un].buf[i % BUF_SZ] = c;
87 state[un].buf_hd = i + 1;
88
89 if (!state[un].irq_r && !state[un].rdr_ok) {
90 xmit(un);
91 }
92}
93
94static void out(int32_t un, uint8_t c)
95{
96 if (SDL_LockMutex(cpu_mutex) < 0) {
97 fail("SDL_LockMutex() failed: %s", SDL_GetError());
98 }
99
100 out_lk(un, c);
101
102 if (SDL_UnlockMutex(cpu_mutex) < 0) {
103 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
104 }
105}
106
107static void midi_callback(double timeStamp, const unsigned char* message, void *userData) {
108 ver2("Timestamp %f\n", timeStamp);
109
110 for (uint8_t i = 0; i < sizeof(message); i++) {
111 ver2("Message %i %u", i, (uint8_t) message[i]);
112 out(0, message[i]);
113 }
114}
115
116void mid_init(void)
117{
118 ver("mid init");
119
120 midiin = rtmidi_in_create_default();
121 ver2("%p", midiin->ptr);
122 // check if null
123 uint32_t portcount = rtmidi_get_port_count(midiin);
124 ver2("%d", midiin->ok);
125
126 if (portcount == 0) {
127 midiin = NULL;
128 ver2("no midi ports\n");
129 return;
130 }
131
132 for (uint32_t i = 0; i < portcount; i++) {
133 ver2("Port %d: %s", i, rtmidi_get_port_name(midiin, i));
134 }
135
136 rtmidi_open_port(midiin, 0, rtmidi_get_port_name(midiin, 0));
137 if(!midiin->ok) {
138 fail("Failed to open Midi port");
139 }
140
141 rtmidi_in_set_callback(midiin, midi_callback, midiin->data);
142 if(!midiin->ok) {
143 fail("Failed to set Midi Callback");
144 }
145}
146
147void mid_quit(void)
148{
149 if(midiin) {
150 rtmidi_close_port(midiin);
151 rtmidi_in_free(midiin);
152 }
153
154 ver("mid quit");
155}
156
157bool mid_exec(void)
158{
159 ver3("mid exec");
160 return state[0].irq_r || state[0].irq_t || state[1].irq_r || state[1].irq_t;
161}
162
163uint32_t mid_read(uint32_t off, int32_t sz)
164{
165 ver2("mid rd %u:%d", off, sz * 8);
166
167 if (sz != 1 || off > 7) {
168 fail("invalid mid rd %u:%d", off, sz * 8);
169 }
170
171 int32_t rg = (int32_t)(off % 4);
172 int32_t un = (int32_t)(off / 4);
173
174 uint32_t rv;
175
176 switch (rg) {
177 case REG_IER_ISR:
178 rv = (uint32_t)(0xc0 | (state[un].rdr_ok ? 0x01 : 0x00));
179 state[un].irq_r = false;
180 state[un].irq_t = false;
181 ver2("ISR[%d] 0x%02x", un, rv);
182 break;
183
184 case REG_TDR_RDR:
185 rv = state[un].rdr;
186 state[un].rdr_ok = false;
187 ver2("RDR[%d] 0x%02x", un, rv);
188 break;
189
190 default:
191 rv = 0x00;
192 break;
193 }
194
195 if (!state[un].irq_r && !state[un].rdr_ok) {
196 xmit(un);
197 }
198
199 return rv;
200}
201
202void mid_write(uint32_t off, int32_t sz, uint32_t val)
203{
204 ver2("mid wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
205
206 if (sz != 1 || off > 7) {
207 fail("invalid mid wr %u:%d", off, sz * 8);
208 }
209
210 int32_t rg = (int32_t)(off % 4);
211 int32_t un = (int32_t)(off / 4);
212
213 switch (rg) {
214 case REG_CFR_SR:
215 ver2("CFR[%d] 0x%02x", un, val);
216
217 if (un == 1) {
218 fdd_set_side((int32_t)val & 0x01);
219 }
220 else {
221 fdd_set_sel((int32_t)val & 0x01);
222 }
223
224 break;
225
226 default:
227 break;
228 }
229}
Note: See TracBrowser for help on using the repository browser.