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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/*
Drumstick RT Windows Backend
Copyright (C) 2009-2024 Pedro Lopez-Cabanillas <plcl@users.sf.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QString>
#include <QMap>
#include <windows.h>
#include <mmsystem.h>
#include "winmidiinput.h"
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
#define right Qt::right
#define left Qt::left
#define hex Qt::hex
#define dec Qt::dec
#define endl Qt::endl
#endif
namespace drumstick {
namespace rt {
static QLatin1String DEFAULT_PUBLIC_NAME = QLatin1String("MIDI In");
void CALLBACK midiCallback( HMIDIIN hMidiIn,
UINT wMsg,
DWORD_PTR dwInstance,
DWORD_PTR dwParam1,
DWORD_PTR dwParam2 );
class WinMIDIInput::WinMIDIInputPrivate {
public:
WinMIDIInput *m_inp;
MIDIOutput *m_out;
bool m_thruEnabled;
bool m_clientFilter;
HMIDIIN m_handle;
QString m_publicName;
MIDIConnection m_currentInput;
QStringList m_excludedNames;
QList<MIDIConnection> m_inputDevices;
bool m_status;
QStringList m_diagnostics;
#define SYSEX_BUF_SIZE 32768
MIDIHDR inMidiHeader;
unsigned char inSysexBuf[SYSEX_BUF_SIZE];
explicit WinMIDIInputPrivate(WinMIDIInput *inp):
m_inp(inp),
m_out(nullptr),
m_thruEnabled(false),
m_clientFilter(true),
m_handle(nullptr),
m_publicName(DEFAULT_PUBLIC_NAME)
{
reloadDeviceList(true);
}
~WinMIDIInputPrivate()
{
close();
}
void open(const MIDIConnection &conn) {
MMRESULT res;
m_status = false;
m_diagnostics.clear();
if (conn != m_currentInput) {
if (m_handle != nullptr)
close();
reloadDeviceList(!m_clientFilter);
if (!conn.first.isEmpty()) {
res = midiInOpen(&m_handle, conn.second.toInt(), (DWORD_PTR) midiCallback, (DWORD_PTR) this, CALLBACK_FUNCTION | MIDI_IO_STATUS );
if (res != MMSYSERR_NOERROR) {
logError("midiInOpen()",res);
} else {
/* Double buffering configuration, to manage Sysexes */
/* Store pointer to our input buffer for System Exclusive messages in MIDIHDR */
inMidiHeader.lpData = (LPSTR)&inSysexBuf[0];
/* Store its size in the MIDIHDR */
inMidiHeader.dwBufferLength = SYSEX_BUF_SIZE;
/* Flags must be set to 0 */
inMidiHeader.dwFlags = 0;
res = midiInPrepareHeader(m_handle, &inMidiHeader, sizeof(MIDIHDR));
if (res != MMSYSERR_NOERROR){
logError("midiInPrepareHeader()", res);
}
res = midiInAddBuffer(m_handle, &inMidiHeader, sizeof(MIDIHDR));
if (res != MMSYSERR_NOERROR){
logError("midiInAddBuffer()", res);
}
res = midiInStart(m_handle);
if (res != MMSYSERR_NOERROR) {
logError("midiInStart()", res);
}
}
m_currentInput = conn;
m_status = (res == MMSYSERR_NOERROR);
}
}
}
void close() {
MMRESULT res;
m_status = false;
m_diagnostics.clear();
if (m_handle != nullptr) {
res = midiInUnprepareHeader(m_handle, &inMidiHeader, sizeof(MIDIHDR));
if (res != MMSYSERR_NOERROR)
logError("midiInUnprepareHeader()", res);
res = midiInStop(m_handle);
if (res != MMSYSERR_NOERROR)
logError("midiInStop()", res);
res = midiInReset( m_handle );
if (res != MMSYSERR_NOERROR)
logError("midiInReset()", res);
res = midiInClose( m_handle );
if (res != MMSYSERR_NOERROR)
logError("midiInClose()", res);
m_handle = nullptr;
}
m_currentInput = MIDIConnection();
}
void reloadDeviceList(bool advanced)
{
MMRESULT res;
MIDIINCAPS deviceCaps;
QString devName;
unsigned int dev, max = midiInGetNumDevs();
m_inputDevices.clear();
m_clientFilter = !advanced;
for ( dev = 0; dev < max; ++dev) {
bool excluded = false;
res = midiInGetDevCaps( dev, &deviceCaps, sizeof(MIDIINCAPS));
if (res != MMSYSERR_NOERROR)
break;
#if defined(UNICODE)
devName = QString::fromWCharArray(deviceCaps.szPname);
#else
devName = QString::fromLocal8Bit(deviceCaps.szPname);
#endif
foreach (const QString& n , m_excludedNames) {
if (devName.startsWith(n)) {
excluded = true;
break;
}
}
if (!excluded) {
m_inputDevices << MIDIConnection(devName, dev);
}
}
}
void setPublicName(QString name)
{
if (m_publicName != name) {
m_publicName = name;
}
}
void emitSignals(int status, int channel, int data1, int data2)
{
switch (status) {
case MIDI_STATUS_NOTEOFF:
if(m_out != nullptr && m_thruEnabled)
m_out->sendNoteOff(channel, data1, data2);
emit m_inp->midiNoteOff(channel, data1, data2);
break;
case MIDI_STATUS_NOTEON:
if(m_out != nullptr && m_thruEnabled)
m_out->sendNoteOn(channel, data1, data2);
emit m_inp->midiNoteOn(channel, data1, data2);
break;
case MIDI_STATUS_KEYPRESURE:
if(m_out != nullptr && m_thruEnabled)
m_out->sendKeyPressure(channel, data1, data2);
emit m_inp->midiKeyPressure(channel, data1, data2);
break;
case MIDI_STATUS_CONTROLCHANGE:
if(m_out != nullptr && m_thruEnabled)
m_out->sendController(channel, data1, data2);
emit m_inp->midiController(channel, data1, data2);
break;
case MIDI_STATUS_PROGRAMCHANGE:
if(m_out != nullptr && m_thruEnabled)
m_out->sendProgram(channel, data1);
emit m_inp->midiProgram(channel, data1);
break;
case MIDI_STATUS_CHANNELPRESSURE:
if(m_out != nullptr && m_thruEnabled)
m_out->sendChannelPressure(channel, data1);
emit m_inp->midiChannelPressure(channel, data1);
break;
case MIDI_STATUS_PITCHBEND: {
int value = (data1 + data2 * 0x80) - 8192;
if(m_out != nullptr && m_thruEnabled)
m_out->sendPitchBend(channel, value);
emit m_inp->midiPitchBend(channel, value);
}
break;
default:
m_diagnostics << QString("MIDI in status? %1").arg(status);
}
}
void emitSysex(QByteArray data)
{
if(m_out != nullptr && m_thruEnabled)
m_out->sendSysex(data);
emit m_inp->midiSysex(data);
}
QString mmErrorString(MMRESULT err)
{
QString errstr;
#ifdef UNICODE
WCHAR buffer[1024];
midiInGetErrorText(err, &buffer[0], sizeof(buffer));
errstr = QString::fromWCharArray(buffer);
#else
char buffer[1024];
midiOutGetErrorText(err, &buffer[0], sizeof(buffer));
errstr = QString::fromLocal8Bit(buffer);
#endif
return errstr;
}
void logError(const QString context, const MMRESULT code)
{
m_diagnostics << QString("%1 error: %2 (%3)").arg(context).arg(code).arg(mmErrorString(code));
}
};
void CALLBACK midiCallback( HMIDIIN hMidiIn,
UINT wMsg,
DWORD_PTR dwInstance,
DWORD_PTR dwParam1,
DWORD_PTR dwParam2 )
{
Q_UNUSED(hMidiIn)
Q_UNUSED(dwParam2)
WinMIDIInput::WinMIDIInputPrivate* object = (WinMIDIInput::WinMIDIInputPrivate*) dwInstance;
switch( wMsg ) {
case MIM_OPEN:
object->m_diagnostics << "Open input";
break;
case MIM_CLOSE:
object->m_diagnostics << "Close input";
break;
case MIM_ERROR:
case MIM_LONGERROR:
object->m_diagnostics << "Errors input";
break;
case MIM_LONGDATA:
{
object->m_diagnostics << "Sysex data input";
// Extract MIDI SysEx message information
MIDIHDR* pMidiHdr = reinterpret_cast<MIDIHDR*>(dwParam1);
// Check for SysEx messages
if (pMidiHdr->dwBytesRecorded > 0 && (unsigned char)pMidiHdr->lpData[0] == 0xF0) // Start of SysEx
{
QByteArray sysExByteArray(reinterpret_cast<const char*>(pMidiHdr->lpData), static_cast<int>(pMidiHdr->dwBytesRecorded));
object->emitSysex(sysExByteArray);
}
if (object->m_status) {
/* Queue the MIDIHDR for more input */
midiInAddBuffer(hMidiIn, pMidiHdr, sizeof(MIDIHDR));
}
break;
}
case MIM_DATA:
case MIM_MOREDATA: {
int status = dwParam1 & 0xf0;
int channel = dwParam1 & 0x0f;
int data1 = (dwParam1 & 0x7f00) >> 8;
int data2 = (dwParam1 & 0x7f0000) >> 16;
object->emitSignals(status, channel, data1, data2);
}
break;
default:
object->m_diagnostics << QString("unknown input message: %1").arg(wMsg);
break;
}
}
WinMIDIInput::WinMIDIInput(QObject *parent) :
MIDIInput(parent), d(new WinMIDIInputPrivate(this))
{ }
WinMIDIInput::~WinMIDIInput()
{
delete d;
}
void WinMIDIInput::initialize(QSettings *settings)
{
Q_UNUSED(settings)
}
QString WinMIDIInput::backendName()
{
return QLatin1String("Windows MM");
}
QString WinMIDIInput::publicName()
{
return d->m_publicName;
}
void WinMIDIInput::setPublicName(QString name)
{
d->setPublicName(name);
}
QList<MIDIConnection> WinMIDIInput::connections(bool advanced)
{
d->reloadDeviceList(advanced);
return d->m_inputDevices;
}
void WinMIDIInput::setExcludedConnections(QStringList conns)
{
d->m_excludedNames = conns;
}
void WinMIDIInput::open(const MIDIConnection& name)
{
d->open(name);
}
void WinMIDIInput::close()
{
d->close();
}
MIDIConnection WinMIDIInput::currentConnection()
{
return d->m_currentInput;
}
void WinMIDIInput::setMIDIThruDevice(MIDIOutput *device)
{
d->m_out = device;
}
void WinMIDIInput::enableMIDIThru(bool enable)
{
d->m_thruEnabled = enable;
}
bool WinMIDIInput::isEnabledMIDIThru()
{
return d->m_thruEnabled && d->m_out != nullptr;
}
QStringList WinMIDIInput::getDiagnostics()
{
return d->m_diagnostics;
}
bool WinMIDIInput::getStatus()
{
return d->m_status;
}
}} // namespace drumstick::rt
|