never ever change sample-rate saved with the session after creation.
[ardour.git] / libs / ardour / ardour / dsp_load_calculator.h
index 1da9786d367d6da007e098ed3cd87e0df785ff68..c7c0c0b83e074384ba4d55f86c719d9b5bbb10e2 100644 (file)
@@ -36,6 +36,10 @@ public:
 
        }
 
+       void set_max_time(double samplerate, uint32_t period_size) {
+               m_max_time_us = period_size * 1e6 / samplerate;
+       }
+
        void set_max_time_us(uint64_t max_time_us) {
                assert(max_time_us != 0);
                m_max_time_us = max_time_us;
@@ -60,20 +64,17 @@ public:
                 */
                if (m_start_timestamp_us < 0 || m_stop_timestamp_us < 0 ||
                    m_start_timestamp_us > m_stop_timestamp_us ||
-                   elapsed_time_us() > max_timer_error()) {
+                   elapsed_time_us() > max_timer_error_us()) {
                        return;
                }
 
-               if (elapsed_time_us() > m_max_time_us) {
-                       m_dsp_load = 1.0f;
+               const float load = (float) elapsed_time_us() / (float)m_max_time_us;
+               if (load > m_dsp_load || load > 1.0) {
+                       m_dsp_load = load;
                } else {
-                       const float load = elapsed_time_us() / (float)m_max_time_us;
-                       if (load > m_dsp_load) {
-                               m_dsp_load = load;
-                       } else {
-                               const float alpha = 0.2f * (m_max_time_us * 1e-6f);
-                               m_dsp_load = m_dsp_load + alpha * (load - m_dsp_load) + 1e-12;
-                       }
+                       const float alpha = 0.2f * (m_max_time_us * 1e-6f);
+                       m_dsp_load = std::min (1.f, m_dsp_load);
+                       m_dsp_load += alpha * (load - m_dsp_load) + 1e-12;
                }
        }
 
@@ -89,17 +90,26 @@ public:
         */
        float get_dsp_load() const
        {
-               if (m_dsp_load > m_max_time_us) {
-                       return 1.0f;
-               }
-               if (m_dsp_load < 0.0f) {
-                       return 0.0f;
-               }
+               assert (m_dsp_load >= 0.f); // since stop > start is assured this cannot happen.
+               return std::min (1.f, m_dsp_load);
+       }
+
+       /**
+        * @return an unbound value representing the percentage of time spent between
+        * start and stop in proportion to the max expected time in microseconds(us).
+        * This is useful for cases to estimate overload (e.g. Dummy backend)
+        */
+       float get_dsp_load_unbound() const
+       {
+               assert (m_dsp_load >= 0.f);
                return m_dsp_load;
        }
 
-private: // methods
-       int64_t max_timer_error () { return 4 * m_max_time_us; }
+       /**
+        * The maximum error in timestamp values that will be tolerated before the
+        * current dsp load sample will be ignored
+        */
+       int64_t max_timer_error_us() { return 4 * m_max_time_us; }
 
 private: // data
        int64_t m_max_time_us;