| 1 |
/* |
| 2 |
Copyright (C) 2009 Grame |
| 3 |
Copyright (C) 2011 Devin Anderson |
| 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 |
| 16 |
along with this program; if not, write to the Free Software |
| 17 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
|
| 19 |
*/ |
| 20 |
|
| 21 |
#include "JackCompilerDeps.h" |
| 22 |
#include "JackCoreMidiDriver.h" |
| 23 |
#include "JackCoreMidiUtil.h" |
| 24 |
#include "JackEngineControl.h" |
| 25 |
#include "driver_interface.h" |
| 26 |
|
| 27 |
#include <stdexcept> |
| 28 |
#include <mach/mach_time.h> |
| 29 |
#include <mach/mach_error.h> |
| 30 |
|
| 31 |
using Jack::JackCoreMidiDriver; |
| 32 |
|
| 33 |
static char capture_driver_name[256]; |
| 34 |
static char playback_driver_name[256]; |
| 35 |
|
| 36 |
static int in_channels, out_channels; |
| 37 |
static bool capturing, playing, monitor; |
| 38 |
|
| 39 |
static jack_nframes_t capture_latency, playback_latency; |
| 40 |
|
| 41 |
/////////////////////////////////////////////////////////////////////////////// |
| 42 |
// Static callbacks |
| 43 |
/////////////////////////////////////////////////////////////////////////////// |
| 44 |
|
| 45 |
void |
| 46 |
JackCoreMidiDriver::HandleInputEvent(const MIDIPacketList *packet_list, |
| 47 |
void *driver, void *port) |
| 48 |
{ |
| 49 |
((JackCoreMidiPhysicalInputPort *) port)->ProcessCoreMidi(packet_list); |
| 50 |
} |
| 51 |
|
| 52 |
void |
| 53 |
JackCoreMidiDriver::HandleNotificationEvent(const MIDINotification *message, |
| 54 |
void *driver) |
| 55 |
{ |
| 56 |
((JackCoreMidiDriver *) driver)->HandleNotification(message); |
| 57 |
} |
| 58 |
|
| 59 |
/////////////////////////////////////////////////////////////////////////////// |
| 60 |
// Class |
| 61 |
/////////////////////////////////////////////////////////////////////////////// |
| 62 |
|
| 63 |
JackCoreMidiDriver::JackCoreMidiDriver(const char *name, const char *alias, |
| 64 |
JackLockedEngine *engine, |
| 65 |
JackSynchro *table): |
| 66 |
JackMidiDriver(name, alias, engine, table),fThread(this) |
| 67 |
{ |
| 68 |
mach_timebase_info_data_t info; |
| 69 |
kern_return_t result = mach_timebase_info(&info); |
| 70 |
if (result != KERN_SUCCESS) { |
| 71 |
throw std::runtime_error(mach_error_string(result)); |
| 72 |
} |
| 73 |
client = 0; |
| 74 |
fCaptureChannels = 0; |
| 75 |
fPlaybackChannels = 0; |
| 76 |
num_physical_inputs = 0; |
| 77 |
num_physical_outputs = 0; |
| 78 |
num_virtual_inputs = 0; |
| 79 |
num_virtual_outputs = 0; |
| 80 |
physical_input_ports = 0; |
| 81 |
physical_output_ports = 0; |
| 82 |
time_ratio = (((double) info.numer) / info.denom) / 1000.0; |
| 83 |
virtual_input_ports = 0; |
| 84 |
virtual_output_ports = 0; |
| 85 |
internal_input = 0; |
| 86 |
internal_output = 0; |
| 87 |
} |
| 88 |
|
| 89 |
JackCoreMidiDriver::~JackCoreMidiDriver() |
| 90 |
{} |
| 91 |
|
| 92 |
bool JackCoreMidiDriver::Init() |
| 93 |
{ |
| 94 |
return OpenAux(); |
| 95 |
} |
| 96 |
|
| 97 |
bool JackCoreMidiDriver::OpenAux() |
| 98 |
{ |
| 99 |
int pi_count = 0; |
| 100 |
int po_count = 0; |
| 101 |
int vi_count = 0; |
| 102 |
int vo_count = 0; |
| 103 |
ItemCount potential_po_count; |
| 104 |
ItemCount potential_pi_count; |
| 105 |
|
| 106 |
CFStringRef name = CFStringCreateWithCString(0, "JackMidi", |
| 107 |
CFStringGetSystemEncoding()); |
| 108 |
if (! name) { |
| 109 |
jack_error("JackCoreMidiDriver::Open - failed to allocate memory for " |
| 110 |
"client name string"); |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
OSStatus status = MIDIClientCreate(name, HandleNotificationEvent, this, |
| 115 |
&client); |
| 116 |
CFRelease(name); |
| 117 |
|
| 118 |
if (status != noErr) { |
| 119 |
WriteMacOSError("JackCoreMidiDriver::Open", "MIDIClientCreate", |
| 120 |
status); |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
char *client_name = fClientControl.fName; |
| 125 |
|
| 126 |
// Allocate and connect physical inputs |
| 127 |
potential_pi_count = MIDIGetNumberOfSources(); |
| 128 |
if (potential_pi_count) { |
| 129 |
status = MIDIInputPortCreate(client, CFSTR("Physical Input Port"), |
| 130 |
HandleInputEvent, this, &internal_input); |
| 131 |
if (status != noErr) { |
| 132 |
WriteMacOSError("JackCoreMidiDriver::Open", "MIDIInputPortCreate", |
| 133 |
status); |
| 134 |
goto destroy; |
| 135 |
} |
| 136 |
|
| 137 |
try { |
| 138 |
physical_input_ports = |
| 139 |
new JackCoreMidiPhysicalInputPort*[potential_pi_count]; |
| 140 |
} catch (std::exception& e) { |
| 141 |
jack_error("JackCoreMidiDriver::Open - while creating physical " |
| 142 |
"input port array: %s", e.what()); |
| 143 |
goto destroy; |
| 144 |
} |
| 145 |
|
| 146 |
for (ItemCount i = 0; i < potential_pi_count; i++) { |
| 147 |
try { |
| 148 |
physical_input_ports[pi_count] = |
| 149 |
new JackCoreMidiPhysicalInputPort(fAliasName, client_name, |
| 150 |
capture_driver_name, i, |
| 151 |
client, internal_input, |
| 152 |
time_ratio); |
| 153 |
} catch (std::exception& e) { |
| 154 |
jack_error("JackCoreMidiDriver::Open - while creating " |
| 155 |
"physical input port: %s", e.what()); |
| 156 |
goto destroy; |
| 157 |
} |
| 158 |
pi_count++; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// Allocate and connect physical outputs |
| 163 |
potential_po_count = MIDIGetNumberOfDestinations(); |
| 164 |
if (potential_po_count) { |
| 165 |
status = MIDIOutputPortCreate(client, CFSTR("Physical Output Port"), |
| 166 |
&internal_output); |
| 167 |
if (status != noErr) { |
| 168 |
WriteMacOSError("JackCoreMidiDriver::Open", "MIDIOutputPortCreate", |
| 169 |
status); |
| 170 |
goto destroy; |
| 171 |
} |
| 172 |
|
| 173 |
try { |
| 174 |
physical_output_ports = |
| 175 |
new JackCoreMidiPhysicalOutputPort*[potential_po_count]; |
| 176 |
} catch (std::exception& e) { |
| 177 |
jack_error("JackCoreMidiDriver::Open - while creating physical " |
| 178 |
"output port array: %s", e.what()); |
| 179 |
goto destroy; |
| 180 |
} |
| 181 |
|
| 182 |
for (ItemCount i = 0; i < potential_po_count; i++) { |
| 183 |
try { |
| 184 |
physical_output_ports[po_count] = |
| 185 |
new JackCoreMidiPhysicalOutputPort(fAliasName, client_name, |
| 186 |
playback_driver_name, i, |
| 187 |
client, internal_output, |
| 188 |
time_ratio); |
| 189 |
} catch (std::exception& e) { |
| 190 |
jack_error("JackCoreMidiDriver::Open - while creating " |
| 191 |
"physical output port: %s", e.what()); |
| 192 |
goto destroy; |
| 193 |
} |
| 194 |
po_count++; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// Allocate and connect virtual inputs |
| 199 |
if (in_channels) { |
| 200 |
try { |
| 201 |
virtual_input_ports = |
| 202 |
new JackCoreMidiVirtualInputPort*[in_channels]; |
| 203 |
} catch (std::exception& e) { |
| 204 |
jack_error("JackCoreMidiDriver::Open - while creating virtual " |
| 205 |
"input port array: %s", e.what()); |
| 206 |
goto destroy; |
| 207 |
|
| 208 |
} |
| 209 |
for (vi_count = 0; vi_count < in_channels; vi_count++) { |
| 210 |
try { |
| 211 |
virtual_input_ports[vi_count] = |
| 212 |
new JackCoreMidiVirtualInputPort(fAliasName, client_name, |
| 213 |
capture_driver_name, |
| 214 |
vi_count, vi_count + pi_count, client, |
| 215 |
time_ratio); |
| 216 |
} catch (std::exception& e) { |
| 217 |
jack_error("JackCoreMidiDriver::Open - while creating virtual " |
| 218 |
"input port: %s", e.what()); |
| 219 |
goto destroy; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// Allocate and connect virtual outputs |
| 225 |
if (out_channels) { |
| 226 |
try { |
| 227 |
virtual_output_ports = |
| 228 |
new JackCoreMidiVirtualOutputPort*[out_channels]; |
| 229 |
} catch (std::exception& e) { |
| 230 |
jack_error("JackCoreMidiDriver::Open - while creating virtual " |
| 231 |
"output port array: %s", e.what()); |
| 232 |
goto destroy; |
| 233 |
} |
| 234 |
for (vo_count = 0; vo_count < out_channels; vo_count++) { |
| 235 |
try { |
| 236 |
virtual_output_ports[vo_count] = |
| 237 |
new JackCoreMidiVirtualOutputPort(fAliasName, client_name, |
| 238 |
playback_driver_name, |
| 239 |
vo_count, vo_count + po_count, client, |
| 240 |
time_ratio); |
| 241 |
} catch (std::exception& e) { |
| 242 |
jack_error("JackCoreMidiDriver::Open - while creating virtual " |
| 243 |
"output port: %s", e.what()); |
| 244 |
goto destroy; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (! (pi_count || po_count || in_channels || out_channels)) { |
| 250 |
jack_error("JackCoreMidiDriver::Open - no CoreMIDI inputs or outputs " |
| 251 |
"found, and no virtual ports allocated."); |
| 252 |
} |
| 253 |
|
| 254 |
if (! JackMidiDriver::Open(capturing, playing, |
| 255 |
in_channels + pi_count, |
| 256 |
out_channels + po_count, monitor, |
| 257 |
capture_driver_name, |
| 258 |
playback_driver_name, capture_latency, |
| 259 |
playback_latency)) { |
| 260 |
num_physical_inputs = pi_count; |
| 261 |
num_physical_outputs = po_count; |
| 262 |
num_virtual_inputs = in_channels; |
| 263 |
num_virtual_outputs = out_channels; |
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
destroy: |
| 268 |
|
| 269 |
if (physical_input_ports) { |
| 270 |
for (int i = 0; i < pi_count; i++) { |
| 271 |
delete physical_input_ports[i]; |
| 272 |
} |
| 273 |
delete[] physical_input_ports; |
| 274 |
physical_input_ports = 0; |
| 275 |
} |
| 276 |
|
| 277 |
if (physical_output_ports) { |
| 278 |
for (int i = 0; i < po_count; i++) { |
| 279 |
delete physical_output_ports[i]; |
| 280 |
} |
| 281 |
delete[] physical_output_ports; |
| 282 |
physical_output_ports = 0; |
| 283 |
} |
| 284 |
|
| 285 |
if (virtual_input_ports) { |
| 286 |
for (int i = 0; i < vi_count; i++) { |
| 287 |
delete virtual_input_ports[i]; |
| 288 |
} |
| 289 |
delete[] virtual_input_ports; |
| 290 |
virtual_input_ports = 0; |
| 291 |
} |
| 292 |
|
| 293 |
if (virtual_output_ports) { |
| 294 |
for (int i = 0; i < vo_count; i++) { |
| 295 |
delete virtual_output_ports[i]; |
| 296 |
} |
| 297 |
delete[] virtual_output_ports; |
| 298 |
virtual_output_ports = 0; |
| 299 |
} |
| 300 |
|
| 301 |
if (internal_output) { |
| 302 |
status = MIDIPortDispose(internal_output); |
| 303 |
if (status != noErr) { |
| 304 |
WriteMacOSError("JackCoreMidiDriver::Open", "MIDIPortDispose", status); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if (internal_input) { |
| 309 |
status = MIDIPortDispose(internal_input); |
| 310 |
if (status != noErr) { |
| 311 |
WriteMacOSError("JackCoreMidiDriver::Open", "MIDIPortDispose", status); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
if (client) { |
| 316 |
status = MIDIClientDispose(client); |
| 317 |
if (status != noErr) { |
| 318 |
WriteMacOSError("JackCoreMidiDriver::Open", "MIDIClientDispose", |
| 319 |
status); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// Default open |
| 324 |
if (! JackMidiDriver::Open(capturing, playing, |
| 325 |
in_channels + pi_count, |
| 326 |
out_channels + po_count, monitor, |
| 327 |
capture_driver_name, |
| 328 |
playback_driver_name, capture_latency, |
| 329 |
playback_latency)) { |
| 330 |
client = 0; |
| 331 |
num_physical_inputs = 0; |
| 332 |
num_physical_outputs = 0; |
| 333 |
num_virtual_inputs = 0; |
| 334 |
num_virtual_outputs = 0; |
| 335 |
return true; |
| 336 |
} else { |
| 337 |
return false; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
bool JackCoreMidiDriver::Execute() |
| 342 |
{ |
| 343 |
CFRunLoopRun(); |
| 344 |
return false; |
| 345 |
} |
| 346 |
|
| 347 |
int |
| 348 |
JackCoreMidiDriver::Attach() |
| 349 |
{ |
| 350 |
jack_nframes_t buffer_size = fEngineControl->fBufferSize; |
| 351 |
jack_port_id_t index; |
| 352 |
jack_nframes_t latency = buffer_size; |
| 353 |
jack_latency_range_t latency_range; |
| 354 |
const char *name; |
| 355 |
JackPort *port; |
| 356 |
JackCoreMidiPort *port_obj; |
| 357 |
latency_range.max = latency; |
| 358 |
latency_range.min = latency; |
| 359 |
|
| 360 |
// Physical inputs |
| 361 |
for (int i = 0; i < num_physical_inputs; i++) { |
| 362 |
port_obj = physical_input_ports[i]; |
| 363 |
name = port_obj->GetName(); |
| 364 |
if (fEngine->PortRegister(fClientControl.fRefNum, name, |
| 365 |
JACK_DEFAULT_MIDI_TYPE, |
| 366 |
CaptureDriverFlags, buffer_size, &index) < 0) { |
| 367 |
jack_error("JackCoreMidiDriver::Attach - cannot register physical " |
| 368 |
"input port with name '%s'.", name); |
| 369 |
// X: Do we need to deallocate ports? |
| 370 |
return -1; |
| 371 |
} |
| 372 |
port = fGraphManager->GetPort(index); |
| 373 |
port->SetAlias(port_obj->GetAlias()); |
| 374 |
port->SetLatencyRange(JackCaptureLatency, &latency_range); |
| 375 |
fEngine->PortSetDefaultMetadata(fClientControl.fRefNum, index, |
| 376 |
port_obj->GetDeviceName()); |
| 377 |
fCapturePortList[i] = index; |
| 378 |
} |
| 379 |
|
| 380 |
// Virtual inputs |
| 381 |
for (int i = 0; i < num_virtual_inputs; i++) { |
| 382 |
port_obj = virtual_input_ports[i]; |
| 383 |
name = port_obj->GetName(); |
| 384 |
if (fEngine->PortRegister(fClientControl.fRefNum, name, |
| 385 |
JACK_DEFAULT_MIDI_TYPE, |
| 386 |
CaptureDriverFlags, buffer_size, &index) < 0) { |
| 387 |
jack_error("JackCoreMidiDriver::Attach - cannot register virtual " |
| 388 |
"input port with name '%s'.", name); |
| 389 |
// X: Do we need to deallocate ports? |
| 390 |
return -1; |
| 391 |
} |
| 392 |
port = fGraphManager->GetPort(index); |
| 393 |
port->SetAlias(port_obj->GetAlias()); |
| 394 |
port->SetLatencyRange(JackCaptureLatency, &latency_range); |
| 395 |
fEngine->PortSetDefaultMetadata(fClientControl.fRefNum, index, |
| 396 |
port_obj->GetDeviceName()); |
| 397 |
fCapturePortList[num_physical_inputs + i] = index; |
| 398 |
} |
| 399 |
|
| 400 |
if (! fEngineControl->fSyncMode) { |
| 401 |
latency += buffer_size; |
| 402 |
latency_range.max = latency; |
| 403 |
latency_range.min = latency; |
| 404 |
} |
| 405 |
|
| 406 |
// Physical outputs |
| 407 |
for (int i = 0; i < num_physical_outputs; i++) { |
| 408 |
port_obj = physical_output_ports[i]; |
| 409 |
name = port_obj->GetName(); |
| 410 |
fEngine->PortRegister(fClientControl.fRefNum, name, |
| 411 |
JACK_DEFAULT_MIDI_TYPE, |
| 412 |
PlaybackDriverFlags, buffer_size, &index); |
| 413 |
if (index == NO_PORT) { |
| 414 |
jack_error("JackCoreMidiDriver::Attach - cannot register physical " |
| 415 |
"output port with name '%s'.", name); |
| 416 |
// X: Do we need to deallocate ports? |
| 417 |
return -1; |
| 418 |
} |
| 419 |
port = fGraphManager->GetPort(index); |
| 420 |
port->SetAlias(port_obj->GetAlias()); |
| 421 |
port->SetLatencyRange(JackPlaybackLatency, &latency_range); |
| 422 |
fEngine->PortSetDefaultMetadata(fClientControl.fRefNum, index, |
| 423 |
port_obj->GetDeviceName()); |
| 424 |
fPlaybackPortList[i] = index; |
| 425 |
} |
| 426 |
|
| 427 |
// Virtual outputs |
| 428 |
for (int i = 0; i < num_virtual_outputs; i++) { |
| 429 |
port_obj = virtual_output_ports[i]; |
| 430 |
name = port_obj->GetName(); |
| 431 |
fEngine->PortRegister(fClientControl.fRefNum, name, |
| 432 |
JACK_DEFAULT_MIDI_TYPE, |
| 433 |
PlaybackDriverFlags, buffer_size, &index); |
| 434 |
if (index == NO_PORT) { |
| 435 |
jack_error("JackCoreMidiDriver::Attach - cannot register virtual " |
| 436 |
"output port with name '%s'.", name); |
| 437 |
// X: Do we need to deallocate ports? |
| 438 |
return -1; |
| 439 |
} |
| 440 |
port = fGraphManager->GetPort(index); |
| 441 |
port->SetAlias(port_obj->GetAlias()); |
| 442 |
port->SetLatencyRange(JackPlaybackLatency, &latency_range); |
| 443 |
fEngine->PortSetDefaultMetadata(fClientControl.fRefNum, index, |
| 444 |
port_obj->GetDeviceName()); |
| 445 |
fPlaybackPortList[num_physical_outputs + i] = index; |
| 446 |
} |
| 447 |
|
| 448 |
return 0; |
| 449 |
} |
| 450 |
|
| 451 |
int |
| 452 |
JackCoreMidiDriver::Close() |
| 453 |
{ |
| 454 |
fThread.Kill(); |
| 455 |
return CloseAux(); |
| 456 |
} |
| 457 |
|
| 458 |
int |
| 459 |
JackCoreMidiDriver::CloseAux() |
| 460 |
{ |
| 461 |
// Generic MIDI driver close |
| 462 |
int result = JackMidiDriver::Close(); |
| 463 |
|
| 464 |
OSStatus status; |
| 465 |
if (physical_input_ports) { |
| 466 |
for (int i = 0; i < num_physical_inputs; i++) { |
| 467 |
delete physical_input_ports[i]; |
| 468 |
} |
| 469 |
delete[] physical_input_ports; |
| 470 |
num_physical_inputs = 0; |
| 471 |
physical_input_ports = 0; |
| 472 |
if (internal_input) { |
| 473 |
status = MIDIPortDispose(internal_input); |
| 474 |
if (status != noErr) { |
| 475 |
WriteMacOSError("JackCoreMidiDriver::Close", "MIDIPortDispose", |
| 476 |
status); |
| 477 |
result = -1; |
| 478 |
} |
| 479 |
internal_input = 0; |
| 480 |
} |
| 481 |
} |
| 482 |
if (physical_output_ports) { |
| 483 |
for (int i = 0; i < num_physical_outputs; i++) { |
| 484 |
delete physical_output_ports[i]; |
| 485 |
} |
| 486 |
delete[] physical_output_ports; |
| 487 |
num_physical_outputs = 0; |
| 488 |
physical_output_ports = 0; |
| 489 |
if (internal_output) { |
| 490 |
status = MIDIPortDispose(internal_output); |
| 491 |
if (status != noErr) { |
| 492 |
WriteMacOSError("JackCoreMidiDriver::Close", "MIDIPortDispose", |
| 493 |
status); |
| 494 |
result = -1; |
| 495 |
} |
| 496 |
internal_output = 0; |
| 497 |
} |
| 498 |
} |
| 499 |
if (virtual_input_ports) { |
| 500 |
for (int i = 0; i < num_virtual_inputs; i++) { |
| 501 |
delete virtual_input_ports[i]; |
| 502 |
} |
| 503 |
delete[] virtual_input_ports; |
| 504 |
num_virtual_inputs = 0; |
| 505 |
virtual_input_ports = 0; |
| 506 |
} |
| 507 |
if (virtual_output_ports) { |
| 508 |
for (int i = 0; i < num_virtual_outputs; i++) { |
| 509 |
delete virtual_output_ports[i]; |
| 510 |
} |
| 511 |
delete[] virtual_output_ports; |
| 512 |
num_virtual_outputs = 0; |
| 513 |
virtual_output_ports = 0; |
| 514 |
} |
| 515 |
|
| 516 |
if (client) { |
| 517 |
status = MIDIClientDispose(client); |
| 518 |
if (status != noErr) { |
| 519 |
WriteMacOSError("JackCoreMidiDriver::Close", "MIDIClientDispose", |
| 520 |
status); |
| 521 |
result = -1; |
| 522 |
} |
| 523 |
client = 0; |
| 524 |
} |
| 525 |
return result; |
| 526 |
} |
| 527 |
|
| 528 |
void |
| 529 |
JackCoreMidiDriver::Restart() |
| 530 |
{ |
| 531 |
// Lock between this thread and RT thread |
| 532 |
JackLock lock(this); |
| 533 |
|
| 534 |
// Lock between this thread and request thread |
| 535 |
if (fEngine->Lock()) { |
| 536 |
// Use first alias |
| 537 |
SaveConnections(1); |
| 538 |
Stop(); |
| 539 |
Detach(); |
| 540 |
CloseAux(); |
| 541 |
OpenAux(); |
| 542 |
Attach(); |
| 543 |
Start(); |
| 544 |
// Use first alias and partial port naming |
| 545 |
LoadConnections(1, false); |
| 546 |
fEngine->Unlock(); |
| 547 |
} else { |
| 548 |
jack_error("Cannot acquire engine lock..."); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
void |
| 553 |
JackCoreMidiDriver::HandleNotification(const MIDINotification *message) |
| 554 |
{ |
| 555 |
switch (message->messageID) { |
| 556 |
|
| 557 |
case kMIDIMsgObjectAdded: { |
| 558 |
/* |
| 559 |
We don't want to restart when our internal virtual in/out are created. |
| 560 |
*/ |
| 561 |
const MIDIObjectAddRemoveNotification* add_message = reinterpret_cast<const MIDIObjectAddRemoveNotification*>(message); |
| 562 |
if (!JackCoreMidiPort::IsInternalPort(add_message->child)) { |
| 563 |
Restart(); |
| 564 |
} |
| 565 |
break; |
| 566 |
} |
| 567 |
|
| 568 |
case kMIDIMsgObjectRemoved: { |
| 569 |
/* |
| 570 |
We don't want to restart when our internal virtual in/out are created. |
| 571 |
*/ |
| 572 |
const MIDIObjectAddRemoveNotification* remove_message = reinterpret_cast<const MIDIObjectAddRemoveNotification*>(message); |
| 573 |
if (!JackCoreMidiPort::IsInternalPort(remove_message->child)) { |
| 574 |
Restart(); |
| 575 |
} |
| 576 |
break; |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
int |
| 582 |
JackCoreMidiDriver::Open(bool capturing_aux, bool playing_aux, int in_channels_aux, |
| 583 |
int out_channels_aux, bool monitor_aux, |
| 584 |
const char* capture_driver_name_aux, |
| 585 |
const char* playback_driver_name_aux, |
| 586 |
jack_nframes_t capture_latency_aux, |
| 587 |
jack_nframes_t playback_latency_aux) |
| 588 |
{ |
| 589 |
|
| 590 |
strcpy(capture_driver_name, capture_driver_name_aux); |
| 591 |
strcpy(playback_driver_name, playback_driver_name_aux); |
| 592 |
|
| 593 |
capturing = capturing_aux; |
| 594 |
playing = playing_aux; |
| 595 |
in_channels = in_channels_aux; |
| 596 |
out_channels = out_channels_aux; |
| 597 |
monitor = monitor_aux; |
| 598 |
capture_latency = capture_latency_aux; |
| 599 |
playback_latency = playback_latency_aux; |
| 600 |
|
| 601 |
fThread.StartSync(); |
| 602 |
|
| 603 |
int count = 0; |
| 604 |
while (fThread.GetStatus() != JackThread::kRunning && ++count < WAIT_COUNTER) { |
| 605 |
JackSleep(100000); |
| 606 |
jack_log("JackCoreMidiDriver::Open wait count = %d", count); |
| 607 |
|
| 608 |
} |
| 609 |
if (count == WAIT_COUNTER) { |
| 610 |
jack_info("Cannot open CoreMIDI driver"); |
| 611 |
fThread.Kill(); |
| 612 |
return -1; |
| 613 |
} else { |
| 614 |
JackSleep(10000); |
| 615 |
jack_info("CoreMIDI driver is opened..."); |
| 616 |
} |
| 617 |
|
| 618 |
return 0; |
| 619 |
} |
| 620 |
|
| 621 |
int |
| 622 |
JackCoreMidiDriver::Start() |
| 623 |
{ |
| 624 |
jack_info("JackCoreMidiDriver::Start - Starting driver."); |
| 625 |
|
| 626 |
JackMidiDriver::Start(); |
| 627 |
|
| 628 |
int pi_count = 0; |
| 629 |
int po_count = 0; |
| 630 |
int vi_count = 0; |
| 631 |
int vo_count = 0; |
| 632 |
|
| 633 |
jack_info("JackCoreMidiDriver::Start - Enabling physical input ports."); |
| 634 |
|
| 635 |
for (; pi_count < num_physical_inputs; pi_count++) { |
| 636 |
if (!physical_input_ports[pi_count]->Start()) { |
| 637 |
jack_error("JackCoreMidiDriver::Start - Failed to enable physical " |
| 638 |
"input port."); |
| 639 |
goto stop_physical_input_ports; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
jack_info("JackCoreMidiDriver::Start - Enabling physical output ports."); |
| 644 |
|
| 645 |
for (; po_count < num_physical_outputs; po_count++) { |
| 646 |
if (!physical_output_ports[po_count]->Start()) { |
| 647 |
jack_error("JackCoreMidiDriver::Start - Failed to enable physical " |
| 648 |
"output port."); |
| 649 |
goto stop_physical_output_ports; |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
jack_info("JackCoreMidiDriver::Start - Enabling virtual input ports."); |
| 654 |
|
| 655 |
for (; vi_count < num_virtual_inputs; vi_count++) { |
| 656 |
if (!virtual_input_ports[vi_count]->Start()) { |
| 657 |
jack_error("JackCoreMidiDriver::Start - Failed to enable virtual " |
| 658 |
"input port."); |
| 659 |
goto stop_virtual_input_ports; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
jack_info("JackCoreMidiDriver::Start - Enabling virtual output ports."); |
| 664 |
|
| 665 |
for (; vo_count < num_virtual_outputs; vo_count++) { |
| 666 |
if (!virtual_output_ports[vo_count]->Start()) { |
| 667 |
jack_error("JackCoreMidiDriver::Start - Failed to enable virtual " |
| 668 |
"output port."); |
| 669 |
goto stop_virtual_output_ports; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
jack_info("JackCoreMidiDriver::Start - Driver started."); |
| 674 |
|
| 675 |
return 0; |
| 676 |
|
| 677 |
stop_virtual_output_ports: |
| 678 |
for (int i = 0; i < vo_count; i++) { |
| 679 |
if (!virtual_output_ports[i]->Stop()) { |
| 680 |
jack_error("JackCoreMidiDriver::Start - Failed to disable virtual " |
| 681 |
"output port."); |
| 682 |
} |
| 683 |
} |
| 684 |
stop_virtual_input_ports: |
| 685 |
for (int i = 0; i < vi_count; i++) { |
| 686 |
if (!virtual_input_ports[i]->Stop()) { |
| 687 |
jack_error("JackCoreMidiDriver::Start - Failed to disable virtual " |
| 688 |
"input port."); |
| 689 |
} |
| 690 |
} |
| 691 |
stop_physical_output_ports: |
| 692 |
for (int i = 0; i < po_count; i++) { |
| 693 |
if (!physical_output_ports[i]->Stop()) { |
| 694 |
jack_error("JackCoreMidiDriver::Start - Failed to disable " |
| 695 |
"physical output port."); |
| 696 |
} |
| 697 |
} |
| 698 |
stop_physical_input_ports: |
| 699 |
for (int i = 0; i < pi_count; i++) { |
| 700 |
if (!physical_input_ports[i]->Stop()) { |
| 701 |
jack_error("JackCoreMidiDriver::Start - Failed to disable " |
| 702 |
"physical input port."); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
return -1; |
| 707 |
} |
| 708 |
|
| 709 |
int |
| 710 |
JackCoreMidiDriver::Stop() |
| 711 |
{ |
| 712 |
int result = 0; |
| 713 |
|
| 714 |
JackMidiDriver::Stop(); |
| 715 |
|
| 716 |
jack_info("JackCoreMidiDriver::Stop - disabling physical input ports."); |
| 717 |
|
| 718 |
for (int i = 0; i < num_physical_inputs; i++) { |
| 719 |
if (!physical_input_ports[i]->Stop()) { |
| 720 |
jack_error("JackCoreMidiDriver::Stop - Failed to disable physical " |
| 721 |
"input port."); |
| 722 |
result = -1; |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
jack_info("JackCoreMidiDriver::Stop - disabling physical output ports."); |
| 727 |
|
| 728 |
for (int i = 0; i < num_physical_outputs; i++) { |
| 729 |
if (!physical_output_ports[i]->Stop()) { |
| 730 |
jack_error("JackCoreMidiDriver::Stop - Failed to disable physical " |
| 731 |
"output port."); |
| 732 |
result = -1; |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
jack_info("JackCoreMidiDriver::Stop - disabling virtual input ports."); |
| 737 |
|
| 738 |
for (int i = 0; i < num_virtual_inputs; i++) { |
| 739 |
if (!virtual_input_ports[i]->Stop()) { |
| 740 |
jack_error("JackCoreMidiDriver::Stop - Failed to disable virtual " |
| 741 |
"input port."); |
| 742 |
result = -1; |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
jack_info("JackCoreMidiDriver::Stop - disabling virtual output ports."); |
| 747 |
|
| 748 |
for (int i = 0; i < num_virtual_outputs; i++) { |
| 749 |
if (!virtual_output_ports[i]->Stop()) { |
| 750 |
jack_error("JackCoreMidiDriver::Stop - Failed to disable virtual " |
| 751 |
"output port."); |
| 752 |
result = -1; |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
return result; |
| 757 |
} |
| 758 |
|
| 759 |
int |
| 760 |
JackCoreMidiDriver::ProcessRead() |
| 761 |
{ |
| 762 |
int res; |
| 763 |
if (Trylock()) { |
| 764 |
res = (fEngineControl->fSyncMode) ? ProcessReadSync() : ProcessReadAsync(); |
| 765 |
Unlock(); |
| 766 |
} else { |
| 767 |
res = -1; |
| 768 |
} |
| 769 |
return res; |
| 770 |
} |
| 771 |
|
| 772 |
int |
| 773 |
JackCoreMidiDriver::ProcessWrite() |
| 774 |
{ |
| 775 |
int res; |
| 776 |
if (Trylock()) { |
| 777 |
res = (fEngineControl->fSyncMode) ? ProcessWriteSync() : ProcessWriteAsync(); |
| 778 |
Unlock(); |
| 779 |
} else { |
| 780 |
res = -1; |
| 781 |
} |
| 782 |
return res; |
| 783 |
} |
| 784 |
|
| 785 |
int |
| 786 |
JackCoreMidiDriver::Read() |
| 787 |
{ |
| 788 |
jack_nframes_t buffer_size = fEngineControl->fBufferSize; |
| 789 |
for (int i = 0; i < num_physical_inputs; i++) { |
| 790 |
physical_input_ports[i]->ProcessJack(GetInputBuffer(i), buffer_size); |
| 791 |
} |
| 792 |
for (int i = 0; i < num_virtual_inputs; i++) { |
| 793 |
virtual_input_ports[i]-> |
| 794 |
ProcessJack(GetInputBuffer(num_physical_inputs + i), buffer_size); |
| 795 |
} |
| 796 |
return 0; |
| 797 |
} |
| 798 |
|
| 799 |
int |
| 800 |
JackCoreMidiDriver::Write() |
| 801 |
{ |
| 802 |
jack_nframes_t buffer_size = fEngineControl->fBufferSize; |
| 803 |
for (int i = 0; i < num_physical_outputs; i++) { |
| 804 |
physical_output_ports[i]->ProcessJack(GetOutputBuffer(i), buffer_size); |
| 805 |
} |
| 806 |
for (int i = 0; i < num_virtual_outputs; i++) { |
| 807 |
virtual_output_ports[i]-> |
| 808 |
ProcessJack(GetOutputBuffer(num_physical_outputs + i), buffer_size); |
| 809 |
} |
| 810 |
return 0; |
| 811 |
} |
| 812 |
|
| 813 |
#ifdef __cplusplus |
| 814 |
extern "C" { |
| 815 |
#endif |
| 816 |
|
| 817 |
// singleton kind of driver |
| 818 |
static Jack::JackCoreMidiDriver* driver = NULL; |
| 819 |
|
| 820 |
SERVER_EXPORT jack_driver_desc_t * driver_get_descriptor() |
| 821 |
{ |
| 822 |
jack_driver_desc_t * desc; |
| 823 |
jack_driver_desc_filler_t filler; |
| 824 |
jack_driver_param_value_t value; |
| 825 |
|
| 826 |
desc = jack_driver_descriptor_construct("coremidi", JackDriverSlave, "Apple CoreMIDI API based MIDI backend", &filler); |
| 827 |
|
| 828 |
value.ui = 0; |
| 829 |
jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "CoreMIDI virtual bus", NULL); |
| 830 |
jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "CoreMIDI virtual bus", NULL); |
| 831 |
|
| 832 |
return desc; |
| 833 |
} |
| 834 |
|
| 835 |
SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params) |
| 836 |
{ |
| 837 |
const JSList * node; |
| 838 |
const jack_driver_param_t * param; |
| 839 |
int virtual_in = 2; |
| 840 |
int virtual_out = 2; |
| 841 |
|
| 842 |
for (node = params; node; node = jack_slist_next (node)) { |
| 843 |
param = (const jack_driver_param_t *) node->data; |
| 844 |
|
| 845 |
switch (param->character) { |
| 846 |
|
| 847 |
case 'i': |
| 848 |
virtual_in = param->value.ui; |
| 849 |
break; |
| 850 |
|
| 851 |
case 'o': |
| 852 |
virtual_out = param->value.ui; |
| 853 |
break; |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
// singleton kind of driver |
| 858 |
if (!driver) { |
| 859 |
driver = new Jack::JackCoreMidiDriver("system_midi", "coremidi", engine, table); |
| 860 |
if (driver->Open(1, 1, virtual_in, virtual_out, false, "in", "out", 0, 0) == 0) { |
| 861 |
return driver; |
| 862 |
} else { |
| 863 |
delete driver; |
| 864 |
return NULL; |
| 865 |
} |
| 866 |
} else { |
| 867 |
jack_info("JackCoreMidiDriver already allocated, cannot be loaded twice"); |
| 868 |
return NULL; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
#ifdef __cplusplus |
| 873 |
} |
| 874 |
#endif |