summaryrefslogtreecommitdiff
path: root/libs/midi++2/mtc.cc
blob: 25d118372e385ec1ada440313487099869f8a007 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (C) 2004-2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2008-2012 David Robillard <d@drobilla.net>
 * Copyright (C) 2014-2019 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <iostream>

#include "midi++/types.h"
#include "midi++/parser.h"
#include "midi++/port.h"
#include "midi++/mmc.h"
#include "pbd/transmitter.h"

using namespace std;
using namespace sigc;
using namespace MIDI;

#undef DEBUG_MTC

bool
Parser::possible_mtc (MIDI::byte *sysex_buf, size_t msglen)
{
	byte fake_mtc_time[5];

	if (msglen != 10 || sysex_buf[0] != 0xf0 || sysex_buf[1] != 0x7f || sysex_buf[3] != 0x01 || sysex_buf[4] != 0x01) {
		return false;
	}

        /* full MTC */

	fake_mtc_time[0] = sysex_buf[8]; // frames
	fake_mtc_time[1] = sysex_buf[7]; // minutes
	fake_mtc_time[2] = sysex_buf[6]; // seconds
	fake_mtc_time[3] = (sysex_buf[5] & 0x1f); // hours

	_mtc_fps = MTC_FPS ((sysex_buf[5] & 0x60) >> 5); // fps
	fake_mtc_time[4] = (byte) _mtc_fps;

	/* wait for first quarter frame, which could indicate forwards
	   or backwards ...
	*/

	reset_mtc_state ();

	/* emit signals */

	mtc (*this, &sysex_buf[1], msglen - 1);
	mtc_time (fake_mtc_time, true, _timestamp);
#ifdef DEBUG_MTC
	cerr << "New full-MTC message marks state stopped" << endl;
#endif
	mtc_status (MTC_Stopped);

	return true;
}

void
Parser::reset_mtc_state ()
{
#ifdef DEBUG_MTC
	cerr << "MTC state reset" << endl;
#endif
	/* MUST REMAIN RT-SAFE */

	_mtc_forward = false;
	_mtc_running = MTC_Stopped;
	_mtc_locked = false;
	expected_mtc_quarter_frame_code = 0;
	memset (_mtc_time, 0, sizeof (_mtc_time));
	memset (_qtr_mtc_time, 0, sizeof (_mtc_time));
	consecutive_qtr_frame_cnt = 0;
	last_qtr_frame = 0;
}

