Copy another test over from DCP-o-matic.
[libsub.git] / src / frame_time.cc
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 #include "frame_time.h"
21 #include "compose.hpp"
22 #include <iostream>
23
24 using std::ostream;
25 using std::string;
26 using namespace sub;
27
28 bool
29 sub::operator== (FrameTime const & a, FrameTime const & b)
30 {
31         return a.hours() == b.hours() && a.minutes() == b.minutes() && a.seconds() == b.seconds() && a.frames() == b.frames();
32 }
33
34 bool
35 sub::operator< (FrameTime const & a, FrameTime const & b)
36 {
37         if (a.hours() != b.hours()) {
38                 return a.hours() < b.hours();
39         }
40
41         if (a.minutes() != b.minutes()) {
42                 return a.minutes() < b.minutes();
43         }
44
45         if (a.seconds() != b.seconds()) {
46                 return a.seconds() < b.seconds();
47         }
48
49         return a.frames() < b.frames();
50 }
51
52 ostream&
53 sub::operator<< (ostream& s, FrameTime const & t)
54 {
55         s << t.hours() << ":" << t.minutes() << ":" << t.seconds() << ":" << t.frames();
56         return s;
57 }
58
59 string
60 FrameTime::timecode () const
61 {
62         return String::compose ("%1:%2:%3:%4", _hours, _minutes, _seconds, _frames);
63 }
64
65 FrameTime::FrameTime (int64_t f, float fps)
66 {
67         set_from_frames (f, fps);
68 }
69
70 void
71 FrameTime::set_from_frames (int64_t f, float fps)
72 {
73         _hours = f / (60 * 60 * fps);
74         f -= _hours * 60 * 60 * fps;
75         _minutes = f / (60 * fps);
76         f -= _minutes * 60 * fps;
77         _seconds = f / fps;
78         f -= _seconds * fps;
79         _frames = int (f);
80 }
81
82 void
83 FrameTime::add (FrameTime t, float fps)
84 {
85         _frames += t.frames ();
86         if (_frames > fps) {
87                 _frames -= fps;
88                 _seconds++;
89         }
90
91         _seconds += t.seconds ();
92         if (_seconds >= 60) {
93                 _seconds -= 60;
94                 ++_minutes;
95         }
96
97         _minutes += t.minutes ();
98         if (_minutes >= 60) {
99                 _minutes -= 60;
100                 ++_hours;
101         }
102
103         _hours += t.hours ();
104 }
105
106 void
107 FrameTime::scale (float f, float frames_per_second)
108 {
109         set_from_frames (
110                 (((_hours * 3600 + _minutes * 60 + _seconds) * frames_per_second) + _frames) * f,
111                 frames_per_second
112                 );
113 }