bd3205a8c1768fff0c68d8352d48215fffbabdd3
[ardour.git] / libs / midi++2 / mtc.cc
1 /*
2     Copyright (C) 2004 Paul Barton-Davis
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
21 #include <cstdlib>
22 #include <unistd.h>
23 #include <cstring>
24 #include <iostream>
25
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"
31
32 using namespace std;
33 using namespace sigc;
34 using namespace MIDI;
35
36 bool
37 Parser::possible_mtc (byte *sysex_buf, size_t msglen)
38 {
39         byte fake_mtc_time[5];
40
41         if (msglen != 10 || sysex_buf[0] != 0xf0 || sysex_buf[1] != 0x7f || sysex_buf[3] != 0x01 || sysex_buf[4] != 0x01) {
42                 return false;
43         }
44
45         /* full MTC */
46         
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
51         
52         _mtc_fps = MTC_FPS ((sysex_buf[5] & 0x60) >> 5); // fps
53         fake_mtc_time[4] = (byte) _mtc_fps;
54
55         /* wait for first quarter frame, which could indicate forwards
56            or backwards ...
57         */
58
59         reset_mtc_state ();
60
61         /* emit signals */
62
63         mtc (*this, &sysex_buf[1], msglen - 1);
64         mtc_time (fake_mtc_time, true);
65         mtc_status (MTC_Stopped);
66
67         return true;
68 }
69
70 void
71 Parser::reset_mtc_state ()
72 {
73         _mtc_forward = false;
74         _mtc_running = MTC_Stopped;
75         _mtc_locked = false;
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;
80         last_qtr_frame = 0;
81 }
82
83 void
84 Parser::process_mtc_quarter_frame (byte *msg)
85 {
86         int which_quarter_frame = (msg[1] & 0xf0) >> 4;
87
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 ...
91         */
92
93 #define DEBUG_MTC
94 #ifdef DEBUG_MTC
95          cerr << "MTC: (state = " << _mtc_running << ") " 
96               << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code
97               << " consecutive ? " << consecutive_qtr_frame_cnt
98               << endl;
99 #endif
100
101         if (_mtc_running == MTC_Stopped) {
102         
103                 /* we are stopped but are seeing qtr frame messages */
104
105                 if (consecutive_qtr_frame_cnt == 0) {
106
107                         /* first quarter frame */
108
109                         if (which_quarter_frame != 0 && which_quarter_frame != 7) {
110                                 
111                                 last_qtr_frame = which_quarter_frame;
112                                 consecutive_qtr_frame_cnt++;
113                         }
114                         
115                         cerr << "first seen qframe = " << (int) last_qtr_frame << endl;
116
117                         return;
118
119                 } else if (consecutive_qtr_frame_cnt == 1) {
120
121                         /* third quarter frame */
122                         
123                         cerr << "second seen qframe = " << (int) which_quarter_frame << endl;
124
125                         if (last_qtr_frame < which_quarter_frame) {
126                                 _mtc_running = MTC_Forward;
127                         } else if (last_qtr_frame > which_quarter_frame) {
128                                 _mtc_running = MTC_Backward;
129                         }
130                         
131                         mtc_status (_mtc_running);
132                 } 
133
134                 switch (_mtc_running) {
135                 case MTC_Forward:
136                         if (which_quarter_frame == 7) {
137                                 expected_mtc_quarter_frame_code = 0;
138                         } else {
139                                 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
140                         }
141                         break;
142
143                 case MTC_Backward:
144                         if (which_quarter_frame == 0) {
145                                 expected_mtc_quarter_frame_code = 7;
146                                 
147                         } else {
148                                 expected_mtc_quarter_frame_code = which_quarter_frame - 1;
149                         }
150                         break;
151
152                 case MTC_Stopped:
153                         break;
154                 }
155                 
156         } else {
157                 
158                 /* already running */
159
160 // for testing bad MIDI connections etc.
161 //              if ((random() % 500) < 10) {
162
163                 if (which_quarter_frame != expected_mtc_quarter_frame_code) {
164
165                         consecutive_qtr_frame_cnt = 0;
166
167 #ifdef DEBUG_MTC
168                         cerr << "MTC: (state = " << _mtc_running << ") " 
169                              << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code << endl;
170 #endif
171
172                         /* tell listener(s) that we skipped. if they return
173                            true, just ignore this in terms of it being an error.
174                         */
175
176                         if (1) { /* mtc_skipped () */
177
178                                 /* no error, reset next expected frame */
179
180                                 switch (_mtc_running) {
181                                 case MTC_Forward:
182                                         if (which_quarter_frame == 7) {
183                                                 expected_mtc_quarter_frame_code = 0;
184                                         } else {
185                                                 expected_mtc_quarter_frame_code = which_quarter_frame + 1;
186                                         }
187                                         break;
188
189                                 case MTC_Backward:
190                                         if (which_quarter_frame == 0) {
191                                                 expected_mtc_quarter_frame_code = 7;
192                                                 
193                                         } else {
194                                                 expected_mtc_quarter_frame_code = which_quarter_frame - 1;
195                                         }
196                                         break;
197
198                                 case MTC_Stopped:
199                                         break;
200                                 }
201
202 #ifdef DEBUG_MTC
203                                 cerr << "SKIPPED, next expected = " << expected_mtc_quarter_frame_code << endl;
204 #endif                          
205                                 return;
206                         }
207
208                         /* go back to waiting for the first frame */
209
210                         expected_mtc_quarter_frame_code = 0;
211                         memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
212
213                         _mtc_running = MTC_Stopped;
214                         _mtc_locked = false;
215                         mtc_status (MTC_Stopped);
216                         
217                         return;
218
219                 } else {
220
221                         /* received qtr frame matched expected */
222                         consecutive_qtr_frame_cnt++;
223
224                 }
225         }
226
227         /* time code is looking good */
228
229 #ifdef DEBUG_MTC
230         cerr << "for quarter frame " << which_quarter_frame << " byte = " << hex << (int) msg[1] << dec << endl;
231 #endif
232
233         switch (which_quarter_frame) {
234         case 0: // frames LS nibble
235                 _qtr_mtc_time[0] |= msg[1] & 0xf;
236                 break;
237
238         case 1:  // frames MS nibble
239                 _qtr_mtc_time[0] |= (msg[1] & 0xf)<<4;
240                 break;
241
242         case 2: // seconds LS nibble
243                 _qtr_mtc_time[1] |= msg[1] & 0xf;
244                 break;
245
246         case 3: // seconds MS nibble
247                 _qtr_mtc_time[1] |= (msg[1] & 0xf)<<4;
248                 break;
249
250         case 4: // minutes LS nibble
251                 _qtr_mtc_time[2] |= msg[1] & 0xf;
252                 break;
253
254         case 5: // minutes MS nibble
255                 _qtr_mtc_time[2] |= (msg[1] & 0xf)<<4;
256                 break;
257                 
258         case 6: // hours LS nibble
259                 _qtr_mtc_time[3] |= msg[1] & 0xf;
260                 break;
261
262         case 7: 
263                 
264                 /* last quarter frame msg has the MS bit of
265                    the hour in bit 0, and the SMPTE FPS type
266                    in bits 5 and 6 
267                 */
268                 
269                 _qtr_mtc_time[3] |= ((msg[1] & 0x1) << 4);
270                 _mtc_fps = MTC_FPS ((msg[1] & 0x6) >> 1);
271                 _qtr_mtc_time[4] = _mtc_fps;
272                 break;
273
274         default:
275                 /*NOTREACHED*/
276                 break;
277
278         } 
279         
280         mtc_qtr (*this, which_quarter_frame); /* EMIT_SIGNAL */
281
282         // mtc (*this, &msg[1], msglen - 1);
283
284         switch (_mtc_running) {
285         case MTC_Forward:
286                 if ((which_quarter_frame == 7)) {
287                         
288                         /* we've reached the final of 8 quarter frame messages.
289                            store the time, reset the pending time holder,
290                            and signal anyone who wants to know the time.
291                         */
292                         
293                         if (consecutive_qtr_frame_cnt >= 8) {
294                                 cerr << hex;
295                                 for (size_t xx = 0; xx < sizeof (_qtr_mtc_time); ++xx) {
296                                         cerr << (int) _qtr_mtc_time[xx] << ' ';
297                                 } 
298                                 cerr << dec << endl;
299                                 memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
300                                 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
301                                 if (!_mtc_locked) {
302                                         _mtc_locked = true;
303                                 }
304
305                                 mtc_time (_mtc_time, false);
306                         }
307                         expected_mtc_quarter_frame_code = 0;
308                         
309                 } else {
310                         expected_mtc_quarter_frame_code = which_quarter_frame + 1;
311                 }
312                 break;
313                 
314         case MTC_Backward:
315                 if (which_quarter_frame == 0) {
316                         
317                         /* we've reached the final of 8 quarter frame messages.
318                            store the time, reset the pending time holder,
319                            and signal anyone who wants to know the time.
320                         */
321
322                         if (consecutive_qtr_frame_cnt >= 8) {
323                                 memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
324                                 memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
325                                 if (!_mtc_locked) {
326                                         _mtc_locked = true;
327                                 }
328                                 mtc_time (_mtc_time, false);
329                         }
330
331                         expected_mtc_quarter_frame_code = 7;
332
333                 } else {
334                         expected_mtc_quarter_frame_code = which_quarter_frame - 1;
335                 }
336                 break;
337
338         default:
339                 break;
340         }
341
342 }