Add test for DSPLoadCalculator to libardour tests
[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         std::cout << std::endl;
65
66         // test the rate of rolloff of the LPF from 100% with load at constant 50%
67         // over the equivalent of 1 second
68         for (int i = 0; i < 1e6 / dsp_100_pc_48k_us; ++i) {
69                 dsp_calc.set_start_timestamp_us(0);
70                 dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us / 2);
71                 CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() == 5333);
72                 std::cout << "DSP 50% load value = " << dsp_calc.get_dsp_load() << std::endl;
73         }
74
75         // test that the LPF is still working after one second of values
76         // TODO need to work out what is required in terms of responsiveness etc
77         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() > 0.5f);
78
79         // compare 96k to 48k
80         DSPLoadCalculator dsp_calc_96k;
81         dsp_calc_96k.set_max_time(96000, 512);
82         int64_t dsp_100_pc_96k_us = 5333;
83
84         // reset both to 100%
85         dsp_calc.set_start_timestamp_us(dsp_100_pc_48k_us);
86         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us * 2);
87         CPPUNIT_ASSERT(dsp_calc.elapsed_time_us() == dsp_100_pc_48k_us);
88         CPPUNIT_ASSERT(dsp_calc.get_dsp_load() <= 1.0f);
89         dsp_calc_96k.set_start_timestamp_us(dsp_100_pc_96k_us);
90         dsp_calc_96k.set_stop_timestamp_us(dsp_100_pc_96k_us * 2);
91         CPPUNIT_ASSERT(dsp_calc_96k.elapsed_time_us() == dsp_100_pc_96k_us);
92         CPPUNIT_ASSERT(dsp_calc_96k.get_dsp_load() <= 1.0f);
93
94         // test the rate of rolloff of the LPF from 100% with load at constant 50%
95         // over the equivalent of 1 second for 48k and 96k and test for ~equality
96         for (int i = 0; i < 1e6 / dsp_100_pc_96k_us; ++i) {
97                 dsp_calc_96k.set_start_timestamp_us(0);
98                 dsp_calc_96k.set_stop_timestamp_us(dsp_100_pc_96k_us / 2);
99                 if (i % 2 == 0) {
100                         dsp_calc.set_start_timestamp_us(0);
101                         dsp_calc.set_stop_timestamp_us(dsp_100_pc_48k_us / 2);
102
103                         std::cout << "DSP 50% load value 48k = " << dsp_calc.get_dsp_load()
104                                   << std::endl;
105                         std::cout << "DSP 50% load value 96k = " << dsp_calc_96k.get_dsp_load()
106                                   << std::endl;
107                         CPPUNIT_ASSERT_DOUBLES_EQUAL(dsp_calc.get_dsp_load(),
108                                                      dsp_calc_96k.get_dsp_load(), 0.001);
109                 }
110         }
111
112 }