Jamoma API  0.6.0.a19
TTPhasor.test.cpp
1 /*
2  * Unit tests for the Phasor Object for Jamoma DSP
3  * Copyright © 2011, Nils Peters
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 #include "TTPhasor.h"
10 
11 
12 TTErr TTPhasor::test(TTValue& returnedTestInfo)
13 {
14  int errorCount = 0;
15  int testAssertionCount = 0;
16  int badSampleCount = 0;
17  int badSampleCountTotal = 0;
18  TTAudioSignalPtr output = NULL;
19 
20  /*0. We assume that have an object
21 
22  1. We set the frequency and gain
23 
24  2. We process N vectors, each containing M samples
25 
26  3. We check to see that several of the samples are what we expect
27 
28  4. Optionally go back to #1 with some different settings and repeat
29  */
30 
31  // create 1 channel audio signal objects
32  TTObjectBaseInstantiate(kTTSym_audiosignal, &output, 1);
33  output->allocWithVectorSize(64);
34 
35  // setup the generator
36  this->setAttributeValue(TT("frequency"), sr/64.0);
37  this->setAttributeValue(TT("gain"), 0);
38  this->setAttributeValue(TT("phase"), 0);
39  this->process(output);
40  // created with Octave: linspace(0,1,64)
41  TTFloat64 expectedSignalTest1[64] = {
42  0.0,
43  1.5873015873015872e-02,
44  3.1746031746031744e-02,
45  4.7619047619047616e-02,
46  6.3492063492063489e-02,
47  7.9365079365079361e-02,
48  9.5238095238095233e-02,
49  1.1111111111111110e-01,
50  1.2698412698412698e-01,
51  1.4285714285714285e-01,
52  1.5873015873015872e-01,
53  1.7460317460317459e-01,
54  1.9047619047619047e-01,
55  2.0634920634920634e-01,
56  2.2222222222222221e-01,
57  2.3809523809523808e-01,
58  2.5396825396825395e-01,
59  2.6984126984126983e-01,
60  2.8571428571428570e-01,
61  3.0158730158730157e-01,
62  3.1746031746031744e-01,
63  3.3333333333333331e-01,
64  3.4920634920634919e-01,
65  3.6507936507936506e-01,
66  3.8095238095238093e-01,
67  3.9682539682539680e-01,
68  4.1269841269841268e-01,
69  4.2857142857142855e-01,
70  4.4444444444444442e-01,
71  4.6031746031746029e-01,
72  4.7619047619047616e-01,
73  4.9206349206349204e-01,
74  5.0793650793650791e-01,
75  5.2380952380952384e-01,
76  5.3968253968253965e-01,
77  5.5555555555555558e-01,
78  5.7142857142857140e-01,
79  5.8730158730158732e-01,
80  6.0317460317460314e-01,
81  6.1904761904761907e-01,
82  6.3492063492063489e-01,
83  6.5079365079365081e-01,
84  6.6666666666666663e-01,
85  6.8253968253968256e-01,
86  6.9841269841269837e-01,
87  7.1428571428571430e-01,
88  7.3015873015873012e-01,
89  7.4603174603174605e-01,
90  7.6190476190476186e-01,
91  7.7777777777777779e-01,
92  7.9365079365079361e-01,
93  8.0952380952380953e-01,
94  8.2539682539682535e-01,
95  8.4126984126984128e-01,
96  8.5714285714285710e-01,
97  8.7301587301587302e-01,
98  8.8888888888888884e-01,
99  9.0476190476190477e-01,
100  9.2063492063492058e-01,
101  9.3650793650793651e-01,
102  9.5238095238095233e-01,
103  9.6825396825396826e-01,
104  9.8412698412698407e-01,
105  1.0,
106  };
107 
108  for (int i=0; i<64; i++) {
109  TTBoolean result = !TTTestFloatEquivalence(output->mSampleVectors[0][i], expectedSignalTest1[i]);
110  badSampleCount += result;
111  if (result)
112  TTTestLog("BAD SAMPLE @ i=%i ( value=%.10f expected=%.10f )", i, output->mSampleVectors[0][i], expectedSignalTest1[i]);
113  }
114 
115  TTTestAssertion("Test 1: Produces correct ramp from 0 to 1 when a positive Frequency is defined",
116  badSampleCount == 0,
117  testAssertionCount,
118  errorCount);
119  if (badSampleCount)
120  TTTestLog("badSampleCount is %i", badSampleCount);
121 
122  badSampleCountTotal += badSampleCount;
123  //reinitializing for next test
124  badSampleCount = 0;
125 
126  // Second test: now with a negative Frequency: ramp should go from 1 to 0
127 
128  // setup the generator
129  this->setAttributeValue(TT("frequency"), sr/-64.0);
130  this->setAttributeValue(TT("gain"), 0);
131  this->setAttributeValue(TT("phase"), 0);
132  this->process(output);
133  // created with Octave: linspace(1,0,64)
134  TTFloat64 expectedSignalTest2[64] = {
135  1.0,
136  9.8412698412698418e-01,
137  9.6825396825396826e-01,
138  9.5238095238095233e-01,
139  9.3650793650793651e-01,
140  9.2063492063492069e-01,
141  9.0476190476190477e-01,
142  8.8888888888888884e-01,
143  8.7301587301587302e-01,
144  8.5714285714285721e-01,
145  8.4126984126984128e-01,
146  8.2539682539682535e-01,
147  8.0952380952380953e-01,
148  7.9365079365079372e-01,
149  7.7777777777777779e-01,
150  7.6190476190476186e-01,
151  7.4603174603174605e-01,
152  7.3015873015873023e-01,
153  7.1428571428571430e-01,
154  6.9841269841269837e-01,
155  6.8253968253968256e-01,
156  6.6666666666666674e-01,
157  6.5079365079365081e-01,
158  6.3492063492063489e-01,
159  6.1904761904761907e-01,
160  6.0317460317460325e-01,
161  5.8730158730158732e-01,
162  5.7142857142857140e-01,
163  5.5555555555555558e-01,
164  5.3968253968253976e-01,
165  5.2380952380952384e-01,
166  5.0793650793650791e-01,
167  4.9206349206349209e-01,
168  4.7619047619047616e-01,
169  4.6031746031746035e-01,
170  4.4444444444444442e-01,
171  4.2857142857142860e-01,
172  4.1269841269841268e-01,
173  3.9682539682539686e-01,
174  3.8095238095238093e-01,
175  3.6507936507936511e-01,
176  3.4920634920634919e-01,
177  3.3333333333333337e-01,
178  3.1746031746031744e-01,
179  3.0158730158730163e-01,
180  2.8571428571428570e-01,
181  2.6984126984126988e-01,
182  2.5396825396825395e-01,
183  2.3809523809523814e-01,
184  2.2222222222222221e-01,
185  2.0634920634920639e-01,
186  1.9047619047619047e-01,
187  1.7460317460317465e-01,
188  1.5873015873015872e-01,
189  1.4285714285714290e-01,
190  1.2698412698412698e-01,
191  1.1111111111111116e-01,
192  9.5238095238095233e-02,
193  7.9365079365079416e-02,
194  6.3492063492063489e-02,
195  4.7619047619047672e-02,
196  3.1746031746031744e-02,
197  1.5873015873015928e-02,
198  0.0,
199  };
200 
201  for (int i=0; i<64; i++) {
202  TTBoolean result = !TTTestFloatEquivalence(output->mSampleVectors[0][i], expectedSignalTest2[i]);
203  badSampleCount += result;
204  if (result)
205  TTTestLog("BAD SAMPLE @ i=%i ( value=%.10f expected=%.10f )", i, output->mSampleVectors[0][i], expectedSignalTest2[i]);
206  }
207 
208  TTTestAssertion("Test 2: Produces correct ramp from 1 to 0 when a negative Frequency is defined",
209  badSampleCount == 0,
210  testAssertionCount,
211  errorCount);
212  if (badSampleCount)
213  TTTestLog("badSampleCount is %i", badSampleCount);
214 
215  badSampleCountTotal += badSampleCount;
216  //reinitializing for next test
217  badSampleCount = 0;
218 
219  // Third test: two Ramps within a block (we want to check if the jump from 0 to 1 is correct)
220 
221  // setup the generator
222  this->setAttributeValue(TT("frequency"), (sr)/-32.0);
223  this->setAttributeValue(TT("gain"), 0);
224  this->setAttributeValue(TT("phase"), 0);
225  this->process(output);
226  // created with Octave: expectedSignalTest3 = [linspace(1,0,32)';linspace(1,0,32)']
227  TTFloat64 expectedSignalTest3[64] = {
228  1.0,
229  9.6774193548387100e-01,
230  9.3548387096774199e-01,
231  9.0322580645161288e-01,
232  8.7096774193548387e-01,
233  8.3870967741935487e-01,
234  8.0645161290322576e-01,
235  7.7419354838709675e-01,
236  7.4193548387096775e-01,
237  7.0967741935483875e-01,
238  6.7741935483870974e-01,
239  6.4516129032258063e-01,
240  6.1290322580645162e-01,
241  5.8064516129032251e-01,
242  5.4838709677419351e-01,
243  5.1612903225806450e-01,
244  4.8387096774193550e-01,
245  4.5161290322580649e-01,
246  4.1935483870967738e-01,
247  3.8709677419354838e-01,
248  3.5483870967741937e-01,
249  3.2258064516129037e-01,
250  2.9032258064516125e-01,
251  2.5806451612903225e-01,
252  2.2580645161290325e-01,
253  1.9354838709677424e-01,
254  1.6129032258064513e-01,
255  1.2903225806451613e-01,
256  9.6774193548387122e-02,
257  6.4516129032258118e-02,
258  3.2258064516129004e-02,
259  0.0,
260  1.0,
261  9.6774193548387100e-01,
262  9.3548387096774199e-01,
263  9.0322580645161288e-01,
264  8.7096774193548387e-01,
265  8.3870967741935487e-01,
266  8.0645161290322576e-01,
267  7.7419354838709675e-01,
268  7.4193548387096775e-01,
269  7.0967741935483875e-01,
270  6.7741935483870974e-01,
271  6.4516129032258063e-01,
272  6.1290322580645162e-01,
273  5.8064516129032251e-01,
274  5.4838709677419351e-01,
275  5.1612903225806450e-01,
276  4.8387096774193550e-01,
277  4.5161290322580649e-01,
278  4.1935483870967738e-01,
279  3.8709677419354838e-01,
280  3.5483870967741937e-01,
281  3.2258064516129037e-01,
282  2.9032258064516125e-01,
283  2.5806451612903225e-01,
284  2.2580645161290325e-01,
285  1.9354838709677424e-01,
286  1.6129032258064513e-01,
287  1.2903225806451613e-01,
288  9.6774193548387122e-02,
289  6.4516129032258118e-02,
290  3.2258064516129004e-02,
291  0.0,
292 
293  };
294 
295  for (int i=0; i<64; i++) {
296  TTBoolean result = !TTTestFloatEquivalence(output->mSampleVectors[0][i], expectedSignalTest3[i]);
297  badSampleCount += result;
298  if (result)
299  TTTestLog("BAD SAMPLE @ i=%i ( value=%.10f expected=%.10f )", i, output->mSampleVectors[0][i], expectedSignalTest3[i]);
300  }
301 
302  TTTestAssertion("Test 3: Produces correct signal when phase is wrapped from 0 to 1",
303  badSampleCount == 0,
304  testAssertionCount,
305  errorCount);
306 
307  if (badSampleCount)
308  TTTestLog("badSampleCount is %i", badSampleCount);
309 
310  badSampleCountTotal += badSampleCount;
311  //reinitializing for next test
312  badSampleCount = 0;
313 
314  // Forth test: two Ramps within a block (we want to check if the jump from 1 to 0 is correct)
315 
316  // setup the generator
317  this->setAttributeValue(TT("frequency"), (sr)/32.0);
318  this->setAttributeValue(TT("gain"), 0);
319  this->setAttributeValue(TT("phase"), 0);
320  this->process(output);
321  // created with Octave: expectedSignalTest4 = [linspace(,1,32)';linspace(0,1,32)']
322  TTFloat64 expectedSignalTest4[64] = {
323  0.0000000000000000e+00,
324  3.2258064516129031e-02,
325  6.4516129032258063e-02,
326  9.6774193548387094e-02,
327  1.2903225806451613e-01,
328  1.6129032258064516e-01,
329  1.9354838709677419e-01,
330  2.2580645161290322e-01,
331  2.5806451612903225e-01,
332  2.9032258064516131e-01,
333  3.2258064516129031e-01,
334  3.5483870967741937e-01,
335  3.8709677419354838e-01,
336  4.1935483870967744e-01,
337  4.5161290322580644e-01,
338  4.8387096774193550e-01,
339  5.1612903225806450e-01,
340  5.4838709677419351e-01,
341  5.8064516129032262e-01,
342  6.1290322580645162e-01,
343  6.4516129032258063e-01,
344  6.7741935483870963e-01,
345  7.0967741935483875e-01,
346  7.4193548387096775e-01,
347  7.7419354838709675e-01,
348  8.0645161290322576e-01,
349  8.3870967741935487e-01,
350  8.7096774193548387e-01,
351  9.0322580645161288e-01,
352  9.3548387096774188e-01,
353  9.6774193548387100e-01,
354  1.0000000000000000e+00,
355  0.0000000000000000e+00,
356  3.2258064516129031e-02,
357  6.4516129032258063e-02,
358  9.6774193548387094e-02,
359  1.2903225806451613e-01,
360  1.6129032258064516e-01,
361  1.9354838709677419e-01,
362  2.2580645161290322e-01,
363  2.5806451612903225e-01,
364  2.9032258064516131e-01,
365  3.2258064516129031e-01,
366  3.5483870967741937e-01,
367  3.8709677419354838e-01,
368  4.1935483870967744e-01,
369  4.5161290322580644e-01,
370  4.8387096774193550e-01,
371  5.1612903225806450e-01,
372  5.4838709677419351e-01,
373  5.8064516129032262e-01,
374  6.1290322580645162e-01,
375  6.4516129032258063e-01,
376  6.7741935483870963e-01,
377  7.0967741935483875e-01,
378  7.4193548387096775e-01,
379  7.7419354838709675e-01,
380  8.0645161290322576e-01,
381  8.3870967741935487e-01,
382  8.7096774193548387e-01,
383  9.0322580645161288e-01,
384  9.3548387096774188e-01,
385  9.6774193548387100e-01,
386  1.0000000000000000e+00,
387 
388  };
389 
390  for (int i=0; i<64; i++) {
391  TTBoolean result = !TTTestFloatEquivalence(output->mSampleVectors[0][i], expectedSignalTest4[i]);
392  badSampleCount += result;
393  if (result)
394  TTTestLog("BAD SAMPLE @ i=%i ( value=%.10f expected=%.10f )", i, output->mSampleVectors[0][i], expectedSignalTest4[i]);
395  }
396 
397  TTTestAssertion("Test 4: Produces correct signal when phase is wrapped from 1 to 0",
398  badSampleCount == 0,
399  testAssertionCount,
400  errorCount);
401  if (badSampleCount)
402  TTTestLog("badSampleCount is %i", badSampleCount);
403 
404  badSampleCountTotal += badSampleCount;
405  //reinitializing for next test
406  badSampleCount = 0;
407 
408 
409  // Total number of bad samples:
410  if (badSampleCountTotal)
411  TTTestLog("badSampleCountTotal is %i", badSampleCountTotal);
412 
413  TTObjectBaseRelease(&output);
414 
415 
416  // Wrap up the test results to pass back to whoever called this test
417  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
418 }
419 
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set 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
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
TTSampleValue ** mSampleVectors
An array of pointers to the first sample in each vector. Declared Public for fast access...
Definition: TTAudioSignal.h:74
TTErr process(TTAudioSignal &in, TTAudioSignal &out)
Process the input signal, resulting in an output signal.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTErr allocWithVectorSize(const TTUInt16 newVectorSize)
Allocate memory for all channels at the specified vectorsize, if the vectorsize is different from the...
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
virtual TTErr test(TTValue &returnedTestInfo)
Unit Tests.
TTUInt32 sr
Current sample rate being used by this object.