void
Parser::process_mtc_quarter_frame (MIDI::byte *msg)
{
	int which_quarter_frame = (msg[1] & 0xf0) >> 4;

	/* Is it an expected frame?
	   Remember, the first can be frame 7 or frame 0,
	   depending on the direction of the MTC generator ...
	*/

#ifdef DEBUG_MTC
	 cerr << "MTC: (state = " << _mtc_running << ") "
	      << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code
	      << " consecutive ? " << consecutive_qtr_frame_cnt
	      << endl;
#endif

	if (_mtc_running == MTC_Stopped) {

		/* we are stopped but are seeing qtr frame messages */

		if (consecutive_qtr_frame_cnt == 0) {

			/* first quarter frame */

			if (which_quarter_frame != 0 && which_quarter_frame != 7) {

				last_qtr_frame = which_quarter_frame;
				consecutive_qtr_frame_cnt++;
			}

			// cerr << "first seen qframe = " << (int) last_qtr_frame << endl;

			return;

		} else if (consecutive_qtr_frame_cnt == 1) {

			/* third quarter frame */

#ifdef DEBUG_MTC
			cerr << "second seen qframe = " << (int) which_quarter_frame << endl;
#endif
			if (last_qtr_frame < which_quarter_frame) {
				_mtc_running = MTC_Forward;
			} else if (last_qtr_frame > which_quarter_frame) {
				_mtc_running = MTC_Backward;
			}
#ifdef DEBUG_MTC
			cerr << "Send MTC status as " << _mtc_running << endl;
#endif
			mtc_status (_mtc_running);
		}

		switch (_mtc_running) {
		case MTC_Forward:
			if (which_quarter_frame == 7) {
				expected_mtc_quarter_frame_code = 0;
			} else {
				expected_mtc_quarter_frame_code = which_quarter_frame + 1;
			}
			break;

		case MTC_Backward:
			if (which_quarter_frame == 0) {
				expected_mtc_quarter_frame_code = 7;

			} else {
				expected_mtc_quarter_frame_code = which_quarter_frame - 1;
			}
			break;

		case MTC_Stopped:
			break;
		}

	} else {

		/* already running */

// for testing bad MIDI connections etc.
//		if ((random() % 500) < 10) {

		if (which_quarter_frame != expected_mtc_quarter_frame_code) {

			consecutive_qtr_frame_cnt = 0;

#ifdef DEBUG_MTC
			cerr << "MTC: (state = " << _mtc_running << ") "
			     << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code << endl;
#endif

			/* tell listener(s) that we skipped. if they return
			   true, just ignore this in terms of it being an error.
			*/

			boost::optional<bool> res = mtc_skipped ();

			if (res.value_or (false)) {

				/* no error, reset next expected frame */

				switch (_mtc_running) {
				case MTC_Forward:
					if (which_quarter_frame == 7) {
						expected_mtc_quarter_frame_code = 0;
					} else {
						expected_mtc_quarter_frame_code = which_quarter_frame + 1;
					}
					break;

				case MTC_Backward:
					if (which_quarter_frame == 0) {
						expected_mtc_quarter_frame_code = 7;

					} else {
						expected_mtc_quarter_frame_code = which_quarter_frame - 1;
					}
					break;

				case MTC_Stopped:
					break;
				}

#ifdef DEBUG_MTC
				cerr << "SKIPPED, next expected = " << expected_mtc_quarter_frame_code << endl;
#endif
				return;
			}

			/* skip counts as an error ... go back to waiting for the first frame */

#ifdef DEBUG_MTC
			cerr << "Skipped MTC qtr frame, return to stopped state" << endl;
#endif
			reset_mtc_state ();
			mtc_status (MTC_Stopped);

			return;

		} else {

			/* received qtr frame matched expected */
			consecutive_qtr_frame_cnt++;

		}
	}

	/* time code is looking good */

#ifdef DEBUG_MTC
	cerr << "for quarter frame " << which_quarter_frame << " byte = " << hex << (int) msg[1] << dec << endl;
#endif

	switch (which_quarter_frame) {
	case 0: // frames LS nibble
		_qtr_mtc_time[0] |= msg[1] & 0xf;
		break;

	case 1:  // frames MS nibble
		_qtr_mtc_time[0] |= (msg[1] & 0xf)<<4;
		break;

	case 2: // seconds LS nibble
		_qtr_mtc_time[1] |= msg[1] & 0xf;
		break;

	case 3: // seconds MS nibble
		_qtr_mtc_time[1] |= (msg[1] & 0xf)<<4;
		break;

	case 4: // minutes LS nibble
		_qtr_mtc_time[2] |= msg[1] & 0xf;
		break;

	case 5: // minutes MS nibble
		_qtr_mtc_time[2] |= (msg[1] & 0xf)<<4;
		break;

	case 6: // hours LS nibble
		_qtr_mtc_time[3] |= msg[1] & 0xf;
		break;

	case 7:

		/* last quarter frame msg has the MS bit of
		   the hour in bit 0, and the SMPTE FPS type
		   in bits 5 and 6
		*/

		_qtr_mtc_time[3] |= ((msg[1] & 0x1) << 4);
		_mtc_fps = MTC_FPS ((msg[1] & 0x6) >> 1);
		_qtr_mtc_time[4] = _mtc_fps;
		break;

	default:
		abort(); /*NOTREACHED*/
		break;

	}

#ifdef DEBUG_MTC
	cerr << "Emit MTC Qtr\n";
#endif

	mtc_qtr (*this, which_quarter_frame, _timestamp); /* EMIT_SIGNAL */

	// mtc (*this, &msg[1], msglen - 1);

	switch (_mtc_running) {
	case MTC_Forward:
		if (which_quarter_frame == 7) {

			/* we've reached the final of 8 quarter frame messages.
			   store the time, reset the pending time holder,
			   and signal anyone who wants to know the time.
			*/

			if (consecutive_qtr_frame_cnt >= 8) {
				memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
				memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
				if (!_mtc_locked) {
					_mtc_locked = true;
				}

				mtc_time (_mtc_time, false, _timestamp);
			}
			expected_mtc_quarter_frame_code = 0;

		} else {
			expected_mtc_quarter_frame_code = which_quarter_frame + 1;
		}
		break;

	case MTC_Backward:
		if (which_quarter_frame == 0) {

			/* we've reached the final of 8 quarter frame messages.
			   store the time, reset the pending time holder,
			   and signal anyone who wants to know the time.
			*/

			if (consecutive_qtr_frame_cnt >= 8) {
				memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
				memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
				if (!_mtc_locked) {
					_mtc_locked = true;
				}
				mtc_time (_mtc_time, false, _timestamp);
			}

			expected_mtc_quarter_frame_code = 7;

		} else {
			expected_mtc_quarter_frame_code = which_quarter_frame - 1;
		}
		break;

	default:
		break;
	}

}