66c9a498b8e17811d6d1314b81b6fd42debe4266
[goodguy/history.git] / cinelerra-5.1 / cinelerra / pluginlv2config.C
1 #ifdef HAVE_LV2
2
3 /*
4  * CINELERRA
5  * Copyright (C) 2018 GG
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include "arraylist.h"
24 #include "cstrdup.h"
25 #include "language.h"
26 #include "pluginlv2config.h"
27
28 #include <ctype.h>
29 #include <string.h>
30
31 PluginLV2UriTable::PluginLV2UriTable()
32  : Mutex("PluginLV2UriTable::PluginLV2UriTable")
33 {
34         set_array_delete();
35 }
36
37 PluginLV2UriTable::~PluginLV2UriTable()
38 {
39         remove_all_objects();
40 }
41
42 LV2_URID PluginLV2UriTable::map(const char *uri)
43 {
44         lock("PluginLV2UriTable::map");
45         int i = 0, n = size();
46         while( i<n && strcmp(uri, get(i)) ) ++i;
47         if( i >= n ) append(cstrdup(uri));
48         unlock();
49         return i+1;
50 }
51
52 const char *PluginLV2UriTable::unmap(LV2_URID urid)
53 {
54         lock("PluginLV2UriTable::unmap");
55         int idx = urid - 1;
56         const char *ret = idx>=0 && idx<size() ? get(idx) : 0;
57         unlock();
58         return ret;
59 }
60
61 PluginLV2Client_OptName:: PluginLV2Client_OptName(PluginLV2Client_Opt *opt)
62 {
63         this->opt = opt;
64         set_text(opt->get_name());
65 }
66
67 PluginLV2Client_OptValue::PluginLV2Client_OptValue(PluginLV2Client_Opt *opt)
68 {
69         this->opt = opt;
70         update();
71 }
72
73 int PluginLV2Client_OptValue::update()
74 {
75         char val[BCSTRLEN];
76         sprintf(val, "%f", opt->get_value());
77         if( !strcmp(val, get_text()) ) return 0;
78         set_text(val);
79         return 1;
80 }
81
82
83 PluginLV2Client_Opt::PluginLV2Client_Opt(PluginLV2ClientConfig *conf, int idx)
84 {
85         this->conf = conf;
86         this->idx = idx;
87         item_name = new PluginLV2Client_OptName(this);
88         item_value = new PluginLV2Client_OptValue(this);
89 }
90
91 PluginLV2Client_Opt::~PluginLV2Client_Opt()
92 {
93         delete item_name;
94         delete item_value;
95 }
96
97 float PluginLV2Client_Opt::get_value()
98 {
99         return conf->ctls[idx];
100 }
101
102 const char *PluginLV2Client_Opt::get_symbol()
103 {
104         return conf->syms[idx];
105 }
106
107 void PluginLV2Client_Opt::set_value(float v)
108 {
109         conf->ctls[idx] = v;
110 }
111
112 int PluginLV2Client_Opt::update(float v)
113 {
114         set_value(v);
115         return item_value->update();
116 }
117
118 const char *PluginLV2Client_Opt::get_name()
119 {
120         return conf->names[idx];
121 }
122
123 PluginLV2ClientConfig::PluginLV2ClientConfig()
124 {
125         names = 0;
126         syms = 0;
127         mins = 0;
128         maxs = 0;
129         ctls = 0;
130         nb_ports = 0;
131 }
132
133 PluginLV2ClientConfig::~PluginLV2ClientConfig()
134 {
135         reset();
136         remove_all_objects();
137 }
138
139 void PluginLV2ClientConfig::reset()
140 {
141         for( int i=0; i<nb_ports; ++i ) {
142                 delete [] names[i];
143                 delete [] syms[i];
144         }
145         delete [] names; names = 0;
146         delete [] mins;  mins = 0;
147         delete [] maxs;  maxs = 0;
148         delete [] ctls;  ctls = 0;
149         nb_ports = 0;
150 }
151
152
153 int PluginLV2ClientConfig::equivalent(PluginLV2ClientConfig &that)
154 {
155         PluginLV2ClientConfig &conf = *this;
156         for( int i=0; i<that.size(); ++i ) {
157                 PluginLV2Client_Opt *topt = conf[i], *vopt = that[i];
158                 if( !EQUIV(topt->get_value(), vopt->get_value()) ) return 0;
159         }
160         return 1;
161 }
162
163 void PluginLV2ClientConfig::copy_from(PluginLV2ClientConfig &that)
164 {
165         if( nb_ports != that.nb_ports ) {
166                 reset();
167                 nb_ports = that.nb_ports;
168                 names = new const char *[nb_ports];
169                 syms = new const char *[nb_ports];
170                 for( int i=0; i<nb_ports; ++i ) names[i] = syms[i] = 0;
171                 mins  = new float[nb_ports];
172                 maxs  = new float[nb_ports];
173                 ctls  = new float[nb_ports];
174         }
175         for( int i=0; i<nb_ports; ++i ) {
176                 delete [] names[i];  names[i] = cstrdup(that.names[i]);
177                 delete [] syms[i];   syms[i] = cstrdup(that.syms[i]);
178                 mins[i] = that.mins[i];
179                 maxs[i] = that.maxs[i];
180                 ctls[i] = that.ctls[i];
181         }
182         remove_all_objects();
183         for( int i=0; i<that.size(); ++i ) {
184                 append(new PluginLV2Client_Opt(this, that[i]->idx));
185         }
186 }
187
188 void PluginLV2ClientConfig::interpolate(PluginLV2ClientConfig &prev, PluginLV2ClientConfig &next,
189         int64_t prev_frame, int64_t next_frame, int64_t current_frame)
190 {
191         copy_from(prev);
192 }
193
194 void PluginLV2ClientConfig::init_lv2(const LilvPlugin *lilv)
195 {
196         reset();
197         nb_ports = lilv_plugin_get_num_ports(lilv);
198         names = new const char *[nb_ports];
199         syms = new const char *[nb_ports];
200         mins  = new float[nb_ports];
201         maxs  = new float[nb_ports];
202         ctls  = new float[nb_ports];
203         lilv_plugin_get_port_ranges_float(lilv, mins, maxs, ctls);
204         for( int i=0; i<nb_ports; ++i ) {
205                 const LilvPort *lp = lilv_plugin_get_port_by_index(lilv, i);
206                 LilvNode *pnm = lilv_port_get_name(lilv, lp);
207                 names[i] = cstrdup(lilv_node_as_string(pnm));
208                 lilv_node_free(pnm);
209                 const LilvNode *sym = lilv_port_get_symbol(lilv, lp);
210                 syms[i] = cstrdup(lilv_node_as_string(sym));
211         }
212 }
213
214 int PluginLV2ClientConfig::update()
215 {
216         int ret = 0;
217         PluginLV2ClientConfig &conf = *this;
218         for( int i=0; i<size(); ++i ) {
219                 if( conf[i]->item_value->update() ) ++ret;
220         }
221         return ret;
222 }
223
224 void PluginLV2ClientConfig::dump(FILE *fp)
225 {
226         fprintf(fp, "Controls:\n");
227         for( int i=0; i<size(); ++i ) {
228                 fprintf(fp, " %3d. (%3d)  %-24s  %f\n",
229                         i, get(i)->idx, get(i)->get_symbol(), get(i)->get_value());
230         }
231         fprintf(fp, "Ports:\n");
232         for( int i=0; i<nb_ports; ++i ) {
233                 fprintf(fp, " %3d.  %-24s  %f  (%f - %f) %s\n",
234                         i, syms[i], ctls[i], mins[i], maxs[i], names[i]);
235         }
236 }
237
238 #endif /* HAVE_LV2 */