Jamoma API  0.6.0.a19
TimeDataspace.test.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup foundationDataspaceLib
4  *
5  * @brief Unit tests for the #TimeDataspace.
6  *
7  * @authors Trond Lossius, Tim Place, Nils Peters, ...
8  *
9  * @copyright Copyright © 2011 Trond Lossius @n
10  * This code is licensed under the terms of the "New BSD License" @n
11  * http://creativecommons.org/licenses/BSD/
12  */
13 
14 
15 // Dataspaces and Units employ C++ double-inheritance and are thus unsuitable for direct use
16 // through the usual TTObject API
17 #define TT_NO_DEPRECATION_WARNINGS
18 
19 #include "TimeDataspace.h"
20 
21 
22 TTErr TimeDataspace::test(TTValue& returnedTestInfo)
23 {
24  int errorCount = 0;
25  int testAssertionCount = 0;
26 
27  // Create dataspace object and set to time
28  try {
29 
30  TTObject myDataspace("dataspace");
31  myDataspace.set(TT("dataspace"), TT("time"));
32 
33  TTValue v;
34  TTValue expected;
35 
36 
37  /************************************************/
38  /* */
39  /* Test conversions to neutral unit */
40  /* */
41  /************************************************/
42 
43 
44  // Second to second
45 
46  myDataspace.set(TT("inputUnit"), TT("second"));
47  myDataspace.set(TT("outputUnit"), TT("second"));
48 
49  v = TTValue(256.);
50  expected = TTValue(256.);
51 
52  myDataspace.send(TT("convert"), v, v);
53 
54  TTTestAssertion("Second to second",
55  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
56  testAssertionCount,
57  errorCount);
58 
59 
60 
61  // Millisecond => second
62 
63  myDataspace.set(TT("inputUnit"), TT("millisecond"));
64  myDataspace.set(TT("outputUnit"), TT("second"));
65 
66  v = TTValue(1234.5);
67  expected = TTValue(1.2345);
68 
69  myDataspace.send(TT("convert"), v, v);
70 
71  TTTestAssertion("Millisecond to second",
72  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
73  testAssertionCount,
74  errorCount);
75 
76 
77  // Sample => second
78  // We need to find the sample rate
79 
80  myDataspace.set(TT("inputUnit"), TT("sample"));
81  myDataspace.set(TT("outputUnit"), TT("second"));
82 
83  TTValue globalSampleRate;
84  ttEnvironment->getAttributeValue(kTTSym_sampleRate, globalSampleRate);
85 
86  v = TTValue(1.23*TTFloat64(globalSampleRate));
87  expected = TTValue(1.23);
88 
89  myDataspace.send(TT("convert"), v, v);
90 
91  TTTestAssertion("Sample to second",
92  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
93  testAssertionCount,
94  errorCount);
95 
96 
97  // Rate (Hz) => second
98 
99  myDataspace.set(TT("inputUnit"), TT("Hz"));
100  myDataspace.set(TT("outputUnit"), TT("second"));
101 
102  v = TTValue(4.);
103  expected = TTValue(0.25);
104 
105  myDataspace.send(TT("convert"), v, v);
106 
107  TTTestAssertion("Frequency (Hz) to second",
108  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
109  testAssertionCount,
110  errorCount);
111 
112 
113  // Beats per minute => second
114 
115  myDataspace.set(TT("inputUnit"), TT("bpm"));
116  myDataspace.set(TT("outputUnit"), TT("second"));
117 
118  v = TTValue(120.);
119  expected = TTValue(0.5);
120 
121  myDataspace.send(TT("convert"), v, v);
122 
123  TTTestAssertion("Beats per minute to second",
124  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
125  testAssertionCount,
126  errorCount);
127 
128  // midi note => second (2 tests at midi notes 57 and 69)
129 
130  myDataspace.set(TT("inputUnit"), TT("midinote"));
131  myDataspace.set(TT("outputUnit"), TT("second"));
132 
133  v = TTValue(57.);
134  expected = TTValue(1./220.);
135 
136  myDataspace.send(TT("convert"), v, v);
137 
138  TTTestAssertion("midi note 57 to second",
139  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
140  testAssertionCount,
141  errorCount);
142 
143  v = TTValue(69.);
144  expected = TTValue(1./440.);
145 
146  myDataspace.send(TT("convert"), v, v);
147 
148  TTTestAssertion("midi note 69 to second",
149  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
150  testAssertionCount,
151  errorCount);
152 
153 
154  // cents => second (2 tests at cents values 5700 and 6900)
155 
156  myDataspace.set(TT("inputUnit"), TT("cents"));
157  myDataspace.set(TT("outputUnit"), TT("second"));
158 
159  v = TTValue(5700.);
160  expected = TTValue(1./220.);
161 
162  myDataspace.send(TT("convert"), v, v);
163 
164  TTTestAssertion("Cent value 5700 to second",
165  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
166  testAssertionCount,
167  errorCount);
168 
169  v = TTValue(6900.);
170  expected = TTValue(1./440.);
171 
172  myDataspace.send(TT("convert"), v, v);
173 
174  TTTestAssertion("Cent value 6900 to second",
175  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
176  testAssertionCount,
177  errorCount);
178 
179 
180  // Bark => second
181 
182  myDataspace.set(TT("inputUnit"), TT("bark"));
183  myDataspace.set(TT("outputUnit"), TT("second"));
184 
185  v = TTValue(5.0);
186  expected = TTValue(0.001785990780318596);
187 
188  myDataspace.send(TT("convert"), v, v);
189 
190  TTTestAssertion("Bark to second",
191  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
192  testAssertionCount,
193  errorCount);
194 
195  // Mel => second
196 
197  myDataspace.set(TT("inputUnit"), TT("mel"));
198  myDataspace.set(TT("outputUnit"), TT("second"));
199 
200  v = TTValue(1000.0);
201  expected = TTValue(0.0009999781840186604);
202 
203  myDataspace.send(TT("convert"), v, v);
204 
205  TTTestAssertion("Mel to second",
206  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
207  testAssertionCount,
208  errorCount);
209 
210  // speed => seconds
211  // Rather than checking this, there are tests for speed <=> midi note further down
212 
213  /************************************************/
214  /* */
215  /* Test conversions from neutral unit */
216  /* */
217  /************************************************/
218 
219  // Second => Millisecond
220 
221  myDataspace.set(TT("inputUnit"), TT("second"));
222  myDataspace.set(TT("outputUnit"), TT("millisecond"));
223 
224  v = TTValue(1.2345);
225  expected = TTValue(1234.5);
226 
227  myDataspace.send(TT("convert"), v, v);
228 
229  TTTestAssertion("Second to millisecond",
230  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
231  testAssertionCount,
232  errorCount);
233 
234 
235  // Second => sample
236  // We need to find the sample rate
237 
238  myDataspace.set(TT("inputUnit"), TT("second"));
239  myDataspace.set(TT("outputUnit"), TT("sample"));
240 
241  ttEnvironment->getAttributeValue(kTTSym_sampleRate, globalSampleRate);
242 
243  v = TTValue(192000./TTFloat64(globalSampleRate));
244  expected = TTValue(192000.);
245 
246  myDataspace.send(TT("convert"), v, v);
247 
248  TTTestAssertion("Second to sample",
249  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
250  testAssertionCount,
251  errorCount);
252 
253 
254  // Second => rate (Hz)
255 
256  myDataspace.set(TT("inputUnit"), TT("second"));
257  myDataspace.set(TT("outputUnit"), TT("Hz"));
258 
259  v = TTValue(0.25);
260  expected = TTValue(4.);
261 
262  myDataspace.send(TT("convert"), v, v);
263 
264  TTTestAssertion("Second to frequency (Hz)",
265  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
266  testAssertionCount,
267  errorCount);
268 
269 
270  // Second => Beats per minute
271 
272  myDataspace.set(TT("inputUnit"), TT("second"));
273  myDataspace.set(TT("outputUnit"), TT("bpm"));
274 
275  v = TTValue(0.5);
276  expected = TTValue(120.);
277 
278  myDataspace.send(TT("convert"), v, v);
279 
280  TTTestAssertion("Seconds to beats per minute",
281  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
282  testAssertionCount,
283  errorCount);
284 
285  // Second => midi note (2 tests at midi notes 57 and 69)
286 
287  myDataspace.set(TT("inputUnit"), TT("second"));
288  myDataspace.set(TT("outputUnit"), TT("midinote"));
289 
290  v = TTValue(1./220.);
291  expected = TTValue(57.);
292 
293  myDataspace.send(TT("convert"), v, v);
294 
295  TTTestAssertion("Second to midi note 57",
296  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
297  testAssertionCount,
298  errorCount);
299 
300  v = TTValue(1./440.);
301  expected = TTValue(69.);
302 
303  myDataspace.send(TT("convert"), v, v);
304 
305  TTTestAssertion("Second to midi note 69",
306  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
307  testAssertionCount,
308  errorCount);
309 
310 
311  // Second => cents (2 tests at cent values 5700 and 6900)
312 
313  myDataspace.set(TT("inputUnit"), TT("second"));
314  myDataspace.set(TT("outputUnit"), TT("cents"));
315 
316  v = TTValue(1./220.);
317  expected = TTValue(5700.);
318 
319  myDataspace.send(TT("convert"), v, v);
320 
321  TTTestAssertion("Second to cent value 5700",
322  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
323  testAssertionCount,
324  errorCount);
325 
326  v = TTValue(1./440.);
327  expected = TTValue(6900.);
328 
329  myDataspace.send(TT("convert"), v, v);
330 
331  TTTestAssertion("Second to cent value 6900",
332  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
333  testAssertionCount,
334  errorCount);
335 
336  // Second => Bark
337 
338  myDataspace.set(TT("inputUnit"), TT("second"));
339  myDataspace.set(TT("outputUnit"), TT("bark"));
340 
341  v = TTValue(0.001785990780318596);
342  expected = TTValue(5.0);
343 
344  myDataspace.send(TT("convert"), v, v);
345 
346  TTTestAssertion("Seconds to bark scale",
347  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
348  testAssertionCount,
349  errorCount);
350 
351  // Second => Mel
352 
353  myDataspace.set(TT("inputUnit"), TT("second"));
354  myDataspace.set(TT("outputUnit"), TT("mel"));
355 
356  v = TTValue(0.001);
357  expected = TTValue(999.9855371396243);
358 
359  myDataspace.send(TT("convert"), v, v);
360 
361  TTTestAssertion("Seconds to mel scale",
362  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
363  testAssertionCount,
364  errorCount);
365 
366  // Second => Speed
367  // Rather than checking this, there are tests for speed <=> midi note further down
368 
369  /************************************************/
370  /* */
371  /* Tests bypassing neutral unit */
372  /* - where this helps predict expected result */
373  /* */
374  /************************************************/
375 
376  // Speed => midi note (tests for Speed = 0.5, 1.0 and 2)
377 
378  myDataspace.set(TT("inputUnit"), TT("speed"));
379  myDataspace.set(TT("outputUnit"), TT("midinote"));
380 
381  v = TTValue(0.5);
382  expected = TTValue(-12.0);
383 
384  myDataspace.send(TT("convert"), v, v);
385 
386  TTTestAssertion("0.5 speed to midi note",
387  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
388  testAssertionCount,
389  errorCount);
390 
391  v = TTValue(1.0);
392  expected = TTValue(0.0);
393 
394  myDataspace.send(TT("convert"), v, v);
395 
396  TTTestAssertion("1.0 speed to midi note",
397  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
398  testAssertionCount,
399  errorCount);
400 
401  v = TTValue(2.0);
402  expected = TTValue(12.0);
403 
404  myDataspace.send(TT("convert"), v, v);
405 
406  TTTestAssertion("2.0 speed to midi note",
407  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
408  testAssertionCount,
409  errorCount);
410 
411  // midi note => Speed (tests for Speed = 0.5, 1.0 and 2)*/
412 
413  myDataspace.set(TT("inputUnit"), TT("midinote"));
414  myDataspace.set(TT("outputUnit"), TT("speed"));
415 
416  v = TTValue(-12.0);
417  expected = TTValue(0.5);
418 
419  myDataspace.send(TT("convert"), v, v);
420 
421  TTTestAssertion("-12 midi note to speed",
422  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
423  testAssertionCount,
424  errorCount);
425 
426  v = TTValue(0.0);
427  expected = TTValue(1.0);
428 
429  myDataspace.send(TT("convert"), v, v);
430 
431  TTTestAssertion("0 midi note to speed",
432  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
433  testAssertionCount,
434  errorCount);
435 
436  v = TTValue(12.0);
437  expected = TTValue(2.0);
438 
439  myDataspace.send(TT("convert"), v, v);
440 
441  TTTestAssertion("12 midi note to speed",
442  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
443  testAssertionCount,
444  errorCount);
445 
446 
447  // Hz => Mel
448 
449  myDataspace.set(TT("inputUnit"), TT("Hz"));
450  myDataspace.set(TT("outputUnit"), TT("mel"));
451 
452  v = TTValue(1000.0);
453  expected = TTValue(999.9855371396243);
454 
455  myDataspace.send(TT("convert"), v, v);
456 
457  TTTestAssertion("Hz to mel scale",
458  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
459  testAssertionCount,
460  errorCount);
461 
462  // Mel => Hz
463 
464  myDataspace.set(TT("inputUnit"), TT("mel"));
465  myDataspace.set(TT("outputUnit"), TT("Hz"));
466 
467  v = TTValue(999.9855371396243);
468  expected = TTValue(1000.0);
469 
470  myDataspace.send(TT("convert"), v, v);
471 
472  TTTestAssertion("mel scale to Hz",
473  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
474  testAssertionCount,
475  errorCount);
476 
477  }
478  catch (...) {
479  TTLogMessage("TimeDataspace::test TOTAL FAILURE");
480  errorCount = 1;
481  testAssertionCount = 1;
482  }
483 
484  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
485 }
TTFOUNDATION_EXPORT TTEnvironment * ttEnvironment
The environment object has one instance, which is global in scope.
Create and use Jamoma object instances.
Definition: TTObject.h:29
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.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
void set(const TTUInt16 index, const T &anElementValue)
DEPRECATED.
Definition: TTValue.h:569
void TTFOUNDATION_EXPORT TTLogMessage(TTImmutableCString message,...)
Platform and host independent method for posting log messages.
Definition: TTBase.cpp:534
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34