+ ChangeLog: updated
[calf.git] / src / giface.cpp
blob03209b7c04e8c7a92717d6f3dd5e26a9631cdcc3
1 /* Calf DSP Library
2 * Module wrapper methods.
4 * Copyright (C) 2001-2007 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 #include <config.h>
22 #include <assert.h>
23 #include <memory.h>
24 #include <calf/giface.h>
26 using namespace std;
27 using namespace calf_utils;
28 using namespace calf_plugins;
30 float parameter_properties::from_01(double value01) const
32 double value = dsp::clip(value01, 0., 1.);
33 switch(flags & PF_SCALEMASK)
35 case PF_SCALE_DEFAULT:
36 case PF_SCALE_LINEAR:
37 case PF_SCALE_PERC:
38 default:
39 value = min + (max - min) * value01;
40 break;
41 case PF_SCALE_QUAD:
42 value = min + (max - min) * value01 * value01;
43 break;
44 case PF_SCALE_LOG:
45 value = min * pow(double(max / min), value01);
46 break;
47 case PF_SCALE_GAIN:
48 if (value01 < 0.00001)
49 value = min;
50 else {
51 float rmin = std::max(1.0f / 1024.0f, min);
52 value = rmin * pow(double(max / rmin), value01);
54 break;
55 case PF_SCALE_LOG_INF:
56 assert(step);
57 if (value01 > (step - 1.0) / step)
58 value = FAKE_INFINITY;
59 else
60 value = min * pow(double(max / min), value01 * step / (step - 1.0));
61 break;
63 switch(flags & PF_TYPEMASK)
65 case PF_INT:
66 case PF_BOOL:
67 case PF_ENUM:
68 case PF_ENUM_MULTI:
69 if (value > 0)
70 value = (int)(value + 0.5);
71 else
72 value = (int)(value - 0.5);
73 break;
75 return value;
78 double parameter_properties::to_01(float value) const
80 switch(flags & PF_SCALEMASK)
82 case PF_SCALE_DEFAULT:
83 case PF_SCALE_LINEAR:
84 case PF_SCALE_PERC:
85 default:
86 return double(value - min) / (max - min);
87 case PF_SCALE_QUAD:
88 return sqrt(double(value - min) / (max - min));
89 case PF_SCALE_LOG:
90 value /= min;
91 return log((double)value) / log((double)max / min);
92 case PF_SCALE_LOG_INF:
93 if (IS_FAKE_INFINITY(value))
94 return max;
95 value /= min;
96 assert(step);
97 return (step - 1.0) * log((double)value) / (step * log((double)max / min));
98 case PF_SCALE_GAIN:
99 if (value < 1.0 / 1024.0) // new bottom limit - 60 dB
100 return 0;
101 double rmin = std::max(1.0f / 1024.0f, min);
102 value /= rmin;
103 return log((double)value) / log(max / rmin);
107 float parameter_properties::get_increment() const
109 float increment = 0.01;
110 if (step > 1)
111 increment = 1.0 / (step - 1);
112 else
113 if (step > 0 && step < 1)
114 increment = step;
115 else
116 if ((flags & PF_TYPEMASK) != PF_FLOAT)
117 increment = 1.0 / (max - min);
118 return increment;
121 int parameter_properties::get_char_count() const
123 if ((flags & PF_SCALEMASK) == PF_SCALE_PERC)
124 return 6;
125 if ((flags & PF_SCALEMASK) == PF_SCALE_GAIN) {
126 char buf[256];
127 size_t len = 0;
128 sprintf(buf, "%0.0f dB", 6.0 * log(min) / log(2));
129 len = strlen(buf);
130 sprintf(buf, "%0.0f dB", 6.0 * log(max) / log(2));
131 len = std::max(len, strlen(buf)) + 2;
132 return (int)len;
134 return std::max(to_string(min).length(), std::max(to_string(max).length(), to_string(min + (max-min) * 0.987654).length()));
137 std::string parameter_properties::to_string(float value) const
139 char buf[32];
140 if ((flags & PF_SCALEMASK) == PF_SCALE_PERC) {
141 sprintf(buf, "%0.f%%", 100.0 * value);
142 return string(buf);
144 if ((flags & PF_SCALEMASK) == PF_SCALE_GAIN) {
145 if (value < 1.0 / 1024.0) // new bottom limit - 60 dB
146 return "-inf dB"; // XXXKF change to utf-8 infinity
147 sprintf(buf, "%0.1f dB", 6.0 * log(value) / log(2));
148 return string(buf);
150 switch(flags & PF_TYPEMASK)
152 case PF_STRING:
153 return "N/A";
154 case PF_INT:
155 case PF_BOOL:
156 case PF_ENUM:
157 case PF_ENUM_MULTI:
158 value = (int)value;
159 break;
162 if ((flags & PF_SCALEMASK) == PF_SCALE_LOG_INF && IS_FAKE_INFINITY(value))
163 sprintf(buf, "+inf"); // XXXKF change to utf-8 infinity
164 else
165 sprintf(buf, "%g", value);
167 switch(flags & PF_UNITMASK) {
168 case PF_UNIT_DB: return string(buf) + " dB";
169 case PF_UNIT_HZ: return string(buf) + " Hz";
170 case PF_UNIT_SEC: return string(buf) + " s";
171 case PF_UNIT_MSEC: return string(buf) + " ms";
172 case PF_UNIT_CENTS: return string(buf) + " ct";
173 case PF_UNIT_SEMITONES: return string(buf) + "#";
174 case PF_UNIT_BPM: return string(buf) + " bpm";
175 case PF_UNIT_RPM: return string(buf) + " rpm";
176 case PF_UNIT_DEG: return string(buf) + " deg";
177 case PF_UNIT_NOTE:
179 static const char *notes = "C C#D D#E F F#G G#A A#B ";
180 int note = (int)value;
181 if (note < 0 || note > 127)
182 return "---";
183 return string(notes + 2 * (note % 12), 2) + i2s(note / 12 - 2);
187 return string(buf);
190 void calf_plugins::plugin_ctl_iface::clear_preset() {
191 int param_count = get_param_count();
192 for (int i=0; i < param_count; i++)
194 parameter_properties &pp = *get_param_props(i);
195 if ((pp.flags & PF_TYPEMASK) == PF_STRING)
197 configure(pp.short_name, pp.choices ? pp.choices[0] : "");
199 else
200 set_param_value(i, pp.def_value);
204 const char *calf_plugins::load_gui_xml(const std::string &plugin_id)
206 try {
207 return strdup(calf_utils::load_file((std::string(PKGLIBDIR) + "/gui-" + plugin_id + ".xml").c_str()).c_str());
209 catch(file_exception e)
211 return NULL;
215 bool calf_plugins::check_for_message_context_ports(parameter_properties *parameters, int count)
217 for (int i = count - 1; i >= 0; i--)
219 if (parameters[i].flags & PF_PROP_MSGCONTEXT)
220 return true;
222 return false;
225 bool calf_plugins::check_for_string_ports(parameter_properties *parameters, int count)
227 for (int i = count - 1; i >= 0; i--)
229 if ((parameters[i].flags & PF_TYPEMASK) == PF_STRING)
230 return true;
231 if ((parameters[i].flags & PF_TYPEMASK) < PF_STRING)
232 return false;
234 return false;