Abstract definition of rt-scheduler policy
[ardour.git] / libs / backends / alsa / alsa_midi.cc
1 /*
2  * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <unistd.h>
20
21 #include <glibmm.h>
22
23 #include "alsa_midi.h"
24
25 #include "pbd/error.h"
26 #include "pbd/pthread_utils.h"
27 #include "pbd/i18n.h"
28
29 using namespace ARDOUR;
30
31 #ifndef NDEBUG
32 #define _DEBUGPRINT(STR) fprintf(stderr, STR);
33 #else
34 #define _DEBUGPRINT(STR) ;
35 #endif
36
37 AlsaMidiIO::AlsaMidiIO ()
38         : _state (-1)
39         , _running (false)
40         , _pfds (0)
41         , _sample_length_us (1e6 / 48000.0)
42         , _period_length_us (1.024e6 / 48000.0)
43         , _samples_per_period (1024)
44         , _rb (0)
45 {
46         pthread_mutex_init (&_notify_mutex, 0);
47         pthread_cond_init (&_notify_ready, 0);
48
49         // MIDI (hw port) 31.25 kbaud
50         // worst case here is  8192 SPP and 8KSPS for which we'd need
51         // 4000 bytes sans MidiEventHeader.
52         // since we're not always in sync, let's use 4096.
53         _rb = new RingBuffer<uint8_t>(4096 + 4096 * sizeof(MidiEventHeader));
54 }
55
56 AlsaMidiIO::~AlsaMidiIO ()
57 {
58         delete _rb;
59         pthread_mutex_destroy (&_notify_mutex);
60         pthread_cond_destroy (&_notify_ready);
61         free (_pfds);
62 }
63
64 static void * pthread_process (void *arg)
65 {
66         AlsaMidiIO *d = static_cast<AlsaMidiIO *>(arg);
67         d->main_process_thread ();
68         pthread_exit (0);
69         return 0;
70 }
71
72 int
73 AlsaMidiIO::start ()
74 {
75         if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -21, 100000,
76                                 &_main_thread, pthread_process, this))
77         {
78                 if (pthread_create (&_main_thread, NULL, pthread_process, this)) {
79                         PBD::error << _("AlsaMidiIO: Failed to create process thread.") << endmsg;
80                         return -1;
81                 } else {
82                         PBD::warning << _("AlsaMidiIO: Cannot acquire realtime permissions.") << endmsg;
83                 }
84         }
85         int timeout = 5000;
86         while (!_running && --timeout > 0) { Glib::usleep (1000); }
87         if (timeout == 0 || !_running) {
88                 return -1;
89         }
90         return 0;
91 }
92
93 int
94 AlsaMidiIO::stop ()
95 {
96         void *status;
97         if (!_running) {
98                 return 0;
99         }
100
101         _running = false;
102
103         pthread_mutex_lock (&_notify_mutex);
104         pthread_cond_signal (&_notify_ready);
105         pthread_mutex_unlock (&_notify_mutex);
106
107         if (pthread_join (_main_thread, &status)) {
108                 PBD::error << _("AlsaMidiIO: Failed to terminate.") << endmsg;
109                 return -1;
110         }
111         return 0;
112 }
113
114 void
115 AlsaMidiIO::setup_timing (const size_t samples_per_period, const float samplerate)
116 {
117         _period_length_us = (double) samples_per_period * 1e6 / samplerate;
118         _sample_length_us = 1e6 / samplerate;
119         _samples_per_period = samples_per_period;
120 }
121
122 void
123 AlsaMidiIO::sync_time (const uint64_t tme)
124 {
125         // TODO consider a PLL, if this turns out to be the bottleneck for jitter
126         // also think about using
127         // snd_pcm_status_get_tstamp() and snd_rawmidi_status_get_tstamp()
128         // instead of monotonic clock.
129 #ifdef DEBUG_TIMING
130         double tdiff = (_clock_monotonic + _period_length_us - tme) / 1000.0;
131         if (abs(tdiff) >= .05) {
132                 printf("AlsaMidiIO MJ: %.1f ms\n", tdiff);
133         }
134 #endif
135         _clock_monotonic = tme;
136 }
137
138 ///////////////////////////////////////////////////////////////////////////////
139
140 AlsaMidiOut::AlsaMidiOut ()
141         : AlsaMidiIO ()
142 {
143 }
144
145 int
146 AlsaMidiOut::send_event (const pframes_t time, const uint8_t *data, const size_t size)
147 {
148         const uint32_t  buf_size = sizeof (MidiEventHeader) + size;
149         if (_rb->write_space() < buf_size) {
150                 _DEBUGPRINT("AlsaMidiOut: ring buffer overflow\n");
151                 return -1;
152         }
153         struct MidiEventHeader h (_clock_monotonic + time * _sample_length_us, size);
154         _rb->write ((uint8_t*) &h, sizeof(MidiEventHeader));
155         _rb->write (data, size);
156
157         if (pthread_mutex_trylock (&_notify_mutex) == 0) {
158                 pthread_cond_signal (&_notify_ready);
159                 pthread_mutex_unlock (&_notify_mutex);
160         }
161         return 0;
162 }
163
164 ///////////////////////////////////////////////////////////////////////////////
165
166 AlsaMidiIn::AlsaMidiIn ()
167         : AlsaMidiIO ()
168 {
169 }
170
171 size_t
172 AlsaMidiIn::recv_event (pframes_t &time, uint8_t *data, size_t &size)
173 {
174         const uint32_t read_space = _rb->read_space();
175         struct MidiEventHeader h(0,0);
176
177         if (read_space <= sizeof(MidiEventHeader)) {
178                 return 0;
179         }
180
181         RingBuffer<uint8_t>::rw_vector vector;
182         _rb->get_read_vector(&vector);
183         if (vector.len[0] >= sizeof(MidiEventHeader)) {
184                 memcpy((uint8_t*)&h, vector.buf[0], sizeof(MidiEventHeader));
185         } else {
186                 if (vector.len[0] > 0) {
187                         memcpy ((uint8_t*)&h, vector.buf[0], vector.len[0]);
188                 }
189                 assert(vector.buf[1] || vector.len[0] == sizeof(MidiEventHeader));
190                 memcpy (((uint8_t*)&h) + vector.len[0], vector.buf[1], sizeof(MidiEventHeader) - vector.len[0]);
191         }
192
193         if (h.time >= _clock_monotonic + _period_length_us ) {
194 #ifdef DEBUG_TIMING
195                 printf("AlsaMidiIn DEBUG: POSTPONE EVENT TO NEXT CYCLE: %.1f spl\n", ((h.time - _clock_monotonic) / _sample_length_us));
196 #endif
197                 return 0;
198         }
199         _rb->increment_read_idx (sizeof(MidiEventHeader));
200
201         assert (h.size > 0);
202         if (h.size > size) {
203                 _DEBUGPRINT("AlsaMidiIn::recv_event MIDI event too large!\n");
204                 _rb->increment_read_idx (h.size);
205                 return 0;
206         }
207         if (_rb->read (&data[0], h.size) != h.size) {
208                 _DEBUGPRINT("AlsaMidiIn::recv_event Garbled MIDI EVENT DATA!!\n");
209                 return 0;
210         }
211         if (h.time < _clock_monotonic) {
212 #ifdef DEBUG_TIMING
213                 printf("AlsaMidiIn DEBUG: MIDI TIME < 0 %.1f spl\n", ((_clock_monotonic - h.time) / -_sample_length_us));
214 #endif
215                 time = 0;
216         } else if (h.time >= _clock_monotonic + _period_length_us ) {
217 #ifdef DEBUG_TIMING
218                 printf("AlsaMidiIn DEBUG: MIDI TIME > PERIOD %.1f spl\n", ((h.time - _clock_monotonic) / _sample_length_us));
219 #endif
220                 time = _samples_per_period - 1;
221         } else {
222                 time = floor ((h.time - _clock_monotonic) / _sample_length_us);
223         }
224         assert(time < _samples_per_period);
225         size = h.size;
226         return h.size;
227 }
228
229 int
230 AlsaMidiIn::queue_event (const uint64_t time, const uint8_t *data, const size_t size) {
231         const uint32_t  buf_size = sizeof(MidiEventHeader) + size;
232
233         if (size == 0) {
234                 return -1;
235         }
236         if (_rb->write_space() < buf_size) {
237                 _DEBUGPRINT("AlsaMidiIn: ring buffer overflow\n");
238                 return -1;
239         }
240         struct MidiEventHeader h (time, size);
241         _rb->write ((uint8_t*) &h, sizeof(MidiEventHeader));
242         _rb->write (data, size);
243         return 0;
244 }