Fix build.
[dcpomatic.git] / src / lib / dcpomatic_time.h
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef DCPOMATIC_TIME_H
21 #define DCPOMATIC_TIME_H
22
23 #include <cmath>
24 #include <stdint.h>
25 #include "frame_rate_change.h"
26
27 class dcpomatic_round_up_test;
28
29 class Time;
30
31 /** A time in seconds, expressed as a number scaled up by Time::HZ. */
32 class Time
33 {
34 public:
35         Time ()
36                 : _t (0)
37         {}
38
39         explicit Time (int64_t t)
40                 : _t (t)
41         {}
42
43         virtual ~Time () {}
44
45         int64_t get () const {
46                 return _t;
47         }
48
49         double seconds () const {
50                 return double (_t) / HZ;
51         }
52
53         template <typename T>
54         int64_t frames (T r) const {
55                 return rint (_t * r / HZ);
56         }
57
58         operator bool () const {
59                 return _t != 0;
60         }
61
62 protected:
63         friend class dcptime_round_up_test;
64         
65         int64_t _t;
66         static const int HZ = 96000;
67 };
68
69 class DCPTime;
70
71 class ContentTime : public Time
72 {
73 public:
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);
78
79         bool operator< (ContentTime const & o) const {
80                 return _t < o._t;
81         }
82
83         bool operator<= (ContentTime const & o) const {
84                 return _t <= o._t;
85         }
86
87         bool operator== (ContentTime const & o) const {
88                 return _t == o._t;
89         }
90
91         bool operator!= (ContentTime const & o) const {
92                 return _t != o._t;
93         }
94
95         bool operator>= (ContentTime const & o) const {
96                 return _t >= o._t;
97         }
98
99         bool operator> (ContentTime const & o) const {
100                 return _t > o._t;
101         }
102
103         ContentTime operator+ (ContentTime const & o) const {
104                 return ContentTime (_t + o._t);
105         }
106
107         ContentTime & operator+= (ContentTime const & o) {
108                 _t += o._t;
109                 return *this;
110         }
111
112         ContentTime operator- (ContentTime const & o) const {
113                 return ContentTime (_t - o._t);
114         }
115
116         ContentTime & operator-= (ContentTime const & o) {
117                 _t -= o._t;
118                 return *this;
119         }
120
121         static ContentTime from_seconds (double s) {
122                 return ContentTime (s * HZ);
123         }
124
125         template <class T>
126         static ContentTime from_frames (int64_t f, T r) {
127                 return ContentTime (f * HZ / r);
128         }
129 };
130
131 class DCPTime : public Time
132 {
133 public:
134         DCPTime () : Time () {}
135         explicit DCPTime (int64_t t) : Time (t) {}
136         DCPTime (ContentTime t, FrameRateChange c) : Time (rint (t.get() / c.speed_up)) {}
137
138         bool operator< (DCPTime const & o) const {
139                 return _t < o._t;
140         }
141
142         bool operator<= (DCPTime const & o) const {
143                 return _t <= o._t;
144         }
145
146         bool operator== (DCPTime const & o) const {
147                 return _t == o._t;
148         }
149
150         bool operator!= (DCPTime const & o) const {
151                 return _t != o._t;
152         }
153
154         bool operator>= (DCPTime const & o) const {
155                 return _t >= o._t;
156         }
157
158         bool operator> (DCPTime const & o) const {
159                 return _t > o._t;
160         }
161
162         DCPTime operator+ (DCPTime const & o) const {
163                 return DCPTime (_t + o._t);
164         }
165
166         DCPTime & operator+= (DCPTime const & o) {
167                 _t += o._t;
168                 return *this;
169         }
170
171         DCPTime operator- (DCPTime const & o) const {
172                 return DCPTime (_t - o._t);
173         }
174
175         DCPTime & operator-= (DCPTime const & o) {
176                 _t -= o._t;
177                 return *this;
178         }
179
180         /** Round up to the nearest sampling interval
181          *  at some sampling rate.
182          *  @param r Sampling rate.
183          */
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));
188         }
189
190         DCPTime abs () const {
191                 return DCPTime (std::abs (_t));
192         }
193
194         static DCPTime from_seconds (double s) {
195                 return DCPTime (s * HZ);
196         }
197
198         template <class T>
199         static DCPTime from_frames (int64_t f, T r) {
200                 return DCPTime (f * HZ / r);
201         }
202
203         static DCPTime delta () {
204                 return DCPTime (1);
205         }
206
207         static DCPTime max () {
208                 return DCPTime (INT64_MAX);
209         }
210 };
211
212 DCPTime min (DCPTime a, DCPTime b);
213
214 #endif