Jamoma API  0.6.0.a19
TTRamp.test.cpp
1 /*
2  * Unit tests for the Ramp 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 "TTRamp.h"
10 
11 
12 TTErr TTRamp::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  // create 1 channel audio signal objects
21  TTObjectBaseInstantiate(kTTSym_audiosignal, &output, 1);
22  output->allocWithVectorSize(64);
23 
24  // setup the generator
25  this->setAttributeValue(TT("destinationValue"), 1.0);
26  this->setAttributeValue(TT("startValue"), 0.0);
27  this->setAttributeValue(TT("mode"), TT("sample"));
28  this->setAttributeValue(TT("rampTime"), 64000.0/sr);
29  this->process(output);
30 
31 
32  // created with Octave: sig = linspace(0,1,64)
33  TTFloat64 expectedSignalTest1[64] = {
34  0.0000000000000000e+00,
35  1.5873015873015872e-02,
36  3.1746031746031744e-02,
37  4.7619047619047616e-02,
38  6.3492063492063489e-02,
39  7.9365079365079361e-02,
40  9.5238095238095233e-02,
41  1.1111111111111110e-01,
42  1.2698412698412698e-01,
43  1.4285714285714285e-01,
44  1.5873015873015872e-01,
45  1.7460317460317459e-01,
46  1.9047619047619047e-01,
47  2.0634920634920634e-01,
48  2.2222222222222221e-01,
49  2.3809523809523808e-01,
50  2.5396825396825395e-01,
51  2.6984126984126983e-01,
52  2.8571428571428570e-01,
53  3.0158730158730157e-01,
54  3.1746031746031744e-01,
55  3.3333333333333331e-01,
56  3.4920634920634919e-01,
57  3.6507936507936506e-01,
58  3.8095238095238093e-01,
59  3.9682539682539680e-01,
60  4.1269841269841268e-01,
61  4.2857142857142855e-01,
62  4.4444444444444442e-01,
63  4.6031746031746029e-01,
64  4.7619047619047616e-01,
65  4.9206349206349204e-01,
66  5.0793650793650791e-01,
67  5.2380952380952384e-01,
68  5.3968253968253965e-01,
69  5.5555555555555558e-01,
70  5.7142857142857140e-01,
71  5.8730158730158732e-01,
72  6.0317460317460314e-01,
73  6.1904761904761907e-01,
74  6.3492063492063489e-01,
75  6.5079365079365081e-01,
76  6.6666666666666663e-01,
77  6.8253968253968256e-01,
78  6.9841269841269837e-01,
79  7.1428571428571430e-01,
80  7.3015873015873012e-01,
81  7.4603174603174605e-01,
82  7.6190476190476186e-01,
83  7.7777777777777779e-01,
84  7.9365079365079361e-01,
85  8.0952380952380953e-01,
86  8.2539682539682535e-01,
87  8.4126984126984128e-01,
88  8.5714285714285710e-01,
89  8.7301587301587302e-01,
90  8.8888888888888884e-01,
91  9.0476190476190477e-01,
92  9.2063492063492058e-01,
93  9.3650793650793651e-01,
94  9.5238095238095233e-01,
95  9.6825396825396826e-01,
96  9.8412698412698407e-01,
97  1.0000000000000000e+00
98  };
99 
100  for (int i=0; i<64; i++) {
101  TTBoolean result = !TTTestFloatEquivalence(output->mSampleVectors[0][i], expectedSignalTest1[i]);
102  badSampleCount += result;
103  if (result)
104  //TTTestLog("BAD SAMPLE @ i=%i ( value=%.10f expected=%.10f )", i, output->mSampleVectors[0][i], expectedSignalTest1[i]);
105  std::cout << "BAD SAMPLE @ n=" << i << " ( value=" << output->mSampleVectors[0][i] << " expected=" << expectedSignalTest1[i] << " )\n";
106  }
107 
108  TTTestAssertion("Test 1: Produces correct ramp from 0 to 1 when a positive Frequency is defined",
109  badSampleCount == 0,
110  testAssertionCount,
111  errorCount);
112 
113  if (badSampleCount)
114  //TTTestLog("badSampleCount is %i", badSampleCount);
115  std::cout << "badSampleCount is " << badSampleCount << "\n";
116 
117  badSampleCountTotal += badSampleCount;
118  //reinitializing for next test
119  badSampleCount = 0;
120 
121  // Second test: now the ramp goes from 1 to 0
122 
123  // setup the generator
124 
125  this->setAttributeValue(TT("startValue"), 1.0);
126  this->setAttributeValue(TT("destinationValue"), 0.0);
127  this->setAttributeValue(TT("mode"), TT("sample"));
128  this->setAttributeValue(TT("rampTime"), 64000.0/sr);
129  this->process(output);
130  // created with Octave: linspace(1,0,64)
131  TTFloat64 expectedSignalTest2[64] = {
132  1.0000000000000000e+00,
133  9.8412698412698418e-01,
134  9.6825396825396826e-01,
135  9.5238095238095233e-01,
136  9.3650793650793651e-01,
137  9.2063492063492069e-01,
138  9.0476190476190477e-01,
139  8.8888888888888884e-01,
140  8.7301587301587302e-01,
141  8.5714285714285721e-01,
142  8.4126984126984128e-01,
143  8.2539682539682535e-01,
144  8.0952380952380953e-01,
145  7.9365079365079372e-01,
146  7.7777777777777779e-01,
147  7.6190476190476186e-01,
148  7.4603174603174605e-01,
149  7.3015873015873023e-01,
150  7.1428571428571430e-01,
151  6.9841269841269837e-01,
152  6.8253968253968256e-01,
153  6.6666666666666674e-01,
154  6.5079365079365081e-01,
155  6.3492063492063489e-01,
156  6.1904761904761907e-01,
157  6.0317460317460325e-01,
158  5.8730158730158732e-01,
159  5.7142857142857140e-01,
160  5.5555555555555558e-01,
161  5.3968253968253976e-01,
162  5.2380952380952384e-01,
163  5.0793650793650791e-01,
164  4.9206349206349209e-01,
165  4.7619047619047616e-01,
166  4.6031746031746035e-01,
167  4.4444444444444442e-01,
168  4.2857142857142860e-01,
169  4.1269841269841268e-01,
170  3.9682539682539686e-01,
171  3.8095238095238093e-01,
172  3.6507936507936511e-01,
173  3.4920634920634919e-01,
174  3.3333333333333337e-01,
175  3.1746031746031744e-01,
176  3.0158730158730163e-01,
177  2.8571428571428570e-01,
178  2.6984126984126988e-01,
179  2.5396825396825395e-01,
180  2.3809523809523814e-01,
181  2.2222222222222221e-01,
182  2.0634920634920639e-01,
183  1.9047619047619047e-01,
184  1.7460317460317465e-01,
185  1.5873015873015872e-01,
186  1.4285714285714290e-01,
187  1.2698412698412698e-01,
188  1.1111111111111116e-01,
189  9.5238095238095233e-02,
190  7.9365079365079416e-02,
191  6.3492063492063489e-02,
192  4.7619047619047672e-02,
193  3.1746031746031744e-02,
194  1.5873015873015928e-02,
195  0.0000000000000000e+00
196  };
197 
198  for (int i=0; i<64; i++) {
199  TTBoolean result = !TTTestFloatEquivalence(output->mSampleVectors[0][i], expectedSignalTest2[i]);
200  badSampleCount += result;
201  if (result)
202  //TTTestLog("BAD SAMPLE @ i=%i ( value=%.10f expected=%.10f )", i, output->mSampleVectors[0][i], expectedSignalTest2[i]);
203  std::cout << "BAD SAMPLE @ n=" << i << " ( value=" << output->mSampleVectors[0][i] << " expected=" << expectedSignalTest2[i] << " )\n";
204  }
205 
206  TTTestAssertion("Test 2: Produces correct ramp from 1 to 0 when a negative Frequency is defined",
207  badSampleCount == 0,
208  testAssertionCount,
209  errorCount);
210 
211  if (badSampleCount)
212  //TTTestLog("badSampleCount is %i", badSampleCount);
213  std::cout << "badSampleCount is " << badSampleCount << "\n";
214 
215  badSampleCountTotal += badSampleCount;
216  //reinitializing for next test
217  badSampleCount = 0;
218 
219 
220  // Finish //
221 
222  // Total number of bad samples:
223  if (badSampleCountTotal)
224  TTTestLog("badSampleCountTotal is %i", badSampleCountTotal);
225 
226  TTObjectBaseRelease(&output);
227 
228 
229  // Wrap up the test results to pass back to whoever called this test
230  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
231 }
232 
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
virtual TTErr test(TTValue &returnedTestInfo)
Unit Tests.
Definition: TTRamp.test.cpp:12
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
TTUInt32 sr
Current sample rate being used by this object.