Skeleton tests for BBT_Time.
[ardour.git] / libs / ardour / mtc_slave.cc
index d241636785008bee2a830344aed0a6e5dda15fbb..4034ef32be3c46be1aa821b1a6552941a85e05e3 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2002-4 Paul Davis 
+    Copyright (C) 2002-4 Paul Davis
 
     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
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 */
-
+#include <iostream>
 #include <errno.h>
 #include <poll.h>
 #include <sys/types.h>
 #include <unistd.h>
-#include <pbd/error.h>
-#include <pbd/failed_constructor.h>
-#include <pbd/pthread_utils.h>
+#include "pbd/error.h"
+#include "pbd/failed_constructor.h"
+#include "pbd/pthread_utils.h"
 
-#include <midi++/port.h>
-#include <ardour/slave.h>
-#include <ardour/session.h>
-#include <ardour/audioengine.h>
-#include <ardour/cycles.h>
+#include "midi++/port.h"
+#include "ardour/slave.h"
+#include "ardour/session.h"
+#include "ardour/audioengine.h"
+#include "ardour/cycles.h"
 
 #include "i18n.h"
 
+using namespace std;
 using namespace ARDOUR;
 using namespace sigc;
 using namespace MIDI;
 using namespace PBD;
 
-MTC_Slave::MTC_Slave (Session& s, MIDI::Port& p) 
+MTC_Slave::MTC_Slave (Session& s, MIDI::Port& p)
        : session (s)
 {
+       can_notify_on_unknown_rate = true;
+
+       last_mtc_fps_byte = session.get_mtc_timecode_bits ();
+
        rebind (p);
        reset ();
 }
@@ -64,14 +69,14 @@ MTC_Slave::rebind (MIDI::Port& p)
 }
 
 void
-MTC_Slave::update_mtc_qtr (Parser& p)
+MTC_Slave::update_mtc_qtr (Parser& /*p*/)
 {
        cycles_t cnow = get_cycles ();
        nframes_t now = session.engine().frame_time();
        nframes_t qtr;
        static cycles_t last_qtr = 0;
 
-       qtr = (long) (session.frames_per_smpte_frame() / 4);
+       qtr = (long) (session.frames_per_timecode_frame() / 4);
        mtc_frame += qtr;
        last_qtr = cnow;
 
@@ -87,57 +92,94 @@ void
 MTC_Slave::update_mtc_time (const byte *msg, bool was_full)
 {
        nframes_t now = session.engine().frame_time();
-       SMPTE::Time smpte;
-       
-       smpte.hours = msg[3];
-       smpte.minutes = msg[2];
-       smpte.seconds = msg[1];
-       smpte.frames = msg[0];
-       
-       session.smpte_to_sample( smpte, mtc_frame, true, false );
-       
+       Timecode::Time timecode;
+
+       timecode.hours = msg[3];
+       timecode.minutes = msg[2];
+       timecode.seconds = msg[1];
+       timecode.frames = msg[0];
+
+       last_mtc_fps_byte = msg[4];
+
+       switch (msg[4]) {
+       case MTC_24_FPS:
+               timecode.rate = 24;
+               timecode.drop = false;
+               can_notify_on_unknown_rate = true;
+               break;
+       case MTC_25_FPS:
+               timecode.rate = 25;
+               timecode.drop = false;
+               can_notify_on_unknown_rate = true;
+               break;
+       case MTC_30_FPS_DROP:
+               timecode.rate = 30;
+               timecode.drop = true;
+               can_notify_on_unknown_rate = true;
+               break;
+       case MTC_30_FPS:
+               timecode.rate = 30;
+               timecode.drop = false;
+               can_notify_on_unknown_rate = true;
+               break;
+       default:
+               /* throttle error messages about unknown MTC rates */
+               if (can_notify_on_unknown_rate) {
+                       error << string_compose (_("Unknown rate/drop value %1 in incoming MTC stream, session values used instead"),
+                                                (int) msg[4])
+                             << endmsg;
+                       can_notify_on_unknown_rate = false;
+               }
+               timecode.rate = session.timecode_frames_per_second();
+               timecode.drop = session.timecode_drop_frames();
+       }
+
+       session.timecode_to_sample (timecode, mtc_frame, true, false);
+
        if (was_full) {
-               
-               current.guard1++;        
-               current.position = mtc_frame;    
-               current.timestamp = 0;   
-               current.guard2++;        
-               
-               session.request_locate (mtc_frame, false);       
-               update_mtc_status (MIDI::Parser::MTC_Stopped);   
-
-               reset ();
-               
+
+               current.guard1++;
+               current.position = mtc_frame;
+               current.timestamp = 0;
+               current.guard2++;
+
+               session.request_locate (mtc_frame, false);
+               session.request_transport_speed (0);
+               update_mtc_status (MIDI::Parser::MTC_Stopped);
+
+               reset ();
+
        } else {
-               
+
                /* We received the last quarter frame 7 quarter frames (1.75 mtc
                   frames) after the instance when the contents of the mtc quarter
                   frames were decided. Add time to compensate for the elapsed 1.75
                   frames.
-                  Also compensate for audio latency. 
+                  Also compensate for audio latency.
                */
 
-               mtc_frame += (long) (1.75 * session.frames_per_smpte_frame()) + session.worst_output_latency();
-               
+               mtc_frame += (long) (1.75 * session.frames_per_timecode_frame()) + session.worst_output_latency();
+
                if (first_mtc_frame == 0) {
                        first_mtc_frame = mtc_frame;
                        first_mtc_time = now;
-               } 
-               
+               }
+
                current.guard1++;
                current.position = mtc_frame;
                current.timestamp = now;
                current.guard2++;
        }
-       
+
        last_inbound_frame = now;
 }
 
 void
 MTC_Slave::handle_locate (const MIDI::byte* mmc_tc)
 {
-       MIDI::byte mtc[4];
-       
+       MIDI::byte mtc[5];
+
+       mtc[4] = last_mtc_fps_byte;
        mtc[3] = mmc_tc[0] & 0xf; /* hrs only */
        mtc[2] = mmc_tc[1];
        mtc[1] = mmc_tc[2];
@@ -215,14 +257,14 @@ MTC_Slave::ok() const
        return true;
 }
 
