SpectMorph
smlinearsmooth.hh
1 /*
2  * liquidsfz - sfz sampler
3  *
4  * Copyright (C) 2019 Stefan Westerfeld
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your
9  * option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef SPECTMORPH_LINEAR_SMOOTH_HH
22 #define SPECTMORPH_LINEAR_SMOOTH_HH
23 
24 namespace SpectMorph
25 {
26 
28 {
29  float value_ = 0;
30  float linear_value_ = 0;
31  float linear_step_ = 0;
32  uint total_steps_ = 1;
33  uint steps_ = 0;
34 public:
35  void
36  reset (uint rate, float time)
37  {
38  total_steps_ = std::max<int> (rate * time, 1);
39  }
40  void
41  set (float new_value, bool now = false)
42  {
43  if (now)
44  {
45  steps_ = 0;
46  value_ = new_value;
47  }
48  else if (new_value != value_)
49  {
50  if (!steps_)
51  linear_value_ = value_;
52 
53  linear_step_ = (new_value - linear_value_) / total_steps_;
54  steps_ = total_steps_;
55  value_ = new_value;
56  }
57  }
58  float
59  get_next()
60  {
61  if (!steps_)
62  return value_;
63  else
64  {
65  steps_--;
66  linear_value_ += linear_step_;
67  return linear_value_;
68  }
69  }
70  bool
71  is_constant()
72  {
73  return steps_ == 0;
74  }
75 };
76 
77 }
78 
79 #endif /* SPECTMOPRH_FILTER_ENVELOPE_HH */
Definition: smlinearsmooth.hh:28