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
28 #include "frame_rate_change.h"
30 class dcpomatic_round_up_test;
34 /** A time in seconds, expressed as a number scaled up by Time::HZ. */
44 explicit Time (Type t)
54 double seconds () const {
55 return double (_t) / HZ;
59 int64_t frames (T r) const {
60 return rint (_t * r / HZ);
64 void split (T r, int& h, int& m, int& s, int& f) const
66 /* Do this calculation with frames so that we can round
67 to a frame boundary at the start rather than the end.
69 int64_t ff = frames (r);
78 f = static_cast<int> (ff);
82 std::string timecode (T r) const {
87 split (r, h, m, s, f);
92 o << std::setw(2) << std::setfill('0') << h << ":"
93 << std::setw(2) << std::setfill('0') << m << ":"
94 << std::setw(2) << std::setfill('0') << s << ":"
95 << std::setw(2) << std::setfill('0') << f;
100 friend class dcptime_round_up_test;
103 static const int HZ = 96000;
108 class ContentTime : public Time
111 ContentTime () : Time () {}
112 explicit ContentTime (Type t) : Time (t) {}
113 ContentTime (Type n, Type d) : Time (n * HZ / d) {}
114 ContentTime (DCPTime d, FrameRateChange f);
116 bool operator< (ContentTime const & o) const {
120 bool operator<= (ContentTime const & o) const {
124 bool operator== (ContentTime const & o) const {
128 bool operator!= (ContentTime const & o) const {
132 bool operator>= (ContentTime const & o) const {
136 bool operator> (ContentTime const & o) const {
140 ContentTime operator+ (ContentTime const & o) const {
141 return ContentTime (_t + o._t);
144 ContentTime & operator+= (ContentTime const & o) {
149 ContentTime operator- () const {
150 return ContentTime (-_t);
153 ContentTime operator- (ContentTime const & o) const {
154 return ContentTime (_t - o._t);
157 ContentTime & operator-= (ContentTime const & o) {
162 /** Round up to the nearest sampling interval
163 * at some sampling rate.
164 * @param r Sampling rate.
166 ContentTime round_up (float r) {
167 Type const n = rint (HZ / r);
168 Type const a = _t + n - 1;
169 return ContentTime (a - (a % n));
172 static ContentTime from_seconds (double s) {
173 return ContentTime (s * HZ);
177 static ContentTime from_frames (int64_t f, T r) {
179 return ContentTime (f * HZ / r);
182 static ContentTime max () {
183 return ContentTime (INT64_MAX);
187 std::ostream& operator<< (std::ostream& s, ContentTime t);
189 class ContentTimePeriod
192 ContentTimePeriod () {}
193 ContentTimePeriod (ContentTime f, ContentTime t)
201 ContentTimePeriod operator+ (ContentTime const & o) const {
202 return ContentTimePeriod (from + o, to + o);
205 bool overlaps (ContentTimePeriod const & o) const;
206 bool contains (ContentTime const & o) const;
209 class DCPTime : public Time
212 DCPTime () : Time () {}
213 explicit DCPTime (Type t) : Time (t) {}
214 DCPTime (ContentTime t, FrameRateChange c) : Time (rint (t.get() / c.speed_up)) {}
216 bool operator< (DCPTime const & o) const {
220 bool operator<= (DCPTime const & o) const {
224 bool operator== (DCPTime const & o) const {
228 bool operator!= (DCPTime const & o) const {
232 bool operator>= (DCPTime const & o) const {
236 bool operator> (DCPTime const & o) const {
240 DCPTime operator+ (DCPTime const & o) const {
241 return DCPTime (_t + o._t);
244 DCPTime & operator+= (DCPTime const & o) {
249 DCPTime operator- () const {
250 return DCPTime (-_t);
253 DCPTime operator- (DCPTime const & o) const {
254 return DCPTime (_t - o._t);
257 DCPTime & operator-= (DCPTime const & o) {
262 /** Round up to the nearest sampling interval
263 * at some sampling rate.
264 * @param r Sampling rate.
266 DCPTime round_up (float r) {
267 Type const n = rint (HZ / r);
268 Type const a = _t + n - 1;
269 return DCPTime (a - (a % n));
272 DCPTime abs () const {
273 return DCPTime (std::abs (_t));
276 static DCPTime from_seconds (double s) {
277 return DCPTime (s * HZ);
281 static DCPTime from_frames (int64_t f, T r) {
283 return DCPTime (f * HZ / r);
286 static DCPTime delta () {
290 static DCPTime max () {
291 return DCPTime (INT64_MAX);
295 DCPTime min (DCPTime a, DCPTime b);
296 std::ostream& operator<< (std::ostream& s, DCPTime t);