1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
* Copyright (C) 2013-2014 Robin Gareus <robin@gareus.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "ardour/mididm.h"
#include "ardour/port_engine.h"
using namespace ARDOUR;
MIDIDM::MIDIDM (framecnt_t sample_rate)
: _sample_rate (sample_rate)
, _monotonic_cnt (sample_rate)
, _last_signal_tme (0)
, _cnt_total (0)
, _dly_total (0)
, _min_delay (INT32_MAX)
, _max_delay (0)
, _avg_delay (0)
, _var_m (0)
, _var_s (0)
{
}
int64_t
MIDIDM::parse_mclk (uint8_t* buf, pframes_t timestamp) const
{
/* calculate time difference */
#define MODCLK (16384) // 1<<(2*7)
const int64_t tc = (_monotonic_cnt + timestamp) & 0x3fff; // MODCLK - 1;
const int64_t ti = ((buf[2] & 0x7f) << 7) | (buf[1] & 0x7f);
const int64_t tdiff = (MODCLK + tc - ti) % MODCLK;
#ifdef DEBUG_MIDIDM
printf("MCLK DELAY: #%5"PRId64" dt:%6"PRId64" [spl] (%6"PRId64" - %8"PRId64") @(%8"PRId64" + %d)\n",
_cnt_total, tdiff, tc, ti, _monotonic_cnt, timestamp);
#endif
return tdiff;
}
int64_t
MIDIDM::parse_mtc (uint8_t* buf, pframes_t timestamp) const
{
#define MODTC (2097152) // 1<<(3*7)
const int64_t tc = (_monotonic_cnt + timestamp) & 0x001FFFFF;
const int64_t ti = (buf[5] & 0x7f)
| ((buf[6] & 0x7f) << 7)
| ((buf[7] & 0x7f) << 14)
| ((buf[8] & 0x7f) << 21);
const int64_t tdiff = (MODTC + tc - ti) % MODTC;
#ifdef DEBUG_MIDIDM
printf("MTC DELAY: #%5"PRId64" dt:%6"PRId64" [spl] (%6"PRId64" - %8"PRId64") @(%8"PRId64" + %d)\n",
_cnt_total, tdiff, tc, ti, _monotonic_cnt, timestamp);
#endif
return tdiff;
}
int MIDIDM::process (pframes_t nframes, PortEngine &pe, void *midi_in, void *midi_out)
{
/* send midi event */
pe.midi_clear(midi_out);
#ifndef USE_MTC // use 3-byte song position
uint8_t obuf[3];
obuf[0] = 0xf2;
obuf[1] = (_monotonic_cnt) & 0x7f;
obuf[2] = (_monotonic_cnt >> 7) & 0x7f;
pe.midi_event_put (midi_out, 0, obuf, 3);
#else // sysex MTC frame
uint8_t obuf[10];
obuf[0] = 0xf0;
obuf[1] = 0x7f;
obuf[2] = 0x7f;
obuf[3] = 0x01;
obuf[4] = 0x01;
obuf[9] = 0xf7;
obuf[5] = (_monotonic_cnt ) & 0x7f;
obuf[6] = (_monotonic_cnt >> 7) & 0x7f;
obuf[7] = (_monotonic_cnt >> 14) & 0x7f;
obuf[8] = (_monotonic_cnt >> 21) & 0x7f;
pe.midi_event_put (midi_out, 0, obuf, 10);
#endif
/* process incoming */
const pframes_t nevents = pe.get_midi_event_count (midi_in);
#ifdef DEBUG_MIDIDM
printf("MIDI SEND: @%8"PRId64", recv: %d systime:%"PRId64"\n", _monotonic_cnt, nevents, g_get_monotonic_time());
#endif
for (pframes_t n = 0; n < nevents; ++n) {
pframes_t timestamp;
size_t size;
uint8_t* buf;
int64_t tdiff;
pe.midi_event_get (timestamp, size, &buf, midi_in, n);
if (size == 3 && buf[0] == 0xf2 )
{
tdiff = parse_mclk(buf, timestamp);
} else if (size == 10 && buf[0] == 0xf0)
{
tdiff = parse_mtc(buf, timestamp);
}
else
{
continue;
}
_last_signal_tme = _monotonic_cnt;
/* running variance */
if (_cnt_total == 0) {
_var_m = tdiff;
} else {
const double var_m1 = _var_m;
_var_m = _var_m + ((double)tdiff - _var_m) / (double)(_cnt_total + 1);
_var_s = _var_s + ((double)tdiff - _var_m) * ((double)tdiff - var_m1);
}
/* average and mix/max */
++_cnt_total;
_dly_total += tdiff;
_avg_delay = _dly_total / _cnt_total;
if (tdiff < _min_delay) _min_delay = tdiff;
if (tdiff > _max_delay) _max_delay = tdiff;
}
_monotonic_cnt += nframes;
return 0;
}
|