Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / dcpomatic_time.h
index 05b4e1a5d7d1d2061fe26327068632e03c79c78b..a5a0684e591b104656985e8dfcf427b19100c703 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
 
     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
 
 */
 
+/** @file  src/lib/dcpomatic_time.h
+ *  @brief Types to describe time.
+ */
+
 #ifndef DCPOMATIC_TIME_H
 #define DCPOMATIC_TIME_H
 
-#include <cmath>
-#include <stdint.h>
 #include "frame_rate_change.h"
+#include "safe_stringstream.h"
+#include "dcpomatic_assert.h"
+#include <stdint.h>
+#include <cmath>
+#include <ostream>
+#include <iomanip>
 
 class dcpomatic_round_up_test;
 
-class Time;
-
-/** A time in seconds, expressed as a number scaled up by Time::HZ. */
+/** A time in seconds, expressed as a number scaled up by Time::HZ.  We want two different
+ *  versions of this class, ContentTime and DCPTime, and we want it to be impossible to
+ *  convert implicitly between the two.  Hence there's this template hack.  I'm not
+ *  sure if it's the best way to do it.
+ *
+ *  S is the name of `this' class and O is its opposite (see the typedefs below).
+ */
+template <class S, class O>
 class Time
 {
 public:
@@ -36,179 +49,215 @@ public:
                : _t (0)
        {}
 
-       explicit Time (int64_t t)
+       typedef int64_t Type;
+
+       explicit Time (Type t)
                : _t (t)
        {}
 
-       virtual ~Time () {}
-
-       int64_t get () const {
-               return _t;
-       }
-
-       double seconds () const {
-               return double (_t) / HZ;
-       }
+       explicit Time (Type n, Type d)
+               : _t (n * HZ / d)
+       {}
 
-       template <typename T>
-       int64_t frames (T r) const {
-               return rint (_t * r / HZ);
-       }
+       /* Explicit conversion from type O */
+       Time (Time<O, S> d, FrameRateChange f);
 
-       operator bool () const {
-               return _t != 0;
+       Type get () const {
+               return _t;
        }
 
-protected:
-       friend class dcptime_round_up_test;
-       
-       int64_t _t;
-       static const int HZ = 96000;
-};
-
-class DCPTime;
-
-class ContentTime : public Time
-{
-public:
-       ContentTime () : Time () {}
-       explicit ContentTime (int64_t t) : Time (t) {}
-       ContentTime (int64_t n, int64_t d) : Time (n * HZ / d) {}
-       ContentTime (DCPTime d, FrameRateChange f);
-
-       bool operator< (ContentTime const & o) const {
+       bool operator< (Time<S, O> const & o) const {
                return _t < o._t;
        }
 
-       bool operator<= (ContentTime const & o) const {
+       bool operator<= (Time<S, O> const & o) const {
                return _t <= o._t;
        }
 
-       bool operator== (ContentTime const & o) const {
+       bool operator== (Time<S, O> const & o) const {
                return _t == o._t;
        }
 
-       bool operator!= (ContentTime const & o) const {
+       bool operator!= (Time<S, O> const & o) const {
                return _t != o._t;
        }
 
-       bool operator>= (ContentTime const & o) const {
+       bool operator>= (Time<S, O> const & o) const {
                return _t >= o._t;
        }
 
-       bool operator> (ContentTime const & o) const {
+       bool operator> (Time<S, O> const & o) const {
                return _t > o._t;
        }
 
-       ContentTime operator+ (ContentTime const & o) const {
-               return ContentTime (_t + o._t);
+       Time<S, O> operator+ (Time<S, O> const & o) const {
+               return Time<S, O> (_t + o._t);
        }
 
-       ContentTime & operator+= (ContentTime const & o) {
+       Time<S, O> & operator+= (Time<S, O> const & o) {
                _t += o._t;
                return *this;
        }
 
-       ContentTime operator- (ContentTime const & o) const {
-               return ContentTime (_t - o._t);
+       Time<S, O> operator- () const {
+               return Time<S, O> (-_t);
        }
 
-       ContentTime & operator-= (ContentTime const & o) {
+       Time<S, O> operator- (Time<S, O> const & o) const {
+               return Time<S, O> (_t - o._t);
+       }
+
+       Time<S, O> & operator-= (Time<S, O> const & o) {
                _t -= o._t;
                return *this;
        }
 
-       static ContentTime from_seconds (double s) {
-               return ContentTime (s * HZ);
+       /** Round up to the nearest sampling interval
+        *  at some sampling rate.
+        *  @param r Sampling rate.
+        */
+       Time<S, O> round_up (float r) const {
+               Type const n = llrintf (HZ / r);
+               Type const a = _t + n - 1;
+               return Time<S, O> (a - (a % n));
        }
 
-       template <class T>
-       static ContentTime from_frames (int64_t f, T r) {
-               return ContentTime (f * HZ / r);
+       double seconds () const {
+               return double (_t) / HZ;
        }
-};
 
-class DCPTime : public Time
-{
-public:
-       DCPTime () : Time () {}
-       explicit DCPTime (int64_t t) : Time (t) {}
-       DCPTime (ContentTime t, FrameRateChange c) : Time (rint (t.get() / c.speed_up)) {}
-
-       bool operator< (DCPTime const & o) const {
-               return _t < o._t;
+       Time<S, O> abs () const {
+               return Time<S, O> (std::abs (_t));
        }
 
-       bool operator<= (DCPTime const & o) const {
-               return _t <= o._t;
+       template <typename T>
+       int64_t frames_round (T r) const {
+               return llrint (_t * r / HZ);
        }
 
-       bool operator== (DCPTime const & o) const {
-               return _t == o._t;
+       template <typename T>
+       int64_t frames_floor (T r) const {
+               return floor (_t * r / HZ);
        }
 
-       bool operator!= (DCPTime const & o) const {
-               return _t != o._t;
+       template <typename T>
+       int64_t frames_ceil (T r) const {
+               return ceil (_t * r / HZ);
        }
 
-       bool operator>= (DCPTime const & o) const {
-               return _t >= o._t;
-       }
+       /** @param r Frames per second */
+       template <typename T>
+       void split (T r, int& h, int& m, int& s, int& f) const
+       {
+               /* Do this calculation with frames so that we can round
+                  to a frame boundary at the start rather than the end.
+               */
+               int64_t ff = frames_round (r);
 
-       bool operator> (DCPTime const & o) const {
-               return _t > o._t;
-       }
+               h = ff / (3600 * r);
+               ff -= h * 3600 * r;
+               m = ff / (60 * r);
+               ff -= m * 60 * r;
+               s = ff / r;
+               ff -= s * r;
 
-       DCPTime operator+ (DCPTime const & o) const {
-               return DCPTime (_t + o._t);
+               f = static_cast<int> (ff);
        }
 
-       DCPTime & operator+= (DCPTime const & o) {
-               _t += o._t;
-               return *this;
-       }
+       template <typename T>
+       std::string timecode (T r) const {
+               int h;
+               int m;
+               int s;
+               int f;
+               split (r, h, m, s, f);
 
-       DCPTime operator- (DCPTime const & o) const {
-               return DCPTime (_t - o._t);
+               SafeStringStream o;
+               o.width (2);
+               o.fill ('0');
+               o << std::setw(2) << std::setfill('0') << h << ":"
+                 << std::setw(2) << std::setfill('0') << m << ":"
+                 << std::setw(2) << std::setfill('0') << s << ":"
+                 << std::setw(2) << std::setfill('0') << f;
+               return o.str ();
        }
 
-       DCPTime & operator-= (DCPTime const & o) {
-               _t -= o._t;
-               return *this;
+
+       static Time<S, O> from_seconds (double s) {
+               return Time<S, O> (llrint (s * HZ));
        }
 
-       /** Round up to the nearest sampling interval
-        *  at some sampling rate.
-        *  @param r Sampling rate.
-        */
-       DCPTime round_up (int r) {
-               int64_t const n = HZ / r;
-               int64_t const a = _t + n - 1;
-               return DCPTime (a - (a % n));
+       template <class T>
+       static Time<S, O> from_frames (int64_t f, T r) {
+               DCPOMATIC_ASSERT (r > 0);
+               return Time<S, O> (f * HZ / r);
        }
 
-       DCPTime abs () const {
-               return DCPTime (std::abs (_t));
+       static Time<S, O> delta () {
+               return Time<S, O> (1);
        }
 
-       static DCPTime from_seconds (double s) {
-               return DCPTime (s * HZ);
+       static Time<S, O> min () {
+               return Time<S, O> (-INT64_MAX);
        }
 
-       template <class T>
-       static DCPTime from_frames (int64_t f, T r) {
-               return DCPTime (f * HZ / r);
+       static Time<S, O> max () {
+               return Time<S, O> (INT64_MAX);
        }
 
-       static DCPTime delta () {
-               return DCPTime (1);
+private:
+       friend struct dcptime_round_up_test;
+
+       Type _t;
+       static const int HZ = 96000;
+};
+
+class ContentTimeDifferentiator {};
+class DCPTimeDifferentiator {};
+
+/* Specializations for the two allowed explicit conversions */
+
+template<>
+Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (Time<DCPTimeDifferentiator, ContentTimeDifferentiator> d, FrameRateChange f);
+
+template<>
+Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (Time<ContentTimeDifferentiator, DCPTimeDifferentiator> d, FrameRateChange f);
+
+/** Time relative to the start or position of a piece of content in its native frame rate */
+typedef Time<ContentTimeDifferentiator, DCPTimeDifferentiator> ContentTime;
+/** Time relative to the start of the output DCP in its frame rate */
+typedef Time<DCPTimeDifferentiator, ContentTimeDifferentiator> DCPTime;
+
+class ContentTimePeriod
+{
+public:
+       ContentTimePeriod () {}
+
+       ContentTimePeriod (ContentTime f, ContentTime t)
+               : from (f)
+               , to (t)
+       {}
+
+       ContentTime from;
+       ContentTime to;
+
+       ContentTimePeriod operator+ (ContentTime const & o) const {
+               return ContentTimePeriod (from + o, to + o);
        }
 
-       static DCPTime max () {
-               return DCPTime (INT64_MAX);
+       bool overlaps (ContentTimePeriod const & o) const;
+       bool contains (ContentTime const & o) const;
+
+       bool operator== (ContentTimePeriod const & o) const {
+               return from == o.from && to == o.to;
        }
 };
 
 DCPTime min (DCPTime a, DCPTime b);
+DCPTime max (DCPTime a, DCPTime b);
+ContentTime min (ContentTime a, ContentTime b);
+ContentTime max (ContentTime a, ContentTime b);
+std::ostream& operator<< (std::ostream& s, ContentTime t);
+std::ostream& operator<< (std::ostream& s, DCPTime t);
 
 #endif