forked from RebelTechnology/OpenWare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidiWriter.cpp
200 lines (187 loc) · 4.69 KB
/
MidiWriter.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
#include <string.h>
#include "device.h"
#include "MidiStatus.h"
#include "MidiWriter.h"
#include "OpenWareMidiControl.h"
#if defined USE_USBD_MIDI || defined USE_USBH_MIDI
#include "midi.h"
#endif
#ifdef USE_BLE_MIDI
#include "ble_midi.h"
#endif /* USE_BLE_MIDI */
#ifdef USE_UART_MIDI_TX
#include "uart_midi.h"
#endif
#include "SerialBuffer.hpp"
void MidiWriter::sendErrorMessage(const char* msg){
char buf[64];
buf[0] = 0x30; // error message
char* p = &buf[1];
p = stpncpy(p, msg, 62);
sendSysEx((uint8_t*)buf, p-buf);
}
void MidiWriter::sendPc(uint8_t pc){
send(MidiMessage::pc(channel, pc));
}
void MidiWriter::sendCc(uint8_t cc, uint8_t value){
send(MidiMessage::cc(channel, cc, value));
}
void MidiWriter::sendNote(uint8_t note, uint8_t velocity){
send(MidiMessage::note(channel, note, velocity));
}
void MidiWriter::sendPitchBend(uint16_t value){
send(MidiMessage::pb(channel, value));
}
void MidiWriter::sendSysEx(uint8_t* data, uint16_t size){
/* USB-MIDI devices transmit sysex messages in 4-byte packets which
* contain a status byte and up to 3 bytes of the message itself.
* If the message ends with fewer than 3 bytes, a different code is
* sent. Go through the sysex 3 bytes at a time, including the leading
* 0xF0 and trailing 0xF7.
*/
MidiMessage packet(USB_COMMAND_SYSEX, SYSEX, MIDI_SYSEX_MANUFACTURER,
uint8_t(MIDI_SYSEX_OWL_DEVICE | channel));
send(packet);
int count = size/3;
uint8_t* src = data;
while(count-- > 0){
packet.data[1] = (*src++ & 0x7f);
packet.data[2] = (*src++ & 0x7f);
packet.data[3] = (*src++ & 0x7f);
send(packet);
}
count = size % 3;
switch(count){
case 0:
packet.data[0] = USB_COMMAND_SYSEX_EOX1;
packet.data[1] = SYSEX_EOX;
packet.data[2] = 0;
packet.data[3] = 0;
break;
case 1:
packet.data[0] = USB_COMMAND_SYSEX_EOX2;
packet.data[1] = (*src++ & 0x7f);
packet.data[2] = SYSEX_EOX;
packet.data[3] = 0;
break;
case 2:
packet.data[0] = USB_COMMAND_SYSEX_EOX3;
packet.data[1] = (*src++ & 0x7f);
packet.data[2] = (*src++ & 0x7f);
packet.data[3] = SYSEX_EOX;
break;
}
send(packet);
}
class MidiTransmitter {
public:
virtual void write(MidiMessage msg){};
virtual void transmit(){};
};
#ifdef USE_USBD_MIDI
class UsbdMidiTransmitter : public MidiTransmitter {
private:
SerialBuffer<MIDI_OUTPUT_BUFFER_SIZE> buffer;
public:
void write(MidiMessage msg){
if(buffer.getContiguousWriteCapacity() <= 4)
transmit();
buffer.push(msg.data, 4);
}
void transmit(){
size_t len = buffer.getContiguousReadCapacity();
if(len >= 4 && usbd_midi_ready()){
usbd_midi_tx(buffer.getReadHead(), len);
buffer.incrementReadHead(len);
// len = buffer.getContiguousReadCapacity();
}
}
};
UsbdMidiTransmitter usbd_midi;
#endif
#ifdef USE_USBH_MIDI
class UsbhMidiTransmitter : public MidiTransmitter {
private:
SerialBuffer<MIDI_OUTPUT_BUFFER_SIZE> buffer;
public:
void write(MidiMessage msg){
buffer.push(msg.data, 4);
}
void transmit(){
size_t len = buffer.getContiguousReadCapacity();
if(len >= 4 && usbh_midi_ready()){
usbh_midi_tx(buffer.getReadHead(), len);
buffer.incrementReadHead(len);
// len = buffer.getContiguousReadCapacity();
}
}
};
UsbhMidiTransmitter usbh_midi;
#endif
#ifdef USE_BLE
class BleMidiTransmitter : public MidiTransmitter {
private:
SerialBuffer<MIDI_OUTPUT_BUFFER_SIZE> buffer;
public:
void write(MidiMessage msg){
buffer.push(msg.data, 4);
}
void transmit(){
size_t len = buffer.getContiguousReadCapacity();
if(len >= 4){
ble_tx(buffer.getReadHead(), len);
buffer.incrementReadHead(len);
}
}
};
BleMidiTransmitter ble_midi;
#endif
#ifdef USE_UART_MIDI_TX
class UartMidiTransmitter : public MidiTransmitter {
private:
SerialBuffer<MIDI_OUTPUT_BUFFER_SIZE> buffer;
public:
void write(MidiMessage msg){
buffer.push(msg.data+1, 3); // skip USB byte
}
void transmit(){
size_t len = buffer.getContiguousReadCapacity();
if(len && uart_ready()){
uart_tx(buffer.getReadHead(), len);
buffer.incrementReadHead(len);
}
}
};
UartMidiTransmitter uart_midi DMA_RAM;
#endif
void MidiWriter::send(MidiMessage msg){
#ifdef USE_USBD_MIDI
usbd_midi.write(msg);
#endif
#ifdef USE_USBH_MIDI
usbh_midi.write(msg);
#endif
#ifdef USE_BLE
ble_midi.write(msg);
#endif
#ifdef USE_UART_MIDI_TX
uart_midi.write(msg);
#endif
// #ifdef USE_DIGITALBUS
// bus_tx_frame(msg.data);
// #endif /* USE_DIGITALBUS */
}
void MidiWriter::transmit(){
#ifdef USE_USBD_MIDI
usbd_midi.transmit();
#endif
#ifdef USE_USBH_MIDI
usbh_midi.transmit();
#endif
#ifdef USE_BLE
ble_midi.transmit();
#endif
#ifdef USE_UART_MIDI_TX
uart_midi.transmit();
#endif
}