2008-06-02 17:41:35 -04:00
|
|
|
/*
|
2019-08-02 23:10:55 -04:00
|
|
|
* 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.
|
|
|
|
*/
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
2008-12-12 09:43:24 -05:00
|
|
|
#include <cstring>
|
2008-06-02 17:41:35 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "midi++/types.h"
|
|
|
|
#include "midi++/parser.h"
|
|
|
|
#include "midi++/port.h"
|
|
|
|
#include "midi++/mmc.h"
|
|
|
|
#include "pbd/transmitter.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace sigc;
|
|
|
|
using namespace MIDI;
|
|
|
|
|
2010-01-27 13:50:24 -05:00
|
|
|
#undef DEBUG_MTC
|
2010-01-26 17:23:15 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
bool
|
2013-07-25 02:19:51 -04:00
|
|
|
Parser::possible_mtc (MIDI::byte *sysex_buf, size_t msglen)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
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 */
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
fake_mtc_time[0] = sysex_buf[8]; // frames
|
2008-06-02 17:41:35 -04:00
|
|
|
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
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
_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);
|
2009-12-01 13:31:00 -05:00
|
|
|
mtc_time (fake_mtc_time, true, _timestamp);
|
2010-01-26 17:23:15 -05:00
|
|
|
#ifdef DEBUG_MTC
|
|
|
|
cerr << "New full-MTC message marks state stopped" << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
mtc_status (MTC_Stopped);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Parser::reset_mtc_state ()
|
|
|
|
{
|
2010-01-26 17:23:15 -05:00
|
|
|
#ifdef DEBUG_MTC
|
|
|
|
cerr << "MTC state reset" << endl;
|
|
|
|
#endif
|
2009-12-03 13:44:06 -05:00
|
|
|
/* MUST REMAIN RT-SAFE */
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
_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));
|
2019-04-07 18:18:08 -04:00
|
|
|
consecutive_qtr_frame_cnt = 0;
|
|
|
|
last_qtr_frame = 0;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-25 02:19:51 -04:00
|
|
|
Parser::process_mtc_quarter_frame (MIDI::byte *msg)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
int which_quarter_frame = (msg[1] & 0xf0) >> 4;
|
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
/* Is it an expected frame?
|
|
|
|
Remember, the first can be frame 7 or frame 0,
|
2008-06-02 17:41:35 -04:00
|
|
|
depending on the direction of the MTC generator ...
|
|
|
|
*/
|
|
|
|
|
2009-11-30 14:11:04 -05:00
|
|
|
#ifdef DEBUG_MTC
|
2015-10-04 14:51:05 -04:00
|
|
|
cerr << "MTC: (state = " << _mtc_running << ") "
|
2008-06-02 17:41:35 -04:00
|
|
|
<< which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code
|
2019-04-07 18:18:08 -04:00
|
|
|
<< " consecutive ? " << consecutive_qtr_frame_cnt
|
2008-06-02 17:41:35 -04:00
|
|
|
<< endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (_mtc_running == MTC_Stopped) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
/* we are stopped but are seeing qtr frame messages */
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
if (consecutive_qtr_frame_cnt == 0) {
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
/* first quarter frame */
|
|
|
|
|
|
|
|
if (which_quarter_frame != 0 && which_quarter_frame != 7) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
last_qtr_frame = which_quarter_frame;
|
|
|
|
consecutive_qtr_frame_cnt++;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
// cerr << "first seen qframe = " << (int) last_qtr_frame << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
} else if (consecutive_qtr_frame_cnt == 1) {
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
/* third quarter frame */
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2010-01-26 19:17:01 -05:00
|
|
|
#ifdef DEBUG_MTC
|
|
|
|
cerr << "second seen qframe = " << (int) which_quarter_frame << endl;
|
|
|
|
#endif
|
2019-04-07 18:18:08 -04:00
|
|
|
if (last_qtr_frame < which_quarter_frame) {
|
2008-06-02 17:41:35 -04:00
|
|
|
_mtc_running = MTC_Forward;
|
2019-04-07 18:18:08 -04:00
|
|
|
} else if (last_qtr_frame > which_quarter_frame) {
|
2008-06-02 17:41:35 -04:00
|
|
|
_mtc_running = MTC_Backward;
|
|
|
|
}
|
2010-01-26 19:17:01 -05:00
|
|
|
#ifdef DEBUG_MTC
|
|
|
|
cerr << "Send MTC status as " << _mtc_running << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
mtc_status (_mtc_running);
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
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;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
|
|
|
expected_mtc_quarter_frame_code = which_quarter_frame - 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTC_Stopped:
|
|
|
|
break;
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* already running */
|
|
|
|
|
|
|
|
// for testing bad MIDI connections etc.
|
|
|
|
// if ((random() % 500) < 10) {
|
|
|
|
|
|
|
|
if (which_quarter_frame != expected_mtc_quarter_frame_code) {
|
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
consecutive_qtr_frame_cnt = 0;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_MTC
|
2015-10-04 14:51:05 -04:00
|
|
|
cerr << "MTC: (state = " << _mtc_running << ") "
|
2008-06-02 17:41:35 -04:00
|
|
|
<< 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.
|
|
|
|
*/
|
|
|
|
|
2024-10-18 20:23:13 -04:00
|
|
|
std::optional<bool> res = mtc_skipped ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2019-11-21 11:48:56 -05:00
|
|
|
if (res.value_or (false)) {
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2019-04-07 21:46:02 -04:00
|
|
|
/* no error, reset next expected frame */
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
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;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
} 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;
|
2015-10-05 10:17:49 -04:00
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:09 -04:00
|
|
|
/* skip counts as an error ... go back to waiting for the first frame */
|
2010-01-26 17:23:15 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG_MTC
|
2019-04-07 21:46:02 -04:00
|
|
|
cerr << "Skipped MTC qtr frame, return to stopped state" << endl;
|
2010-01-26 17:23:15 -05:00
|
|
|
#endif
|
2010-01-26 22:26:41 -05:00
|
|
|
reset_mtc_state ();
|
2008-06-02 17:41:35 -04:00
|
|
|
mtc_status (MTC_Stopped);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2019-04-07 21:46:02 -04:00
|
|
|
/* received qtr frame matched expected */
|
2019-04-07 18:18:08 -04:00
|
|
|
consecutive_qtr_frame_cnt++;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* time code is looking good */
|
|
|
|
|
2009-12-01 08:24:08 -05:00
|
|
|
#ifdef DEBUG_MTC
|
2013-08-12 05:40:11 -04:00
|
|
|
cerr << "for quarter frame " << which_quarter_frame << " byte = " << hex << (int) msg[1] << dec << endl;
|
2009-12-01 08:24:08 -05:00
|
|
|
#endif
|
2009-11-30 14:11:04 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
switch (which_quarter_frame) {
|
2019-04-07 18:18:08 -04:00
|
|
|
case 0: // frames LS nibble
|
2008-06-02 17:41:35 -04:00
|
|
|
_qtr_mtc_time[0] |= msg[1] & 0xf;
|
|
|
|
break;
|
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
case 1: // frames MS nibble
|
2008-06-02 17:41:35 -04:00
|
|
|
_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;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case 6: // hours LS nibble
|
|
|
|
_qtr_mtc_time[3] |= msg[1] & 0xf;
|
|
|
|
break;
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
case 7:
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* last quarter frame msg has the MS bit of
|
|
|
|
the hour in bit 0, and the SMPTE FPS type
|
2015-10-04 14:51:05 -04:00
|
|
|
in bits 5 and 6
|
2008-06-02 17:41:35 -04:00
|
|
|
*/
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
_qtr_mtc_time[3] |= ((msg[1] & 0x1) << 4);
|
|
|
|
_mtc_fps = MTC_FPS ((msg[1] & 0x6) >> 1);
|
|
|
|
_qtr_mtc_time[4] = _mtc_fps;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-11-14 04:47:43 -05:00
|
|
|
abort(); /*NOTREACHED*/
|
2008-06-02 17:41:35 -04:00
|
|
|
break;
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-02 16:26:26 -05:00
|
|
|
#ifdef DEBUG_MTC
|
|
|
|
cerr << "Emit MTC Qtr\n";
|
|
|
|
#endif
|
|
|
|
|
2009-12-01 13:31:00 -05:00
|
|
|
mtc_qtr (*this, which_quarter_frame, _timestamp); /* EMIT_SIGNAL */
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
// mtc (*this, &msg[1], msglen - 1);
|
|
|
|
|
|
|
|
switch (_mtc_running) {
|
|
|
|
case MTC_Forward:
|
2012-05-23 22:54:10 -04:00
|
|
|
if (which_quarter_frame == 7) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* 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.
|
|
|
|
*/
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
if (consecutive_qtr_frame_cnt >= 8) {
|
2008-06-02 17:41:35 -04:00
|
|
|
memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
|
|
|
|
memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
|
|
|
|
if (!_mtc_locked) {
|
|
|
|
_mtc_locked = true;
|
|
|
|
}
|
2008-12-12 09:43:24 -05:00
|
|
|
|
2009-12-01 13:31:00 -05:00
|
|
|
mtc_time (_mtc_time, false, _timestamp);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
expected_mtc_quarter_frame_code = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
|
|
|
expected_mtc_quarter_frame_code = which_quarter_frame + 1;
|
|
|
|
}
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MTC_Backward:
|
|
|
|
if (which_quarter_frame == 0) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2019-04-07 18:18:08 -04:00
|
|
|
if (consecutive_qtr_frame_cnt >= 8) {
|
2008-06-02 17:41:35 -04:00
|
|
|
memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
|
|
|
|
memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
|
|
|
|
if (!_mtc_locked) {
|
|
|
|
_mtc_locked = true;
|
|
|
|
}
|
2009-12-01 13:31:00 -05:00
|
|
|
mtc_time (_mtc_time, false, _timestamp);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
expected_mtc_quarter_frame_code = 7;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
expected_mtc_quarter_frame_code = which_quarter_frame - 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|