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<float(const MorphOp&)> get_value;
34  std::function<void (MorphOp&, float)> set_value;
35 public:
36  IProperty (MorphOp *morph_op,
37  const std::string& label,
38  const std::string& format,
39  std::function<float(const MorphOp&)> get_value,
40  std::function<void (MorphOp&, float)> set_value) :
41  morph_op (*morph_op),
42  m_label (label),
43  m_format (format),
44  get_value (get_value),
45  set_value (set_value)
46  {
47  }
48  int min() { return 0; }
49  int max() { return 1000; }
50  int get() { return lrint (value2ui (get_value (morph_op)) * 1000); }
51  void set (int v) { set_value (morph_op, ui2value (v / 1000.)); }
52 
53  virtual double value2ui (double value) = 0;
54  virtual double ui2value (double ui) = 0;
55 
56  std::string label() { return m_label; }
57  std::string value_label() { return string_locale_printf (m_format.c_str(), get_value (morph_op)); }
58 };
59 
60 template<class MorphOp>
61 class XParamProperty : public IProperty<MorphOp>
62 {
63  double m_min_value;
64  double m_max_value;
65  double m_slope;
66 public:
67  XParamProperty (MorphOp *morph_op,
68  const std::string& label,
69  const std::string& format,
70  double min_value,
71  double max_value,
72  double slope,
73  std::function<float(const MorphOp&)> get_value,
74  std::function<void (MorphOp&, float)> set_value) :
75  IProperty<MorphOp> (morph_op, label, format, get_value, set_value),
76  m_min_value (min_value),
77  m_max_value (max_value),
78  m_slope (slope)
79  {
80  }
81  double
82  value2ui (double v)
83  {
84  return sm_xparam_inv ((v - m_min_value) / (m_max_value - m_min_value), m_slope);
85  }
86  double
87  ui2value (double ui)
88  {
89  return sm_xparam (ui, m_slope) * (m_max_value - m_min_value) + m_min_value;
90  }
91 };
92 
93 template<class MorphOp>
94 class LogParamProperty : public IProperty<MorphOp>
95 {
96  double m_min_value;
97  double m_max_value;
98 public:
99  LogParamProperty (MorphOp *morph_op,
100  const std::string& label,
101  const std::string& format,
102  double min_value,
103  double max_value,
104  std::function<float(const MorphOp&)> get_value,
105  std::function<void (MorphOp&, float)> set_value) :
106  IProperty<MorphOp> (morph_op, label, format, get_value, set_value),
107  m_min_value (min_value),
108  m_max_value (max_value)
109  {
110  }
111  double
112  value2ui (double v)
113  {
114  return (log (v) - log (m_min_value)) / (log (m_max_value) - log (m_min_value));
115  }
116  double
117  ui2value (double ui)
118  {
119  return exp (ui * (log (m_max_value) - log (m_min_value)) + log (m_min_value));
120  }
121 };
122 
123 template<class MorphOp>
124 class LinearParamProperty : public IProperty<MorphOp>
125 {
126  double m_min_value;
127  double m_max_value;
128 public:
129  LinearParamProperty (MorphOp *morph_op,
130  const std::string& label,
131  const std::string& format,
132  double min_value,
133  double max_value,
134  std::function<float(const MorphOp&)> get_value,
135  std::function<void (MorphOp&, float)> set_value) :
136  IProperty<MorphOp> (morph_op, label, format, get_value, set_value),
137  m_min_value (min_value),
138  m_max_value (max_value)
139  {
140  }
141  double
142  value2ui (double v)
143  {
144  return (v - m_min_value) / (m_max_value - m_min_value);
145  }
146  double
147  ui2value (double ui)
148  {
149  return ui * (m_max_value - m_min_value) + m_min_value;
150  }
151 };
152 
153 }
154 
155 #endif
Definition: smproperty.hh:124
Definition: smproperty.hh:61
Definition: smproperty.hh:15
Definition: smproperty.hh:94
Definition: smproperty.hh:28
Definition: smalignedarray.cc:7