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. */
42 explicit Time (int64_t t)
48 int64_t get () const {
52 double seconds () const {
53 return double (_t) / HZ;
57 int64_t frames (T r) const {
58 return rint (_t * r / HZ);
62 void split (T r, int& h, int& m, int& s, int& f) const
64 /* Do this calculation with frames so that we can round
65 to a frame boundary at the start rather than the end.
67 int64_t ff = frames (r);
76 f = static_cast<int> (ff);
80 std::string timecode (T r) const {
85 split (r, h, m, s, f);
90 o << std::setw(2) << std::setfill('0') << h << ":"
91 << std::setw(2) << std::setfill('0') << m << ":"
92 << std::setw(2) << std::setfill('0') << s << ":"
93 << std::setw(2) << std::setfill('0') << f;
98 friend class dcptime_round_up_test;
101 static const int HZ = 96000;
106 class ContentTime : public Time
109 ContentTime () : Time () {}
110 explicit ContentTime (int64_t t) : Time (t) {}
111 ContentTime (int64_t n, int64_t d) : Time (n * HZ / d) {}
112 ContentTime (DCPTime d, FrameRateChange f);
114 bool operator< (ContentTime const & o) const {
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 ContentTime operator+ (ContentTime const & o) const {
139 return ContentTime (_t + o._t);
142 ContentTime & operator+= (ContentTime const & o) {
147 ContentTime operator- () const {
148 return ContentTime (-_t);
151 ContentTime operator- (ContentTime const & o) const {
152 return ContentTime (_t - o._t);
155 ContentTime & operator-= (ContentTime const & o) {
160 /** Round up to the nearest sampling interval
161 * at some sampling rate.
162 * @param r Sampling rate.
164 ContentTime round_up (float r) {
165 int64_t const n = rint (HZ / r);
166 int64_t const a = _t + n - 1;
167 return ContentTime (a - (a % n));
170 static ContentTime from_seconds (double s) {
171 return ContentTime (s * HZ);
175 static ContentTime from_frames (int64_t f, T r) {
177 return ContentTime (f * HZ / r);
180 static ContentTime max () {
181 return ContentTime (INT64_MAX);
185 std::ostream& operator<< (std::ostream& s, ContentTime t);
187 class ContentTimePeriod
190 ContentTimePeriod () {}
191 ContentTimePeriod (ContentTime f, ContentTime t)
199 ContentTimePeriod operator+ (ContentTime const & o) const {
200 return ContentTimePeriod (from + o, to + o);
203 bool overlaps (ContentTimePeriod const & o) const;
206 class DCPTime : public Time
209 DCPTime () : Time () {}
210 explicit DCPTime (int64_t t) : Time (t) {}
211 DCPTime (ContentTime t, FrameRateChange c) : Time (rint (t.get() / c.speed_up)) {}
213 bool operator< (DCPTime const & o) const {
217 bool operator<= (DCPTime const & o) const {
221 bool operator== (DCPTime const & o) const {
225 bool operator!= (DCPTime const & o) const {
229 bool operator>= (DCPTime const & o) const {
233 bool operator> (DCPTime const & o) const {
237 DCPTime operator+ (DCPTime const & o) const {
238 return DCPTime (_t + o._t);
241 DCPTime & operator+= (DCPTime const & o) {
246 DCPTime operator- () const {
247 return DCPTime (-_t);
250 DCPTime operator- (DCPTime const & o) const {
251 return DCPTime (_t - o._t);
254 DCPTime & operator-= (DCPTime const & o) {
259 /** Round up to the nearest sampling interval
260 * at some sampling rate.
261 * @param r Sampling rate.
263 DCPTime round_up (float r) {
264 int64_t const n = rint (HZ / r);
265 int64_t const a = _t + n - 1;
266 return DCPTime (a - (a % n));
269 DCPTime abs () const {
270 return DCPTime (std::abs (_t));
273 static DCPTime from_seconds (double s) {
274 return DCPTime (s * HZ);
278 static DCPTime from_frames (int64_t f, T r) {
280 return DCPTime (f * HZ / r);
283 static DCPTime delta () {
287 static DCPTime max () {
288 return DCPTime (INT64_MAX);
292 DCPTime min (DCPTime a, DCPTime b);
293 std::ostream& operator<< (std::ostream& s, DCPTime t);