Tempo ramps - make ramp test more challenging.
[ardour.git] / libs / ardour / test / dsp_load_calculator_test.cc
1 #include <iostream>
2
3 #include "ardour/dsp_load_calculator.h"
4
5 #include "dsp_load_calculator_test.h"
6
7 CPPUNIT_TEST_SUITE_REGISTRATION (DSPLoadCalculatorTest);
8
9
10 #if defined(PLATFORM_WINDOWS) && defined(COMPILER_MINGW)
11 /* cppunit-1.13.2  uses assertion_traits<double>
12  *    sprintf( , "%.*g", precision, x)
13  * to format a double. The actual comparison is performed on a string.
14  * This is problematic with mingw/windows|wine, "%.*g" formatting fails.
15  *
16  * This quick hack compares float, however float compatisons are at most Y.MMMM+eXX,
17  * the max precision needs to be limited. to the last mantissa digit.
18  *
19  * Anyway, actual maths is verified with Linux and OSX unit-tests,
20  * and this needs to go to https://sourceforge.net/p/cppunit/bugs/
21  */
22 #include <math.h>
23 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(A,B,P) CPPUNIT_ASSERT_EQUAL((float)rint ((A) / (P)),(float)rint ((B) / (P)))
24 #endif
25
26 using namespace std;
27 using namespace ARDOUR;
28
29 void
30 DSPLoadCalculatorTest::basicTest ()
31 {
32         DSPLoadCalculator dsp_calc;
33
34         dsp_calc.set_max_time(48000, 512);
35         int64_t dsp_100_pc_48k_us = 10666;
36
37         CPPUNIT_ASSERT(dsp_calc.get_max_time_us() == dsp_100_pc_48k_us);
38
39         // test equivalent of 10% load
40         dsp_calc.set_start_timestamp_us(0);
41         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us/10);
42         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 0.1f);
43
44         // test equivalent of 50% load and check that the load jumps to 50 percent
45         dsp_calc.set_start_timestamp_us(0);
46         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us/2);
47         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 0.5f);
48
49         // test equivalent of 100% load
50         dsp_calc.set_start_timestamp_us(0);
51         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us);
52         CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() == dsp_100_pc_48k_us);
53         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 1.0f);
54
55         // test setting the equivalent of 100% twice doesn't lead to a dsp value > 1.0
56         dsp_calc.set_start_timestamp_us(dsp_100_pc_48k_us);
57         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us * 2);
58         CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() == dsp_100_pc_48k_us);
59         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 1.0f);
60
61         // test setting the equivalent of 200% clamps the value to 1.0
62         dsp_calc.set_start_timestamp_us(dsp_100_pc_48k_us);
63         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us * 3);
64         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() == 1.0f);
65
66         // test setting the an stop timestamp before the start timestamp is ignored
67         // and the previous dsp value is returned
68         dsp_calc.set_start_timestamp_us(dsp_100_pc_48k_us * 2);
69         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us);
70         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() == 1.0f);
71
72         float dsp_load = dsp_calc.get_dsp_load();
73
74         // test setting the equivalent of beyond the max_timer_error_us is ignored and
75         // the previous dsp value is returned
76         dsp_calc.set_start_timestamp_us (0);
77         dsp_calc.set_stop_timestamp_us (dsp_100_pc_48k_us*10);
78         CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() > dsp_calc.max_timer_error_us());
79         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() == dsp_load);
80
81         // test the rate of rolloff of the LPF from 100% with load at constant 50%
82         // over the equivalent of 1 second
83         for (int i = 0; i < 1e6 / dsp_100_pc_48k_us; ++i) {
84                 dsp_calc.set_start_timestamp_us(0);
85                 dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us / 2);
86                 CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() == 5333);
87                 CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 1.0);
88                 CPPUNIT_ASSERT(dsp_calc.get_dsp_load() >= 0.5);
89 #if 0
90                 std::cout << "DSP 50% load value = " << dsp_calc.get_dsp_load() << std::endl;
91 #endif
92         }
93
94         // test that the LPF is still working after one second of values
95         // TODO need to work out what is required in terms of responsiveness etc
96         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() > 0.5f);
97
98         // compare 96k to 48k
99         DSPLoadCalculator dsp_calc_96k;
100         dsp_calc_96k.set_max_time(96000, 512);
101         int64_t dsp_100_pc_96k_us = 5333;
102
103         // reset both to 100%
104         dsp_calc.set_start_timestamp_us(dsp_100_pc_48k_us);
105         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us * 2);
106         CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() == dsp_100_pc_48k_us);
107         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 1.0f);
108         dsp_calc_96k.set_start_timestamp_us(dsp_100_pc_96k_us);
109         dsp_calc_96k.set_stop_timestamp_us(dsp_100_pc_96k_us * 2);
110         CPPUNIT_ASSERT(dsp_calc_96k.elapsed_time_us() == dsp_100_pc_96k_us);
111         CPPUNIT_ASSERT(dsp_calc_96k.get_dsp_load() <= 1.0f);
112
113         // test the rate of rolloff of the LPF from 100% with load at constant 50%
114         // over the equivalent of 1 second for 48k and 96k and test for ~equality
115         for (int i = 0; i < 1e6 / dsp_100_pc_96k_us; ++i) {
116                 dsp_calc_96k.set_start_timestamp_us(0);
117                 dsp_calc_96k.set_stop_timestamp_us(dsp_100_pc_96k_us / 2);
118                 if (i % 2 == 0) {
119                         dsp_calc.set_start_timestamp_us(0);
120                         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us / 2);
121 #if 0
122                         std::cout << "DSP 50% load value 48k = " << dsp_calc.get_dsp_load()
123                                   << std::endl;
124                         std::cout << "DSP 50% load value 96k = " << dsp_calc_96k.get_dsp_load()
125                                   << std::endl;
126 #endif
127                         CPPUNIT_ASSERT_DOUBLES_EQUAL(dsp_calc.get_dsp_load(),
128                                                      dsp_calc_96k.get_dsp_load(), 0.001);
129                 }
130         }
131
132 }