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
|
/*
Copyright (C) 2011 Devin Anderson
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <cassert>
#include <memory>
#include <stdexcept>
#include "JackError.h"
#include "JackTime.h"
#include "JackMidiUtil.h"
#include "JackWinMMEInputPort.h"
#include "JackMidiWriteQueue.h"
using Jack::JackWinMMEInputPort;
///////////////////////////////////////////////////////////////////////////////
// Static callbacks
///////////////////////////////////////////////////////////////////////////////
void CALLBACK
JackWinMMEInputPort::HandleMidiInputEvent(HMIDIIN handle, UINT message,
DWORD port, DWORD param1,
DWORD param2)
{
((JackWinMMEInputPort *) port)->ProcessWinMME(message, param1, param2);
}
///////////////////////////////////////////////////////////////////////////////
// Class
///////////////////////////////////////////////////////////////////////////////
JackWinMMEInputPort::JackWinMMEInputPort(const char *alias_name,
const char *client_name,
const char *driver_name, UINT index,
size_t max_bytes, size_t max_messages)
{
thread_queue = new JackMidiAsyncQueue(max_bytes, max_messages);
std::auto_ptr<JackMidiAsyncQueue> thread_queue_ptr(thread_queue);
write_queue = new JackMidiBufferWriteQueue();
std::auto_ptr<JackMidiBufferWriteQueue> write_queue_ptr(write_queue);
sysex_buffer = new jack_midi_data_t[max_bytes];
char error_message[MAXERRORLENGTH];
MMRESULT result = midiInOpen(&handle, index,
(DWORD_PTR) HandleMidiInputEvent,
(DWORD_PTR)this,
CALLBACK_FUNCTION | MIDI_IO_STATUS);
if (result != MMSYSERR_NOERROR) {
GetInErrorString(result, error_message);
goto delete_sysex_buffer;
}
sysex_header.dwBufferLength = max_bytes;
sysex_header.dwFlags = 0;
sysex_header.lpData = (LPSTR)sysex_buffer;
result = midiInPrepareHeader(handle, &sysex_header, sizeof(MIDIHDR));
if (result != MMSYSERR_NOERROR) {
GetInErrorString(result, error_message);
goto close_handle;
}
result = midiInAddBuffer(handle, &sysex_header, sizeof(MIDIHDR));
if (result != MMSYSERR_NOERROR) {
GetInErrorString(result, error_message);
goto unprepare_header;
}
MIDIINCAPS capabilities;
char *name_tmp;
result = midiInGetDevCaps(index, &capabilities, sizeof(capabilities));
if (result != MMSYSERR_NOERROR) {
WriteInError("JackWinMMEInputPort [constructor]", "midiInGetDevCaps",
result);
name_tmp = (char*) driver_name;
} else {
name_tmp = capabilities.szPname;
}
snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", alias_name, name_tmp,
index + 1);
snprintf(name, sizeof(name) - 1, "%s:capture_%d", client_name, index + 1);
jack_event = 0;
started = false;
write_queue_ptr.release();
thread_queue_ptr.release();
return;
unprepare_header:
result = midiInUnprepareHeader(handle, &sysex_header, sizeof(MIDIHDR));
if (result != MMSYSERR_NOERROR) {
WriteInError("JackWinMMEInputPort [constructor]",
"midiInUnprepareHeader", result);
}
close_handle:
result = midiInClose(handle);
if (result != MMSYSERR_NOERROR) {
WriteInError("JackWinMMEInputPort [constructor]", "midiInClose",
result);
}
delete_sysex_buffer:
delete[] sysex_buffer;
throw std::runtime_error(error_message);
}
JackWinMMEInputPort::~JackWinMMEInputPort()
{
MMRESULT result = midiInReset(handle);
if (result != MMSYSERR_NOERROR) {
WriteInError("JackWinMMEInputPort [destructor]", "midiInReset", result);
}
result = midiInUnprepareHeader(handle, &sysex_header, sizeof(MIDIHDR));
if (result != MMSYSERR_NOERROR) {
WriteInError("JackWinMMEInputPort [destructor]",
"midiInUnprepareHeader", result);
}
result = midiInClose(handle);
if (result != MMSYSERR_NOERROR) {
WriteInError("JackWinMMEInputPort [destructor]", "midiInClose", result);
}
delete[] sysex_buffer;
delete thread_queue;
delete write_queue;
}
void
JackWinMMEInputPort::EnqueueMessage(DWORD timestamp, size_t length,
jack_midi_data_t *data)
{
jack_nframes_t frame =
GetFramesFromTime(start_time + (((jack_time_t) timestamp) * 1000));
// Debugging code
jack_time_t current_time = GetMicroSeconds();
jack_log("JackWinMMEInputPort::EnqueueMessage - enqueueing event at %f "
"(frame: %d) with start offset '%d' scheduled for frame '%d'",
((double) current_time) / 1000.0, GetFramesFromTime(current_time),
timestamp, frame);
// End debugging code
switch (thread_queue->EnqueueEvent(frame, length, data)) {
case JackMidiWriteQueue::BUFFER_FULL:
jack_error("JackWinMMEInputPort::EnqueueMessage - The thread queue "
"cannot currently accept a %d-byte event. Dropping event.",
length);
break;
case JackMidiWriteQueue::BUFFER_TOO_SMALL:
jack_error("JackWinMMEInputPort::EnqueueMessage - The thread queue "
"buffer is too small to enqueue a %d-byte event. Dropping "
"event.", length);
break;
default:
;
}
}
void
JackWinMMEInputPort::GetInErrorString(MMRESULT error, LPTSTR text)
{
MMRESULT result = midiInGetErrorText(error, text, MAXERRORLENGTH);
if (result != MMSYSERR_NOERROR) {
snprintf(text, MAXERRORLENGTH, "Unknown error code '%d'", error);
}
}
void
JackWinMMEInputPort::ProcessJack(JackMidiBuffer *port_buffer,
jack_nframes_t frames)
{
write_queue->ResetMidiBuffer(port_buffer, frames);
if (! jack_event) {
jack_event = thread_queue->DequeueEvent();
}
for (; jack_event; jack_event = thread_queue->DequeueEvent()) {
switch (write_queue->EnqueueEvent(jack_event, frames)) {
case JackMidiWriteQueue::BUFFER_TOO_SMALL:
jack_error("JackWinMMEMidiInputPort::Process - The buffer write "
"queue couldn't enqueue a %d-byte event. Dropping "
"event.", jack_event->size);
// Fallthrough on purpose
case JackMidiWriteQueue::OK:
continue;
default:
break;
}
break;
}
}
void
JackWinMMEInputPort::ProcessWinMME(UINT message, DWORD param1, DWORD param2)
{
set_threaded_log_function();
switch (message) {
case MIM_CLOSE:
jack_info("JackWinMMEInputPort::ProcessWinMME - MIDI device closed.");
break;
case MIM_MOREDATA:
jack_info("JackWinMMEInputPort::ProcessWinMME - The MIDI input device "
"driver thinks that JACK is not processing messages fast "
"enough.");
// Fallthrough on purpose.
case MIM_DATA: {
jack_midi_data_t message_buffer[3];
jack_midi_data_t status = param1 & 0xff;
int length = GetMessageLength(status);
switch (length) {
case 3:
message_buffer[2] = (param1 >> 16) & 0xff;
// Fallthrough on purpose.
case 2:
message_buffer[1] = (param1 >> 8) & 0xff;
// Fallthrough on purpose.
case 1:
message_buffer[0] = status;
break;
case 0:
jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
"input driver sent an MIM_DATA message with a sysex "
"status byte.");
return;
case -1:
jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
"input driver sent an MIM_DATA message with an invalid "
"status byte.");
return;
}
EnqueueMessage(param2, (size_t) length, message_buffer);
break;
}
case MIM_LONGDATA: {
LPMIDIHDR header = (LPMIDIHDR) param1;
size_t byte_count = header->dwBytesRecorded;
if (! byte_count) {
jack_info("JackWinMMEInputPort::ProcessWinMME - WinMME driver has "
"returned sysex header to us with no bytes. The JACK "
"driver is probably being stopped.");
break;
}
jack_midi_data_t *data = (jack_midi_data_t *) header->lpData;
if ((data[0] != 0xf0) || (data[byte_count - 1] != 0xf7)) {
jack_error("JackWinMMEInputPort::ProcessWinMME - Discarding "
"%d-byte sysex chunk.", byte_count);
} else {
EnqueueMessage(param2, byte_count, data);
}
// Is this realtime-safe? This function isn't run in the JACK thread,
// but we still want it to perform as quickly as possible. Even if
// this isn't realtime safe, it may not be avoidable.
MMRESULT result = midiInAddBuffer(handle, &sysex_header,
sizeof(MIDIHDR));
if (result != MMSYSERR_NOERROR) {
WriteInError("JackWinMMEInputPort::ProcessWinMME",
"midiInAddBuffer", result);
}
break;
}
case MIM_LONGERROR:
jack_error("JackWinMMEInputPort::ProcessWinMME - Invalid or "
"incomplete sysex message received.");
break;
case MIM_OPEN:
jack_info("JackWinMMEInputPort::ProcessWinMME - MIDI device opened.");
}
}
bool
JackWinMMEInputPort::Start()
{
if (! started) {
start_time = GetMicroSeconds();
MMRESULT result = midiInStart(handle);
started = result == MMSYSERR_NOERROR;
if (! started) {
WriteInError("JackWinMMEInputPort::Start", "midiInStart", result);
}
}
return started;
}
bool
JackWinMMEInputPort::Stop()
{
if (started) {
MMRESULT result = midiInStop(handle);
started = result != MMSYSERR_NOERROR;
if (started) {
WriteInError("JackWinMMEInputPort::Stop", "midiInStop", result);
}
}
return ! started;
}
void
JackWinMMEInputPort::WriteInError(const char *jack_func, const char *mm_func,
MMRESULT result)
{
char error_message[MAXERRORLENGTH];
GetInErrorString(result, error_message);
jack_error("%s - %s: %s", jack_func, mm_func, error_message);
}
|