2 * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
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.
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.
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.
23 #include "alsa_midi.h"
24 #include "rt_thread.h"
26 #include "pbd/error.h"
29 using namespace ARDOUR;
32 #define _DEBUGPRINT(STR) fprintf(stderr, STR);
34 #define _DEBUGPRINT(STR) ;
37 AlsaMidiIO::AlsaMidiIO ()
41 , _sample_length_us (1e6 / 48000.0)
42 , _period_length_us (1.024e6 / 48000.0)
43 , _samples_per_period (1024)
46 pthread_mutex_init (&_notify_mutex, 0);
47 pthread_cond_init (&_notify_ready, 0);
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));
56 AlsaMidiIO::~AlsaMidiIO ()
59 pthread_mutex_destroy (&_notify_mutex);
60 pthread_cond_destroy (&_notify_ready);
64 static void * pthread_process (void *arg)
66 AlsaMidiIO *d = static_cast<AlsaMidiIO *>(arg);
67 d->main_process_thread ();
75 if (_realtime_pthread_create (SCHED_FIFO, -21, 100000,
76 &_main_thread, pthread_process, this))
78 if (pthread_create (&_main_thread, NULL, pthread_process, this)) {
79 PBD::error << _("AlsaMidiIO: Failed to create process thread.") << endmsg;
82 PBD::warning << _("AlsaMidiIO: Cannot acquire realtime permissions.") << endmsg;
86 while (!_running && --timeout > 0) { Glib::usleep (1000); }
87 if (timeout == 0 || !_running) {
103 pthread_mutex_lock (&_notify_mutex);
104 pthread_cond_signal (&_notify_ready);
105 pthread_mutex_unlock (&_notify_mutex);
107 if (pthread_join (_main_thread, &status)) {
108 PBD::error << _("AlsaMidiIO: Failed to terminate.") << endmsg;
115 AlsaMidiIO::setup_timing (const size_t samples_per_period, const float samplerate)
117 _period_length_us = (double) samples_per_period * 1e6 / samplerate;
118 _sample_length_us = 1e6 / samplerate;
119 _samples_per_period = samples_per_period;
123 AlsaMidiIO::sync_time (const uint64_t tme)
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.
130 double tdiff = (_clock_monotonic + _period_length_us - tme) / 1000.0;
131 if (abs(tdiff) >= .05) {
132 printf("AlsaMidiIO MJ: %.1f ms\n", tdiff);
135 _clock_monotonic = tme;
138 ///////////////////////////////////////////////////////////////////////////////
140 AlsaMidiOut::AlsaMidiOut ()
146 AlsaMidiOut::send_event (const pframes_t time, const uint8_t *data, const size_t size)
148 const uint32_t buf_size = sizeof (MidiEventHeader) + size;
149 if (_rb->write_space() < buf_size) {
150 _DEBUGPRINT("AlsaMidiOut: ring buffer overflow\n");
153 struct MidiEventHeader h (_clock_monotonic + time * _sample_length_us, size);
154 _rb->write ((uint8_t*) &h, sizeof(MidiEventHeader));
155 _rb->write (data, size);
157 if (pthread_mutex_trylock (&_notify_mutex) == 0) {
158 pthread_cond_signal (&_notify_ready);
159 pthread_mutex_unlock (&_notify_mutex);
164 ///////////////////////////////////////////////////////////////////////////////
166 AlsaMidiIn::AlsaMidiIn ()
172 AlsaMidiIn::recv_event (pframes_t &time, uint8_t *data, size_t &size)
174 const uint32_t read_space = _rb->read_space();
175 struct MidiEventHeader h(0,0);
177 if (read_space <= sizeof(MidiEventHeader)) {
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));
186 if (vector.len[0] > 0) {
187 memcpy ((uint8_t*)&h, vector.buf[0], vector.len[0]);
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]);
193 if (h.time >= _clock_monotonic + _period_length_us ) {
195 printf("AlsaMidiIn DEBUG: POSTPONE EVENT TO NEXT CYCLE: %.1f spl\n", ((h.time - _clock_monotonic) / _sample_length_us));
199 _rb->increment_read_idx (sizeof(MidiEventHeader));
203 _DEBUGPRINT("AlsaMidiIn::recv_event MIDI event too large!\n");
204 _rb->increment_read_idx (h.size);
207 if (_rb->read (&data[0], h.size) != h.size) {
208 _DEBUGPRINT("AlsaMidiIn::recv_event Garbled MIDI EVENT DATA!!\n");
211 if (h.time < _clock_monotonic) {
213 printf("AlsaMidiIn DEBUG: MIDI TIME < 0 %.1f spl\n", ((_clock_monotonic - h.time) / -_sample_length_us));
216 } else if (h.time >= _clock_monotonic + _period_length_us ) {
218 printf("AlsaMidiIn DEBUG: MIDI TIME > PERIOD %.1f spl\n", ((h.time - _clock_monotonic) / _sample_length_us));
220 time = _samples_per_period - 1;
222 time = floor ((h.time - _clock_monotonic) / _sample_length_us);
224 assert(time < _samples_per_period);
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;
236 if (_rb->write_space() < buf_size) {
237 _DEBUGPRINT("AlsaMidiIn: ring buffer overflow\n");
240 struct MidiEventHeader h (time, size);
241 _rb->write ((uint8_t*) &h, sizeof(MidiEventHeader));
242 _rb->write (data, size);