6549544efb7ad5df541ba84ac48949e96ad8ec6d
[ardour.git] / libs / ardour / mididm.cc
1 /*
2  * Copyright (C) 2013-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 "ardour/mididm.h"
20 #include "ardour/port_engine.h"
21
22 using namespace ARDOUR;
23
24 MIDIDM::MIDIDM (framecnt_t sample_rate)
25   : _sample_rate (sample_rate)
26   , _monotonic_cnt (sample_rate)
27   , _last_signal_tme (0)
28   , _cnt_total (0)
29   , _dly_total (0)
30   , _min_delay (INT32_MAX)
31   , _max_delay (0)
32   , _avg_delay (0)
33   , _var_m (0)
34   , _var_s (0)
35 {
36
37 }
38
39
40 int MIDIDM::process (pframes_t nframes, PortEngine &pe, void *midi_in, void *midi_out)
41 {
42         /* send midi event */
43         uint8_t obuf[3];
44         obuf[0] = 0xf2;
45         obuf[1] = (_monotonic_cnt)      & 0x7f;
46         obuf[2] = (_monotonic_cnt >> 7) & 0x7f;
47
48         pe.midi_clear(midi_out);
49         pe.midi_event_put (midi_out, 0, obuf, 3);
50
51         /* process incoming */
52         const pframes_t nevents = pe.get_midi_event_count (midi_in);
53         for (pframes_t n = 0; n < nevents; ++n) {
54                 pframes_t timestamp;
55                 size_t size;
56                 uint8_t* buf;
57                 pe.midi_event_get (timestamp, size, &buf, midi_in, n);
58
59                 if (size != 3 || buf[0] != 0xf2 ) continue;
60
61                 _last_signal_tme = _monotonic_cnt;
62
63                 /* calculate time difference */
64 #define MODX (16384)  // 1<<(2*7)
65 #define MASK (0x3fff) // MODX - 1
66                 const int64_t tc = (_monotonic_cnt + timestamp) & MASK;
67                 const int64_t ti = (buf[2] << 7) | buf[1];
68                 const int64_t tdiff = (MODX + tc - ti) % MODX;
69
70                 /* running variance */
71                 if (_cnt_total == 0) {
72                         _var_m = tdiff;
73                 } else {
74                         const double var_m1 = _var_m;
75                         _var_m = _var_m + ((double)tdiff - _var_m) / (double)(_cnt_total + 1);
76                         _var_s = _var_s + ((double)tdiff - _var_m) * ((double)tdiff - var_m1);
77                 }
78                 /* average and mix/max */
79                 ++_cnt_total;
80                 _dly_total += tdiff;
81                 _avg_delay = _dly_total / _cnt_total;
82                 if (tdiff < _min_delay) _min_delay = tdiff;
83                 if (tdiff > _max_delay) _max_delay = tdiff;
84         }
85
86   _monotonic_cnt += nframes;
87         return 0;
88 }