Jamoma API  0.6.0.a19
TimeDataspace.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup foundationDataspaceLib
4  *
5  * @brief The #TimeDataspace converts between different measurement untis describing time intervals and frequencies.
6  *
7  * @details The neutral unit of the time dataspace is _second (s)_.
8  *
9  * @authors Tim Place, Nils Peters, Trond Lossius, ...
10  *
11  * @copyright Copyright © 2007 by Tim Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "TimeDataspace.h"
18 #include "TTFoundationAPI.h"
19 #include <math.h>
20 
21 #ifdef TT_PLATFORM_WIN
22 template <typename T> T asinh(T value)
23 {
24  T returned;
25  if(value>0)
26  returned = log(value + sqrt(value * value + 1));
27  else
28  returned = -log(-value + sqrt(value * value + 1));
29  return(returned);
30 }
31 #endif
32 
33 /****************************************************************************/
34 /* Bark */
35 /****************************************************************************/
36 
37 #define thisTTClass BarkUnit
38 #define thisTTClassName "unit.bark"
39 #define thisTTClassTags "dataspace.unit, time, pitch, frequency"
40 
42 TTDataspaceUnit(arguments)
43 {;}
44 
45 BarkUnit::~BarkUnit(){;}
46 
47 void BarkUnit::convertToNeutral(const TTValue& input, TTValue& output)
48 { // code from http://labrosa.ee.columbia.edu/matlab/rastamat/bark2hz.m
49  output = 1.0 / (600 * sinh(TTFloat64(input) / 6));
50 }
51 
52 
53 void BarkUnit::convertFromNeutral(const TTValue& input, TTValue& output)
54 { // taken from http://labrosa.ee.columbia.edu/matlab/rastamat/hz2bark.m
55  output = (6 * asinh(1.0 / (TTFloat64(input) * 600.0)));
56 }
57 
58 
59 #undef thisTTClass
60 #undef thisTTClassName
61 #undef thisTTClassTags
62 
63 
64 
65 
66 /****************************************************************************/
67 /* Beats per Minute (bpm) */
68 /****************************************************************************/
69 
70 #define thisTTClass BpmUnit
71 #define thisTTClassName "unit.bpm"
72 #define thisTTClassTags "dataspace.unit, time"
73 
74 
76 TTDataspaceUnit(arguments)
77 {;}
78 
79 BpmUnit::~BpmUnit(){;}
80 
81 void BpmUnit::convertToNeutral(const TTValue& input, TTValue& output)
82 {
83  //TODO: prevent division with zero
84  output = 60.0 / TTFloat64(input);
85 }
86 
87 void BpmUnit::convertFromNeutral(const TTValue& input, TTValue& output)
88 {
89  //TODO: prevent division with zero
90  output = 60.0 / TTFloat64(input);
91 }
92 
93 
94 #undef thisTTClass
95 #undef thisTTClassName
96 #undef thisTTClassTags
97 
98 
99 /****************************************************************************/
100 /* Cent - MIDI Cents */
101 /****************************************************************************/
102 
103 #define thisTTClass CentUnit
104 #define thisTTClassName "unit.cent"
105 #define thisTTClassTags "dataspace.unit, time"
106 
108 TTDataspaceUnit(arguments)
109 {;}
110 
111 CentUnit::~CentUnit(){;}
112 
113 void CentUnit::convertToNeutral(const TTValue& input, TTValue& output)
114 {
115  output = 1. / (440.0 * pow(2.0, (TTFloat64(input)-6900.0) / 1200.0 ));
116 }
117 
118 
119 void CentUnit::convertFromNeutral(const TTValue& input, TTValue& output)
120 {
121  output = 6900.0 + 1200.0 * log(1./(440.0*TTFloat64(input)))/log(2.0);
122 
123 }
124 
125 #undef thisTTClass
126 #undef thisTTClassName
127 #undef thisTTClassTags
128 
129 
130 /****************************************************************************/
131 /* Frequency (Hz) */
132 /****************************************************************************/
133 
134 #define thisTTClass FrequencyUnit
135 #define thisTTClassName "unit.rate"
136 #define thisTTClassTags "dataspace.unit, time"
137 
138 
140 TTDataspaceUnit(arguments)
141 {;}
142 
143 FrequencyUnit::~FrequencyUnit(){;}
144 
146 {
147  //TODO: prevent division with zero
148  output = 1.0 / TTFloat64(input);
149 }
150 
152 {
153  //TODO: prevent division with zero
154  output = 1.0 / TTFloat64(input);
155 }
156 
157 
158 #undef thisTTClass
159 #undef thisTTClassName
160 #undef thisTTClassTags
161 
162 
163 /****************************************************************************/
164 /* Mel */
165 /****************************************************************************/
166 
167 #define thisTTClass MelUnit
168 #define thisTTClassName "unit.mel"
169 #define thisTTClassTags "dataspace.unit, time"
170 
172 TTDataspaceUnit(arguments)
173 {;}
174 
175 MelUnit::~MelUnit(){;}
176 
177 void MelUnit::convertToNeutral(const TTValue& input, TTValue& output)
178 { // HTK-code from http://labrosa.ee.columbia.edu/matlab/rastamat/mel2hz.m
179  output = 1.0 / (700.0 *(pow(10,(TTFloat64(input)/2595.0))-1.0));
180 }
181 
182 
183 void MelUnit::convertFromNeutral(const TTValue& input, TTValue& output)
184 { // HTK-code from http://labrosa.ee.columbia.edu/matlab/rastamat/hz2mel.m
185  output = 2595.0 * log10(1+1.0/(TTFloat64(input)*700.0));
186 }
187 
188 
189 #undef thisTTClass
190 #undef thisTTClassName
191 #undef thisTTClassTags
192 
193 
194 /****************************************************************************/
195 /* MIDI Pitch */
196 /****************************************************************************/
197 
198 #define thisTTClass MidiPitchUnit
199 #define thisTTClassName "unit.midi"
200 #define thisTTClassTags "dataspace.unit, time"
201 
203 TTDataspaceUnit(arguments)
204 {;}
205 
206 MidiPitchUnit::~MidiPitchUnit(){;}
207 
209 {
210  output = 1. / (440.0 * pow(2.0, (TTFloat64(input)-69.0) / 12.0 ));
211 }
212 
213 
215 {
216  //output = 69.0 + 12.0 * log(1./(440.0*TTFloat64(input)))/log(2.0);
217 
218  // The above can be transformed to the slightly more optimised:
219  output = 69.0 - 12.0 * log(440.0*TTFloat64(input))/log(2.0);
220 }
221 
222 #undef thisTTClass
223 #undef thisTTClassName
224 #undef thisTTClassTags
225 
226 
227 /****************************************************************************/
228 /* Millisecond (ms) */
229 /****************************************************************************/
230 
231 #define thisTTClass MillisecondUnit
232 #define thisTTClassName "unit.ms"
233 #define thisTTClassTags "dataspace.unit, time"
234 
235 
237 TTDataspaceUnit(arguments)
238 {;}
239 
240 MillisecondUnit::~MillisecondUnit(){;}
241 
243 {
244  output = 0.001*TTFloat64(input);
245 }
246 
247 
249 {
250  output = 1000*TTFloat64(input);
251 }
252 
253 
254 #undef thisTTClass
255 #undef thisTTClassName
256 #undef thisTTClassTags
257 
258 
259 /****************************************************************************/
260 /* Samples - dynamically adjusted for audio environment sample rate */
261 /****************************************************************************/
262 
263 #define thisTTClass SampleUnit
264 #define thisTTClassName "unit.sample"
265 #define thisTTClassTags "dataspace.unit, time"
266 
267 
269 TTDataspaceUnit(arguments)
270 {
271 }
272 
273 SampleUnit::~SampleUnit()
274 {;}
275 
276 void SampleUnit::convertToNeutral(const TTValue& input, TTValue& output)
277 {
278  TTValue globalSampleRate;
279 
280  ttEnvironment->getAttributeValue(kTTSym_sampleRate, globalSampleRate);
281  double sampleRate = globalSampleRate;
282 
283  output = TTFloat64(input) / sampleRate;
284 }
285 
286 void SampleUnit::convertFromNeutral(const TTValue& input, TTValue& output)
287 {
288  TTValue globalSampleRate;
289 
290  ttEnvironment->getAttributeValue(kTTSym_sampleRate, globalSampleRate);
291  double sampleRate = globalSampleRate;
292 
293  output = TTFloat64(input) * sampleRate;
294 }
295 
296 
297 #undef thisTTClass
298 #undef thisTTClassName
299 #undef thisTTClassTags
300 
301 
302 /****************************************************************************/
303 /* Second (s) */
304 /****************************************************************************/
305 
306 #define thisTTClass SecondUnit
307 #define thisTTClassName "unit.second"
308 #define thisTTClassTags "dataspace.unit, time"
309 
310 
312 TTDataspaceUnit(arguments)
313 {;}
314 
315 SecondUnit::~SecondUnit(){;}
316 
317 void SecondUnit::convertToNeutral(const TTValue& input, TTValue& output)
318 {
319  output = input;
320 }
321 
322 void SecondUnit::convertFromNeutral(const TTValue& input, TTValue& output)
323 {
324  output = input;
325 }
326 
327 
328 #undef thisTTClass
329 #undef thisTTClassName
330 #undef thisTTClassTags
331 
332 
333 /****************************************************************************/
334 /* Speed - Transposition playback speed of buffers or sound files */
335 /****************************************************************************/
336 
337 #define thisTTClass SpeedUnit
338 #define thisTTClassName "unit.speed"
339 #define thisTTClassTags "dataspace.unit, time"
340 
342 TTDataspaceUnit(arguments)
343 {;}
344 
345 SpeedUnit::~SpeedUnit(){;}
346 
347 void SpeedUnit::convertToNeutral(const TTValue& input, TTValue& output)
348 {
349  /*
350  Here's one way of converting:
351 
352  TTFloat64 midi;
353 
354  // 1) speed => midi
355  midi = 12.0 * log(TTFloat64(input))/log(2.0);
356  // 2) midi => second
357  output = 1. / (440.0 * pow(2.0, (midi-69.0) / 12.0 ));
358  */
359 
360  // This is an optimized version of the above:
361  output = pow(2.0, 69./12.) / (440.0*TTFloat64(input));
362 }
363 
364 
365 void SpeedUnit::convertFromNeutral(const TTValue& input, TTValue& output)
366 {
367  /*
368  Here's one way of converting from second to speed:
369 
370  TTFloat64 midi;
371 
372  // 1) second => midi
373  midi = 69.0 - 12.0 * log(440.0*TTFloat64(input))/log(2.0);
374  // 2) midi => speed
375  output = pow(2.0, (midi/12.0));
376  */
377 
378  // Optimized in a similar way to the above:
379  output = pow(2.0, 69./12.) / (440.0*TTFloat64(input));
380 }
381 
382 
383 #undef thisTTClass
384 #undef thisTTClassName
385 #undef thisTTClassTags
386 
387 
388 /****************************************************************************/
389 /* */
390 /****************************************************************************/
391 
392 #define thisTTClass TimeDataspace
393 #define thisTTClassName "dataspace.time"
394 #define thisTTClassTags "foundationDataspaceLib, dataspace, time"
395 
396 
398 {
399  // Register unit names for this dataspace,
400  // and map them to the actual object names implementing the conversion.
401  registerUnit(TT("unit.bark"), TT("bark"));
402  registerUnit(TT("unit.bpm"), TT("bpm"));
403  registerUnit(TT("unit.cent"), TT("cents"));
404  registerUnit(TT("unit.mel"), TT("mel"));
405  registerUnit(TT("unit.midi"), TT("midinote"));
406  registerUnit(TT("unit.ms"), TT("ms"));
407  registerUnit(TT("unit.ms"), TT("millisecond"));
408  registerUnit(TT("unit.rate"), TT("fps"));
409  registerUnit(TT("unit.rate"), TT("Hz"));
410  registerUnit(TT("unit.rate"), TT("hz"));
411  registerUnit(TT("unit.rate"), TT("Hertz"));
412  registerUnit(TT("unit.sample"), TT("sample"));
413  registerUnit(TT("unit.second"), TT("s"));
414  registerUnit(TT("unit.second"), TT("second"));
415  registerUnit(TT("unit.speed"), TT("speed"));
416 
417 
418  // Set our neutral unit (the unit through which all conversions are made)
419  neutralUnit = TT("second");
420 
421  // Now that the cache is created, we can create a set of default units
422  // Typically this should be the neutral unit
423  setInputUnit(neutralUnit);
424  setOutputUnit(neutralUnit);
425 }
426 
427 
428 TimeDataspace::~TimeDataspace()
429 {
430  ;
431 }
432 
433 
434 #undef thisTTClass
435 #undef thisTTClassName
436 #undef thisTTClassTags
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from radians to neutral unit.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to seconds.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from milliseconds to neutral unit.
TTFOUNDATION_EXPORT TTEnvironment * ttEnvironment
The environment object has one instance, which is global in scope.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from MIDI pitch to neutral unit.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from Hz to neutral unit.
The TimeDataspace converts between different measurement untis describing time intervals and frequenc...
TTErr getAttributeValue(const TTSymbol name, TTValue &value)
Get an attribute value for an object.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to MIDI pitch.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from samples to neutral unit.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from Bpm to neutral unit.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to number of samples.
Specification for the base class of each DataspaceUnit.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from Mel to neutral unit.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to Hz.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to Mel.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to milliseconds.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to cents.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from speed to neutral unit.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to speed.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from seconds to neutral unit.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to Bark.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to Bpm.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from Bark to neutral unit.
TT_OBJECT_CONSTRUCTOR
Constructor macro.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34