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
26 #include "frame_rate_change.h"
28 class dcpomatic_round_up_test;
32 /** A time in seconds, expressed as a number scaled up by Time::HZ. */
40 explicit Time (int64_t t)
46 int64_t get () const {
50 double seconds () const {
51 return double (_t) / HZ;
55 int64_t frames (T r) const {
56 return rint (_t * r / HZ);
60 friend class dcptime_round_up_test;
63 static const int HZ = 96000;
68 class ContentTime : public Time
71 ContentTime () : Time () {}
72 explicit ContentTime (int64_t t) : Time (t) {}
73 ContentTime (int64_t n, int64_t d) : Time (n * HZ / d) {}
74 ContentTime (DCPTime d, FrameRateChange f);
76 bool operator< (ContentTime const & o) const {
80 bool operator<= (ContentTime const & o) const {
84 bool operator== (ContentTime const & o) const {
88 bool operator!= (ContentTime const & o) const {
92 bool operator>= (ContentTime const & o) const {
96 bool operator> (ContentTime const & o) const {
100 ContentTime operator+ (ContentTime const & o) const {
101 return ContentTime (_t + o._t);
104 ContentTime & operator+= (ContentTime const & o) {
109 ContentTime operator- () const {
110 return ContentTime (-_t);
113 ContentTime operator- (ContentTime const & o) const {
114 return ContentTime (_t - o._t);
117 ContentTime & operator-= (ContentTime const & o) {
122 /** Round up to the nearest sampling interval
123 * at some sampling rate.
124 * @param r Sampling rate.
126 ContentTime round_up (float r) {
127 int64_t const n = rint (HZ / r);
128 int64_t const a = _t + n - 1;
129 return ContentTime (a - (a % n));
132 static ContentTime from_seconds (double s) {
133 return ContentTime (s * HZ);
137 static ContentTime from_frames (int64_t f, T r) {
139 return ContentTime (f * HZ / r);
142 static ContentTime max () {
143 return ContentTime (INT64_MAX);
147 std::ostream& operator<< (std::ostream& s, ContentTime t);
149 class ContentTimePeriod
152 ContentTimePeriod () {}
153 ContentTimePeriod (ContentTime f, ContentTime t)
161 ContentTimePeriod operator+ (ContentTime const & o) const {
162 return ContentTimePeriod (from + o, to + o);
166 class DCPTime : public Time
169 DCPTime () : Time () {}
170 explicit DCPTime (int64_t t) : Time (t) {}
171 DCPTime (ContentTime t, FrameRateChange c) : Time (rint (t.get() / c.speed_up)) {}
173 bool operator< (DCPTime const & o) const {
177 bool operator<= (DCPTime const & o) const {
181 bool operator== (DCPTime const & o) const {
185 bool operator!= (DCPTime const & o) const {
189 bool operator>= (DCPTime const & o) const {
193 bool operator> (DCPTime const & o) const {
197 DCPTime operator+ (DCPTime const & o) const {
198 return DCPTime (_t + o._t);
201 DCPTime & operator+= (DCPTime const & o) {
206 DCPTime operator- () const {
207 return DCPTime (-_t);
210 DCPTime operator- (DCPTime const & o) const {
211 return DCPTime (_t - o._t);
214 DCPTime & operator-= (DCPTime const & o) {
219 /** Round up to the nearest sampling interval
220 * at some sampling rate.
221 * @param r Sampling rate.
223 DCPTime round_up (float r) {
224 int64_t const n = rint (HZ / r);
225 int64_t const a = _t + n - 1;
226 return DCPTime (a - (a % n));
229 DCPTime abs () const {
230 return DCPTime (std::abs (_t));
233 static DCPTime from_seconds (double s) {
234 return DCPTime (s * HZ);
238 static DCPTime from_frames (int64_t f, T r) {
240 return DCPTime (f * HZ / r);
243 static DCPTime delta () {
247 static DCPTime max () {
248 return DCPTime (INT64_MAX);
252 DCPTime min (DCPTime a, DCPTime b);
253 std::ostream& operator<< (std::ostream& s, DCPTime t);