2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef DCPOMATIC_TIME_H
21 #define DCPOMATIC_TIME_H
23 #include "frame_rate_change.h"
24 #include "safe_stringstream.h"
31 class dcpomatic_round_up_test;
35 /** A time in seconds, expressed as a number scaled up by Time::HZ. */
45 explicit Time (Type t)
55 double seconds () const {
56 return double (_t) / HZ;
60 int64_t frames (T r) const {
61 return rint (_t * r / HZ);
64 /** @param r Frames per second */
66 void split (T r, int& h, int& m, int& s, int& f) const
68 /* Do this calculation with frames so that we can round
69 to a frame boundary at the start rather than the end.
71 int64_t ff = frames (r);
80 f = static_cast<int> (ff);
84 std::string timecode (T r) const {
89 split (r, h, m, s, f);
94 o << std::setw(2) << std::setfill('0') << h << ":"
95 << std::setw(2) << std::setfill('0') << m << ":"
96 << std::setw(2) << std::setfill('0') << s << ":"
97 << std::setw(2) << std::setfill('0') << f;
102 friend struct dcptime_round_up_test;
105 static const int HZ = 96000;
110 class ContentTime : public Time
113 ContentTime () : Time () {}
114 explicit ContentTime (Type t) : Time (t) {}
115 ContentTime (Type n, Type d) : Time (n * HZ / d) {}
116 ContentTime (DCPTime d, FrameRateChange f);
118 bool operator< (ContentTime const & o) const {
122 bool operator<= (ContentTime const & o) const {
126 bool operator== (ContentTime const & o) const {
130 bool operator!= (ContentTime const & o) const {
134 bool operator>= (ContentTime const & o) const {
138 bool operator> (ContentTime const & o) const {
142 ContentTime operator+ (ContentTime const & o) const {
143 return ContentTime (_t + o._t);
146 ContentTime & operator+= (ContentTime const & o) {
151 ContentTime operator- () const {
152 return ContentTime (-_t);
155 ContentTime operator- (ContentTime const & o) const {
156 return ContentTime (_t - o._t);
159 ContentTime & operator-= (ContentTime const & o) {
164 /** Round up to the nearest sampling interval
165 * at some sampling rate.
166 * @param r Sampling rate.
168 ContentTime round_up (float r) {
169 Type const n = rint (HZ / r);
170 Type const a = _t + n - 1;
171 return ContentTime (a - (a % n));
174 static ContentTime from_seconds (double s) {
175 return ContentTime (s * HZ);
179 static ContentTime from_frames (int64_t f, T r) {
181 return ContentTime (f * HZ / r);
184 static ContentTime max () {
185 return ContentTime (INT64_MAX);
189 std::ostream& operator<< (std::ostream& s, ContentTime t);
191 class ContentTimePeriod
194 ContentTimePeriod () {}
195 ContentTimePeriod (ContentTime f, ContentTime t)
203 ContentTimePeriod operator+ (ContentTime const & o) const {
204 return ContentTimePeriod (from + o, to + o);
207 bool overlaps (ContentTimePeriod const & o) const;
208 bool contains (ContentTime const & o) const;
211 class DCPTime : public Time
214 DCPTime () : Time () {}
215 explicit DCPTime (Type t) : Time (t) {}
216 DCPTime (ContentTime t, FrameRateChange c) : Time (rint (t.get() / c.speed_up)) {}
218 bool operator< (DCPTime const & o) const {
222 bool operator<= (DCPTime const & o) const {
226 bool operator== (DCPTime const & o) const {
230 bool operator!= (DCPTime const & o) const {
234 bool operator>= (DCPTime const & o) const {
238 bool operator> (DCPTime const & o) const {
242 DCPTime operator+ (DCPTime const & o) const {
243 return DCPTime (_t + o._t);
246 DCPTime & operator+= (DCPTime const & o) {
251 DCPTime operator- () const {
252 return DCPTime (-_t);
255 DCPTime operator- (DCPTime const & o) const {
256 return DCPTime (_t - o._t);
259 DCPTime & operator-= (DCPTime const & o) {
264 /** Round up to the nearest sampling interval
265 * at some sampling rate.
266 * @param r Sampling rate.
268 DCPTime round_up (float r) {
269 Type const n = rint (HZ / r);
270 Type const a = _t + n - 1;
271 return DCPTime (a - (a % n));
274 DCPTime abs () const {
275 return DCPTime (std::abs (_t));
278 static DCPTime from_seconds (double s) {
279 return DCPTime (s * HZ);
283 static DCPTime from_frames (int64_t f, T r) {
285 return DCPTime (f * HZ / r);
288 static DCPTime delta () {
292 static DCPTime max () {
293 return DCPTime (INT64_MAX);
297 DCPTime min (DCPTime a, DCPTime b);
298 std::ostream& operator<< (std::ostream& s, DCPTime t);