Jamoma API  0.6.0.a19
TTAverage.test.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspAnalysisLib
4  *
5  * @brief Unit tests for the #TTAverage object for Jamoma DSP
6  *
7  * @details
8  *
9  * @authors Nils Peters, Tim Place, Trond Lossius
10  *
11  * @copyright Copyright © 2011, Nils Peters, 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 "TTAverage.h"
18 
19 
20 TTErr TTAverage::test(TTValue& returnedTestInfo)
21 {
22  // preliminary setup
23  int errorCount = 0;
24  int testAssertionCount = 0;
25 
26  // Set up audio signals
27  int badSampleCount = 0;
28  TTAudioSignalPtr input = NULL;
29  TTAudioSignalPtr output = NULL;
30 
31  // Create 1 channel audio signal objects
32  TTObjectBaseInstantiate(kTTSym_audiosignal, &input, 1);
33  TTObjectBaseInstantiate(kTTSym_audiosignal, &output, 1);
34  input->allocWithVectorSize(64);
35  output->allocWithVectorSize(64);
36 
37 
38  /*************************************************************
39  *
40  * bipolar mode unit test
41  *
42  *************************************************************/
43 
44 
45  // Create an impulse
46  input->clear(); // set all samples to zero
47  input->mSampleVectors[0][10] = -2.0; // set the first sample to -2
48  input->mSampleVectors[0][11] = -2.0; // set the second sample to -2
49 
50  // Setup the delay
51  this->setAttributeValue(TT("interval"), 4);
52  //this->setAttributeValue(TT("delayInSamples"), 1);
53  this->setAttributeValue(TT("mode"), TT("bipolar"));
54  this->process(input, output);
55 
56 
57  TTFloat64 expectedImpulseResponseBipolar[64] = {
58  0.0000000000000000e+00, // N = 0
59  0.0000000000000000e+00,
60  0.0000000000000000e+00,
61  0.0000000000000000e+00,
62  0.0000000000000000e+00,
63  0.0000000000000000e+00,
64  0.0000000000000000e+00,
65  0.0000000000000000e+00,
66  0.0000000000000000e+00,
67  0.0000000000000000e+00, // N = 9 : Average of { 0.0, 0.0, 0.0, 0.0}
68  -0.5, // N = 10 : Average of {-2.0, 0.0, 0.0, 0.0}
69  -1.0, // N = 11 : Average of {-2.0, -2.0, 0.0, 0.0}
70  -1.0, // N = 12 : Average of { 0.0, -2.0, -2.0, 0.0}
71  -1.0, // N = 13 : Average of { 0.0, 0.0, -2.0, -2.0}
72  -0.5, // N = 14 : Average of { 0.0, 0.0, 0.0, -2.0}
73  0.0000000000000000e+00, // N = 15 : Average of { 0.0, 0.0, 0.0, 0.0}
74  0.0000000000000000e+00,
75  0.0000000000000000e+00,
76  0.0000000000000000e+00,
77  0.0000000000000000e+00,
78  0.0000000000000000e+00,
79  0.0000000000000000e+00,
80  0.0000000000000000e+00,
81  0.0000000000000000e+00,
82  0.0000000000000000e+00,
83  0.0000000000000000e+00,
84  0.0000000000000000e+00,
85  0.0000000000000000e+00,
86  0.0000000000000000e+00,
87  0.0000000000000000e+00,
88  0.0000000000000000e+00,
89  0.0000000000000000e+00,
90  0.0000000000000000e+00,
91  0.0000000000000000e+00,
92  0.0000000000000000e+00,
93  0.0000000000000000e+00,
94  0.0000000000000000e+00,
95  0.0000000000000000e+00,
96  0.0000000000000000e+00,
97  0.0000000000000000e+00,
98  0.0000000000000000e+00,
99  0.0000000000000000e+00,
100  0.0000000000000000e+00,
101  0.0000000000000000e+00,
102  0.0000000000000000e+00,
103  0.0000000000000000e+00,
104  0.0000000000000000e+00,
105  0.0000000000000000e+00,
106  0.0000000000000000e+00,
107  0.0000000000000000e+00,
108  0.0000000000000000e+00,
109  0.0000000000000000e+00,
110  0.0000000000000000e+00,
111  0.0000000000000000e+00,
112  0.0000000000000000e+00,
113  0.0000000000000000e+00,
114  0.0000000000000000e+00,
115  0.0000000000000000e+00,
116  0.0000000000000000e+00,
117  0.0000000000000000e+00,
118  0.0000000000000000e+00,
119  0.0000000000000000e+00,
120  0.0000000000000000e+00,
121  0.0000000000000000e+00
122  };
123 
124  for (int i=0; i<64; i++) {
125  TTBoolean result = TTTestFloatEquivalence(output->mSampleVectors[0][i], expectedImpulseResponseBipolar[i]);
126  badSampleCount += !result;
127  if (!result)
128  TTTestLog("BAD SAMPLE @ i=%i ( value=%.10f expected=%.10f )", i, output->mSampleVectors[0][i], expectedImpulseResponseBipolar[i]);
129  }
130 
131  TTTestAssertion("Produces expected results with bipolar mode",
132  badSampleCount == 0,
133  testAssertionCount,
134  errorCount);
135  if (badSampleCount)
136  TTTestLog("badSampleCount is %i", badSampleCount);
137 
138 
139 
140  /*************************************************************
141  *
142  * absolute mode unit test
143  *
144  *************************************************************/
145 
146 
147  // Create an impulse
148  input->clear(); // set all samples to zero
149  input->mSampleVectors[0][10] = -2.0; // set the first sample to -2
150  input->mSampleVectors[0][11] = -2.0; // set the second sample to -2
151 
152  // Set mode and do audio prosessing
153  this->setAttributeValue(TT("mode"), TT("absolute"));
154  this->process(input, output);
155 
156 
157  TTFloat64 expectedImpulseResponseAbsolute[64] = {
158  0.0000000000000000e+00, // N = 0
159  0.0000000000000000e+00,
160  0.0000000000000000e+00,
161  0.0000000000000000e+00,
162  0.0000000000000000e+00,
163  0.0000000000000000e+00,
164  0.0000000000000000e+00,
165  0.0000000000000000e+00,
166  0.0000000000000000e+00,
167  0.0000000000000000e+00, // N = 9 : Average of { 0.0, 0.0, 0.0, 0.0}
168  0.5, // N = 10 : Average of {-2.0, 0.0, 0.0, 0.0}
169  1.0, // N = 11 : Average of {-2.0, -2.0, 0.0, 0.0}
170  1.0, // N = 12 : Average of { 0.0, -2.0, -2.0, 0.0}
171  1.0, // N = 13 : Average of { 0.0, 0.0, -2.0, -2.0}
172  0.5, // N = 14 : Average of { 0.0, 0.0, 0.0, -2.0}
173  0.0000000000000000e+00, // N = 15 : Average of { 0.0, 0.0, 0.0, 0.0}
174  0.0000000000000000e+00,
175  0.0000000000000000e+00,
176  0.0000000000000000e+00,
177  0.0000000000000000e+00,
178  0.0000000000000000e+00,
179  0.0000000000000000e+00,
180  0.0000000000000000e+00,
181  0.0000000000000000e+00,
182  0.0000000000000000e+00,
183  0.0000000000000000e+00,
184  0.0000000000000000e+00,
185  0.0000000000000000e+00,
186  0.0000000000000000e+00,
187  0.0000000000000000e+00,
188  0.0000000000000000e+00,
189  0.0000000000000000e+00,
190  0.0000000000000000e+00,
191  0.0000000000000000e+00,
192  0.0000000000000000e+00,
193  0.0000000000000000e+00,
194  0.0000000000000000e+00,
195  0.0000000000000000e+00,
196  0.0000000000000000e+00,
197  0.0000000000000000e+00,
198  0.0000000000000000e+00,
199  0.0000000000000000e+00,
200  0.0000000000000000e+00,
201  0.0000000000000000e+00,
202  0.0000000000000000e+00,
203  0.0000000000000000e+00,
204  0.0000000000000000e+00,
205  0.0000000000000000e+00,
206  0.0000000000000000e+00,
207  0.0000000000000000e+00,
208  0.0000000000000000e+00,
209  0.0000000000000000e+00,
210  0.0000000000000000e+00,
211  0.0000000000000000e+00,
212  0.0000000000000000e+00,
213  0.0000000000000000e+00,
214  0.0000000000000000e+00,
215  0.0000000000000000e+00,
216  0.0000000000000000e+00,
217  0.0000000000000000e+00,
218  0.0000000000000000e+00,
219  0.0000000000000000e+00,
220  0.0000000000000000e+00,
221  0.0000000000000000e+00
222  };
223 
224  for (int i=0; i<64; i++) {
225  TTBoolean result = TTTestFloatEquivalence(output->mSampleVectors[0][i], expectedImpulseResponseAbsolute[i]);
226  badSampleCount += !result;
227  if (!result)
228  TTTestLog("BAD SAMPLE @ i=%i ( value=%.10f expected=%.10f )", i, output->mSampleVectors[0][i], expectedImpulseResponseAbsolute[i]);
229  }
230 
231  TTTestAssertion("Produces expected results with absolute mode",
232  badSampleCount == 0,
233  testAssertionCount,
234  errorCount);
235  if (badSampleCount)
236  TTTestLog("badSampleCount is %i", badSampleCount);
237 
238 
239  /*************************************************************
240  *
241  * rms mode unit test
242  *
243  *************************************************************/
244 
245 
246  // Create an impulse
247  input->clear(); // set all samples to zero
248  input->mSampleVectors[0][10] = -3.0; // set the first sample to -3
249  input->mSampleVectors[0][11] = 4.0; // set the second sample to -4
250 
251  // Set mode and do audio prosessing
252  this->setAttributeValue(TT("mode"), TT("rms"));
253  this->process(input, output);
254 
255 
256  TTFloat64 expectedImpulseResponseRms[64] = {
257  0.0000000000000000e+00, // N = 0
258  0.0000000000000000e+00,
259  0.0000000000000000e+00,
260  0.0000000000000000e+00,
261  0.0000000000000000e+00,
262  0.0000000000000000e+00,
263  0.0000000000000000e+00,
264  0.0000000000000000e+00,
265  0.0000000000000000e+00,
266  0.0000000000000000e+00, // N = 9 : Average of { 0.0, 0.0, 0.0, 0.0}
267  1.5, // N = 10 : Average of {-3.0, 0.0, 0.0, 0.0}
268  2.5, // N = 11 : Average of { 4.0, -3.0, 0.0, 0.0}
269  2.5, // N = 12 : Average of { 0.0, 4.0, -3.0, 0.0}
270  2.5, // N = 13 : Average of { 0.0, 0.0, 4.0, -3.0}
271  2.0, // N = 14 : Average of { 0.0, 0.0, 0.0, 4.0}
272  0.0000000000000000e+00, // N = 15 : Average of { 0.0, 0.0, 0.0, 0.0}
273  0.0000000000000000e+00,
274  0.0000000000000000e+00,
275  0.0000000000000000e+00,
276  0.0000000000000000e+00,
277  0.0000000000000000e+00,
278  0.0000000000000000e+00,
279  0.0000000000000000e+00,
280  0.0000000000000000e+00,
281  0.0000000000000000e+00,
282  0.0000000000000000e+00,
283  0.0000000000000000e+00,
284  0.0000000000000000e+00,
285  0.0000000000000000e+00,
286  0.0000000000000000e+00,
287  0.0000000000000000e+00,
288  0.0000000000000000e+00,
289  0.0000000000000000e+00,
290  0.0000000000000000e+00,
291  0.0000000000000000e+00,
292  0.0000000000000000e+00,
293  0.0000000000000000e+00,
294  0.0000000000000000e+00,
295  0.0000000000000000e+00,
296  0.0000000000000000e+00,
297  0.0000000000000000e+00,
298  0.0000000000000000e+00,
299  0.0000000000000000e+00,
300  0.0000000000000000e+00,
301  0.0000000000000000e+00,
302  0.0000000000000000e+00,
303  0.0000000000000000e+00,
304  0.0000000000000000e+00,
305  0.0000000000000000e+00,
306  0.0000000000000000e+00,
307  0.0000000000000000e+00,
308  0.0000000000000000e+00,
309  0.0000000000000000e+00,
310  0.0000000000000000e+00,
311  0.0000000000000000e+00,
312  0.0000000000000000e+00,
313  0.0000000000000000e+00,
314  0.0000000000000000e+00,
315  0.0000000000000000e+00,
316  0.0000000000000000e+00,
317  0.0000000000000000e+00,
318  0.0000000000000000e+00,
319  0.0000000000000000e+00,
320  0.0000000000000000e+00
321  };
322 
323  for (int i=0; i<64; i++) {
324  TTBoolean result = TTTestFloatEquivalence(output->mSampleVectors[0][i], expectedImpulseResponseRms[i]);
325  badSampleCount += !result;
326  if (!result)
327  TTTestLog("BAD SAMPLE @ i=%i ( value=%.10f expected=%.10f )", i, output->mSampleVectors[0][i], expectedImpulseResponseRms[i]);
328  }
329 
330  TTTestAssertion("Produces expected results with rms mode",
331  badSampleCount == 0,
332  testAssertionCount,
333  errorCount);
334  if (badSampleCount)
335  TTTestLog("badSampleCount is %i", badSampleCount);
336 
337 
338  // Free objects
339 
340  TTObjectBaseRelease(&input);
341  TTObjectBaseRelease(&output);
342 
343 
344  // Wrap up the test results to pass back to whoever called this test
345  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
346 }
347 
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
TTAverage - measuring of averaged and RMS signal energy
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
virtual TTErr test(TTValue &returnedTestInfo)
Unit test.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTErr clear()
Zero out all of the sample values in the audio signal.
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