-
Notifications
You must be signed in to change notification settings - Fork 0
/
myMidiHost.cpp
247 lines (193 loc) · 7.51 KB
/
myMidiHost.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <myDebug.h>
#include "myMidiHost.h"
#include "prefs.h"
#include "defines.h"
#include "midiQueue.h"
#include "expSystem.h"
// #define HOST_CABLE_BIT 0x80
USBHost myusb;
// MIDIDevice midi1(myusb);
myMidiHostDevice midi_host(myusb);
void myMidiHostDevice::init()
{
// Wait 1.5 seconds before turning on USB Host. If connected USB devices
// use too much power, Teensy at least completes USB enumeration, which
// makes isolating the power issue easier.
myusb.begin();
}
bool passFilter(uint32_t iii)
{
// don't pass em if filter is not on
msgUnion msg(iii);
int type = msg.getMsgType();
if (getPref8(PREF_PERF_FILTER)) // filter is on
{
// only accept messages from cable 0
if (msg.isCable1())
return 0;
// only accept note on, note off, or pitchbends if the pref is cleared
bool filter_bends = getPref8(PREF_PERF_FILTER_BENDS);
if (type!=0x08 && type!=0x09 && (type!=0x0E || filter_bends))
return 0;
}
// prh 2020-08-02 - removed initial hack of layer filters
// send it to the teensyduino
usb_midi_write_packed(msg.i);
theSystem.midiActivity(INDEX_MASK_OUTPUT);
// it IS port one, cable 0
// if "monitor performanc" pref is set
// enqueue it for display as PORT_INDEX_DUINO_OUTPUT0
// with the PORT_MASK_PERFORM flag to display it differently
if (getPref8(PREF_MONITOR_PERFORMANCE))
{
msg.i &= ~PORT_MASK; // clear the old port
msg.i |= PORT_MASK_OUTPUT | PORT_MASK_PERFORM; // output to teensyDuino0
enqueueProcess(msg.i);
}
return 1; // flush the usb_midi buffer
}
// made virtual in USBHost_t36.h
void myMidiHostDevice::rx_data(const Transfer_t *transfer)
{
uint32_t len = (transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF)) >> 2;
if (len)
{
bool any = 0;
bool spoof_ftp = getPref8(PREF_SPOOF_FTP);
uint8_t ftp_port = getPref8(PREF_FTP_PORT);
for (uint32_t i=0; i < len; i++)
{
uint32_t msg = rx_buffer[i];
if (msg)
{
int pindex = ((msg >> 4) & PORT_INDEX_MASK) | INDEX_MASK_HOST;
theSystem.midiActivity(pindex);
//===========================================================
// WRITE THE MESSAGE DIRECTLY TO THE TEENSY_DUINO MIDI DEVICE
//===========================================================
// if spoofing, otherwise, let the filter decide
if (spoof_ftp)
{
any = 1;
usb_midi_write_packed(msg);
theSystem.midiActivity((pindex & ~INDEX_MASK_HOST) | INDEX_MASK_OUTPUT);
}
//-------------------
// Enqueue message
//-------------------
// We enqueue the message if (a) the port has been selected for monitoring,
// or (b) if it is the ftp port
msg |= PORT_MASK_HOST;
if (getPref8(PREF_MONITOR_PORT0 + pindex) || ( // if monitoring the port, OR
(ftp_port == FTP_PORT_HOST) && // if this is the PREF_FTP_PORT==1==Host, AND
INDEX_CABLE(pindex))) // cable=1
// if (msg & 0x10)
{
enqueueProcess(msg);
}
//----------------------
// output performance
//----------------------
if (!spoof_ftp && passFilter(msg))
any = 1;
}
}
if (any)
usb_midi_flush_output();
}
queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
}
#if 0 // possible code
int cur_buffer = 0;
void myMidiHostDevice::flush()
{
uint32_t tx_max = tx_size / 4;
if (cur_buffer == 0 && tx1_count)
{
display(0,"flushing buffer1 tx1_count=%d",tx1_count);
display_bytes(0,"bf1",(uint8_t *)tx_buffer1,tx_max*4);
tx1_count = tx_max;
queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
cur_buffer = 1;
}
else if (cur_buffer == 1 && tx2_count)
{
display(0,"flushing buffer2 tx2_count=%d",tx2_count);
display_bytes(0,"bf2",(uint8_t *)tx_buffer2,tx_max*4);
tx2_count = tx_max;
queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
cur_buffer = 0;
}
}
void myMidiHostDevice::write_packed(uint32_t data)
{
if (!txpipe) return;
uint32_t tx_max = tx_size / 4;
uint32_t tx_count = cur_buffer ? tx2_count : tx1_count;
uint32_t *tx_buffer = cur_buffer ? tx_buffer2 : tx_buffer1;
display(0,"my_write_packed(%08x) cur_buffer=%d tx_max=%d tx_count=%d",data,cur_buffer,tx_max,tx_count);
if (tx_count >= tx_max)
{
display(0,"my_write_packed() calling flush()",0);
flush();
tx_count = cur_buffer ? tx2_count : tx1_count;
tx_buffer = cur_buffer ? tx_buffer2 : tx_buffer1;
display(0,"after flush my_write_packed(%08x) cur_buffer=%d tx_max=%d tx_count=%d",data,cur_buffer,tx_max,tx_count);
if (tx_count >= tx_max)
{
my_error("could not write midi cur_buffer=%d!!!",cur_buffer);
return;
}
}
tx_buffer[tx_count++] = data;
if (cur_buffer)
tx2_count = tx_count;
else
tx1_count = tx_count;
return;
while (1)
{
uint32_t tx1 = tx1_count;
uint32_t tx2 = tx2_count;
display(0,"my_write_packed tx1=%d tx2=%d",tx1,tx2);
display_bytes(0,"bf1",(uint8_t *)tx_buffer1,tx_max*4);
display_bytes(0,"bf2",(uint8_t *)tx_buffer2,tx_max*4);
if (tx1 < tx_max && (tx2 == 0 || tx2 >= tx_max))
{
// use tx_buffer1
tx_buffer1[tx1++] = data;
tx1_count = tx1;
if (tx1 >= tx_max)
{
queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
}
else
{
// TODO: start a timer, rather than sending the buffer
// before it's full, to make best use of bandwidth
tx1_count = tx_max;
queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
}
return;
}
if (tx2 < tx_max)
{
// use tx_buffer2
tx_buffer2[tx2++] = data;
tx2_count = tx2;
if (tx2 >= tx_max)
{
queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
}
else
{
// TODO: start a timer, rather than sending the buffer
// before it's full, to make best use of bandwidth
tx2_count = tx_max;
queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
}
return;
}
}
}
#endif // disabled