b3a5720aa27632e2e510f10f188658d1ab2d325d
[ardour.git] / libs / backends / alsa / alsa_rawmidi.cc
1 /*
2  * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2010 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 #include <unistd.h>
21
22 #include <glibmm.h>
23
24 #include "alsa_rawmidi.h"
25 #include "rt_thread.h"
26
27 #include "pbd/error.h"
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31
32 /* max bytes per individual midi-event
33  * events larger than this are ignored */
34 #define MaxAlsaRawEventSize (64)
35
36 #ifndef NDEBUG
37 #define _DEBUGPRINT(STR) fprintf(stderr, STR);
38 #else
39 #define _DEBUGPRINT(STR) ;
40 #endif
41
42 AlsaRawMidiIO::AlsaRawMidiIO (const char *device, const bool input)
43         : _state (-1)
44         , _running (false)
45         , _device (0)
46         , _pfds (0)
47         , _sample_length_us (1e6 / 48000.0)
48         , _period_length_us (1.024e6 / 48000.0)
49         , _samples_per_period (1024)
50         , _rb (0)
51 {
52         pthread_mutex_init (&_notify_mutex, 0);
53         pthread_cond_init (&_notify_ready, 0);
54         init (device, input);
55 }
56
57 AlsaRawMidiIO::~AlsaRawMidiIO ()
58 {
59         if (_device) {
60                 snd_rawmidi_drain (_device);
61                 snd_rawmidi_close (_device);
62                 _device = 0;
63         }
64         delete _rb;
65         pthread_mutex_destroy (&_notify_mutex);
66         pthread_cond_destroy (&_notify_ready);
67         free (_pfds);
68 }
69
70 void
71 AlsaRawMidiIO::init (const char *device_name, const bool input)
72 {
73         if (snd_rawmidi_open (
74                                 input ? &_device : NULL,
75                                 input ? NULL : &_device,
76                                 device_name, SND_RAWMIDI_NONBLOCK) < 0) {
77                 return;
78         }
79
80         _npfds = snd_rawmidi_poll_descriptors_count (_device);
81         if (_npfds < 1) {
82                 _DEBUGPRINT("AlsaRawMidiIO: no poll descriptor(s).\n");
83                 snd_rawmidi_close (_device);
84                 _device = 0;
85                 return;
86         }
87         _pfds = (struct pollfd*) malloc (_npfds * sizeof(struct pollfd));
88         snd_rawmidi_poll_descriptors (_device, _pfds, _npfds);
89
90         // MIDI (hw port) 31.25 kbaud
91         // worst case here is  8192 SPP and 8KSPS for which we'd need
92         // 4000 bytes sans MidiEventHeader.
93         // since we're not always in sync, let's use 4096.
94         _rb = new RingBuffer<uint8_t>(4096 + 4096 * sizeof(MidiEventHeader));
95
96 #if 0
97         _state = 0;
98 #else
99         snd_rawmidi_params_t *params;
100         if (snd_rawmidi_params_malloc (&params)) {
101                 goto initerr;
102         }
103         if (snd_rawmidi_params_current (_device, params)) {
104                 goto initerr;
105         }
106         if (snd_rawmidi_params_set_avail_min (_device, params, 1)) {
107                 goto initerr;
108         }
109         if ( snd_rawmidi_params_set_buffer_size (_device, params, 64)) {
110                 goto initerr;
111         }
112         if (snd_rawmidi_params_set_no_active_sensing (_device, params, 1)) {
113                 goto initerr;
114         }
115
116         _state = 0;
117         return;
118
119 initerr:
120         _DEBUGPRINT("AlsaRawMidiIO: parameter setup error\n");
121         snd_rawmidi_close (_device);
122         _device = 0;
123 #endif
124         return;
125 }
126
127 static void * pthread_process (void *arg)
128 {
129         AlsaRawMidiIO *d = static_cast<AlsaRawMidiIO *>(arg);
130         d->main_process_thread ();
131         pthread_exit (0);
132         return 0;
133 }
134
135 int
136 AlsaRawMidiIO::start ()
137 {
138         if (_realtime_pthread_create (SCHED_FIFO, -21, 100000,
139                                 &_main_thread, pthread_process, this))
140         {
141                 if (pthread_create (&_main_thread, NULL, pthread_process, this)) {
142                         PBD::error << _("AlsaRawMidiIO: Failed to create process thread.") << endmsg;
143                         return -1;
144                 } else {
145                         PBD::warning << _("AlsaRawMidiIO: Cannot acquire realtime permissions.") << endmsg;
146                 }
147         }
148         int timeout = 5000;
149         while (!_running && --timeout > 0) { Glib::usleep (1000); }
150         if (timeout == 0 || !_running) {
151                 return -1;
152         }
153         return 0;
154 }
155
156 int
157 AlsaRawMidiIO::stop ()
158 {
159         void *status;
160         if (!_running) {
161                 return 0;
162         }
163
164         _running = false;
165
166         pthread_mutex_lock (&_notify_mutex);
167         pthread_cond_signal (&_notify_ready);
168         pthread_mutex_unlock (&_notify_mutex);
169
170         if (pthread_join (_main_thread, &status)) {
171                 PBD::error << _("AlsaRawMidiIO: Failed to terminate.") << endmsg;
172                 return -1;
173         }
174         return 0;
175 }
176
177 void
178 AlsaRawMidiIO::setup_timing (const size_t samples_per_period, const float samplerate)
179 {
180         _period_length_us = (double) samples_per_period * 1e6 / samplerate;
181         _sample_length_us = 1e6 / samplerate;
182         _samples_per_period = samples_per_period;
183 }
184
185 void
186 AlsaRawMidiIO::sync_time (const uint64_t tme)
187 {
188         // TODO consider a PLL, if this turns out to be the bottleneck for jitter
189         // also think about using
190         // snd_pcm_status_get_tstamp() and snd_rawmidi_status_get_tstamp()
191         // instead of monotonic clock.
192 #ifdef DEBUG_TIMING
193         double tdiff = (_clock_monotonic + _period_length_us - tme) / 1000.0;
194         if (abs(tdiff) >= .05) {
195                 printf("AlsaRawMidiIO MJ: %.1f ms\n", tdiff);
196         }
197 #endif
198         _clock_monotonic = tme;
199 }
200
201 ///////////////////////////////////////////////////////////////////////////////
202
203 // select sleeps _at most_ (compared to usleep() which sleeps at least)
204 static void select_sleep (uint32_t usec) {
205         if (usec <= 10) return;
206         fd_set fd;
207         int max_fd=0;
208         struct timeval tv;
209         tv.tv_sec = usec / 1000000;
210         tv.tv_usec = usec % 1000000;
211         FD_ZERO (&fd);
212         select (max_fd, &fd, NULL, NULL, &tv);
213 }
214
215 ///////////////////////////////////////////////////////////////////////////////
216
217 AlsaRawMidiOut::AlsaRawMidiOut (const char *device)
218                 : AlsaRawMidiIO (device, false)
219 {
220 }
221
222
223 int
224 AlsaRawMidiOut::send_event (const pframes_t time, const uint8_t *data, const size_t size)
225 {
226         const uint32_t  buf_size = sizeof (MidiEventHeader) + size;
227         if (_rb->write_space() < buf_size) {
228                 _DEBUGPRINT("AlsaRawMidiOut: ring buffer overflow\n");
229                 return -1;
230         }
231         struct MidiEventHeader h (_clock_monotonic + time * _sample_length_us, size);
232         _rb->write ((uint8_t*) &h, sizeof(MidiEventHeader));
233         _rb->write (data, size);
234
235         if (pthread_mutex_trylock (&_notify_mutex) == 0) {
236                 pthread_cond_signal (&_notify_ready);
237                 pthread_mutex_unlock (&_notify_mutex);
238         }
239         return 0;
240 }
241
242 void *
243 AlsaRawMidiOut::main_process_thread ()
244 {
245         _running = true;
246         pthread_mutex_lock (&_notify_mutex);
247         bool need_drain = false;
248         while (_running) {
249                 bool have_data = false;
250                 struct MidiEventHeader h(0,0);
251                 uint8_t data[MaxAlsaRawEventSize];
252
253                 const uint32_t read_space = _rb->read_space();
254
255                 if (read_space > sizeof(MidiEventHeader)) {
256                         if (_rb->read ((uint8_t*)&h, sizeof(MidiEventHeader)) != sizeof(MidiEventHeader)) {
257                                 _DEBUGPRINT("AlsaRawMidiOut: Garbled MIDI EVENT HEADER!!\n");
258                                 break;
259                         }
260                         assert (read_space >= h.size);
261                         if (h.size > MaxAlsaRawEventSize) {
262                                 _rb->increment_read_idx (h.size);
263                                 _DEBUGPRINT("AlsaRawMidiOut: MIDI event too large!\n");
264                                 continue;
265                         }
266                         if (_rb->read (&data[0], h.size) != h.size) {
267                                 _DEBUGPRINT("AlsaRawMidiOut: Garbled MIDI EVENT DATA!!\n");
268                                 break;
269                         }
270                         have_data = true;
271                 }
272
273                 if (!have_data) {
274                         if (need_drain) {
275                                 snd_rawmidi_drain (_device);
276                                 need_drain = false;
277                         }
278                         pthread_cond_wait (&_notify_ready, &_notify_mutex);
279                         continue;
280                 }
281
282                 uint64_t now = g_get_monotonic_time();
283                 while (h.time > now + 500) {
284                         if (need_drain) {
285                                 snd_rawmidi_drain (_device);
286                                 need_drain = false;
287                         } else {
288                                 select_sleep(h.time - now);
289                         }
290                         now = g_get_monotonic_time();
291                 }
292
293 retry:
294                 int perr = poll (_pfds, _npfds, 10 /* ms */);
295                 if (perr < 0) {
296                         PBD::error << _("AlsaRawMidiOut: Error polling device. Terminating Midi Thread.") << endmsg;
297                         break;
298                 }
299                 if (perr == 0) {
300                         _DEBUGPRINT("AlsaRawMidiOut: poll() timed out.\n");
301                         goto retry;
302                 }
303
304                 unsigned short revents = 0;
305                 if (snd_rawmidi_poll_descriptors_revents (_device, _pfds, _npfds, &revents)) {
306                         PBD::error << _("AlsaRawMidiOut: Failed to poll device. Terminating Midi Thread.") << endmsg;
307                         break;
308                 }
309
310                 if (revents & (POLLERR | POLLHUP | POLLNVAL)) {
311                         PBD::error << _("AlsaRawMidiOut: poll error. Terminating Midi Thread.") << endmsg;
312                         break;
313                 }
314
315                 if (!(revents & POLLOUT)) {
316                         _DEBUGPRINT("AlsaRawMidiOut: POLLOUT not ready.\n");
317                         select_sleep (1000);
318                         goto retry;
319                 }
320
321                 ssize_t err = snd_rawmidi_write (_device, data, h.size);
322
323                 if ((err == -EAGAIN)) {
324                         snd_rawmidi_drain (_device);
325                         goto retry;
326                 }
327                 if (err == -EWOULDBLOCK) {
328                         select_sleep (1000);
329                         goto retry;
330                 }
331                 if (err < 0) {
332                         PBD::error << _("AlsaRawMidiOut: write failed. Terminating Midi Thread.") << endmsg;
333                         break;
334                 }
335                 if ((size_t) err < h.size) {
336                         _DEBUGPRINT("AlsaRawMidiOut: short write\n");
337                         memmove(&data[0], &data[err], err);
338                         h.size -= err;
339                         goto retry;
340                 }
341                 need_drain = true;
342         }
343
344         pthread_mutex_unlock (&_notify_mutex);
345         _DEBUGPRINT("AlsaRawMidiOut: MIDI OUT THREAD STOPPED\n");
346         return 0;
347 }
348
349
350 ///////////////////////////////////////////////////////////////////////////////
351
352 AlsaRawMidiIn::AlsaRawMidiIn (const char *device)
353                 : AlsaRawMidiIO (device, true)
354                 , _event(0,0)
355                 , _first_time(true)
356                 , _unbuffered_bytes(0)
357                 , _total_bytes(0)
358                 , _expected_bytes(0)
359                 , _status_byte(0)
360 {
361 }
362
363 size_t
364 AlsaRawMidiIn::recv_event (pframes_t &time, uint8_t *data, size_t &size)
365 {
366         const uint32_t read_space = _rb->read_space();
367         struct MidiEventHeader h(0,0);
368
369         if (read_space <= sizeof(MidiEventHeader)) {
370                 return 0;
371         }
372
373 #if 1
374         // check if event is in current cycle
375         RingBuffer<uint8_t>::rw_vector vector;
376         _rb->get_read_vector(&vector);
377         if (vector.len[0] >= sizeof(MidiEventHeader)) {
378                 memcpy((uint8_t*)&h, vector.buf[0], sizeof(MidiEventHeader));
379         } else {
380                 if (vector.len[0] > 0) {
381                         memcpy ((uint8_t*)&h, vector.buf[0], vector.len[0]);
382                 }
383                 memcpy (((uint8_t*)&h) + vector.len[0], vector.buf[1], sizeof(MidiEventHeader) - vector.len[0]);
384         }
385
386         if (h.time >= _clock_monotonic + _period_length_us ) {
387 #ifdef DEBUG_TIMING
388                 printf("AlsaRawMidiIn DEBUG: POSTPONE EVENT TO NEXT CYCLE: %.1f spl\n", ((h.time - _clock_monotonic) / _sample_length_us));
389 #endif
390                 return 0;
391         }
392         _rb->increment_read_idx (sizeof(MidiEventHeader));
393 #else
394         if (_rb->read ((uint8_t*)&h, sizeof(MidiEventHeader)) != sizeof(MidiEventHeader)) {
395                 _DEBUGPRINT("AlsaRawMidiIn::recv_event Garbled MIDI EVENT HEADER!!\n");
396                 return 0;
397         }
398 #endif
399         assert (h.size > 0);
400         if (h.size > size) {
401                 _DEBUGPRINT("AlsaRawMidiIn::recv_event MIDI event too large!\n");
402                 _rb->increment_read_idx (h.size);
403                 return 0;
404         }
405         if (_rb->read (&data[0], h.size) != h.size) {
406                 _DEBUGPRINT("AlsaRawMidiIn::recv_event Garbled MIDI EVENT DATA!!\n");
407                 return 0;
408         }
409         if (h.time < _clock_monotonic) {
410 #ifdef DEBUG_TIMING
411                 printf("AlsaRawMidiIn DEBUG: MIDI TIME < 0 %.1f spl\n", ((_clock_monotonic - h.time) / -_sample_length_us));
412 #endif
413                 time = 0;
414         } else if (h.time >= _clock_monotonic + _period_length_us ) {
415 #ifdef DEBUG_TIMING
416                 printf("AlsaRawMidiIn DEBUG: MIDI TIME > PERIOD %.1f spl\n", ((h.time - _clock_monotonic) / _sample_length_us));
417 #endif
418                 time = _samples_per_period - 1;
419         } else {
420                 time = floor ((h.time - _clock_monotonic) / _sample_length_us);
421         }
422         assert(time < _samples_per_period);
423         size = h.size;
424         return h.size;
425 }
426
427 void *
428 AlsaRawMidiIn::main_process_thread ()
429 {
430         _running = true;
431         while (_running) {
432                 unsigned short revents = 0;
433
434                 int perr = poll (_pfds, _npfds, 100 /* ms */);
435                 if (perr < 0) {
436                         PBD::error << _("AlsaRawMidiIn: Error polling device. Terminating Midi Thread.") << endmsg;
437                         break;
438                 }
439                 if (perr == 0) {
440                         continue;
441                 }
442
443                 if (snd_rawmidi_poll_descriptors_revents (_device, _pfds, _npfds, &revents)) {
444                         PBD::error << _("AlsaRawMidiIn: Failed to poll device. Terminating Midi Thread.") << endmsg;
445                         break;
446                 }
447
448                 if (revents & (POLLERR | POLLHUP | POLLNVAL)) {
449                         PBD::error << _("AlsaRawMidiIn: poll error. Terminating Midi Thread.") << endmsg;
450                         break;
451                 }
452
453                 if (!(revents & POLLIN)) {
454                         _DEBUGPRINT("AlsaRawMidiOut: POLLIN not ready.\n");
455                         select_sleep (1000);
456                         continue;
457                 }
458
459                 uint8_t data[MaxAlsaRawEventSize];
460                 uint64_t time = g_get_monotonic_time();
461                 ssize_t err = snd_rawmidi_read (_device, data, sizeof(data));
462
463                 if ((err == -EAGAIN) || (err == -EWOULDBLOCK)) {
464                         continue;
465                 }
466                 if (err < 0) {
467                         PBD::error << _("AlsaRawMidiIn: read error. Terminating Midi") << endmsg;
468                         break;
469                 }
470                 if (err == 0) {
471                         _DEBUGPRINT("AlsaRawMidiIn: zero read\n");
472                         continue;
473                 }
474
475 #if 0
476                 queue_event (time, data, err);
477 #else
478                 parse_events (time, data, err);
479 #endif
480         }
481
482         _DEBUGPRINT("AlsaRawMidiIn: MIDI IN THREAD STOPPED\n");
483         return 0;
484 }
485
486 int
487 AlsaRawMidiIn::queue_event (const uint64_t time, const uint8_t *data, const size_t size) {
488         const uint32_t  buf_size = sizeof(MidiEventHeader) + size;
489         _event._pending = false;
490
491         if (size == 0) {
492                 return -1;
493         }
494         if (_rb->write_space() < buf_size) {
495                 _DEBUGPRINT("AlsaRawMidiIn: ring buffer overflow\n");
496                 return -1;
497         }
498         struct MidiEventHeader h (time, size);
499         _rb->write ((uint8_t*) &h, sizeof(MidiEventHeader));
500         _rb->write (data, size);
501         return 0;
502 }
503
504 void
505 AlsaRawMidiIn::parse_events (const uint64_t time, const uint8_t *data, const size_t size) {
506         if (_event._pending) {
507                 _DEBUGPRINT("AlsaRawMidiIn: queue pending event\n");
508                 if (queue_event (_event._time, _parser_buffer, _event._size)) {
509                         return;
510                 }
511         }
512         for (size_t i = 0; i < size; ++i) {
513                 if (_first_time && !(data[i] & 0x80)) {
514                         continue;
515                 }
516                 _first_time = false; /// TODO optimize e.g. use fn pointer to different parse_events()
517                 if (process_byte(time, data[i])) {
518                         if (queue_event (_event._time, _parser_buffer, _event._size)) {
519                                 return;
520                         }
521                 }
522         }
523 }
524
525 // based on JackMidiRawInputWriteQueue by Devin Anderson //
526 bool
527 AlsaRawMidiIn::process_byte(const uint64_t time, const uint8_t byte)
528 {
529         if (byte >= 0xf8) {
530                 // Realtime
531                 if (byte == 0xfd) {
532                         return false;
533                 }
534                 _parser_buffer[0] = byte;
535                 prepare_byte_event(time, byte);
536                 return true;
537         }
538         if (byte == 0xf7) {
539                 // Sysex end
540                 if (_status_byte == 0xf0) {
541                         record_byte(byte);
542                         return prepare_buffered_event(time);
543                 }
544     _total_bytes = 0;
545     _unbuffered_bytes = 0;
546                 _expected_bytes = 0;
547                 _status_byte = 0;
548                 return false;
549         }
550         if (byte >= 0x80) {
551                 // Non-realtime status byte
552                 if (_total_bytes) {
553                         _DEBUGPRINT("AlsaRawMidiIn: discarded bogus midi message\n");
554 #if 0
555                         for (size_t i=0; i < _total_bytes; ++i) {
556                                 printf("%02x ", _parser_buffer[i]);
557                         }
558                         printf("\n");
559 #endif
560                         _total_bytes = 0;
561                         _unbuffered_bytes = 0;
562                 }
563                 _status_byte = byte;
564                 switch (byte & 0xf0) {
565                         case 0x80:
566                         case 0x90:
567                         case 0xa0:
568                         case 0xb0:
569                         case 0xe0:
570                                 // Note On, Note Off, Aftertouch, Control Change, Pitch Wheel
571                                 _expected_bytes = 3;
572                                 break;
573                         case 0xc0:
574                         case 0xd0:
575                                 // Program Change, Channel Pressure
576                                 _expected_bytes = 2;
577                                 break;
578                         case 0xf0:
579                                 switch (byte) {
580                                         case 0xf0:
581                                                 // Sysex
582                                                 _expected_bytes = 0;
583                                                 break;
584                                         case 0xf1:
585                                         case 0xf3:
586                                                 // MTC Quarter Frame, Song Select
587                                                 _expected_bytes = 2;
588                                                 break;
589                                         case 0xf2:
590                                                 // Song Position
591                                                 _expected_bytes = 3;
592                                                 break;
593                                         case 0xf4:
594                                         case 0xf5:
595                                                 // Undefined
596                                                 _expected_bytes = 0;
597                                                 _status_byte = 0;
598                                                 return false;
599                                         case 0xf6:
600                                                 // Tune Request
601                                                 prepare_byte_event(time, byte);
602                                                 _expected_bytes = 0;
603                                                 _status_byte = 0;
604                                                 return true;
605                                 }
606                 }
607                 record_byte(byte);
608                 return false;
609         }
610         // Data byte
611         if (! _status_byte) {
612                 // Data bytes without a status will be discarded.
613                 _total_bytes++;
614                 _unbuffered_bytes++;
615                 return false;
616         }
617         if (! _total_bytes) {
618                 _DEBUGPRINT("AlsaRawMidiIn: apply running status\n");
619                 record_byte(_status_byte);
620         }
621         record_byte(byte);
622         return (_total_bytes == _expected_bytes) ? prepare_buffered_event(time) : false;
623 }