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
25 #include "frame_rate_change.h"
27 class dcpomatic_round_up_test;
31 /** A time in seconds, expressed as a number scaled up by Time::HZ. */
39 explicit Time (int64_t t)
45 int64_t get () const {
49 double seconds () const {
50 return double (_t) / HZ;
54 int64_t frames (T r) const {
55 return rint (_t * r / HZ);
58 operator bool () const {
63 friend class dcptime_round_up_test;
66 static const int HZ = 96000;
71 class ContentTime : public Time
74 ContentTime () : Time () {}
75 explicit ContentTime (int64_t t) : Time (t) {}
76 ContentTime (int64_t n, int64_t d) : Time (n * HZ / d) {}
77 ContentTime (DCPTime d, FrameRateChange f);
79 bool operator< (ContentTime const & o) const {
83 bool operator<= (ContentTime const & o) const {
87 bool operator== (ContentTime const & o) const {
91 bool operator!= (ContentTime const & o) const {
95 bool operator>= (ContentTime const & o) const {
99 bool operator> (ContentTime const & o) const {
103 ContentTime operator+ (ContentTime const & o) const {
104 return ContentTime (_t + o._t);
107 ContentTime & operator+= (ContentTime const & o) {
112 ContentTime operator- (ContentTime const & o) const {
113 return ContentTime (_t - o._t);
116 ContentTime & operator-= (ContentTime const & o) {
121 static ContentTime from_seconds (double s) {
122 return ContentTime (s * HZ);
126 static ContentTime from_frames (int64_t f, T r) {
127 return ContentTime (f * HZ / r);
131 class DCPTime : public Time
134 DCPTime () : Time () {}
135 explicit DCPTime (int64_t t) : Time (t) {}
136 DCPTime (ContentTime t, FrameRateChange c) : Time (rint (t.get() / c.speed_up)) {}
138 bool operator< (DCPTime const & o) const {
142 bool operator<= (DCPTime const & o) const {
146 bool operator== (DCPTime const & o) const {
150 bool operator!= (DCPTime const & o) const {
154 bool operator>= (DCPTime const & o) const {
158 bool operator> (DCPTime const & o) const {
162 DCPTime operator+ (DCPTime const & o) const {
163 return DCPTime (_t + o._t);
166 DCPTime & operator+= (DCPTime const & o) {
171 DCPTime operator- (DCPTime const & o) const {
172 return DCPTime (_t - o._t);
175 DCPTime & operator-= (DCPTime const & o) {
180 /** Round up to the nearest sampling interval
181 * at some sampling rate.
182 * @param r Sampling rate.
184 DCPTime round_up (int r) {
185 int64_t const n = HZ / r;
186 int64_t const a = _t + n - 1;
187 return DCPTime (a - (a % n));
190 DCPTime abs () const {
191 return DCPTime (std::abs (_t));
194 static DCPTime from_seconds (double s) {
195 return DCPTime (s * HZ);
199 static DCPTime from_frames (int64_t f, T r) {
200 return DCPTime (f * HZ / r);
203 static DCPTime delta () {
207 static DCPTime max () {
208 return DCPTime (INT64_MAX);
212 DCPTime min (DCPTime a, DCPTime b);