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[5];
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
53 fake_mtc_time[4] = (byte) _mtc_fps;
55 /* wait for first quarter frame, which could indicate forwards
63 mtc (*this, &sysex_buf[1], msglen - 1);
64 mtc_time (fake_mtc_time, true);
65 mtc_status (MTC_Stopped);
71 Parser::reset_mtc_state ()
74 _mtc_running = MTC_Stopped;
76 expected_mtc_quarter_frame_code = 0;
77 memset (_mtc_time, 0, sizeof (_mtc_time));
78 memset (_qtr_mtc_time, 0, sizeof (_mtc_time));
79 consecutive_qtr_frame_cnt = 0;
84 Parser::process_mtc_quarter_frame (byte *msg)
86 int which_quarter_frame = (msg[1] & 0xf0) >> 4;
88 /* Is it an expected frame?
89 Remember, the first can be frame 7 or frame 0,
90 depending on the direction of the MTC generator ...
94 cerr << "MTC: (state = " << _mtc_running << ") "
95 << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code
96 << " consecutive ? " << consecutive_qtr_frame_cnt
100 if (_mtc_running == MTC_Stopped) {
102 /* we are stopped but are seeing qtr frame messages */
104 if (consecutive_qtr_frame_cnt == 0) {
106 /* first quarter frame */
108 if (which_quarter_frame != 0 && which_quarter_frame != 7) {
110 last_qtr_frame = which_quarter_frame;
111 consecutive_qtr_frame_cnt++;
114 // cerr << "first seen qframe = " << (int) last_qtr_frame << endl;
118 } else if (consecutive_qtr_frame_cnt == 1) {
120 /* third quarter frame */
122 // cerr << "second seen qframe = " << (int) which_quarter_frame << endl;
124 if (last_qtr_frame < which_quarter_frame) {
125 _mtc_running = MTC_Forward;
126 } else if (last_qtr_frame > which_quarter_frame) {
127 _mtc_running = MTC_Backward;
130 mtc_status (_mtc_running);
133 switch (_mtc_running) {
135 if (which_quarter_frame == 7) {
136 expected_mtc_quarter_frame_code = 0;
138 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
143 if (which_quarter_frame == 0) {
144 expected_mtc_quarter_frame_code = 7;
147 expected_mtc_quarter_frame_code = which_quarter_frame - 1;
157 /* already running */
159 // for testing bad MIDI connections etc.
160 // if ((random() % 500) < 10) {
162 if (which_quarter_frame != expected_mtc_quarter_frame_code) {
164 consecutive_qtr_frame_cnt = 0;
167 cerr << "MTC: (state = " << _mtc_running << ") "
168 << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code << endl;
171 /* tell listener(s) that we skipped. if they return
172 true, just ignore this in terms of it being an error.
175 if (1) { /* mtc_skipped () */
177 /* no error, reset next expected frame */
179 switch (_mtc_running) {
181 if (which_quarter_frame == 7) {
182 expected_mtc_quarter_frame_code = 0;
184 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
189 if (which_quarter_frame == 0) {
190 expected_mtc_quarter_frame_code = 7;
193 expected_mtc_quarter_frame_code = which_quarter_frame - 1;
202 cerr << "SKIPPED, next expected = " << expected_mtc_quarter_frame_code << endl;
207 /* go back to waiting for the first frame */
209 expected_mtc_quarter_frame_code = 0;
210 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
212 _mtc_running = MTC_Stopped;
214 mtc_status (MTC_Stopped);
220 /* received qtr frame matched expected */
221 consecutive_qtr_frame_cnt++;
226 /* time code is looking good */
228 switch (which_quarter_frame) {
229 case 0: // frames LS nibble
230 _qtr_mtc_time[0] |= msg[1] & 0xf;
233 case 1: // frames MS nibble
234 _qtr_mtc_time[0] |= (msg[1] & 0xf)<<4;
237 case 2: // seconds LS nibble
238 _qtr_mtc_time[1] |= msg[1] & 0xf;
241 case 3: // seconds MS nibble
242 _qtr_mtc_time[1] |= (msg[1] & 0xf)<<4;
245 case 4: // minutes LS nibble
246 _qtr_mtc_time[2] |= msg[1] & 0xf;
249 case 5: // minutes MS nibble
250 _qtr_mtc_time[2] |= (msg[1] & 0xf)<<4;
253 case 6: // hours LS nibble
254 _qtr_mtc_time[3] |= msg[1] & 0xf;
259 /* last quarter frame msg has the MS bit of
260 the hour in bit 0, and the SMPTE FPS type
264 _qtr_mtc_time[3] |= ((msg[1] & 0x1) << 4);
265 _mtc_fps = MTC_FPS ((msg[1] & 0x6) >> 1);
266 _qtr_mtc_time[4] = _mtc_fps;
275 mtc_qtr (*this); /* EMIT_SIGNAL */
277 // mtc (*this, &msg[1], msglen - 1);
279 switch (_mtc_running) {
281 if ((which_quarter_frame == 7)) {
283 /* we've reached the final of 8 quarter frame messages.
284 store the time, reset the pending time holder,
285 and signal anyone who wants to know the time.
288 if (consecutive_qtr_frame_cnt >= 8) {
289 memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
290 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
295 mtc_time (_mtc_time, false);
297 expected_mtc_quarter_frame_code = 0;
300 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
305 if (which_quarter_frame == 0) {
307 /* we've reached the final of 8 quarter frame messages.
308 store the time, reset the pending time holder,
309 and signal anyone who wants to know the time.
312 if (consecutive_qtr_frame_cnt >= 8) {
313 memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
314 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
318 mtc_time (_mtc_time, false);
321 expected_mtc_quarter_frame_code = 7;
324 expected_mtc_quarter_frame_code = which_quarter_frame - 1;