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) {
138 return ContentTime (f * HZ / r);
141 static ContentTime max () {
142 return ContentTime (INT64_MAX);
146 std::ostream& operator<< (std::ostream& s, ContentTime t);
148 class DCPTime : public Time
151 DCPTime () : Time () {}
152 explicit DCPTime (int64_t t) : Time (t) {}
153 DCPTime (ContentTime t, FrameRateChange c) : Time (rint (t.get() / c.speed_up)) {}
155 bool operator< (DCPTime const & o) const {
159 bool operator<= (DCPTime const & o) const {
163 bool operator== (DCPTime const & o) const {
167 bool operator!= (DCPTime const & o) const {
171 bool operator>= (DCPTime const & o) const {
175 bool operator> (DCPTime const & o) const {
179 DCPTime operator+ (DCPTime const & o) const {
180 return DCPTime (_t + o._t);
183 DCPTime & operator+= (DCPTime const & o) {
188 DCPTime operator- (DCPTime const & o) const {
189 return DCPTime (_t - o._t);
192 DCPTime & operator-= (DCPTime const & o) {
197 /** Round up to the nearest sampling interval
198 * at some sampling rate.
199 * @param r Sampling rate.
201 DCPTime round_up (float r) {
202 int64_t const n = rint (HZ / r);
203 int64_t const a = _t + n - 1;
204 return DCPTime (a - (a % n));
207 DCPTime abs () const {
208 return DCPTime (std::abs (_t));
211 static DCPTime from_seconds (double s) {
212 return DCPTime (s * HZ);
216 static DCPTime from_frames (int64_t f, T r) {
217 return DCPTime (f * HZ / r);
220 static DCPTime delta () {
224 static DCPTime max () {
225 return DCPTime (INT64_MAX);
229 DCPTime min (DCPTime a, DCPTime b);
230 std::ostream& operator<< (std::ostream& s, DCPTime t);