Add check for invalid timer values from the DummyBackend
[ardour.git] / libs / ardour / ardour / dsp_load_calculator.h
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 #ifndef ARDOUR_DSP_LOAD_CALCULATOR_H
20 #define ARDOUR_DSP_LOAD_CALCULATOR_H
21
22 #include <stdint.h>
23 #include <cassert>
24 #include <algorithm>
25
26 #include <pbd/ringbuffer.h>
27
28 #include "ardour/libardour_visibility.h"
29
30 namespace ARDOUR {
31
32 class LIBARDOUR_API DSPLoadCalculator {
33 public:
34         DSPLoadCalculator()
35             : m_max_time_us(0)
36             , m_start_timestamp_us(0)
37             , m_stop_timestamp_us(0)
38             , m_dsp_load(0)
39             , m_value_history (max_value_history())
40             , m_num_values(0)
41         {
42
43         }
44
45         void set_max_time_us(uint64_t max_time_us) {
46                 assert(max_time_us != 0);
47                 m_max_time_us = max_time_us;
48
49                 // Use average of last 1/4 second of values so responsiveness
50                 // remains consistent independent of max time
51                 uint32_t max_dsp_samples_per_qtr_second = (250000 / m_max_time_us);
52                 m_num_values =
53                     std::min(max_value_history() - 1, max_dsp_samples_per_qtr_second);
54
55                 m_value_history.reset();
56         }
57
58
59         int64_t get_max_time_us() const { return m_max_time_us; }
60
61         void set_start_timestamp_us(int64_t start_timestamp_us)
62         {
63                 m_start_timestamp_us = start_timestamp_us;
64         }
65
66         void set_stop_timestamp_us(int64_t stop_timestamp_us);
67
68         int64_t elapsed_time_us()
69         {
70                 return m_stop_timestamp_us - m_start_timestamp_us;
71         }
72
73         /**
74          * @return a decimal value between 0.0 and 1.0 representing the percentage
75          * of time spent between start and stop in proportion to the max expected time
76          * in microseconds(us).
77          */
78         float get_dsp_load() const
79         {
80                 if (m_dsp_load > m_max_time_us) {
81                         return 1.0f;
82                 }
83                 if (m_dsp_load < 0.0f) {
84                         return 0.0f;
85                 }
86                 return m_dsp_load;
87         }
88 private: // methods
89         static uint32_t max_value_history () { return 16; }
90
91         int64_t max_timer_error () { return 4 * m_max_time_us; }
92
93 private: // data
94         int64_t m_max_time_us;
95         int64_t m_start_timestamp_us;
96         int64_t m_stop_timestamp_us;
97         float m_dsp_load;
98         RingBuffer<float> m_value_history;
99         uint32_t m_num_values;
100 };
101
102 } // namespace ARDOUR
103
104 #endif // ARDOUR_DSP_LOAD_CALCULATOR_H