SpectMorph
smproperty.hh
1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
2 
3 #ifndef SPECTMORPH_PROPERTY_HH
4 #define SPECTMORPH_PROPERTY_HH
5 
6 #include "smmath.hh"
7 #include "smutils.hh"
8 
9 #include <functional>
10 #include <string>
11 
12 namespace SpectMorph
13 {
14 
15 class Property
16 {
17 public:
18  virtual int min() = 0;
19  virtual int max() = 0;
20  virtual int get() = 0;
21  virtual void set (int v) = 0;
22 
23  virtual std::string label() = 0;
24  virtual std::string value_label() = 0;
25 };
26 
27 template<class MorphOp>
28 class IProperty : public Property
29 {
30  MorphOp& morph_op;
31  std::string m_label;
32  std::string m_format;
33  std::function<std::string (float)> m_custom_formatter;
34 
35  std::function<float(const MorphOp&)> get_value;
36  std::function<void (MorphOp&, float)> set_value;
37 public:
38  IProperty (MorphOp *morph_op,
39  const std::string& label,
40  const std::string& format,
41  std::function<float(const MorphOp&)> get_value,
42  std::function<void (MorphOp&, float)> set_value) :
43  morph_op (*morph_op),
44  m_label (label),
45  m_format (format),
46  get_value (get_value),
47  set_value (set_value)
48  {
49  }
50  int min() { return 0; }
51  int max() { return 1000; }
52  int get() { return lrint (value2ui (get_value (morph_op)) * 1000); }
53  void set (int v) { set_value (morph_op, ui2value (v / 1000.)); }
54 
55  virtual double value2ui (double value) = 0;
56  virtual double ui2value (double ui) = 0;
57 
58  std::string label() { return m_label; }
59 
60  std::string
61  value_label()
62  {
63  if (m_custom_formatter)
64  return m_custom_formatter (get_value (morph_op));
65  else
66  return string_locale_printf (m_format.c_str(), get_value (morph_op));
67  }
68 
69  void
70  set_custom_formatter (const std::function<std::string (float)>& formatter)
71  {
72  m_custom_formatter = formatter;
73  }
74 };
75 
76 template<class MorphOp>
77 class XParamProperty : public IProperty<MorphOp>
78 {
79  double m_min_value;
80  double m_max_value;
81  double m_slope;
82 public:
83  XParamProperty (MorphOp *morph_op,
84  const std::string& label,
85  const std::string& format,
86  double min_value,
87  double max_value,
88  double slope,
89  std::function<float(const MorphOp&)> get_value,
90  std::function<void (MorphOp&, float)> set_value) :
91  IProperty<MorphOp> (morph_op, label, format, get_value, set_value),
92  m_min_value (min_value),
93  m_max_value (max_value),
94  m_slope (slope)
95  {
96  }
97  double
98  value2ui (double v)
99  {
100  return sm_xparam_inv ((v - m_min_value) / (m_max_value - m_min_value), m_slope);
101  }
102  double
103  ui2value (double ui)
104  {
105  return sm_xparam (ui, m_slope) * (m_max_value - m_min_value) + m_min_value;
106  }
107 };
108 
109 template<class MorphOp>
110 class LogParamProperty : public IProperty<MorphOp>
111 {
112  double m_min_value;
113  double m_max_value;
114 public:
115  LogParamProperty (MorphOp *morph_op,
116  const std::string& label,
117  const std::string& format,
118  double min_value,
119  double max_value,
120  std::function<float(const MorphOp&)> get_value,
121  std::function<void (MorphOp&, float)> set_value) :
122  IProperty<MorphOp> (morph_op, label, format, get_value, set_value),
123  m_min_value (min_value),
124  m_max_value (max_value)
125  {
126  }
127  double
128  value2ui (double v)
129  {
130  return (log (v) - log (m_min_value)) / (log (m_max_value) - log (m_min_value));
131  }
132  double
133  ui2value (double ui)
134  {
135  return exp (ui * (log (m_max_value) - log (m_min_value)) + log (m_min_value));
136  }
137 };
138 
139 template<class MorphOp>
140 class LinearParamProperty : public IProperty<MorphOp>
141 {
142  double m_min_value;
143  double m_max_value;
144 public:
145  LinearParamProperty (MorphOp *morph_op,
146  const std::string& label,
147  const std::string& format,
148  double min_value,
149  double max_value,
150  std::function<float(const MorphOp&)> get_value,
151  std::function<void (MorphOp&, float)> set_value) :
152  IProperty<MorphOp> (morph_op, label, format, get_value, set_value),
153  m_min_value (min_value),
154  m_max_value (max_value)
155  {
156  }
157  double
158  value2ui (double v)
159  {
160  return (v - m_min_value) / (m_max_value - m_min_value);
161  }
162  double
163  ui2value (double ui)
164  {
165  return ui * (m_max_value - m_min_value) + m_min_value;
166  }
167 };
168 
169 }
170 
171 #endif
Definition: smproperty.hh:140
Definition: smproperty.hh:77
Definition: smproperty.hh:15
Definition: smproperty.hh:110
Definition: smproperty.hh:28
Definition: smadsrenvelope.hh:8