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.
26 #include <midi++/types.h>
27 #include <midi++/parser.h>
28 #include <midi++/port.h>
29 #include <midi++/mmc.h>
30 #include <pbd/transmitter.h>
37 Parser::possible_mtc (byte *sysex_buf, size_t msglen)
39 byte fake_mtc_time[4];
41 if (msglen != 10 || sysex_buf[0] != 0xf0 || sysex_buf[1] != 0x7f || sysex_buf[3] != 0x01 || sysex_buf[4] != 0x01) {
47 fake_mtc_time[0] = sysex_buf[8]; // frames
48 fake_mtc_time[1] = sysex_buf[7]; // minutes
49 fake_mtc_time[2] = sysex_buf[6]; // seconds
50 fake_mtc_time[3] = (sysex_buf[5] & 0x1f); // hours
52 _mtc_fps = MTC_FPS ((sysex_buf[5] & 0x60) >> 5); // fps
54 /* wait for first quarter frame, which could indicate forwards
62 mtc (*this, &sysex_buf[1], msglen - 1);
63 mtc_time (fake_mtc_time, true);
64 mtc_status (MTC_Stopped);
70 Parser::reset_mtc_state ()
73 _mtc_running = MTC_Stopped;
75 expected_mtc_quarter_frame_code = 0;
76 memset (_mtc_time, 0, sizeof (_mtc_time));
77 memset (_qtr_mtc_time, 0, sizeof (_mtc_time));
78 consecutive_qtr_frame_cnt = 0;
83 Parser::process_mtc_quarter_frame (byte *msg)
85 int which_quarter_frame = (msg[1] & 0xf0) >> 4;
87 /* Is it an expected frame?
88 Remember, the first can be frame 7 or frame 0,
89 depending on the direction of the MTC generator ...
93 cerr << "MTC: (state = " << _mtc_running << ") "
94 << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code
95 << " consecutive ? " << consecutive_qtr_frame_cnt
99 if (_mtc_running == MTC_Stopped) {
101 /* we are stopped but are seeing qtr frame messages */
103 if (consecutive_qtr_frame_cnt == 0) {
105 /* first quarter frame */
107 if (which_quarter_frame != 0 && which_quarter_frame != 7) {
109 last_qtr_frame = which_quarter_frame;
110 consecutive_qtr_frame_cnt++;
113 // cerr << "first seen qframe = " << (int) last_qtr_frame << endl;
117 } else if (consecutive_qtr_frame_cnt == 1) {
119 /* third quarter frame */
121 // cerr << "second seen qframe = " << (int) which_quarter_frame << endl;
123 if (last_qtr_frame < which_quarter_frame) {
124 _mtc_running = MTC_Forward;
125 } else if (last_qtr_frame > which_quarter_frame) {
126 _mtc_running = MTC_Backward;
129 mtc_status (_mtc_running);
132 switch (_mtc_running) {
134 if (which_quarter_frame == 7) {
135 expected_mtc_quarter_frame_code = 0;
137 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
142 if (which_quarter_frame == 0) {
143 expected_mtc_quarter_frame_code = 7;
146 expected_mtc_quarter_frame_code = which_quarter_frame - 1;
156 /* already running */
158 // for testing bad MIDI connections etc.
159 // if ((random() % 500) < 10) {
161 if (which_quarter_frame != expected_mtc_quarter_frame_code) {
163 consecutive_qtr_frame_cnt = 0;
166 cerr << "MTC: (state = " << _mtc_running << ") "
167 << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code << endl;
170 /* tell listener(s) that we skipped. if they return
171 true, just ignore this in terms of it being an error.
174 if (1) { /* mtc_skipped () */
176 /* no error, reset next expected frame */
178 switch (_mtc_running) {
180 if (which_quarter_frame == 7) {
181 expected_mtc_quarter_frame_code = 0;
183 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
188 if (which_quarter_frame == 0) {
189 expected_mtc_quarter_frame_code = 7;
192 expected_mtc_quarter_frame_code = which_quarter_frame - 1;
201 cerr << "SKIPPED, next expected = " << expected_mtc_quarter_frame_code << endl;
206 /* go back to waiting for the first frame */
208 expected_mtc_quarter_frame_code = 0;
209 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
211 _mtc_running = MTC_Stopped;
213 mtc_status (MTC_Stopped);
219 /* received qtr frame matched expected */
220 consecutive_qtr_frame_cnt++;
225 /* time code is looking good */
227 switch (which_quarter_frame) {
228 case 0: // frames LS nibble
229 _qtr_mtc_time[0] |= msg[1] & 0xf;
232 case 1: // frames MS nibble
233 _qtr_mtc_time[0] |= (msg[1] & 0xf)<<4;
236 case 2: // seconds LS nibble
237 _qtr_mtc_time[1] |= msg[1] & 0xf;
240 case 3: // seconds MS nibble
241 _qtr_mtc_time[1] |= (msg[1] & 0xf)<<4;
244 case 4: // minutes LS nibble
245 _qtr_mtc_time[2] |= msg[1] & 0xf;
248 case 5: // minutes MS nibble
249 _qtr_mtc_time[2] |= (msg[1] & 0xf)<<4;
252 case 6: // hours LS nibble
253 _qtr_mtc_time[3] |= msg[1] & 0xf;
258 /* last quarter frame msg has the MS bit of
259 the hour in bit 0, and the SMPTE FPS type
263 _qtr_mtc_time[3] |= ((msg[1] & 0x1) << 4);
264 _mtc_fps = MTC_FPS ((msg[1] & 0x6) >> 1);
273 mtc_qtr (*this); /* EMIT_SIGNAL */
275 // mtc (*this, &msg[1], msglen - 1);
277 switch (_mtc_running) {
279 if ((which_quarter_frame == 7)) {
281 /* we've reached the final of 8 quarter frame messages.
282 store the time, reset the pending time holder,
283 and signal anyone who wants to know the time.
286 if (consecutive_qtr_frame_cnt >= 8) {
287 memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
288 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
292 mtc_time (_mtc_time, false);
294 expected_mtc_quarter_frame_code = 0;
297 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
302 if (which_quarter_frame == 0) {
304 /* we've reached the final of 8 quarter frame messages.
305 store the time, reset the pending time holder,
306 and signal anyone who wants to know the time.
309 if (consecutive_qtr_frame_cnt >= 8) {
310 memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
311 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
315 mtc_time (_mtc_time, false);
318 expected_mtc_quarter_frame_code = 7;
321 expected_mtc_quarter_frame_code = which_quarter_frame - 1;