Some improvements to performance with crossfades: don't recompute a whole track's...
[ardour.git] / libs / ardour / mtc_slave.cc
index 0caa86196a868571463338d0919a90ad07ab4132..587d6007b3e2ae7adab2a499c6bfd6ae6a8f2472 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
-
+#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;
@@ -42,6 +42,10 @@ using namespace PBD;
 MTC_Slave::MTC_Slave (Session& s, MIDI::Port& p) 
        : session (s)
 {
+       can_notify_on_unknown_rate = true;
+
+       last_mtc_fps_byte = session.get_mtc_smpte_bits ();
+
        rebind (p);
        reset ();
 }
@@ -65,7 +69,7 @@ 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();
@@ -94,8 +98,43 @@ MTC_Slave::update_mtc_time (const byte *msg, bool was_full)
        smpte.minutes = msg[2];
        smpte.seconds = msg[1];
        smpte.frames = msg[0];
+
+       last_mtc_fps_byte = msg[4];
        
-       session.smpte_to_sample( smpte, mtc_frame, true, false );
+       switch (msg[4]) {
+       case MTC_24_FPS:
+               smpte.rate = 24;
+               smpte.drop = false;
+               can_notify_on_unknown_rate = true;
+               break;
+       case MTC_25_FPS:
+               smpte.rate = 25;
+               smpte.drop = false;
+               can_notify_on_unknown_rate = true;
+               break;
+       case MTC_30_FPS_DROP:
+               smpte.rate = 30;
+               smpte.drop = true;
+               can_notify_on_unknown_rate = true;
+               break;
+       case MTC_30_FPS:
+               smpte.rate = 30;
+               smpte.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;
+               }
+               smpte.rate = session.smpte_frames_per_second();
+               smpte.drop = session.smpte_drop_frames();
+       }
+
+       session.smpte_to_sample (smpte, mtc_frame, true, false);
        
        if (was_full) {
                
@@ -105,6 +144,7 @@ MTC_Slave::update_mtc_time (const byte *msg, bool was_full)
                current.guard2++;        
                
                session.request_locate (mtc_frame, false);       
+               session.request_transport_speed (0);
                update_mtc_status (MIDI::Parser::MTC_Stopped);   
 
                reset ();
@@ -137,8 +177,9 @@ MTC_Slave::update_mtc_time (const byte *msg, bool was_full)
 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];
@@ -217,13 +258,13 @@ MTC_Slave::ok() const
 }
 
 bool 
-MTC_Slave::speed_and_position (float& speed, nframes_t& pos)
+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);
 
@@ -239,6 +280,7 @@ MTC_Slave::speed_and_position (float& speed, nframes_t& pos)
                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;
@@ -246,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;
 
@@ -256,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];
@@ -293,7 +335,7 @@ 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();