This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
frei0r.cpp
330 lines (267 loc) · 9.68 KB
/
frei0r.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* FreeJ - Frei0r wrapper
*
* Copyright (C) 2007-2010 Denis Roio <jaromil@dyne.org>
* Copyright (C) 2010 Andrea Guzzo <xant@xant.net>
*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Please refer to the GNU Public License for more details.
*
* You should have received a copy of the GNU Public License along with
* this source code; if not, write to:
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* "$Id: $"
*
*/
#include <config.h>
#ifdef WITH_FREI0R
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h> // for snprintf()
#include <cstdlib>
#include <string>
#include <frei0r_freej.h>
#include <layer.h>
#include <jutils.h>
FACTORY_REGISTER_INSTANTIATOR(Filter, Freior, Frei0rFilter, core);
/// frei0r parameter callbacks
static void get_frei0r_parameter(FilterInstance *filt, Parameter *param, int idx) {
Freior *f = (Freior *)filt->proto;
switch(f->param_infos[idx-1].type) {
// idx-1 because frei0r's index starts from 0
case F0R_PARAM_BOOL:
(*f->f0r_get_param_value)(filt->core, (f0r_param_t)param->value, idx-1);
func("bool value is %s",(*(bool*)param->value==true) ? "true" : "false");
break;
case F0R_PARAM_DOUBLE:
(*f->f0r_get_param_value)(filt->core, (f0r_param_t)param->value, idx-1);
func("number value is %g",*(double*)param->value);
break;
case F0R_PARAM_COLOR:
{ f0r_param_color *color = new f0r_param_color;
(*f->f0r_get_param_value)(filt->core, (f0r_param_t)color, idx-1);
((double*)param->value)[0] = (double)color->r;
((double*)param->value)[1] = (double)color->g;
((double*)param->value)[2] = (double)color->b;
delete color;
} break;
case F0R_PARAM_POSITION:
{ f0r_param_position *position = new f0r_param_position;
(*f->f0r_get_param_value)(filt->core, (f0r_param_t)position, idx-1);
((double*)param->value)[0] = (double)position->x;
((double*)param->value)[1] = (double)position->y;
delete position;
} break;
default:
error("Unrecognized parameter type %u for get_parameter_value",
f->param_infos[idx].type);
}
}
static void set_frei0r_parameter(FilterInstance *filt, Parameter *param, int idx) {
func("set_frei0r_param callback on %s for parameter %s at pos %u",
filt->proto->name, param->name, idx);
Freior *f = (Freior *)filt->proto;
double *val = (double*)param->value;
switch(f->param_infos[idx-1].type) {
// idx-1 because frei0r's index starts from 0
case F0R_PARAM_BOOL:
func("bool value is %s",(*(bool*)param->value==true) ? "true" : "false");
(*f->f0r_set_param_value)
(filt->core, new f0r_param_bool(*(bool*)param->value), idx-1);
break;
case F0R_PARAM_DOUBLE:
func("number value is %g",*(double*)param->value);
(*f->f0r_set_param_value)(filt->core, new f0r_param_double( *(double*)param->value), idx-1);
break;
case F0R_PARAM_COLOR:
{ f0r_param_color *color = new f0r_param_color;
color->r = val[0];
color->g = val[1];
color->b = val[2];
(*f->f0r_set_param_value)(filt->core, color, idx-1);
delete color;
// QUAAA: should we delete the new allocated object? -jrml
} break;
case F0R_PARAM_POSITION:
{ f0r_param_position *position = new f0r_param_position;
position->x = val[0];
position->y = val[1];
(*f->f0r_set_param_value)(filt->core, position, idx-1);
} break;
default:
error("Unrecognized parameter type %u for set_parameter_value",
f->param_infos[idx].type);
}
}
// end of parameter callbacks
Freior::Freior()
: Filter()
{
handle = NULL;
opened = false;
set_name((char *)"Unknown");
}
Freior::~Freior() {
if(handle)
dlclose(handle);
}
int Freior::open(char *file) {
void *sym;
#ifdef HAVE_FREEBSD
const char *err;
#else
char *err;
#endif
if(opened) {
error("Freior object %p has already opened file %s",this, filename);
return 0;
}
// clear up the errors if there were any
dlerror();
#if 0
if (!dlopen_preflight(file)) {
warning("plugin '%s' failed: %s", file, dlerror());
return 0;
}
#endif
// Create dynamic handle to the library file.
handle = dlopen(file, RTLD_LAZY);
if(!handle) {
warning("can't dlopen plugin: %s", file);
return 0;
}
// try the frei0r symbol
sym = dlsym(handle, "f0r_init");
// return if error
if( dlerror() ) {
// func("not a valid frei0r plugin: %s", file);
// don't forget to close
dlclose(handle);
handle = NULL;
return 0;
}
// Get interface function pointers
*(void**) (&f0r_init) = sym; // dlsym(handle, "f0r_init");
*(void**) (&f0r_get_plugin_info) = dlsym(handle, "f0r_get_plugin_info");
err = dlerror();
if (err)
warning("%s in frei0r plugin %s", err, file);
*(void**) (&f0r_get_param_info) = dlsym(handle, "f0r_get_param_info");
err = dlerror();
if (err)
warning("%s in frei0r plugin %s", err, file);
*(void**) (&f0r_construct) = dlsym(handle, "f0r_construct");
err = dlerror();
if (err)
warning("%s in frei0r plugin %s", err, file);
*(void**) (&f0r_destruct) = dlsym(handle, "f0r_destruct");
err = dlerror();
if (err)
warning("%s in frei0r plugin %s", err, file);
*(void**) (&f0r_set_param_value) = dlsym(handle, "f0r_set_param_value");
err = dlerror();
if (err)
warning("%s in frei0r plugin %s", err, file);
*(void**) (&f0r_get_param_value) = dlsym(handle, "f0r_get_param_value");
err = dlerror();
if (err)
warning("%s in frei0r plugin %s", err, file);
*(void**) (&f0r_update) = dlsym(handle, "f0r_update");
// XXX - don't output an error since f0r_update() is exported only by generators
// and it won't be found when opening filters
//err = dlerror(); if (err) warning("%s in frei0r plugin %s", err, file);
// get the info on the plugin
(*f0r_get_plugin_info)(&info);
opened = true;
snprintf(filename,255,"%s",file);
f0r_init();
set_name((char*)info.name);
if(get_debug()>2)
print_info();
return 1;
}
void Freior::init_parameters(Linklist<Parameter> ¶meters) {
// Get the list of params.
param_infos.resize(info.num_params);
for (int i = 0; i < info.num_params; ++i) {
(f0r_get_param_info)(¶m_infos[i], i);
Parameter *param = new Parameter((Parameter::Type)param_infos[i].type);
snprintf(param->name, 255, "%s", param_infos[i].name);
func("registering parameter %s for filter %s\n", param->name, info.name);
snprintf(param->description, 512, "%s", param_infos[i].explanation);
param->filter_set_f = set_frei0r_parameter;
param->filter_get_f = get_frei0r_parameter;
parameters.append(param);
}
}
void Freior::print_info() {
notice("Name : %s", info.name);
act("%s",info.explanation);
switch(info.plugin_type) {
case F0R_PLUGIN_TYPE_FILTER: act("Type : Filter"); break;
case F0R_PLUGIN_TYPE_SOURCE: act("Type : Source"); break;
case F0R_PLUGIN_TYPE_MIXER2: act("Type : Mixer2"); break;
case F0R_PLUGIN_TYPE_MIXER3: act("Type : Mixer3"); break;
default: error("Unrecognized plugin type");
}
act("Author : %s", info.author);
act("Parameters [%i total]",info.num_params);
for (int i=0; i<info.num_params; ++i) {
char tmp[256];
snprintf(tmp,255," [%i] %s ",i, param_infos[i].name);
switch (param_infos[i].type) {
case F0R_PARAM_BOOL: act("%s (bool) %s",tmp, param_infos[i].explanation); break;
case F0R_PARAM_DOUBLE: act("%s (double) %s",tmp, param_infos[i].explanation); break;
case F0R_PARAM_COLOR: act("%s (color) %s",tmp, param_infos[i].explanation); break;
case F0R_PARAM_POSITION: act("%s (position) %s",tmp, param_infos[i].explanation); break;
case F0R_PARAM_STRING: act("%s (string) %s",tmp, param_infos[i].explanation); break;
default: error("%s Unrecognized info type.",tmp );
}
}
}
bool Freior::apply(Layer *lay, FilterInstance *instance)
{
instance->core = (*f0r_construct)(lay->geo.w, lay->geo.h);
if (!instance->core)
return false;
return Filter::apply(lay, instance);
}
void Freior::destruct(FilterInstance *inst)
{
if(inst->core) {
f0r_destruct((f0r_instance_t*)inst->core);
inst->core = NULL;
}
}
void Freior::update(FilterInstance *inst, double time, uint32_t *inframe, uint32_t *outframe) {
Filter::update(inst, time, inframe, outframe);
f0r_update((f0r_instance_t*)inst->core, time, inframe, outframe);
}
const char *Freior::description()
{
return info.explanation;
}
const char *Freior::author()
{
return info.author;
}
int Freior::get_parameter_type(int i)
{
return param_infos[i].type;
}
char *Freior::get_parameter_description(int i)
{
return (char*)param_infos[i].explanation;
}
int Freior::type()
{
return Filter::FREIOR;
}
#endif // WITH_FREI0R