OSSIA
Open Scenario System for Interactive Application
ossia/network/dataspace/time.hpp
1 #pragma once
2 #include <ossia/network/dataspace/dataspace_base.hpp>
3 #include <ossia/network/domain/domain.hpp>
4 
5 namespace ossia
6 {
7 struct timing_u;
8 struct second_u;
9 template <typename Impl>
10 struct timing_unit
11 {
12  using is_unit = std::true_type;
13  using neutral_unit = second_u;
14  using value_type = float;
15  using concrete_type = Impl;
16  using dataspace_type = timing_u;
17  using is_multidimensional = std::false_type;
18 };
19 
20 struct second_u : public timing_unit<second_u>
21 {
22  static constexpr auto text()
23  {
24  constexpr_return(ossia::make_string_array("second", "s"));
25  }
26 
27  static constexpr strong_value<neutral_unit>
28  to_neutral(strong_value<concrete_type> self)
29  {
30  return self;
31  }
32 
33  static constexpr value_type from_neutral(strong_value<neutral_unit> self)
34  {
35  return self.dataspace_value;
36  }
37  static ossia::domain domain() { return {}; }
38 
39  static constexpr auto bounding() { return ossia::bounding_mode::FREE; }
40 };
41 
42 struct bark_u : public timing_unit<bark_u>
43 {
44  static constexpr auto text() { constexpr_return(ossia::make_string_array("bark")); }
45 
46  static strong_value<neutral_unit> to_neutral(strong_value<concrete_type> self)
47  {
48  return 1.0f / (600.f * std::sinh(self.dataspace_value / 6.f));
49  }
50 
51  static value_type from_neutral(strong_value<neutral_unit> self)
52  {
53  return 6.f * ossia::asinh(1.0f / (self.dataspace_value * 600.0f));
54  }
55  static ossia::domain domain() { return {}; }
56 
57  static constexpr auto bounding() { return ossia::bounding_mode::FREE; }
58 };
59 
60 struct bpm_u : public timing_unit<bpm_u>
61 {
62  static constexpr auto text() { constexpr_return(ossia::make_string_array("bpm")); }
63 
64  static constexpr strong_value<neutral_unit>
65  to_neutral(strong_value<concrete_type> self)
66  {
67  return 60.0f / self.dataspace_value;
68  }
69 
70  static constexpr value_type from_neutral(strong_value<neutral_unit> self)
71  {
72  return 60.0f / self.dataspace_value;
73  }
74  static ossia::domain_base<float> domain() { return {0.f, 240.f}; }
75 
76  static constexpr auto bounding() { return ossia::bounding_mode::FREE; }
77 };
78 
79 struct cent_u : public timing_unit<cent_u>
80 {
81  static constexpr auto text() { constexpr_return(ossia::make_string_array("cents")); }
82 
83  static strong_value<neutral_unit> to_neutral(strong_value<concrete_type> self)
84  {
85  return 1.f / (440.0f * ossia::exp2((self.dataspace_value - 6900.0f) / 1200.0f));
86  }
87 
88  static value_type from_neutral(strong_value<neutral_unit> self)
89  {
90  return 6900.0f
91  + 1200.0f * std::log(1.f / (440.0f * self.dataspace_value))
92  / float(ossia::ln_2);
93  }
94 
95  static ossia::domain_base<float> domain() { return {0.f, 12700.f}; }
96 
97  static constexpr auto bounding() { return ossia::bounding_mode::FREE; }
98 };
99 
100 struct frequency_u : public timing_unit<frequency_u>
101 {
102  static constexpr auto text()
103  {
104  constexpr_return(ossia::make_string_array("Hz", "hz", "Hertz"));
105  }
106 
107  static constexpr strong_value<neutral_unit>
108  to_neutral(strong_value<concrete_type> self)
109  {
110  return 1.0f / self.dataspace_value;
111  }
112 
113  static constexpr value_type from_neutral(strong_value<neutral_unit> self)
114  {
115  return 1.0f / self.dataspace_value;
116  }
117 
118  static ossia::domain_base<float> domain() { return {0.f, 24000.f}; }
119 
120  static constexpr auto bounding() { return ossia::bounding_mode::LOW; }
121 };
122 
123 struct mel_u : public timing_unit<mel_u>
124 {
125  static constexpr auto text() { constexpr_return(ossia::make_string_array("mel")); }
126 
127  static strong_value<neutral_unit> to_neutral(strong_value<concrete_type> self)
128  {
129  return 1.0f / (700.0f * (std::pow(10.f, self.dataspace_value / 2595.0f) - 1.0f));
130  }
131 
132  static value_type from_neutral(strong_value<neutral_unit> self)
133  {
134  return 2595.0f * std::log10(1.f + 1.0f / (self.dataspace_value * 700.0f));
135  }
136 
137  static ossia::domain_base<float> domain() { return {0.f, 4016.f}; }
138 
139  static constexpr auto bounding() { return ossia::bounding_mode::LOW; }
140 };
141 
142 struct midi_pitch_u : public timing_unit<midi_pitch_u>
143 {
144  static constexpr auto text()
145  {
146  constexpr_return(ossia::make_string_array("midinote", "midipitch", "pitch"));
147  }
148 
149  static strong_value<neutral_unit> to_neutral(strong_value<concrete_type> self)
150  {
151  return 1.f / (440.0f * ossia::exp2((self.dataspace_value - 69.0f) / 12.0f));
152  }
153 
154  static value_type from_neutral(strong_value<neutral_unit> self)
155  {
156  return 69.0f - 12.0f * std::log(440.0f * self.dataspace_value) / float(ossia::ln_2);
157  }
158 
159  static ossia::domain_base<float> domain() { return {0.f, 127.f}; }
160 
161  static constexpr auto bounding() { return ossia::bounding_mode::LOW; }
162 };
163 
164 struct millisecond_u : public timing_unit<millisecond_u>
165 {
166  static constexpr auto text()
167  {
168  constexpr_return(ossia::make_string_array("ms", "millisecond"));
169  }
170 
171  static constexpr strong_value<neutral_unit>
172  to_neutral(strong_value<concrete_type> self)
173  {
174  return 0.001f * self.dataspace_value;
175  }
176 
177  static constexpr value_type from_neutral(strong_value<neutral_unit> self)
178  {
179  return 1000.0f * self.dataspace_value;
180  }
181 
182  static ossia::domain domain() { return {}; }
183 
184  static constexpr auto bounding() { return ossia::bounding_mode::FREE; }
185 };
186 
187 struct sample_u : public timing_unit<sample_u>
188 {
189  static constexpr auto text() { constexpr_return(ossia::make_string_array("sample")); }
190  float rate = 44100;
191 
192  void set_rate(double r) { rate = r; }
193 
194  constexpr strong_value<neutral_unit> to_neutral(strong_value<concrete_type> self)
195  {
196  return self.dataspace_value / rate;
197  }
198 
199  constexpr value_type from_neutral(strong_value<neutral_unit> self)
200  {
201  return self.dataspace_value * rate;
202  }
203 
204  static ossia::domain_base<float> domain() { return {0.f, 44100.f}; }
205 
206  static constexpr auto bounding() { return ossia::bounding_mode::LOW; }
207 };
208 
209 template <typename T>
210 // segfaults on MSVC... ossia::exp2(69. / 12.);
211 static constexpr T exp_69_12 = 53.817370576237730753992030478925753282562192;
212 
213 struct playback_speed_u : public timing_unit<playback_speed_u>
214 {
215  static constexpr auto text() { constexpr_return(ossia::make_string_array("speed")); }
216 
217  static strong_value<neutral_unit> to_neutral(strong_value<concrete_type> self)
218  {
219  return exp_69_12<float> / (440.0f * self.dataspace_value);
220  }
221 
222  static value_type from_neutral(strong_value<neutral_unit> self)
223  {
224  return exp_69_12<float> / (440.0f * self.dataspace_value);
225  }
226 
227  static ossia::domain_base<float> domain() { return {0.f, 2.f}; }
228 
229  static constexpr auto bounding() { return ossia::bounding_mode::LOW; }
230 };
231 
232 // template<int N>
233 // using sample = strong_value<sample_u<N>>;
234 }
Definition: git_info.h:7
domain A domain of values
Definition: domain_base.hpp:23