TimeValue.hpp
1 #pragma once
2 #include <Process/ZoomHelper.hpp>
3 
4 #include <score/serialization/DataStreamFwd.hpp>
5 #include <score/serialization/IsTemplate.hpp>
6 
7 #include <ossia/detail/flicks.hpp>
8 #include <ossia/editor/scenario/time_value.hpp>
9 
10 #include <ossia-qt/time_value.hpp>
11 
12 #include <cmath>
13 // #include <flicks.h>
14 #include <score_lib_process_export.h>
15 
16 // #include <chrono>
17 #include <verdigris>
18 
19 class QTime;
20 struct SCORE_LIB_PROCESS_EXPORT TimeVal : ossia::time_value
21 {
22  using ossia::time_value::time_value;
23 
24  static constexpr TimeVal fromMsecs(double msecs)
25  {
26  TimeVal time;
27  time.impl = msecs * ossia::flicks_per_millisecond<double>;
28  return time;
29  }
30  static constexpr TimeVal fromPixels(double pixels, double flicksPerPixel)
31  {
32  TimeVal time;
33  time.impl = pixels * flicksPerPixel;
34  return time;
35  }
36 
37  constexpr TimeVal() noexcept
38  : time_value{0}
39  {
40  }
41 
42  ~TimeVal() = default;
43  constexpr TimeVal(const TimeVal&) = default;
44  constexpr TimeVal(TimeVal&&) noexcept = default;
45  constexpr TimeVal& operator=(const TimeVal&) = default;
46  constexpr TimeVal& operator=(TimeVal&&) noexcept = default;
47 
48  constexpr TimeVal(ossia::time_value v) noexcept
49  : time_value{v}
50  {
51  }
52  explicit constexpr TimeVal(int64_t v) noexcept
53  : time_value{v}
54  {
55  }
56 
57  static constexpr TimeVal zero() noexcept { return TimeVal{time_value{}}; }
58 
59  explicit TimeVal(const QTime& t) noexcept;
60 
61  /*
62  template <
63  typename Duration,
64  std::enable_if_t<
65  std::is_class<typename Duration::period>::value>* = nullptr>
66  constexpr TimeVal(Duration&& dur) noexcept
67  : time_value{util::flicks_cast(dur).count()}
68  {
69  }
70  */
71 
72  constexpr TimeVal operator-(TimeVal t) const noexcept
73  {
74  if(infinite() || t.infinite())
75  return TimeVal{infinity};
76 
77  return TimeVal{impl - t.impl};
78  }
79  constexpr TimeVal operator+(TimeVal t) const noexcept
80  {
81  if(infinite() || t.infinite())
82  return TimeVal{infinity};
83 
84  return TimeVal{impl + t.impl};
85  }
86 
87  constexpr TimeVal& operator=(bool d) noexcept = delete;
88  constexpr TimeVal& operator=(double d) noexcept = delete;
89  constexpr TimeVal& operator=(float d) noexcept = delete;
90  constexpr TimeVal& operator=(uint64_t d) noexcept = delete;
91 
92  constexpr TimeVal& operator=(int64_t d) noexcept
93  {
94  impl = d;
95  return *this;
96  }
97  constexpr TimeVal& operator=(int32_t d) noexcept
98  {
99  impl = d;
100  return *this;
101  }
102 
103  constexpr TimeVal& operator-() noexcept
104  {
105  if(!infinite())
106  impl = -impl;
107 
108  return *this;
109  }
110 
111  constexpr time_value operator*(time_value d) const noexcept
112  {
113  return time_value{impl * d.impl};
114  }
115 
116  constexpr time_value operator*(double d) const noexcept
117  {
118  time_value res = *this;
119  res.impl *= d;
120  return res;
121  }
122  constexpr time_value operator*(int64_t d) const noexcept
123  {
124  return time_value{impl * d};
125  }
126 
127  operator bool() const noexcept = delete;
128 
129  constexpr double msec() const noexcept
130  {
131  if(!infinite())
132  return impl / ossia::flicks_per_millisecond<double>;
133 
134  return 0;
135  }
136 
137  constexpr double sec() const noexcept
138  {
139  if(!infinite())
140  return double(impl) / ossia::flicks_per_second<double>;
141  return 0;
142  }
143 
144  constexpr double toPixels(ZoomRatio ratio) const noexcept
145  {
146  return (ratio > 0 && !infinite()) ? std::round(impl / ratio) : 0;
147  }
148 
149  constexpr double toPixelsRaw(ZoomRatio ratio) const noexcept
150  {
151  return (ratio > 0 && !infinite()) ? impl / ratio : 0;
152  }
153 
154  constexpr int64_t toSample(double sampleRate) const noexcept
155  {
156  return ossia::to_sample(*this, sampleRate);
157  }
158 
159  QTime toQTime() const noexcept;
160  QString toString() const noexcept;
161 
162  constexpr void setMSecs(double msecs) noexcept
163  {
164  impl = msecs * ossia::flicks_per_millisecond<double>;
165  }
166 
167  constexpr bool operator==(TimeVal other) const noexcept { return impl == other.impl; }
168 
169  constexpr bool operator!=(TimeVal other) const noexcept { return impl != other.impl; }
170 
171  constexpr bool operator>(TimeVal other) const noexcept { return impl > other.impl; }
172 
173  constexpr bool operator>=(TimeVal other) const noexcept { return impl >= other.impl; }
174 
175  constexpr bool operator<(TimeVal other) const noexcept { return impl < other.impl; }
176 
177  constexpr bool operator<=(TimeVal other) const noexcept { return impl <= other.impl; }
178 
179  constexpr bool operator==(time_value other) const noexcept
180  {
181  return impl == other.impl;
182  }
183 
184  constexpr bool operator!=(time_value other) const noexcept
185  {
186  return impl != other.impl;
187  }
188 
189  constexpr bool operator>(time_value other) const noexcept { return impl > other.impl; }
190 
191  constexpr bool operator>=(time_value other) const noexcept
192  {
193  return impl >= other.impl;
194  }
195 
196  constexpr bool operator<(time_value other) const noexcept { return impl < other.impl; }
197 
198  constexpr bool operator<=(time_value other) const noexcept
199  {
200  return impl <= other.impl;
201  }
202 };
203 
204 inline const TimeVal& max(const TimeVal& lhs, const TimeVal& rhs) noexcept
205 {
206  if(lhs < rhs)
207  return rhs;
208  else
209  return lhs;
210 }
211 
212 #include <qhash.h>
213 namespace std
214 {
215 template <>
216 struct hash<TimeVal>
217 {
218  std::size_t operator()(const TimeVal& t) const { return qHash(t.impl); }
219 };
220 }
221 template <>
222 struct is_custom_serialized<TimeVal> : std::true_type
223 {
224 };
225 
226 SCORE_SERIALIZE_DATASTREAM_DECLARE(SCORE_LIB_PROCESS_EXPORT, TimeVal)
227 Q_DECLARE_METATYPE(TimeVal)
228 W_REGISTER_ARGTYPE(TimeVal)
Definition: TimeValue.hpp:21