Bump libdcp for build fixes.
[libsub.git] / src / sub_time.cc
index 68dac19e40af64bb3d48ac7c4c6b7a5e7f55947b..763b4e44aba4e1389f118ba401fd3a0670bf3031 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include "sub_time.h"
+#include "sub_assert.h"
 #include "exceptions.h"
 #include <cmath>
 #include <iomanip>
@@ -154,8 +155,42 @@ Time::from_hms (int h, int m, int s, int ms)
        return Time (h * 3600 + m * 60 + s, ms, Rational (1000, 1));
 }
 
+/** Create a Time from a number of frames.
+ *  rate must be integer.
+ */
+Time
+Time::from_frames (int f, Rational rate)
+{
+       SUB_ASSERT (rate.denominator != 0);
+       SUB_ASSERT (rate.integer ());
+       return Time (f / rate.integer_fraction(), f % rate.integer_fraction(), rate);
+}
+
 double
 Time::all_as_seconds () const
 {
-       return _seconds + double(milliseconds ()) / 1000;
+       return _seconds + double(milliseconds()) / 1000;
+}
+
+/** Add a time to this one.  Both *this and t must have a specified _rate */
+void
+Time::add (Time t)
+{
+       SUB_ASSERT (_rate);
+       SUB_ASSERT (t._rate);
+
+       Rational result_rate = max (*_rate, *t._rate);
+       *this = Time::from_frames((all_as_seconds() + t.all_as_seconds()) * result_rate.fraction(), result_rate);
+}
+
+void
+Time::scale (float f)
+{
+       SUB_ASSERT (_rate);
+       SUB_ASSERT (_rate->denominator != 0);
+       SUB_ASSERT (_rate->integer ());
+
+       double const s = Time::all_as_seconds() * f;
+       _seconds = floor (s);
+       _frames = rint ((s - _seconds) * _rate->fraction());
 }