2 Copyright (C) 2004 Paul Barton-Davis
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.
27 #include <midi++/types.h>
28 #include <midi++/parser.h>
29 #include <midi++/port.h>
30 #include <midi++/mmc.h>
31 #include <pbd/transmitter.h>
38 Parser::possible_mtc (byte *sysex_buf, size_t msglen)
40 byte fake_mtc_time[5];
42 if (msglen != 10 || sysex_buf[0] != 0xf0 || sysex_buf[1] != 0x7f || sysex_buf[3] != 0x01 || sysex_buf[4] != 0x01) {
48 fake_mtc_time[0] = sysex_buf[8]; // frames
49 fake_mtc_time[1] = sysex_buf[7]; // minutes
50 fake_mtc_time[2] = sysex_buf[6]; // seconds
51 fake_mtc_time[3] = (sysex_buf[5] & 0x1f); // hours
53 _mtc_fps = MTC_FPS ((sysex_buf[5] & 0x60) >> 5); // fps
55 fake_mtc_time[4] = (byte) _mtc_fps;
57 /* wait for first quarter frame, which could indicate forwards
65 mtc (*this, &sysex_buf[1], msglen - 1);
66 mtc_time (fake_mtc_time, true);
67 mtc_status (MTC_Stopped);
73 Parser::reset_mtc_state ()
76 _mtc_running = MTC_Stopped;
78 expected_mtc_quarter_frame_code = 0;
79 memset (_mtc_time, 0, sizeof (_mtc_time));
80 memset (_qtr_mtc_time, 0, sizeof (_mtc_time));
81 consecutive_qtr_frame_cnt = 0;
86 Parser::process_mtc_quarter_frame (byte *msg)
88 int which_quarter_frame = (msg[1] & 0xf0) >> 4;
90 /* Is it an expected frame?
91 Remember, the first can be frame 7 or frame 0,
92 depending on the direction of the MTC generator ...
96 cerr << "MTC: (state = " << _mtc_running << ") "
97 << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code
98 << " consecutive ? " << consecutive_qtr_frame_cnt
102 if (_mtc_running == MTC_Stopped) {
104 /* we are stopped but are seeing qtr frame messages */
106 if (consecutive_qtr_frame_cnt == 0) {
108 /* first quarter frame */
110 if (which_quarter_frame != 0 && which_quarter_frame != 7) {
112 last_qtr_frame = which_quarter_frame;
113 consecutive_qtr_frame_cnt++;
116 // cerr << "first seen qframe = " << (int) last_qtr_frame << endl;
120 } else if (consecutive_qtr_frame_cnt == 1) {
122 /* third quarter frame */
124 // cerr << "second seen qframe = " << (int) which_quarter_frame << endl;
126 if (last_qtr_frame < which_quarter_frame) {
127 _mtc_running = MTC_Forward;
128 } else if (last_qtr_frame > which_quarter_frame) {
129 _mtc_running = MTC_Backward;
132 mtc_status (_mtc_running);
135 switch (_mtc_running) {
137 if (which_quarter_frame == 7) {
138 expected_mtc_quarter_frame_code = 0;
140 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
145 if (which_quarter_frame == 0) {
146 expected_mtc_quarter_frame_code = 7;
149 expected_mtc_quarter_frame_code = which_quarter_frame - 1;
159 /* already running */
161 // for testing bad MIDI connections etc.
162 // if ((random() % 500) < 10) {
164 if (which_quarter_frame != expected_mtc_quarter_frame_code) {
166 consecutive_qtr_frame_cnt = 0;
169 cerr << "MTC: (state = " << _mtc_running << ") "
170 << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code << endl;
173 /* tell listener(s) that we skipped. if they return
174 true, just ignore this in terms of it being an error.
177 if (1) { /* mtc_skipped () */
179 /* no error, reset next expected frame */
181 switch (_mtc_running) {
183 if (which_quarter_frame == 7) {
184 expected_mtc_quarter_frame_code = 0;
186 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
191 if (which_quarter_frame == 0) {
192 expected_mtc_quarter_frame_code = 7;
195 expected_mtc_quarter_frame_code = which_quarter_frame - 1;
204 cerr << "SKIPPED, next expected = " << expected_mtc_quarter_frame_code << endl;
209 /* go back to waiting for the first frame */
211 expected_mtc_quarter_frame_code = 0;
212 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
214 _mtc_running = MTC_Stopped;
216 mtc_status (MTC_Stopped);
222 /* received qtr frame matched expected */
223 consecutive_qtr_frame_cnt++;
228 /* time code is looking good */
230 switch (which_quarter_frame) {
231 case 0: // frames LS nibble
232 _qtr_mtc_time[0] |= msg[1] & 0xf;
235 case 1: // frames MS nibble
236 _qtr_mtc_time[0] |= (msg[1] & 0xf)<<4;
239 case 2: // seconds LS nibble
240 _qtr_mtc_time[1] |= msg[1] & 0xf;
243 case 3: // seconds MS nibble
244 _qtr_mtc_time[1] |= (msg[1] & 0xf)<<4;
247 case 4: // minutes LS nibble
248 _qtr_mtc_time[2] |= msg[1] & 0xf;
251 case 5: // minutes MS nibble
252 _qtr_mtc_time[2] |= (msg[1] & 0xf)<<4;
255 case 6: // hours LS nibble
256 _qtr_mtc_time[3] |= msg[1] & 0xf;
261 /* last quarter frame msg has the MS bit of
262 the hour in bit 0, and the SMPTE FPS type
266 _qtr_mtc_time[3] |= ((msg[1] & 0x1) << 4);
267 _mtc_fps = MTC_FPS ((msg[1] & 0x6) >> 1);
268 _qtr_mtc_time[4] = _mtc_fps;
277 mtc_qtr (*this); /* EMIT_SIGNAL */
279 // mtc (*this, &msg[1], msglen - 1);
281 switch (_mtc_running) {
283 if ((which_quarter_frame == 7)) {
285 /* we've reached the final of 8 quarter frame messages.
286 store the time, reset the pending time holder,
287 and signal anyone who wants to know the time.
290 if (consecutive_qtr_frame_cnt >= 8) {
291 memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
292 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
296 mtc_time (_mtc_time, false);
298 expected_mtc_quarter_frame_code = 0;
301 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
306 if (which_quarter_frame == 0) {
308 /* we've reached the final of 8 quarter frame messages.
309 store the time, reset the pending time holder,
310 and signal anyone who wants to know the time.
313 if (consecutive_qtr_frame_cnt >= 8) {
314 memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
315 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
319 mtc_time (_mtc_time, false);
322 expected_mtc_quarter_frame_code = 7;
325 expected_mtc_quarter_frame_code = which_quarter_frame - 1;