3 #ifndef SPECTMORPH_PROPERTY_HH
4 #define SPECTMORPH_PROPERTY_HH
8 #include "smoutfile.hh"
10 #include "smsignal.hh"
27 std::unique_ptr<ModulationList> m_modulation_list;
29 std::string m_identifier;
30 double m_modulation_range_ui = 1;
36 std::string identifier() {
return m_identifier; }
38 enum class Type { BOOL, INT, ENUM, FLOAT };
39 enum class Scale { NONE, LINEAR, LOG };
41 virtual Type type() = 0;
43 virtual int min() = 0;
44 virtual int max() = 0;
45 virtual int get() = 0;
46 virtual void set (
int v) = 0;
48 virtual std::string label() = 0;
49 virtual std::string value_label() = 0;
51 virtual std::string get_edit_str() {
return "<none>"; }
52 virtual void set_edit_str (
const std::string& s) {}
54 virtual void save (
OutFile& out_file) = 0;
55 virtual bool load (
InFile& in_file) = 0;
61 bool get_bool() {
return get() != 0; }
62 void set_bool (
bool b) { set (b ? 1 : 0); }
64 virtual float get_float()
const {
return 0; }
65 virtual void set_float (
float f) {}
67 virtual const EnumInfo *enum_info()
const {
return nullptr; }
69 ModulationList *modulation_list() {
return m_modulation_list.get(); }
73 void set_modulation_range_ui (
double range_ui) { m_modulation_range_ui = range_ui; }
74 double modulation_range_ui()
const {
return m_modulation_range_ui; }
81 Range (
double min_value,
double max_value) :
82 min_value (min_value),
87 clamp (
double value)
const
89 return sm_clamp (value, min_value, max_value);
92 virtual Range float_range() {
return Range (min(), max()); }
93 virtual Scale float_scale() {
return Scale::NONE; }
102 std::string m_format;
104 Type type()
override {
return Type::INT; }
105 int min()
override {
return m_min_value; }
106 int max()
override {
return m_max_value; }
107 int get()
override {
return *m_value; }
109 IntProperty (
MorphOperator *op,
int *value,
const std::string& identifier,
const std::string& label,
const std::string& format,
110 int def,
int mn,
int mx) :
126 value_label()
override
128 return string_locale_printf (m_format.c_str(), *m_value);
131 get_edit_str()
override
133 return string_locale_printf (
"%d", get());
136 set_edit_str (
const std::string& s)
override
138 set (atoi (s.c_str()));
143 *m_value = std::clamp (v, min(), max());
144 signal_value_changed();
147 save (
OutFile& out_file)
override
149 out_file.write_int (m_identifier, *m_value);
152 load (
InFile& in_file)
override
154 if (in_file.
event() == InFile::INT)
169 std::vector<int> m_valid_values;
171 std::string m_format;
173 Type type() {
return Type::INT; }
174 int min() {
return 0; }
175 int max() {
return m_valid_values.size() - 1; }
179 for (
size_t i = 0; i < m_valid_values.size(); i++)
180 if (*m_value == m_valid_values[i])
189 *m_value = m_valid_values[std::clamp (v, min(), max())];
190 signal_value_changed();
192 std::string label() {
return m_label; }
197 return string_locale_printf (m_format.c_str(), *m_value);
202 return string_locale_printf (
"%d", *m_value);
205 set_edit_str (
const std::string& s)
207 int i = atoi (s.c_str());
209 for (
size_t idx = 0; idx < m_valid_values.size(); idx++)
211 if (std::abs (m_valid_values[idx] - i) < std::abs (m_valid_values[best_idx] - i))
217 int def,
const std::vector<int>& valid_values) :
220 m_valid_values (valid_values),
229 out_file.write_int (m_identifier, *m_value);
234 if (in_file.
event() == InFile::INT)
251 Type type() {
return Type::BOOL; }
252 int min() {
return 0; }
253 int max() {
return 1; }
254 int get() {
return *m_value; }
263 std::string label() {
return m_label; }
274 *m_value = v ? true :
false;
275 signal_value_changed();
280 out_file.write_bool (m_identifier, *m_value);
285 if (in_file.
event() == InFile::BOOL)
305 EnumInfo (
const std::vector<Item>& items) :
310 const std::vector<Item>
316 std::vector<Item> m_items;
323 std::function<int()> m_read_func;
324 std::function<void(
int)> m_write_func;
329 const std::string& identifier,
330 const std::string& label,
333 std::function<
int()> read_func,
334 std::function<
void(
int)> write_func) :
338 m_read_func (read_func),
339 m_write_func (write_func)
343 g_return_if_fail (ei.items().size());
344 m_min_value = ei.items()[0].value;
345 m_max_value = ei.items()[0].value;
346 for (
auto item : ei.items())
348 m_min_value = std::min (item.value, m_min_value);
349 m_max_value = std::max (item.value, m_max_value);
352 Type type() {
return Type::ENUM; }
353 int min() {
return m_min_value; }
354 int max() {
return m_max_value; }
355 int get() {
return m_read_func(); }
359 signal_value_changed();
361 std::string label() {
return m_label; }
362 std::string value_label() {
return "-"; }
363 virtual const EnumInfo *enum_info()
const {
return &m_enum_info; }
367 out_file.write_int (m_identifier, m_read_func());
372 if (in_file.
event() == InFile::INT)
391 std::string m_format;
392 std::function<std::string (
float)> m_custom_formatter;
398 const std::string& identifier,
399 const std::string& label,
400 const std::string& format) :
409 Type type()
override {
return Type::FLOAT; }
410 int min()
override {
return 0; }
411 int max()
override {
return 1000; }
412 int get()
override {
return lrint (value2ui (*m_value) * 1000); }
417 *m_value = m_range.clamp (ui2value (v / 1000.));
418 signal_value_changed();
422 get_float()
const override
427 set_float (
float f)
override
429 *m_value = m_range.clamp (f);
430 signal_value_changed();
433 get_edit_str()
override
435 return string_locale_printf (
"%.3f", get_float());
438 set_edit_str (
const std::string& s)
override
440 set_float (sm_atof_any (s.c_str()));
443 float_range()
override
448 float_scale()
override
453 virtual double value2ui (
double value) = 0;
454 virtual double ui2value (
double ui) = 0;
462 value_label()
override
464 if (m_custom_formatter)
465 return m_custom_formatter (*m_value);
467 return string_locale_printf (m_format.c_str(), *m_value);
471 set_custom_formatter (
const std::function<std::string (
float)>& formatter)
473 m_custom_formatter = formatter;
476 save (
OutFile& out_file)
override
478 out_file.write_float (m_identifier, *m_value);
481 load (
InFile& in_file)
override
483 if (in_file.
event() == InFile::FLOAT)
500 const std::string& identifier,
501 const std::string& label,
502 const std::string& format,
506 FloatProperty (op, value, { min_value, max_value }, Scale::LOG, identifier, label, format)
514 return (log (v) - log (m_range.min_value)) / (log (m_range.max_value) - log (m_range.min_value));
519 return exp (ui * (log (m_range.max_value) - log (m_range.min_value)) + log (m_range.min_value));
528 const std::string& identifier,
529 const std::string& label,
530 const std::string& format,
534 FloatProperty (op, value, { min_value, max_value }, Scale::LINEAR, identifier, label, format)
541 return (v - m_range.min_value) / (m_range.max_value - m_range.min_value);
546 return ui * (m_range.max_value - m_range.min_value) + m_range.min_value;
556 const std::string& identifier,
557 const std::string& label,
558 const std::string& format,
563 FloatProperty (op, value, { min_value, max_value }, Scale::NONE, identifier, label, format),
571 return sm_xparam_inv ((v - m_range.min_value) / (m_range.max_value - m_range.min_value), m_slope);
576 return sm_xparam (ui, m_slope) * (m_range.max_value - m_range.min_value) + m_range.min_value;
Definition: smproperty.hh:247
Definition: smproperty.hh:298
Definition: smproperty.hh:320
Definition: smproperty.hh:385
Class to read SpectMorph binary data.
Definition: sminfile.hh:27
Event event()
Definition: sminfile.cc:69
float event_float()
Definition: sminfile.cc:380
int event_int()
Definition: sminfile.cc:391
std::string event_name()
Definition: sminfile.cc:369
bool event_bool()
Definition: sminfile.cc:402
Definition: smproperty.hh:97
Definition: smproperty.hh:167
Definition: smproperty.hh:524
Definition: smproperty.hh:496
Definition: smmodulationlist.hh:17
Definition: smmodulationlist.hh:41
Definition: smmorphoperator.hh:27
Definition: smoutfile.hh:19
Definition: smproperty.hh:25
Definition: smsignal.hh:35
Definition: smsignal.hh:150
Definition: smproperty.hh:551
Definition: smproperty.hh:301
Definition: smproperty.hh:77