-bool 
-MTC_Slave::speed_and_position (float& speed, nframes_t& pos)
+bool
+MTC_Slave::speed_and_position (double& speed, nframes_t& pos)
 {
        nframes_t now = session.engine().frame_time();
        SafeTime last;
        nframes_t frame_rate;
        nframes_t elapsed;
-       float speed_now;
+       double speed_now;
 
        read_current (&last);
 
@@ -231,13 +273,14 @@ MTC_Slave::speed_and_position (float& speed, nframes_t& pos)
                pos = last.position;
                return true;
        }
-       
+
        /* no timecode for 1/4 second ? conclude that its stopped */
 
        if (last_inbound_frame && now > last_inbound_frame && now - last_inbound_frame > session.frame_rate() / 4) {
                mtc_speed = 0;
                pos = last.position;
                session.request_locate (pos, false);
+               session.request_transport_speed (0);
                update_mtc_status (MIDI::Parser::MTC_Stopped);
                reset();
                return false;
@@ -245,7 +288,7 @@ MTC_Slave::speed_and_position (float& speed, nframes_t& pos)
 
        frame_rate = session.frame_rate();
 
-       speed_now = (float) ((last.position - first_mtc_frame) / (double) (now - first_mtc_time));
+       speed_now = (double) ((last.position - first_mtc_frame) / (double) (now - first_mtc_time));
 
        accumulator[accumulator_index++] = speed_now;
 
@@ -255,7 +298,7 @@ MTC_Slave::speed_and_position (float& speed, nframes_t& pos)
        }
 
        if (have_first_accumulated_speed) {
-               float total = 0;
+               double total = 0;
 
                for (int32_t i = 0; i < accumulator_size; ++i) {
                        total += accumulator[i];
@@ -274,9 +317,9 @@ MTC_Slave::speed_and_position (float& speed, nframes_t& pos)
                elapsed = 0;
 
        } else {
-       
+
                /* scale elapsed time by the current MTC speed */
-               
+
                if (last.timestamp && (now > last.timestamp)) {
                        elapsed = (nframes_t) floor (mtc_speed * (now - last.timestamp));
                } else {
@@ -292,10 +335,10 @@ MTC_Slave::speed_and_position (float& speed, nframes_t& pos)
        return true;
 }
 
-nframes_t
+ARDOUR::nframes_t
 MTC_Slave::resolution() const
 {
-       return (nframes_t) session.frames_per_smpte_frame();
+       return (nframes_t) session.frames_per_timecode_frame();
 }
 
 void
@@ -305,9 +348,9 @@ MTC_Slave::reset ()
           be being updated as we call this. but this
           supposed to be a realtime-safe call.
        */
-       
+
        port->input()->reset_mtc_state ();
-       
+
        last_inbound_frame = 0;
        current.guard1++;
        current.position = 0;