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 DCPTime : public Time
152 DCPTime () : Time () {}
153 explicit DCPTime (int64_t t) : Time (t) {}
154 DCPTime (ContentTime t, FrameRateChange c) : Time (rint (t.get() / c.speed_up)) {}
156 bool operator< (DCPTime const & o) const {
160 bool operator<= (DCPTime const & o) const {
164 bool operator== (DCPTime const & o) const {
168 bool operator!= (DCPTime const & o) const {
172 bool operator>= (DCPTime const & o) const {
176 bool operator> (DCPTime const & o) const {
180 DCPTime operator+ (DCPTime const & o) const {
181 return DCPTime (_t + o._t);
184 DCPTime & operator+= (DCPTime const & o) {
189 DCPTime operator- (DCPTime const & o) const {
190 return DCPTime (_t - o._t);
193 DCPTime & operator-= (DCPTime const & o) {
198 /** Round up to the nearest sampling interval
199 * at some sampling rate.
200 * @param r Sampling rate.
202 DCPTime round_up (float r) {
203 int64_t const n = rint (HZ / r);
204 int64_t const a = _t + n - 1;
205 return DCPTime (a - (a % n));
208 DCPTime abs () const {
209 return DCPTime (std::abs (_t));
212 static DCPTime from_seconds (double s) {
213 return DCPTime (s * HZ);
217 static DCPTime from_frames (int64_t f, T r) {
219 return DCPTime (f * HZ / r);
222 static DCPTime delta () {
226 static DCPTime max () {
227 return DCPTime (INT64_MAX);
231 DCPTime min (DCPTime a, DCPTime b);
232 std::ostream& operator<< (std::ostream& s, DCPTime t);