Add DSPLoadCalculator class to libardour
[ardour.git] / libs / ardour / dsp_load_calculator.cc
1 /*
2  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include "ardour/dsp_load_calculator.h"
20
21 namespace ARDOUR {
22
23 void
24 DSPLoadCalculator::set_stop_timestamp_us(uint64_t stop_timestamp_us)
25 {
26         // We could only bother with calculations if a certain amount of time
27         // has passed, or the Raw DSP value is > X% different than last calc
28         // which would mean consistent overhead for small values of m_max_time_us
29
30         m_stop_timestamp_us = stop_timestamp_us;
31         float load = 0;
32
33         if (elapsed_time_us() > m_max_time_us) {
34                 load = 1.0f;
35         } else {
36                 load = elapsed_time_us() / (float)m_max_time_us;
37         }
38
39         assert(m_value_history.write_space() >= 1);
40
41         // push raw load value onto history
42         m_value_history.write(&load, 1);
43
44         // if load is under 80% use an average of past values
45         if (elapsed_time_us() < ((m_max_time_us * 80) / 100)) {
46
47                 RingBuffer<float>::rw_vector vec;
48                 m_value_history.get_read_vector(&vec);
49                 uint32_t values_read = 0;
50                 float dsp_accumulator = 0.0f;
51
52                 // iterate through the read vectors accumulating the dsp load
53                 for (unsigned int i = 0; i < vec.len[0]; ++i) {
54                         dsp_accumulator += vec.buf[0][i];
55                         values_read++;
56                 }
57
58                 for (unsigned int i = 0; i < vec.len[1]; ++i) {
59                         dsp_accumulator += vec.buf[1][i];
60                         values_read++;
61                 }
62
63                 load = dsp_accumulator / (float)values_read;
64
65                 const float alpha = 0.2f * (m_max_time_us * 1e-6f);
66                 m_dsp_load = m_dsp_load + alpha * (load - m_dsp_load) + 1e-12;
67
68         } else {
69                 // Use raw load value otherwise 100% may never be indicated because of
70                 // averaging/LPF etc
71                 m_dsp_load = load;
72         }
73
74         if (m_value_history.read_space() >= m_num_values) {
75                 // "remove" the oldest value
76                 m_value_history.increment_read_idx(1);
77         }
78 }
79
80 } // namespace ARDOUR