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