-
Notifications
You must be signed in to change notification settings - Fork 376
/
JackALSARawMidiDriver.cpp
667 lines (614 loc) · 22.4 KB
/
JackALSARawMidiDriver.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
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 <memory>
#include <new>
#include <stdexcept>
#include <alsa/asoundlib.h>
#include "JackALSARawMidiDriver.h"
#include "JackALSARawMidiUtil.h"
#include "JackEngineControl.h"
#include "JackError.h"
#include "JackMidiUtil.h"
#include "driver_interface.h"
using Jack::JackALSARawMidiDriver;
JackALSARawMidiDriver::JackALSARawMidiDriver(const char *name,
const char *alias,
JackLockedEngine *engine,
JackSynchro *table):
JackMidiDriver(name, alias, engine, table)
{
thread = new JackThread(this);
fds[0] = -1;
fds[1] = -1;
input_ports = 0;
output_ports = 0;
output_port_timeouts = 0;
poll_fds = 0;
}
JackALSARawMidiDriver::~JackALSARawMidiDriver()
{
delete thread;
}
int
JackALSARawMidiDriver::Attach()
{
const char *alias;
jack_nframes_t buffer_size = fEngineControl->fBufferSize;
jack_port_id_t index;
jack_nframes_t latency = buffer_size;
jack_latency_range_t latency_range;
const char *name;
JackPort *port;
latency_range.max = latency;
latency_range.min = latency;
for (int i = 0; i < fCaptureChannels; i++) {
JackALSARawMidiInputPort *input_port = input_ports[i];
name = input_port->GetName();
fEngine->PortRegister(fClientControl.fRefNum, name,
JACK_DEFAULT_MIDI_TYPE,
CaptureDriverFlags, buffer_size, &index);
if (index == NO_PORT) {
jack_error("JackALSARawMidiDriver::Attach - cannot register input "
"port with name '%s'.", name);
// XX: Do we need to deallocate ports?
return -1;
}
alias = input_port->GetAlias();
port = fGraphManager->GetPort(index);
port->SetAlias(alias);
port->SetLatencyRange(JackCaptureLatency, &latency_range);
fEngine->PortSetDefaultMetadata(fClientControl.fRefNum, index,
input_port->GetDeviceName());
fCapturePortList[i] = index;
jack_info("JackALSARawMidiDriver::Attach - input port registered "
"(name='%s', alias='%s').", name, alias);
}
if (! fEngineControl->fSyncMode) {
latency += buffer_size;
latency_range.max = latency;
latency_range.min = latency;
}
for (int i = 0; i < fPlaybackChannels; i++) {
JackALSARawMidiOutputPort *output_port = output_ports[i];
name = output_port->GetName();
fEngine->PortRegister(fClientControl.fRefNum, name,
JACK_DEFAULT_MIDI_TYPE,
PlaybackDriverFlags, buffer_size, &index);
if (index == NO_PORT) {
jack_error("JackALSARawMidiDriver::Attach - cannot register "
"output port with name '%s'.", name);
// XX: Do we need to deallocate ports?
return -1;
}
alias = output_port->GetAlias();
port = fGraphManager->GetPort(index);
port->SetAlias(alias);
port->SetLatencyRange(JackPlaybackLatency, &latency_range);
fEngine->PortSetDefaultMetadata(fClientControl.fRefNum, index,
output_port->GetDeviceName());
fPlaybackPortList[i] = index;
jack_info("JackALSARawMidiDriver::Attach - output port registered "
"(name='%s', alias='%s').", name, alias);
}
return 0;
}
int
JackALSARawMidiDriver::Close()
{
// Generic MIDI driver close
int result = JackMidiDriver::Close();
if (input_ports) {
for (int i = 0; i < fCaptureChannels; i++) {
delete input_ports[i];
}
delete[] input_ports;
input_ports = 0;
}
if (output_ports) {
for (int i = 0; i < fPlaybackChannels; i++) {
delete output_ports[i];
}
delete[] output_ports;
output_ports = 0;
}
return result;
}
bool
JackALSARawMidiDriver::Execute()
{
jack_nframes_t timeout_frame = 0;
for (;;) {
struct timespec timeout;
struct timespec *timeout_ptr;
if (! timeout_frame) {
timeout_ptr = 0;
} else {
// The timeout value is relative to the time that
// 'GetMicroSeconds()' is called, not the time that 'poll()' is
// called. This means that the amount of time that passes between
// 'GetMicroSeconds()' and 'ppoll()' is time that will be lost
// while waiting for 'poll() to timeout.
//
// I tried to replace the timeout with a 'timerfd' with absolute
// times, but, strangely, it actually slowed things down, and made
// the code a lot more complicated.
//
// I wonder about using the 'epoll' interface instead of 'ppoll()'.
// The problem with the 'epoll' interface is that the timeout
// resolution of 'epoll_wait()' is set in milliseconds. We need
// microsecond resolution. Without microsecond resolution, we
// impose the same jitter as USB MIDI.
//
// Another problem is that 'ppoll()' returns later than the wait
// time. The problem can be minimized with high precision timers.
timeout_ptr = &timeout;
jack_time_t next_time = GetTimeFromFrames(timeout_frame);
jack_time_t now = GetMicroSeconds();
if (next_time <= now) {
timeout.tv_sec = 0;
timeout.tv_nsec = 0;
} else {
jack_time_t wait_time = next_time - now;
timeout.tv_sec = wait_time / 1000000;
timeout.tv_nsec = (wait_time % 1000000) * 1000;
}
}
int poll_result = ppoll(poll_fds, poll_fd_count, timeout_ptr, 0);
// Getting the current frame value here allows us to use it for
// incoming MIDI bytes. This makes sense, as the data has already
// arrived at this point.
jack_nframes_t current_frame = GetCurrentFrame();
if (poll_result == -1) {
if (errno == EINTR) {
continue;
}
jack_error("JackALSARawMidiDriver::Execute - poll error: %s",
strerror(errno));
break;
}
jack_nframes_t port_timeout;
timeout_frame = 0;
if (! poll_result) {
// No I/O events occurred. So, only handle timeout events on
// output ports.
for (int i = 0; i < fPlaybackChannels; i++) {
port_timeout = output_port_timeouts[i];
if (port_timeout && (port_timeout <= current_frame)) {
if (! output_ports[i]->ProcessPollEvents(false, true,
&port_timeout)) {
jack_error("JackALSARawMidiDriver::Execute - a fatal "
"error occurred while processing ALSA "
"output events.");
goto cleanup;
}
output_port_timeouts[i] = port_timeout;
}
if (port_timeout && ((! timeout_frame) ||
(port_timeout < timeout_frame))) {
timeout_frame = port_timeout;
}
}
continue;
}
// See if it's time to shutdown.
unsigned short revents = poll_fds[0].revents;
if (revents) {
if (revents & (~ POLLHUP)) {
jack_error("JackALSARawMidiDriver::Execute - unexpected poll "
"event on pipe file descriptor.");
}
break;
}
// Handle I/O events *and* timeout events on output ports.
for (int i = 0; i < fPlaybackChannels; i++) {
port_timeout = output_port_timeouts[i];
bool timeout = port_timeout && (port_timeout <= current_frame);
if (! output_ports[i]->ProcessPollEvents(true, timeout,
&port_timeout)) {
jack_error("JackALSARawMidiDriver::Execute - a fatal error "
"occurred while processing ALSA output events.");
goto cleanup;
}
output_port_timeouts[i] = port_timeout;
if (port_timeout && ((! timeout_frame) ||
(port_timeout < timeout_frame))) {
timeout_frame = port_timeout;
}
}
// Handle I/O events on input ports. We handle these last because we
// already computed the arrival time above, and will impose a delay on
// the events by 'period-size' frames anyway, which gives us a bit of
// borrowed time.
for (int i = 0; i < fCaptureChannels; i++) {
if (! input_ports[i]->ProcessPollEvents(current_frame)) {
jack_error("JackALSARawMidiDriver::Execute - a fatal error "
"occurred while processing ALSA input events.");
goto cleanup;
}
}
}
cleanup:
close(fds[0]);
fds[0] = -1;
jack_info("JackALSARawMidiDriver::Execute - ALSA thread exiting.");
return false;
}
void
JackALSARawMidiDriver::
FreeDeviceInfo(std::vector<snd_rawmidi_info_t *> *in_info_list,
std::vector<snd_rawmidi_info_t *> *out_info_list)
{
size_t length = in_info_list->size();
for (size_t i = 0; i < length; i++) {
snd_rawmidi_info_free(in_info_list->at(i));
}
length = out_info_list->size();
for (size_t i = 0; i < length; i++) {
snd_rawmidi_info_free(out_info_list->at(i));
}
}
void
JackALSARawMidiDriver::
GetDeviceInfo(snd_ctl_t *control, snd_rawmidi_info_t *info,
std::vector<snd_rawmidi_info_t *> *info_list)
{
snd_rawmidi_info_set_subdevice(info, 0);
int code = snd_ctl_rawmidi_info(control, info);
if (code) {
if (code != -ENOENT) {
HandleALSAError("GetDeviceInfo", "snd_ctl_rawmidi_info", code);
}
return;
}
unsigned int count = snd_rawmidi_info_get_subdevices_count(info);
for (unsigned int i = 0; i < count; i++) {
snd_rawmidi_info_set_subdevice(info, i);
int code = snd_ctl_rawmidi_info(control, info);
if (code) {
HandleALSAError("GetDeviceInfo", "snd_ctl_rawmidi_info", code);
continue;
}
snd_rawmidi_info_t *info_copy;
code = snd_rawmidi_info_malloc(&info_copy);
if (code) {
HandleALSAError("GetDeviceInfo", "snd_rawmidi_info_malloc", code);
continue;
}
snd_rawmidi_info_copy(info_copy, info);
try {
info_list->push_back(info_copy);
} catch (std::bad_alloc &e) {
snd_rawmidi_info_free(info_copy);
jack_error("JackALSARawMidiDriver::GetDeviceInfo - "
"std::vector::push_back: %s", e.what());
}
}
}
void
JackALSARawMidiDriver::HandleALSAError(const char *driver_func,
const char *alsa_func, int code)
{
jack_error("JackALSARawMidiDriver::%s - %s: %s", driver_func, alsa_func,
snd_strerror(code));
}
bool
JackALSARawMidiDriver::Init()
{
set_threaded_log_function();
if (thread->AcquireSelfRealTime(fEngineControl->fServerPriority + 1)) {
jack_error("JackALSARawMidiDriver::Init - could not acquire realtime "
"scheduling. Continuing anyway.");
}
return true;
}
int
JackALSARawMidiDriver::Open(bool capturing, bool playing, int in_channels,
int out_channels, bool monitor,
const char *capture_driver_name,
const char *playback_driver_name,
jack_nframes_t capture_latency,
jack_nframes_t playback_latency)
{
snd_rawmidi_info_t *info;
int code = snd_rawmidi_info_malloc(&info);
if (code) {
HandleALSAError("Open", "snd_rawmidi_info_malloc", code);
return -1;
}
std::vector<snd_rawmidi_info_t *> in_info_list;
std::vector<snd_rawmidi_info_t *> out_info_list;
for (int card = -1;;) {
int code = snd_card_next(&card);
if (code) {
HandleALSAError("Open", "snd_card_next", code);
continue;
}
if (card == -1) {
break;
}
char name[32];
snprintf(name, sizeof(name), "hw:%d", card);
snd_ctl_t *control;
code = snd_ctl_open(&control, name, SND_CTL_NONBLOCK);
if (code) {
HandleALSAError("Open", "snd_ctl_open", code);
continue;
}
for (int device = -1;;) {
code = snd_ctl_rawmidi_next_device(control, &device);
if (code) {
HandleALSAError("Open", "snd_ctl_rawmidi_next_device", code);
continue;
}
if (device == -1) {
break;
}
snd_rawmidi_info_set_device(info, device);
snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT);
GetDeviceInfo(control, info, &in_info_list);
snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT);
GetDeviceInfo(control, info, &out_info_list);
}
snd_ctl_close(control);
}
snd_rawmidi_info_free(info);
size_t potential_inputs = in_info_list.size();
size_t potential_outputs = out_info_list.size();
if (! (potential_inputs || potential_outputs)) {
jack_error("JackALSARawMidiDriver::Open - no ALSA raw MIDI input or "
"output ports found.");
FreeDeviceInfo(&in_info_list, &out_info_list);
return -1;
}
size_t num_inputs = 0;
size_t num_outputs = 0;
const char *client_name = fClientControl.fName;
if (potential_inputs) {
try {
input_ports = new JackALSARawMidiInputPort *[potential_inputs];
} catch (std::exception& e) {
jack_error("JackALSARawMidiDriver::Open - while creating input "
"port array: %s", e.what());
FreeDeviceInfo(&in_info_list, &out_info_list);
return -1;
}
}
if (potential_outputs) {
try {
output_ports = new JackALSARawMidiOutputPort *[potential_outputs];
} catch (std::exception& e) {
jack_error("JackALSARawMidiDriver::Open - while creating output "
"port array: %s", e.what());
FreeDeviceInfo(&in_info_list, &out_info_list);
goto delete_input_ports;
}
}
for (size_t i = 0; i < potential_inputs; i++) {
snd_rawmidi_info_t *info = in_info_list.at(i);
try {
input_ports[num_inputs] = new JackALSARawMidiInputPort(client_name, info, i);
num_inputs++;
} catch (std::exception& e) {
jack_error("JackALSARawMidiDriver::Open - while creating new "
"JackALSARawMidiInputPort: %s", e.what());
}
snd_rawmidi_info_free(info);
}
for (size_t i = 0; i < potential_outputs; i++) {
snd_rawmidi_info_t *info = out_info_list.at(i);
try {
output_ports[num_outputs] = new JackALSARawMidiOutputPort(client_name, info, i);
num_outputs++;
} catch (std::exception& e) {
jack_error("JackALSARawMidiDriver::Open - while creating new "
"JackALSARawMidiOutputPort: %s", e.what());
}
snd_rawmidi_info_free(info);
}
if (! (num_inputs || num_outputs)) {
jack_error("JackALSARawMidiDriver::Open - none of the potential "
"inputs or outputs were successfully opened.");
} else if (JackMidiDriver::Open(capturing, playing, num_inputs,
num_outputs, monitor, capture_driver_name,
playback_driver_name, capture_latency,
playback_latency)) {
jack_error("JackALSARawMidiDriver::Open - JackMidiDriver::Open error");
} else {
return 0;
}
if (output_ports) {
for (size_t i = 0; i < num_outputs; i++) {
delete output_ports[i];
}
delete[] output_ports;
output_ports = 0;
}
delete_input_ports:
if (input_ports) {
for (size_t i = 0; i < num_inputs; i++) {
delete input_ports[i];
}
delete[] input_ports;
input_ports = 0;
}
return -1;
}
int
JackALSARawMidiDriver::Read()
{
jack_nframes_t buffer_size = fEngineControl->fBufferSize;
for (int i = 0; i < fCaptureChannels; i++) {
if (! input_ports[i]->ProcessJack(GetInputBuffer(i), buffer_size)) {
return -1;
}
}
return 0;
}
int
JackALSARawMidiDriver::Start()
{
jack_info("JackALSARawMidiDriver::Start - Starting 'alsarawmidi' driver.");
JackMidiDriver::Start();
poll_fd_count = 1;
for (int i = 0; i < fCaptureChannels; i++) {
poll_fd_count += input_ports[i]->GetPollDescriptorCount();
}
for (int i = 0; i < fPlaybackChannels; i++) {
poll_fd_count += output_ports[i]->GetPollDescriptorCount();
}
try {
poll_fds = new pollfd[poll_fd_count];
} catch (std::exception& e) {
jack_error("JackALSARawMidiDriver::Start - creating poll descriptor "
"structures failed: %s", e.what());
return -1;
}
if (fPlaybackChannels) {
try {
output_port_timeouts = new jack_nframes_t[fPlaybackChannels];
} catch (std::exception& e) {
jack_error("JackALSARawMidiDriver::Start - creating array for "
"output port timeout values failed: %s", e.what());
goto free_poll_descriptors;
}
}
struct pollfd *poll_fd_iter;
try {
CreateNonBlockingPipe(fds);
} catch (std::exception& e) {
jack_error("JackALSARawMidiDriver::Start - while creating wake pipe: "
"%s", e.what());
goto free_output_port_timeouts;
}
poll_fds[0].events = POLLERR | POLLIN | POLLNVAL;
poll_fds[0].fd = fds[0];
poll_fd_iter = poll_fds + 1;
for (int i = 0; i < fCaptureChannels; i++) {
JackALSARawMidiInputPort *input_port = input_ports[i];
input_port->PopulatePollDescriptors(poll_fd_iter);
poll_fd_iter += input_port->GetPollDescriptorCount();
}
for (int i = 0; i < fPlaybackChannels; i++) {
JackALSARawMidiOutputPort *output_port = output_ports[i];
output_port->PopulatePollDescriptors(poll_fd_iter);
poll_fd_iter += output_port->GetPollDescriptorCount();
output_port_timeouts[i] = 0;
}
jack_info("JackALSARawMidiDriver::Start - starting ALSA thread ...");
if (! thread->StartSync()) {
jack_info("JackALSARawMidiDriver::Start - started ALSA thread.");
return 0;
}
jack_error("JackALSARawMidiDriver::Start - failed to start MIDI "
"processing thread.");
DestroyNonBlockingPipe(fds);
fds[1] = -1;
fds[0] = -1;
free_output_port_timeouts:
delete[] output_port_timeouts;
output_port_timeouts = 0;
free_poll_descriptors:
delete[] poll_fds;
poll_fds = 0;
return -1;
}
int
JackALSARawMidiDriver::Stop()
{
jack_info("JackALSARawMidiDriver::Stop - stopping 'alsarawmidi' driver.");
JackMidiDriver::Stop();
if (fds[1] != -1) {
close(fds[1]);
fds[1] = -1;
}
int result;
const char *verb;
switch (thread->GetStatus()) {
case JackThread::kIniting:
case JackThread::kStarting:
result = thread->Kill();
verb = "kill";
break;
case JackThread::kRunning:
result = thread->Stop();
verb = "stop";
break;
default:
result = 0;
verb = 0;
}
if (fds[0] != -1) {
close(fds[0]);
fds[0] = -1;
}
if (output_port_timeouts) {
delete[] output_port_timeouts;
output_port_timeouts = 0;
}
if (poll_fds) {
delete[] poll_fds;
poll_fds = 0;
}
if (result) {
jack_error("JackALSARawMidiDriver::Stop - could not %s MIDI "
"processing thread.", verb);
}
return result;
}
int
JackALSARawMidiDriver::Write()
{
jack_nframes_t buffer_size = fEngineControl->fBufferSize;
for (int i = 0; i < fPlaybackChannels; i++) {
if (! output_ports[i]->ProcessJack(GetOutputBuffer(i), buffer_size)) {
return -1;
}
}
return 0;
}
#ifdef __cplusplus
extern "C" {
#endif
// singleton kind of driver
static Jack::JackALSARawMidiDriver* driver = NULL;
SERVER_EXPORT jack_driver_desc_t *
driver_get_descriptor()
{
// X: There could be parameters here regarding setting I/O buffer
// sizes. I don't think MIDI drivers can accept parameters right
// now without being set as the main driver.
return jack_driver_descriptor_construct("alsarawmidi", JackDriverSlave, "Alternative ALSA raw MIDI backend.", NULL);
}
SERVER_EXPORT Jack::JackDriverClientInterface *
driver_initialize(Jack::JackLockedEngine *engine, Jack::JackSynchro *table,
const JSList *params)
{
// singleton kind of driver
if (!driver) {
driver = new Jack::JackALSARawMidiDriver("system_midi", "alsarawmidi", engine, table);
if (driver->Open(1, 1, 0, 0, false, "midi in", "midi out", 0, 0) == 0) {
return driver;
} else {
delete driver;
return NULL;
}
} else {
jack_info("JackALSARawMidiDriver already allocated, cannot be loaded twice");
return NULL;
}
}
#ifdef __cplusplus
}
#endif