From 69eb659d4e5676fe10fb925bf679a9535239309c Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 22 Jan 2011 18:33:15 +0100 Subject: [PATCH] refactor to extract a special "flip representation" operation --- src/lib/time/formats.hpp | 2 +- src/lib/time/timecode.cpp | 83 ++++++++++++++++++--------------------- src/lib/time/timecode.hpp | 3 ++ 3 files changed, 42 insertions(+), 46 deletions(-) diff --git a/src/lib/time/formats.hpp b/src/lib/time/formats.hpp index abe07eca4..b82a8f631 100644 --- a/src/lib/time/formats.hpp +++ b/src/lib/time/formats.hpp @@ -84,7 +84,7 @@ namespace time { static void rebuild (SmpteTC&, QuantR, TimeValue const&); static TimeValue evaluate (SmpteTC const&, QuantR); static uint getFramerate (QuantR, TimeValue const&); - static void rangeLimitStrategy (SmpteTC&, int rawHours); + static void rangeLimitStrategy (SmpteTC&); }; diff --git a/src/lib/time/timecode.cpp b/src/lib/time/timecode.cpp index 070e8d1eb..dd4b67015 100644 --- a/src/lib/time/timecode.cpp +++ b/src/lib/time/timecode.cpp @@ -71,7 +71,7 @@ namespace time { { tc.clear(); tc.frames = quantiser.gridPoint (rawTime); - // will automatically wrap over to the secs, minutes and hour fields + // will automatically wrap over to the seconds, minutes and hour fields } /** calculate the time point denoted by this SMPTE timecode, @@ -79,7 +79,7 @@ namespace time { TimeValue Smpte::evaluate (SmpteTC const& tc, QuantR quantiser) { - uint frameRate = tc.getFps(); + long frameRate = tc.getFps(); long gridPoint = tc.frames + tc.secs * frameRate + tc.mins * frameRate * 60 @@ -131,49 +131,10 @@ namespace time { * orientation (0:0:0:0 - 1sec = -1:59:59:0) */ void - Smpte::rangeLimitStrategy (SmpteTC& tc, int hours) + Smpte::rangeLimitStrategy (SmpteTC& tc) { - if (hours >= 0) return; // no need to flip representation - - tc.sgn *= hours; // transfer sign into the sign field - hours = abs(hours); - - REQUIRE (0 <= tc.frames && uint(tc.frames) < tc.getFps()); - REQUIRE (0 <= tc.secs && tc.secs < 60 ); - REQUIRE (0 <= tc.mins && tc.mins < 60 ); - - // sign flip was detected: - // switch orientation of all timecode fields - // i.e. -h + (m+s+f) becomes - (h+m+s+f) - uint fr (tc.getFps() - tc.frames); - uint secs (60 - tc.secs); - uint mins (60 - tc.mins); - - ASSERT (fr <= tc.getFps()); - ASSERT (0 < secs); - if (fr < tc.getFps()) - --secs; - else - fr = 0; - - ASSERT (secs <= 60); - ASSERT (0 < mins); - if (secs < 60) - --mins; - else - secs = 0; - - ASSERT (mins <= 60); - ASSERT (0 < hours); - if (mins < 60) - --hours; - else - mins = 0; - - tc.frames.setValueRaw (fr); - tc.secs.setValueRaw (secs); - tc.mins.setValueRaw (mins); - tc.hours.setValueRaw (hours); + if (tc.hours < 0) + tc.invertOrientation(); } } @@ -210,7 +171,7 @@ namespace time { wrapHours (SmpteTC* thisTC, int rawHours) { thisTC->hours.setValueRaw (rawHours); - format::Smpte::rangeLimitStrategy (*thisTC, rawHours); + format::Smpte::rangeLimitStrategy (*thisTC); } @@ -314,6 +275,38 @@ namespace time { } + /** flip the orientation of min, sec, and frames. + * Besides changing the sign, this will flip the + * meaning of the component fields, which by + * definition are always oriented towards zero. + * + * Normalised value fields are defined positive, + * with automatic overflow to next higher field. + * This might cause the hours to become negative. + * When invoked in this case, the meaning changes + * from -h + (m+s+f) to -(h+m+s+f) + */ + void + SmpteTC::invertOrientation() + { + int fr (getFps()); + int f (fr - frames); // revert orientation + int s (60 - secs); // of the components + int m (60 - mins); // + int h = -hours; // assumed to be negative + sgn *= -1; // flip sign field + + if (f < fr) --s; else f -= fr; + if (s < 60) --m; else s -= 60; + if (m < 60) --h; else m -= 60; + + hours.setValueRaw(h); + mins = m; // invoking setters + secs = s; // ensures normalisation + frames = f; + } + + uint SmpteTC::getFps() const { diff --git a/src/lib/time/timecode.hpp b/src/lib/time/timecode.hpp index 8392aed9f..3b5f320a8 100644 --- a/src/lib/time/timecode.hpp +++ b/src/lib/time/timecode.hpp @@ -113,6 +113,7 @@ namespace time { virtual Literal tcID() const { return "SMPTE"; } virtual TimeValue value() const { return Format::evaluate (*this, *quantiser_); } + public: typedef format::Smpte Format; @@ -124,6 +125,8 @@ namespace time { void clear(); void rebuild(); + void invertOrientation(); + HourDigit hours; SexaDigit mins;