Fix completely-broken Time::scale.
authorCarl Hetherington <cth@carlh.net>
Fri, 21 Jul 2017 12:06:07 +0000 (13:06 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 21 Jul 2017 12:06:07 +0000 (13:06 +0100)
src/sub_time.cc
test/time_test.cc

index adaab315d44ebb59d84c63dcf30ce0f6c5fc0332..763b4e44aba4e1389f118ba401fd3a0670bf3031 100644 (file)
@@ -169,7 +169,7 @@ Time::from_frames (int f, Rational 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 */
@@ -190,10 +190,7 @@ Time::scale (float f)
        SUB_ASSERT (_rate->denominator != 0);
        SUB_ASSERT (_rate->integer ());
 
-       _seconds = rint (_seconds * f);
-       _frames = rint (_frames * f);
-       if (_frames >= _rate->integer_fraction()) {
-               _frames -= _rate->integer_fraction ();
-               ++_seconds;
-       }
+       double const s = Time::all_as_seconds() * f;
+       _seconds = floor (s);
+       _frames = rint ((s - _seconds) * _rate->fraction());
 }
index 3fa9cb5882938a93fe907fd0e24297ead37448d8..d103efc580eedb754e5f20c1bcae579c31f08c73 100644 (file)
@@ -81,3 +81,19 @@ BOOST_AUTO_TEST_CASE (time_add_test)
        t.add (sub::Time::from_frames(54641, sub::Rational(24, 1)));
        BOOST_CHECK_EQUAL (t, sub::Time::from_hmsf (0, 42, 4, 916, sub::Rational(1000, 1)));
 }
+
+BOOST_AUTO_TEST_CASE (time_scale_test1)
+{
+       sub::Time t = sub::Time::from_hmsf (0, 0, 1, 0, sub::Rational(1000, 1));
+       t.scale (0.96);
+       BOOST_CHECK_EQUAL (t.seconds(), 0);
+       BOOST_CHECK_EQUAL (t.frames_at(sub::Rational(1000, 1)), 960);
+}
+
+BOOST_AUTO_TEST_CASE (time_scale_test2)
+{
+       sub::Time t = sub::Time::from_hmsf (0, 0, 2, 0, sub::Rational(1000, 1));
+       t.scale (0.96);
+       BOOST_CHECK_EQUAL (t.seconds(), 1);
+       BOOST_CHECK_EQUAL (t.frames_at(sub::Rational(1000, 1)), 920);
+}