2 * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2010 Devin Anderson
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.
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.
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.
24 #include "alsa_rawmidi.h"
25 #include "rt_thread.h"
27 #include "pbd/error.h"
30 using namespace ARDOUR;
32 /* max bytes per individual midi-event
33 * events larger than this are ignored */
34 #define MaxAlsaRawEventSize (64)
37 #define _DEBUGPRINT(STR) fprintf(stderr, STR);
39 #define _DEBUGPRINT(STR) ;
42 AlsaRawMidiIO::AlsaRawMidiIO (const char *device, const bool input)
47 , _sample_length_us (1e6 / 48000.0)
48 , _period_length_us (1.024e6 / 48000.0)
49 , _samples_per_period (1024)
52 pthread_mutex_init (&_notify_mutex, 0);
53 pthread_cond_init (&_notify_ready, 0);
57 AlsaRawMidiIO::~AlsaRawMidiIO ()
60 snd_rawmidi_drain (_device);
61 snd_rawmidi_close (_device);
65 pthread_mutex_destroy (&_notify_mutex);
66 pthread_cond_destroy (&_notify_ready);
71 AlsaRawMidiIO::init (const char *device_name, const bool input)
73 if (snd_rawmidi_open (
74 input ? &_device : NULL,
75 input ? NULL : &_device,
76 device_name, SND_RAWMIDI_NONBLOCK) < 0) {
80 _npfds = snd_rawmidi_poll_descriptors_count (_device);
82 _DEBUGPRINT("AlsaRawMidiIO: no poll descriptor(s).\n");
83 snd_rawmidi_close (_device);
87 _pfds = (struct pollfd*) malloc (_npfds * sizeof(struct pollfd));
88 snd_rawmidi_poll_descriptors (_device, _pfds, _npfds);
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));
99 snd_rawmidi_params_t *params;
100 if (snd_rawmidi_params_malloc (¶ms)) {
103 if (snd_rawmidi_params_current (_device, params)) {
106 if (snd_rawmidi_params_set_avail_min (_device, params, 1)) {
109 if ( snd_rawmidi_params_set_buffer_size (_device, params, 64)) {
112 if (snd_rawmidi_params_set_no_active_sensing (_device, params, 1)) {
120 _DEBUGPRINT("AlsaRawMidiIO: parameter setup error\n");
121 snd_rawmidi_close (_device);
127 static void * pthread_process (void *arg)
129 AlsaRawMidiIO *d = static_cast<AlsaRawMidiIO *>(arg);
130 d->main_process_thread ();
136 AlsaRawMidiIO::start ()
138 if (_realtime_pthread_create (SCHED_FIFO, -21, 100000,
139 &_main_thread, pthread_process, this))
141 if (pthread_create (&_main_thread, NULL, pthread_process, this)) {
142 PBD::error << _("AlsaRawMidiIO: Failed to create process thread.") << endmsg;
145 PBD::warning << _("AlsaRawMidiIO: Cannot acquire realtime permissions.") << endmsg;
149 while (!_running && --timeout > 0) { Glib::usleep (1000); }
150 if (timeout == 0 || !_running) {
157 AlsaRawMidiIO::stop ()
166 pthread_mutex_lock (&_notify_mutex);
167 pthread_cond_signal (&_notify_ready);
168 pthread_mutex_unlock (&_notify_mutex);
170 if (pthread_join (_main_thread, &status)) {
171 PBD::error << _("AlsaRawMidiIO: Failed to terminate.") << endmsg;
178 AlsaRawMidiIO::setup_timing (const size_t samples_per_period, const float samplerate)
180 _period_length_us = (double) samples_per_period * 1e6 / samplerate;
181 _sample_length_us = 1e6 / samplerate;
182 _samples_per_period = samples_per_period;
186 AlsaRawMidiIO::sync_time (const uint64_t tme)
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.
193 double tdiff = (_clock_monotonic + _period_length_us - tme) / 1000.0;
194 if (abs(tdiff) >= .05) {
195 printf("AlsaRawMidiIO MJ: %.1f ms\n", tdiff);
198 _clock_monotonic = tme;
201 ///////////////////////////////////////////////////////////////////////////////
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;
209 tv.tv_sec = usec / 1000000;
210 tv.tv_usec = usec % 1000000;
212 select (max_fd, &fd, NULL, NULL, &tv);
215 ///////////////////////////////////////////////////////////////////////////////
217 AlsaRawMidiOut::AlsaRawMidiOut (const char *device)
218 : AlsaRawMidiIO (device, false)
224 AlsaRawMidiOut::send_event (const pframes_t time, const uint8_t *data, const size_t size)
226 const uint32_t buf_size = sizeof (MidiEventHeader) + size;
227 if (_rb->write_space() < buf_size) {
228 _DEBUGPRINT("AlsaRawMidiOut: ring buffer overflow\n");
231 struct MidiEventHeader h (_clock_monotonic + time * _sample_length_us, size);
232 _rb->write ((uint8_t*) &h, sizeof(MidiEventHeader));
233 _rb->write (data, size);
235 if (pthread_mutex_trylock (&_notify_mutex) == 0) {
236 pthread_cond_signal (&_notify_ready);
237 pthread_mutex_unlock (&_notify_mutex);
243 AlsaRawMidiOut::main_process_thread ()
246 pthread_mutex_lock (&_notify_mutex);
247 bool need_drain = false;
249 bool have_data = false;
250 struct MidiEventHeader h(0,0);
251 uint8_t data[MaxAlsaRawEventSize];
253 const uint32_t read_space = _rb->read_space();
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");
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");
266 if (_rb->read (&data[0], h.size) != h.size) {
267 _DEBUGPRINT("AlsaRawMidiOut: Garbled MIDI EVENT DATA!!\n");
275 snd_rawmidi_drain (_device);
278 pthread_cond_wait (&_notify_ready, &_notify_mutex);
282 uint64_t now = g_get_monotonic_time();
283 while (h.time > now + 500) {
285 snd_rawmidi_drain (_device);
288 select_sleep(h.time - now);
290 now = g_get_monotonic_time();
294 int perr = poll (_pfds, _npfds, 10 /* ms */);
296 PBD::error << _("AlsaRawMidiOut: Error polling device. Terminating Midi Thread.") << endmsg;
300 _DEBUGPRINT("AlsaRawMidiOut: poll() timed out.\n");
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;
310 if (revents & (POLLERR | POLLHUP | POLLNVAL)) {
311 PBD::error << _("AlsaRawMidiOut: poll error. Terminating Midi Thread.") << endmsg;
315 if (!(revents & POLLOUT)) {
316 _DEBUGPRINT("AlsaRawMidiOut: POLLOUT not ready.\n");
321 ssize_t err = snd_rawmidi_write (_device, data, h.size);
323 if ((err == -EAGAIN)) {
324 snd_rawmidi_drain (_device);
327 if (err == -EWOULDBLOCK) {
332 PBD::error << _("AlsaRawMidiOut: write failed. Terminating Midi Thread.") << endmsg;
335 if ((size_t) err < h.size) {
336 _DEBUGPRINT("AlsaRawMidiOut: short write\n");
337 memmove(&data[0], &data[err], err);
344 pthread_mutex_unlock (&_notify_mutex);
345 _DEBUGPRINT("AlsaRawMidiOut: MIDI OUT THREAD STOPPED\n");
350 ///////////////////////////////////////////////////////////////////////////////
352 AlsaRawMidiIn::AlsaRawMidiIn (const char *device)
353 : AlsaRawMidiIO (device, true)
356 , _unbuffered_bytes(0)
364 AlsaRawMidiIn::recv_event (pframes_t &time, uint8_t *data, size_t &size)
366 const uint32_t read_space = _rb->read_space();
367 struct MidiEventHeader h(0,0);
369 if (read_space <= sizeof(MidiEventHeader)) {
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));
380 if (vector.len[0] > 0) {
381 memcpy ((uint8_t*)&h, vector.buf[0], vector.len[0]);
383 memcpy (((uint8_t*)&h) + vector.len[0], vector.buf[1], sizeof(MidiEventHeader) - vector.len[0]);
386 if (h.time >= _clock_monotonic + _period_length_us ) {
388 printf("AlsaRawMidiIn DEBUG: POSTPONE EVENT TO NEXT CYCLE: %.1f spl\n", ((h.time - _clock_monotonic) / _sample_length_us));
392 _rb->increment_read_idx (sizeof(MidiEventHeader));
394 if (_rb->read ((uint8_t*)&h, sizeof(MidiEventHeader)) != sizeof(MidiEventHeader)) {
395 _DEBUGPRINT("AlsaRawMidiIn::recv_event Garbled MIDI EVENT HEADER!!\n");
401 _DEBUGPRINT("AlsaRawMidiIn::recv_event MIDI event too large!\n");
402 _rb->increment_read_idx (h.size);
405 if (_rb->read (&data[0], h.size) != h.size) {
406 _DEBUGPRINT("AlsaRawMidiIn::recv_event Garbled MIDI EVENT DATA!!\n");
409 if (h.time < _clock_monotonic) {
411 printf("AlsaRawMidiIn DEBUG: MIDI TIME < 0 %.1f spl\n", ((_clock_monotonic - h.time) / -_sample_length_us));
414 } else if (h.time >= _clock_monotonic + _period_length_us ) {
416 printf("AlsaRawMidiIn DEBUG: MIDI TIME > PERIOD %.1f spl\n", ((h.time - _clock_monotonic) / _sample_length_us));
418 time = _samples_per_period - 1;
420 time = floor ((h.time - _clock_monotonic) / _sample_length_us);
422 assert(time < _samples_per_period);
428 AlsaRawMidiIn::main_process_thread ()
432 unsigned short revents = 0;
434 int perr = poll (_pfds, _npfds, 100 /* ms */);
436 PBD::error << _("AlsaRawMidiIn: Error polling device. Terminating Midi Thread.") << endmsg;
443 if (snd_rawmidi_poll_descriptors_revents (_device, _pfds, _npfds, &revents)) {
444 PBD::error << _("AlsaRawMidiIn: Failed to poll device. Terminating Midi Thread.") << endmsg;
448 if (revents & (POLLERR | POLLHUP | POLLNVAL)) {
449 PBD::error << _("AlsaRawMidiIn: poll error. Terminating Midi Thread.") << endmsg;
453 if (!(revents & POLLIN)) {
454 _DEBUGPRINT("AlsaRawMidiOut: POLLIN not ready.\n");
459 uint8_t data[MaxAlsaRawEventSize];
460 uint64_t time = g_get_monotonic_time();
461 ssize_t err = snd_rawmidi_read (_device, data, sizeof(data));
463 if ((err == -EAGAIN) || (err == -EWOULDBLOCK)) {
467 PBD::error << _("AlsaRawMidiIn: read error. Terminating Midi") << endmsg;
471 _DEBUGPRINT("AlsaRawMidiIn: zero read\n");
476 queue_event (time, data, err);
478 parse_events (time, data, err);
482 _DEBUGPRINT("AlsaRawMidiIn: MIDI IN THREAD STOPPED\n");
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;
494 if (_rb->write_space() < buf_size) {
495 _DEBUGPRINT("AlsaRawMidiIn: ring buffer overflow\n");
498 struct MidiEventHeader h (time, size);
499 _rb->write ((uint8_t*) &h, sizeof(MidiEventHeader));
500 _rb->write (data, size);
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)) {
512 for (size_t i = 0; i < size; ++i) {
513 if (_first_time && !(data[i] & 0x80)) {
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)) {
525 // based on JackMidiRawInputWriteQueue by Devin Anderson //
527 AlsaRawMidiIn::process_byte(const uint64_t time, const uint8_t byte)
534 _parser_buffer[0] = byte;
535 prepare_byte_event(time, byte);
540 if (_status_byte == 0xf0) {
542 return prepare_buffered_event(time);
545 _unbuffered_bytes = 0;
551 // Non-realtime status byte
553 _DEBUGPRINT("AlsaRawMidiIn: discarded bogus midi message\n");
555 for (size_t i=0; i < _total_bytes; ++i) {
556 printf("%02x ", _parser_buffer[i]);
561 _unbuffered_bytes = 0;
564 switch (byte & 0xf0) {
570 // Note On, Note Off, Aftertouch, Control Change, Pitch Wheel
575 // Program Change, Channel Pressure
586 // MTC Quarter Frame, Song Select
601 prepare_byte_event(time, byte);
611 if (! _status_byte) {
612 // Data bytes without a status will be discarded.
617 if (! _total_bytes) {
618 _DEBUGPRINT("AlsaRawMidiIn: apply running status\n");
619 record_byte(_status_byte);
622 return (_total_bytes == _expected_bytes) ? prepare_buffered_event(time) : false;