| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "rtmidi.h" |
| 21 |
#include "../ta-log.h" |
| 22 |
#include "taAudio.h" |
| 23 |
|
| 24 |
String sanitizePortName(const String& name) { |
| 25 |
#if defined(_WIN32) |
| 26 |
// remove port number |
| 27 |
size_t namePos=name.rfind(' '); |
| 28 |
if (namePos!=String::npos) { |
| 29 |
return name.substr(0,namePos); |
| 30 |
} |
| 31 |
return name; |
| 32 |
#elif defined(__linux__) |
| 33 |
// remove port location |
| 34 |
size_t namePos=name.rfind(' '); |
| 35 |
if (namePos!=String::npos) { |
| 36 |
return name.substr(0,namePos); |
| 37 |
} |
| 38 |
return name; |
| 39 |
#else |
| 40 |
return name; |
| 41 |
#endif |
| 42 |
} |
| 43 |
|
| 44 |
// --- IN --- |
| 45 |
|
| 46 |
bool TAMidiInRtMidi::gather() { |
| 47 |
std::vector<unsigned char> msg; |
| 48 |
if (port==NULL) return false; |
| 49 |
try { |
| 50 |
while (true) { |
| 51 |
TAMidiMessage m; |
| 52 |
double t=port->getMessage(&msg); |
| 53 |
if (msg.empty()) break; |
| 54 |
|
| 55 |
// parse message |
| 56 |
m.time=t; |
| 57 |
m.type=msg[0]; |
| 58 |
if (m.type!=TA_MIDI_SYSEX && msg.size()>1) { |
| 59 |
memcpy(m.data,msg.data()+1,MIN(msg.size()-1,7)); |
| 60 |
} else if (m.type==TA_MIDI_SYSEX) { |
| 61 |
m.sysExData=std::shared_ptr<unsigned char>(new unsigned char[msg.size()],std::default_delete<unsigned char[]>()); |
| 62 |
m.sysExLen=msg.size(); |
| 63 |
logD("got a SysEx of length %ld!",msg.size()); |
| 64 |
memcpy(m.sysExData.get(),msg.data(),msg.size()); |
| 65 |
} |
| 66 |
queue.push(m); |
| 67 |
} |
| 68 |
} catch (RtMidiError& e) { |
| 69 |
logE("MIDI input error! %s",e.what()); |
| 70 |
closeDevice(); |
| 71 |
return false; |
| 72 |
} |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
std::vector<String> TAMidiInRtMidi::listDevices() { |
| 77 |
std::vector<String> ret; |
| 78 |
logD("listing devices."); |
| 79 |
if (port==NULL) return ret; |
| 80 |
|
| 81 |
try { |
| 82 |
unsigned int count=port->getPortCount(); |
| 83 |
logD("got port count."); |
| 84 |
for (unsigned int i=0; i<count; i++) { |
| 85 |
String name=sanitizePortName(port->getPortName(i)); |
| 86 |
if (name!="") ret.push_back(name); |
| 87 |
} |
| 88 |
} catch (RtMidiError& e) { |
| 89 |
logW("could not get MIDI inputs! %s",e.what()); |
| 90 |
} |
| 91 |
return ret; |
| 92 |
} |
| 93 |
|
| 94 |
bool TAMidiInRtMidi::isDeviceOpen() { |
| 95 |
return isOpen; |
| 96 |
} |
| 97 |
|
| 98 |
bool TAMidiInRtMidi::openDevice(String name) { |
| 99 |
if (port==NULL) return false; |
| 100 |
if (isOpen) return false; |
| 101 |
try { |
| 102 |
bool portOpen=false; |
| 103 |
unsigned int count=port->getPortCount(); |
| 104 |
logD("finding port %s...",name); |
| 105 |
for (unsigned int i=0; i<count; i++) { |
| 106 |
String portName=sanitizePortName(port->getPortName(i)); |
| 107 |
logV("- %d: %s",i,portName); |
| 108 |
if (portName==name) { |
| 109 |
logD("opening port %d...",i); |
| 110 |
port->openPort(i); |
| 111 |
portOpen=true; |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
isOpen=portOpen; |
| 116 |
if (!portOpen) logW("could not find MIDI in device..."); |
| 117 |
return portOpen; |
| 118 |
} catch (RtMidiError& e) { |
| 119 |
logW("could not open MIDI in device! %s",e.what()); |
| 120 |
return false; |
| 121 |
} |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
bool TAMidiInRtMidi::closeDevice() { |
| 126 |
if (port==NULL) return false; |
| 127 |
if (!isOpen) return false; |
| 128 |
try { |
| 129 |
port->closePort(); |
| 130 |
} catch (RtMidiError& e) { |
| 131 |
logW("could not close MIDI in device! %s",e.what()); |
| 132 |
isOpen=false; // still |
| 133 |
return false; |
| 134 |
} |
| 135 |
isOpen=false; |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
bool TAMidiInRtMidi::init() { |
| 140 |
if (port!=NULL) return true; |
| 141 |
try { |
| 142 |
port=new RtMidiIn; |
| 143 |
port->ignoreTypes(false,true,true); |
| 144 |
} catch (RtMidiError& e) { |
| 145 |
logW("could not initialize RtMidi in! %s",e.what()); |
| 146 |
return false; |
| 147 |
} |
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
bool TAMidiInRtMidi::quit() { |
| 152 |
if (port!=NULL) { |
| 153 |
delete port; |
| 154 |
port=NULL; |
| 155 |
} |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
// --- OUT --- |
| 160 |
|
| 161 |
bool TAMidiOutRtMidi::send(const TAMidiMessage& what) { |
| 162 |
if (!isOpen) return false; |
| 163 |
if (!isWorking) return false; |
| 164 |
if (what.type<0x80) return false; |
| 165 |
size_t len=0; |
| 166 |
switch (what.type&0xf0) { |
| 167 |
case TA_MIDI_NOTE_OFF: |
| 168 |
case TA_MIDI_NOTE_ON: |
| 169 |
case TA_MIDI_AFTERTOUCH: |
| 170 |
case TA_MIDI_CONTROL: |
| 171 |
case TA_MIDI_PITCH_BEND: |
| 172 |
len=3; |
| 173 |
break; |
| 174 |
case TA_MIDI_PROGRAM: |
| 175 |
case TA_MIDI_CHANNEL_AFTERTOUCH: |
| 176 |
len=2; |
| 177 |
break; |
| 178 |
} |
| 179 |
if (len==0) switch (what.type) { |
| 180 |
case TA_MIDI_SYSEX: |
| 181 |
if (what.sysExLen<1) { |
| 182 |
logE("sysExLen is NULL!"); |
| 183 |
return false; |
| 184 |
} |
| 185 |
if (what.sysExData.get()==NULL) { |
| 186 |
logE("sysExData is NULL!"); |
| 187 |
return false; |
| 188 |
} |
| 189 |
len=what.sysExLen; |
| 190 |
try { |
| 191 |
port->sendMessage(what.sysExData.get(),len); |
| 192 |
} catch (RtMidiError& e) { |
| 193 |
logE("MIDI output error! %s",e.what()); |
| 194 |
isWorking=false; |
| 195 |
return false; |
| 196 |
} |
| 197 |
return true; |
| 198 |
break; |
| 199 |
case TA_MIDI_MTC_FRAME: |
| 200 |
case TA_MIDI_SONG_SELECT: |
| 201 |
len=2; |
| 202 |
break; |
| 203 |
case TA_MIDI_POSITION: |
| 204 |
len=3; |
| 205 |
break; |
| 206 |
default: |
| 207 |
len=1; |
| 208 |
break; |
| 209 |
} |
| 210 |
try { |
| 211 |
port->sendMessage((const unsigned char*)&what.type,len); |
| 212 |
} catch (RtMidiError& e) { |
| 213 |
logE("MIDI output error! %s",e.what()); |
| 214 |
isWorking=false; |
| 215 |
return false; |
| 216 |
} |
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
bool TAMidiOutRtMidi::isDeviceOpen() { |
| 221 |
return isOpen; |
| 222 |
} |
| 223 |
|
| 224 |
bool TAMidiOutRtMidi::openDevice(String name) { |
| 225 |
if (port==NULL) return false; |
| 226 |
if (isOpen) return false; |
| 227 |
try { |
| 228 |
bool portOpen=false; |
| 229 |
unsigned int count=port->getPortCount(); |
| 230 |
logD("finding port %s...",name); |
| 231 |
for (unsigned int i=0; i<count; i++) { |
| 232 |
String portName=sanitizePortName(port->getPortName(i)); |
| 233 |
logV("- %d: %s",i,portName); |
| 234 |
if (portName==name) { |
| 235 |
logD("opening port %d...",i); |
| 236 |
port->openPort(i); |
| 237 |
portOpen=true; |
| 238 |
break; |
| 239 |
} |
| 240 |
} |
| 241 |
isOpen=portOpen; |
| 242 |
if (!portOpen) logW("could not find MIDI out device..."); |
| 243 |
isWorking=true; |
| 244 |
return portOpen; |
| 245 |
} catch (RtMidiError& e) { |
| 246 |
logW("could not open MIDI out device! %s",e.what()); |
| 247 |
return false; |
| 248 |
} |
| 249 |
isWorking=true; |
| 250 |
return true; |
| 251 |
} |
| 252 |
|
| 253 |
bool TAMidiOutRtMidi::closeDevice() { |
| 254 |
if (port==NULL) return false; |
| 255 |
if (!isOpen) return false; |
| 256 |
isWorking=false; |
| 257 |
try { |
| 258 |
port->closePort(); |
| 259 |
} catch (RtMidiError& e) { |
| 260 |
logW("could not close MIDI out device! %s",e.what()); |
| 261 |
isOpen=false; // still |
| 262 |
return false; |
| 263 |
} |
| 264 |
isOpen=false; |
| 265 |
return true; |
| 266 |
} |
| 267 |
|
| 268 |
std::vector<String> TAMidiOutRtMidi::listDevices() { |
| 269 |
std::vector<String> ret; |
| 270 |
if (port==NULL) return ret; |
| 271 |
|
| 272 |
try { |
| 273 |
unsigned int count=port->getPortCount(); |
| 274 |
for (unsigned int i=0; i<count; i++) { |
| 275 |
String name=sanitizePortName(port->getPortName(i)); |
| 276 |
if (name!="") ret.push_back(name); |
| 277 |
} |
| 278 |
} catch (RtMidiError& e) { |
| 279 |
logW("could not get MIDI outputs! %s",e.what()); |
| 280 |
} |
| 281 |
return ret; |
| 282 |
} |
| 283 |
|
| 284 |
bool TAMidiOutRtMidi::init() { |
| 285 |
if (port!=NULL) return true; |
| 286 |
try { |
| 287 |
port=new RtMidiOut; |
| 288 |
} catch (RtMidiError& e) { |
| 289 |
logW("could not initialize RtMidi out! %s",e.what()); |
| 290 |
return false; |
| 291 |
} |
| 292 |
return true; |
| 293 |
} |
| 294 |
|
| 295 |
bool TAMidiOutRtMidi::quit() { |
| 296 |
if (port!=NULL) { |
| 297 |
delete port; |
| 298 |
port=NULL; |
| 299 |
} |
| 300 |
return true; |
| 301 |
} |