additional check for DSP falloff.
[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 using namespace std;
10 using namespace ARDOUR;
11
12 void
13 DSPLoadCalculatorTest::basicTest ()
14 {
15         DSPLoadCalculator dsp_calc;
16
17         dsp_calc.set_max_time(48000, 512);
18         int64_t dsp_100_pc_48k_us = 10666;
19
20         CPPUNIT_ASSERT(dsp_calc.get_max_time_us() == dsp_100_pc_48k_us);
21
22         // test equivalent of 10% load
23         dsp_calc.set_start_timestamp_us(0);
24         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us/10);
25         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 0.1f);
26
27         // test equivalent of 50% load and check that the load jumps to 50 percent
28         dsp_calc.set_start_timestamp_us(0);
29         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us/2);
30         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 0.5f);
31
32         // test equivalent of 100% load
33         dsp_calc.set_start_timestamp_us(0);
34         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us);
35         CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() == dsp_100_pc_48k_us);
36         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 1.0f);
37
38         // test setting the equivalent of 100% twice doesn't lead to a dsp value > 1.0
39         dsp_calc.set_start_timestamp_us(dsp_100_pc_48k_us);
40         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us * 2);
41         CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() == dsp_100_pc_48k_us);
42         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 1.0f);
43
44         // test setting the equivalent of 200% clamps the value to 1.0
45         dsp_calc.set_start_timestamp_us(dsp_100_pc_48k_us);
46         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us * 3);
47         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() == 1.0f);
48
49         // test setting the an stop timestamp before the start timestamp is ignored
50         // and the previous dsp value is returned
51         dsp_calc.set_start_timestamp_us(dsp_100_pc_48k_us * 2);
52         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us);
53         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() == 1.0f);
54
55         float dsp_load = dsp_calc.get_dsp_load();
56
57         // test setting the equivalent of beyond the max_timer_error_us is ignored and
58         // the previous dsp value is returned
59         dsp_calc.set_start_timestamp_us (0);
60         dsp_calc.set_stop_timestamp_us (dsp_100_pc_48k_us*10);
61         CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() > dsp_calc.max_timer_error_us());
62         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() == dsp_load);
63
64         // test the rate of rolloff of the LPF from 100% with load at constant 50%
65         // over the equivalent of 1 second
66         for (int i = 0; i < 1e6 / dsp_100_pc_48k_us; ++i) {
67                 dsp_calc.set_start_timestamp_us(0);
68                 dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us / 2);
69                 CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() == 5333);
70                 CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 1.0);
71                 CPPUNIT_ASSERT(dsp_calc.get_dsp_load() >= 0.5);
72 #if 0
73                 std::cout << "DSP 50% load value = " << dsp_calc.get_dsp_load() << std::endl;
74 #endif
75         }
76
77         // test that the LPF is still working after one second of values
78         // TODO need to work out what is required in terms of responsiveness etc
79         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() > 0.5f);
80
81         // compare 96k to 48k
82         DSPLoadCalculator dsp_calc_96k;
83         dsp_calc_96k.set_max_time(96000, 512);
84         int64_t dsp_100_pc_96k_us = 5333;
85
86         // reset both to 100%
87         dsp_calc.set_start_timestamp_us(dsp_100_pc_48k_us);
88         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us * 2);
89         CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() == dsp_100_pc_48k_us);
90         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 1.0f);
91         dsp_calc_96k.set_start_timestamp_us(dsp_100_pc_96k_us);
92         dsp_calc_96k.set_stop_timestamp_us(dsp_100_pc_96k_us * 2);
93         CPPUNIT_ASSERT(dsp_calc_96k.elapsed_time_us() == dsp_100_pc_96k_us);
94         CPPUNIT_ASSERT(dsp_calc_96k.get_dsp_load() <= 1.0f);
95
96         // test the rate of rolloff of the LPF from 100% with load at constant 50%
97         // over the equivalent of 1 second for 48k and 96k and test for ~equality
98         for (int i = 0; i < 1e6 / dsp_100_pc_96k_us; ++i) {
99                 dsp_calc_96k.set_start_timestamp_us(0);
100                 dsp_calc_96k.set_stop_timestamp_us(dsp_100_pc_96k_us / 2);
101                 if (i % 2 == 0) {
102                         dsp_calc.set_start_timestamp_us(0);
103                         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us / 2);
104 #if 0
105                         std::cout << "DSP 50% load value 48k = " << dsp_calc.get_dsp_load()
106                                   << std::endl;
107                         std::cout << "DSP 50% load value 96k = " << dsp_calc_96k.get_dsp_load()
108                                   << std::endl;
109 #endif
110                         CPPUNIT_ASSERT_DOUBLES_EQUAL(dsp_calc.get_dsp_load(),
111                                                      dsp_calc_96k.get_dsp_load(), 0.001);
112                 }
113         }
114
115 }