ardour
mididm.cc
Go to the documentation of this file.
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 
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 int64_t
40 MIDIDM::parse_mclk (uint8_t* buf, pframes_t timestamp) const
41 {
42  /* calculate time difference */
43 #define MODCLK (16384) // 1<<(2*7)
44  const int64_t tc = (_monotonic_cnt + timestamp) & 0x3fff; // MODCLK - 1;
45  const int64_t ti = ((buf[2] & 0x7f) << 7) | (buf[1] & 0x7f);
46  const int64_t tdiff = (MODCLK + tc - ti) % MODCLK;
47 #ifdef DEBUG_MIDIDM
48  printf("MCLK DELAY: #%5"PRId64" dt:%6"PRId64" [spl] (%6"PRId64" - %8"PRId64") @(%8"PRId64" + %d)\n",
49  _cnt_total, tdiff, tc, ti, _monotonic_cnt, timestamp);
50 #endif
51  return tdiff;
52 }
53 
54 int64_t
55 MIDIDM::parse_mtc (uint8_t* buf, pframes_t timestamp) const
56 {
57 #define MODTC (2097152) // 1<<(3*7)
58  const int64_t tc = (_monotonic_cnt + timestamp) & 0x001FFFFF;
59  const int64_t ti = (buf[5] & 0x7f)
60  | ((buf[6] & 0x7f) << 7)
61  | ((buf[7] & 0x7f) << 14)
62  | ((buf[8] & 0x7f) << 21);
63  const int64_t tdiff = (MODTC + tc - ti) % MODTC;
64 #ifdef DEBUG_MIDIDM
65  printf("MTC DELAY: #%5"PRId64" dt:%6"PRId64" [spl] (%6"PRId64" - %8"PRId64") @(%8"PRId64" + %d)\n",
66  _cnt_total, tdiff, tc, ti, _monotonic_cnt, timestamp);
67 #endif
68  return tdiff;
69 }
70 
71 int MIDIDM::process (pframes_t nframes, PortEngine &pe, void *midi_in, void *midi_out)
72 {
73  /* send midi event */
74  pe.midi_clear(midi_out);
75 #ifndef USE_MTC // use 3-byte song position
76  uint8_t obuf[3];
77  obuf[0] = 0xf2;
78  obuf[1] = (_monotonic_cnt) & 0x7f;
79  obuf[2] = (_monotonic_cnt >> 7) & 0x7f;
80  pe.midi_event_put (midi_out, 0, obuf, 3);
81 #else // sysex MTC frame
82  uint8_t obuf[10];
83  obuf[0] = 0xf0;
84  obuf[1] = 0x7f;
85  obuf[2] = 0x7f;
86  obuf[3] = 0x01;
87  obuf[4] = 0x01;
88  obuf[9] = 0xf7;
89  obuf[5] = (_monotonic_cnt ) & 0x7f;
90  obuf[6] = (_monotonic_cnt >> 7) & 0x7f;
91  obuf[7] = (_monotonic_cnt >> 14) & 0x7f;
92  obuf[8] = (_monotonic_cnt >> 21) & 0x7f;
93  pe.midi_event_put (midi_out, 0, obuf, 10);
94 #endif
95 
96  /* process incoming */
97  const pframes_t nevents = pe.get_midi_event_count (midi_in);
98 #ifdef DEBUG_MIDIDM
99  printf("MIDI SEND: @%8"PRId64", recv: %d systime:%"PRId64"\n", _monotonic_cnt, nevents, g_get_monotonic_time());
100 #endif
101  for (pframes_t n = 0; n < nevents; ++n) {
102  pframes_t timestamp;
103  size_t size;
104  uint8_t* buf;
105  int64_t tdiff;
106  pe.midi_event_get (timestamp, size, &buf, midi_in, n);
107 
108  if (size == 3 && buf[0] == 0xf2 )
109  {
110  tdiff = parse_mclk(buf, timestamp);
111  } else if (size == 10 && buf[0] == 0xf0)
112  {
113  tdiff = parse_mtc(buf, timestamp);
114  }
115  else
116  {
117  continue;
118  }
119 
121 
122  /* running variance */
123  if (_cnt_total == 0) {
124  _var_m = tdiff;
125  } else {
126  const double var_m1 = _var_m;
127  _var_m = _var_m + ((double)tdiff - _var_m) / (double)(_cnt_total + 1);
128  _var_s = _var_s + ((double)tdiff - _var_m) * ((double)tdiff - var_m1);
129  }
130  /* average and mix/max */
131  ++_cnt_total;
132  _dly_total += tdiff;
134  if (tdiff < _min_delay) _min_delay = tdiff;
135  if (tdiff > _max_delay) _max_delay = tdiff;
136  }
137 
138  _monotonic_cnt += nframes;
139  return 0;
140 }
uint64_t _dly_total
Definition: mididm.h:53
double _avg_delay
Definition: mididm.h:56
#define MODTC
#define MODCLK
virtual void midi_clear(void *port_buffer)=0
uint32_t pframes_t
Definition: types.h:61
uint32_t _min_delay
Definition: mididm.h:54
MIDIDM(framecnt_t sample_rate)
Definition: mididm.cc:24
int process(pframes_t nframes, PortEngine &pe, void *midi_in, void *midi_out)
Definition: mididm.cc:71
uint64_t _last_signal_tme
Definition: mididm.h:50
double _var_s
Definition: mididm.h:58
int64_t framecnt_t
Definition: types.h:76
uint32_t _max_delay
Definition: mididm.h:55
Definition: amp.h:29
uint64_t _cnt_total
Definition: mididm.h:52
int64_t parse_mclk(uint8_t *buf, pframes_t timestamp) const
Definition: mididm.cc:40
int64_t parse_mtc(uint8_t *buf, pframes_t timestamp) const
Definition: mididm.cc:55
virtual uint32_t get_midi_event_count(void *port_buffer)=0
uint64_t _monotonic_cnt
Definition: mididm.h:49
double _var_m
Definition: mididm.h:57
virtual int midi_event_get(pframes_t &timestamp, size_t &size, uint8_t **buf, void *port_buffer, uint32_t event_index)=0
virtual int midi_event_put(void *port_buffer, pframes_t timestamp, const uint8_t *buffer, size_t size)=0