1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
// See README.md and COPYING for details.

const SMOOTHING_TIME_MS: f32 = 10.0;

pub struct Smoother {
    slope_samples: usize,
    value: f32,
    inc: f32,
    target: f32,
    count: usize,
    done: bool,
}

impl Smoother {
    pub fn new() -> Self {
        Self { slope_samples: 0, value: 0.0, inc: 0.0, count: 0, target: 0.0, done: true }
    }

    pub fn set_sample_rate(&mut self, sr: f32) {
        self.slope_samples = ((sr * SMOOTHING_TIME_MS) / 1000.0).ceil() as usize;
    }

    #[inline]
    pub fn is_done(&self) -> bool {
        self.done
    }

    #[inline]
    #[allow(dead_code)]
    pub fn stop(&mut self) {
        self.done = true;
    }

    #[inline]
    pub fn set(&mut self, current: f32, target: f32) {
        self.value = current;
        self.count = self.slope_samples;
        self.inc = (target - current) / (self.count as f32);
        self.target = target;
        self.done = false;
    }

    #[allow(dead_code)]
    #[inline]
    pub fn current(&self) -> f32 {
        if self.done {
            self.target
        } else {
            self.value
        }
    }

    #[inline]
    pub fn next(&mut self) -> f32 {
        //d// println!("NEXT: count={}, value={:6.3} inc={:6.4}",
        //d//          self.count,
        //d//          self.value,
        //d//          self.inc);
        if self.count == 0 {
            self.done = true;

            self.target
        } else {
            self.value += self.inc;
            self.count -= 1;
            self.value
        }
    }
}

pub struct PerfTimer {
    lbl: &'static str,
    i: std::time::Instant,
    off: bool,
    // let tb = std::time::Instant::now();
    // let ta = std::time::Instant::now().duration_since(ta);
    // let tb = std::time::Instant::now().duration_since(tb);
    // println!("ta Elapsed: {:?}", ta);
}

impl PerfTimer {
    #[inline]
    pub fn off(mut self) -> Self {
        self.off = true;
        self
    }

    #[inline]
    pub fn new(lbl: &'static str) -> Self {
        Self { lbl, i: std::time::Instant::now(), off: false }
    }

    #[inline]
    pub fn print(&mut self, lbl2: &str) {
        if self.off {
            return;
        }

        let t = std::time::Instant::now().duration_since(self.i);
        println!("*** PERF[{}/{}] {:?}", self.lbl, lbl2, t);
        self.i = std::time::Instant::now();
    }
}