+ Filterclavier: make non-experimental
[calf.git] / src / dssigui.cpp
blob99437b31e91bf8e4b2b0beef222b6ee857a0075a
1 /* Calf DSP Library utility application.
2 * DSSI GUI application.
3 * Copyright (C) 2007 Krzysztof Foltman
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 #include <assert.h>
21 #include <getopt.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <config.h>
25 #include <calf/giface.h>
26 #include <calf/gui.h>
27 #include <calf/main_win.h>
28 #include <calf/osctl.h>
29 #include <calf/osctlnet.h>
30 #include <calf/osctlserv.h>
32 using namespace std;
33 using namespace dsp;
34 using namespace calf_plugins;
35 using namespace osctl;
37 #define debug_printf(...)
39 #if 0
40 void osctl_test()
42 string sdata = string("\000\000\000\003123\000test\000\000\000\000\000\000\000\001\000\000\000\002", 24);
43 osc_stream is(sdata);
44 vector<osc_data> data;
45 is.read("bsii", data);
46 assert(is.pos == sdata.length());
47 assert(data.size() == 4);
48 assert(data[0].type == osc_blob);
49 assert(data[1].type == osc_string);
50 assert(data[2].type == osc_i32);
51 assert(data[3].type == osc_i32);
52 assert(data[0].strval == "123");
53 assert(data[1].strval == "test");
54 assert(data[2].i32val == 1);
55 assert(data[3].i32val == 2);
56 osc_stream os("");
57 os.write(data);
58 assert(os.buffer == sdata);
59 osc_server srv;
60 srv.bind("0.0.0.0", 4541);
62 osc_client cli;
63 cli.bind("0.0.0.0", 0);
64 cli.set_addr("0.0.0.0", 4541);
65 if (!cli.send("/blah", data))
66 g_error("Could not send the OSC message");
68 g_main_loop_run(g_main_loop_new(NULL, FALSE));
70 #endif
72 struct cairo_params
74 enum { HAS_COLOR = 1, HAS_WIDTH = 2 };
75 uint32_t flags;
76 float r, g, b, a;
77 float line_width;
79 cairo_params()
80 : flags(0)
81 , r(0.f)
82 , g(0.f)
83 , b(0.f)
84 , a(1.f)
85 , line_width(1)
90 struct graph_item: public cairo_params
92 float data[128];
95 struct dot_item: public cairo_params
97 float x, y;
98 int32_t size;
101 struct gridline_item: public cairo_params
103 float pos;
104 int32_t vertical;
105 std::string text;
108 struct param_line_graphs
110 vector<graph_item *> graphs;
111 vector<dot_item *> dots;
112 vector<gridline_item *> gridlines;
114 void clear();
117 void param_line_graphs::clear()
119 for (size_t i = 0; i < graphs.size(); i++)
120 delete graphs[i];
121 graphs.clear();
123 for (size_t i = 0; i < dots.size(); i++)
124 delete dots[i];
125 dots.clear();
127 for (size_t i = 0; i < gridlines.size(); i++)
128 delete gridlines[i];
129 gridlines.clear();
133 struct plugin_proxy: public plugin_ctl_iface, public plugin_metadata_proxy, public line_graph_iface
135 osc_client *client;
136 bool send_osc;
137 plugin_gui *gui;
138 map<string, string> cfg_vars;
139 int param_count;
140 float *params;
141 map<int, param_line_graphs> graphs;
142 bool update_graphs;
144 plugin_proxy(plugin_metadata_iface *md)
145 : plugin_metadata_proxy(md)
147 client = NULL;
148 send_osc = false;
149 update_graphs = true;
150 gui = NULL;
151 param_count = md->get_param_count();
152 params = new float[param_count];
153 for (int i = 0; i < param_count; i++)
154 params[i] = get_param_props(i)->def_value;
156 virtual float get_param_value(int param_no) {
157 if (param_no < 0 || param_no >= param_count)
158 return 0;
159 return params[param_no];
161 virtual void set_param_value(int param_no, float value) {
162 if (param_no < 0 || param_no >= param_count)
163 return;
164 update_graphs = true;
165 params[param_no] = value;
166 if (send_osc)
168 osc_inline_typed_strstream str;
169 str << (uint32_t)(param_no + get_param_port_offset()) << value;
170 client->send("/control", str);
173 virtual bool activate_preset(int bank, int program) {
174 if (send_osc) {
175 osc_inline_typed_strstream str;
176 str << (uint32_t)(bank) << (uint32_t)(program);
177 client->send("/program", str);
178 return false;
180 return false;
182 virtual float get_level(unsigned int port) { return 0.f; }
183 virtual void execute(int command_no) {
184 if (send_osc) {
185 stringstream ss;
186 ss << command_no;
188 osc_inline_typed_strstream str;
189 str << "ExecCommand" << ss.str();
190 client->send("/configure", str);
192 str.clear();
193 str << "ExecCommand" << "";
194 client->send("/configure", str);
197 char *configure(const char *key, const char *value) {
198 cfg_vars[key] = value;
199 osc_inline_typed_strstream str;
200 str << key << value;
201 client->send("/configure", str);
202 return NULL;
204 void send_configures(send_configure_iface *sci) {
205 for (map<string, string>::iterator i = cfg_vars.begin(); i != cfg_vars.end(); i++)
206 sci->send_configure(i->first.c_str(), i->second.c_str());
208 virtual line_graph_iface *get_line_graph_iface() { return this; }
209 virtual bool get_graph(int index, int subindex, float *data, int points, cairo_iface *context);
210 virtual bool get_dot(int index, int subindex, float &x, float &y, int &size, cairo_iface *context);
211 virtual bool get_gridline(int index, int subindex, float &pos, bool &vertical, std::string &legend, cairo_iface *context);
212 void update_cairo_context(cairo_iface *context, cairo_params &item);
215 bool plugin_proxy::get_graph(int index, int subindex, float *data, int points, cairo_iface *context)
217 if (!graphs.count(index))
218 return false;
219 param_line_graphs &g = graphs[index];
220 if (subindex < (int)g.graphs.size())
222 float *sdata = g.graphs[subindex]->data;
223 for (int i = 0; i < points; i++) {
224 float pos = i * 127.0 / points;
225 int ipos = i * 127 / points;
226 data[i] = sdata[ipos] + (sdata[ipos + 1] - sdata[ipos]) * (pos-ipos);
228 update_cairo_context(context, *g.graphs[subindex]);
229 return true;
231 return false;
234 bool plugin_proxy::get_dot(int index, int subindex, float &x, float &y, int &size, cairo_iface *context)
236 if (!graphs.count(index))
237 return false;
238 param_line_graphs &g = graphs[index];
239 if (subindex < (int)g.dots.size())
241 dot_item &item = *g.dots[subindex];
242 x = item.x;
243 y = item.y;
244 size = item.size;
245 update_cairo_context(context, item);
246 return true;
248 return false;
251 bool plugin_proxy::get_gridline(int index, int subindex, float &pos, bool &vertical, std::string &legend, cairo_iface *context)
253 if (!graphs.count(index))
254 return false;
255 param_line_graphs &g = graphs[index];
256 if (subindex < (int)g.gridlines.size())
258 gridline_item &item = *g.gridlines[subindex];
259 pos = item.pos;
260 vertical = item.vertical != 0;
261 legend = item.text;
262 update_cairo_context(context, item);
263 return true;
265 return false;
268 void plugin_proxy::update_cairo_context(cairo_iface *context, cairo_params &item)
270 if (item.flags & cairo_params::HAS_COLOR)
271 context->set_source_rgba(item.r, item.g, item.b, item.a);
272 if (item.flags & cairo_params::HAS_WIDTH)
273 context->set_line_width(item.line_width);
276 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
278 GMainLoop *mainloop;
280 static bool osc_debug = false;
282 struct dssi_osc_server: public osc_server, public osc_message_sink<osc_strstream>
284 plugin_proxy *plugin;
285 main_window *main_win;
286 plugin_gui_window *window;
287 string effect_name, title;
288 osc_client cli;
289 bool in_program, enable_dump;
290 vector<plugin_preset> presets;
291 /// Timeout callback source ID
292 int source_id;
293 bool osc_link_active;
295 dssi_osc_server()
296 : plugin(NULL)
297 , main_win(new main_window)
298 , window(new plugin_gui_window(main_win))
300 sink = this;
301 source_id = 0;
302 osc_link_active = false;
305 static void on_destroy(GtkWindow *window, dssi_osc_server *self)
307 debug_printf("on_destroy, have to send \"exiting\"\n");
308 bool result = self->cli.send("/exiting");
309 debug_printf("result = %d\n", result ? 1 : 0);
310 g_main_loop_quit(mainloop);
311 // eliminate a warning with empty debug_printf
312 result = false;
315 void create_window()
317 plugin = NULL;
318 vector<plugin_metadata_iface *> plugins;
319 get_all_plugins(plugins);
320 for (unsigned int i = 0; i < plugins.size(); i++)
322 if (!strcmp(plugins[i]->get_id(), effect_name.c_str()))
324 plugin = new plugin_proxy(plugins[i]);
325 break;
328 if (!plugin)
330 fprintf(stderr, "Unknown plugin: %s\n", effect_name.c_str());
331 exit(1);
333 plugin->client = &cli;
334 plugin->send_osc = true;
335 ((main_window *)window->main)->conditions.insert("dssi");
336 ((main_window *)window->main)->conditions.insert("directlink");
337 window->create(plugin, title.c_str(), effect_name.c_str());
338 plugin->gui = window->gui;
339 gtk_signal_connect(GTK_OBJECT(window->toplevel), "destroy", G_CALLBACK(on_destroy), this);
340 vector<plugin_preset> tmp_presets;
341 get_builtin_presets().get_for_plugin(presets, effect_name.c_str());
342 get_user_presets().get_for_plugin(tmp_presets, effect_name.c_str());
343 presets.insert(presets.end(), tmp_presets.begin(), tmp_presets.end());
344 source_id = g_timeout_add_full(G_PRIORITY_LOW, 1000/30, on_idle, this, NULL);
347 static gboolean on_idle(void *self)
349 dssi_osc_server *self_ptr = (dssi_osc_server *)(self);
350 if (self_ptr->osc_link_active)
351 self_ptr->send_osc_update();
352 return TRUE;
355 void set_osc_update(bool enabled);
356 void send_osc_update();
358 virtual void receive_osc_message(std::string address, std::string args, osc_strstream &buffer);
359 void unmarshal_line_graph(osc_strstream &buffer);
362 void dssi_osc_server::set_osc_update(bool enabled)
364 osc_link_active = enabled;
365 osc_inline_typed_strstream data;
366 data << "OSC:FEEDBACK_URI";
367 data << (enabled ? get_uri() : "");
368 cli.send("/configure", data);
371 void dssi_osc_server::send_osc_update()
373 static int serial_no = 0;
374 osc_inline_typed_strstream data;
375 data << "OSC:UPDATE";
376 data << calf_utils::i2s(serial_no++);
377 cli.send("/configure", data);
380 void dssi_osc_server::unmarshal_line_graph(osc_strstream &buffer)
382 uint32_t cmd;
384 do {
385 buffer >> cmd;
386 if (cmd == LGI_GRAPH)
388 uint32_t param;
389 buffer >> param;
390 param_line_graphs &graphs = plugin->graphs[param];
392 graphs.clear();
393 cairo_params params;
395 do {
396 buffer >> cmd;
397 if (cmd == LGI_SET_RGBA)
399 params.flags |= cairo_params::HAS_COLOR;
400 buffer >> params.r >> params.g >> params.b >> params.a;
402 else
403 if (cmd == LGI_SET_WIDTH)
405 params.flags |= cairo_params::HAS_WIDTH;
406 buffer >> params.line_width;
408 else
409 if (cmd == LGI_SUBGRAPH)
411 buffer >> param; // ignore number of points
412 graph_item *gi = new graph_item;
413 (cairo_params &)*gi = params;
414 for (int i = 0; i < 128; i++)
415 buffer >> gi->data[i];
416 graphs.graphs.push_back(gi);
417 params.flags = 0;
419 else
420 if (cmd == LGI_DOT)
422 dot_item *di = new dot_item;
423 (cairo_params &)*di = params;
424 buffer >> di->x >> di->y >> di->size;
425 graphs.dots.push_back(di);
426 params.flags = 0;
428 else
429 if (cmd == LGI_LEGEND)
431 gridline_item *li = new gridline_item;
432 (cairo_params &)*li = params;
433 buffer >> li->pos >> li->vertical >> li->text;
434 graphs.gridlines.push_back(li);
435 params.flags = 0;
437 else
438 break;
439 } while(1);
442 else
443 break;
444 } while(1);
447 void dssi_osc_server::receive_osc_message(std::string address, std::string args, osc_strstream &buffer)
449 if (osc_debug)
450 dump.receive_osc_message(address, args, buffer);
451 if (address == prefix + "/update" && args == "s")
453 string str;
454 buffer >> str;
455 debug_printf("UPDATE: %s\n", str.c_str());
456 set_osc_update(true);
457 send_osc_update();
458 return;
460 else if (address == prefix + "/quit")
462 set_osc_update(false);
463 debug_printf("QUIT\n");
464 g_main_loop_quit(mainloop);
465 return;
467 else if (address == prefix + "/configure"&& args == "ss")
469 string key, value;
470 buffer >> key >> value;
471 plugin->cfg_vars[key] = value;
472 // XXXKF perhaps this should be queued !
473 window->gui->refresh();
474 return;
476 else if (address == prefix + "/program"&& args == "ii")
478 uint32_t bank, program;
480 buffer >> bank >> program;
482 unsigned int nr = bank * 128 + program;
483 debug_printf("PROGRAM %d\n", nr);
484 if (nr == 0)
486 bool sosc = plugin->send_osc;
487 plugin->send_osc = false;
488 int count = plugin->get_param_count();
489 for (int i =0 ; i < count; i++)
490 plugin->set_param_value(i, plugin->get_param_props(i)->def_value);
491 plugin->send_osc = sosc;
492 window->gui->refresh();
493 // special handling for default preset
494 return;
496 nr--;
497 if (nr >= presets.size())
498 return;
499 bool sosc = plugin->send_osc;
500 plugin->send_osc = false;
501 presets[nr].activate(plugin);
502 plugin->send_osc = sosc;
503 window->gui->refresh();
505 // cli.send("/update", data);
506 return;
508 else if (address == prefix + "/control" && args == "if")
510 uint32_t port;
511 float val;
513 buffer >> port >> val;
515 int idx = port - plugin->get_param_port_offset();
516 debug_printf("CONTROL %d %f\n", idx, val);
517 bool sosc = plugin->send_osc;
518 plugin->send_osc = false;
519 window->gui->set_param_value(idx, val);
520 plugin->send_osc = sosc;
521 if (plugin->get_param_props(idx)->flags & PF_PROP_GRAPH)
522 plugin->update_graphs = true;
523 return;
525 else if (address == prefix + "/show")
527 set_osc_update(true);
529 gtk_widget_show_all(GTK_WIDGET(window->toplevel));
530 return;
532 else if (address == prefix + "/hide")
534 set_osc_update(false);
536 gtk_widget_hide(GTK_WIDGET(window->toplevel));
537 return;
539 else if (address == prefix + "/lineGraph")
541 unmarshal_line_graph(buffer);
542 if (plugin->update_graphs) {
543 // updates graphs that are only redrawn on startup and parameter changes
544 // (the OSC message may come a while after the parameter has been changed,
545 // so the redraw triggered by parameter change usually shows stale values)
546 window->gui->refresh();
547 plugin->update_graphs = false;
549 return;
551 else
552 printf("Unknown OSC address: %s\n", address.c_str());
555 //////////////////////////////////////////////////////////////////////////////////
557 void help(char *argv[])
559 printf("GTK+ user interface for Calf DSSI plugins\nSyntax: %s [--help] [--version] <osc-url> <so-file> <plugin-label> <instance-name>\n", argv[0]);
562 static struct option long_options[] = {
563 {"help", 0, 0, 'h'},
564 {"version", 0, 0, 'v'},
565 {"debug", 0, 0, 'd'},
566 {0,0,0,0},
569 int main(int argc, char *argv[])
571 char *debug_var = getenv("OSC_DEBUG");
572 if (debug_var && atoi(debug_var))
573 osc_debug = true;
575 gtk_init(&argc, &argv);
576 while(1) {
577 int option_index;
578 int c = getopt_long(argc, argv, "dhv", long_options, &option_index);
579 if (c == -1)
580 break;
581 switch(c) {
582 case 'd':
583 osc_debug = true;
584 break;
585 case 'h':
586 help(argv);
587 return 0;
588 case 'v':
589 printf("%s\n", PACKAGE_STRING);
590 return 0;
593 if (argc - optind != 4)
595 help(argv);
596 exit(0);
599 try {
600 get_builtin_presets().load_defaults(true);
601 get_user_presets().load_defaults(false);
603 catch(calf_plugins::preset_exception &e)
605 fprintf(stderr, "Error while loading presets: %s\n", e.what());
606 exit(1);
609 dssi_osc_server srv;
610 srv.prefix = "/dssi/"+string(argv[optind + 1]) + "/" + string(argv[optind + 2]);
611 for (char *p = argv[optind + 2]; *p; p++)
612 *p = tolower(*p);
613 srv.effect_name = argv[optind + 2];
614 srv.title = argv[optind + 3];
616 srv.bind();
617 srv.create_window();
619 mainloop = g_main_loop_new(NULL, FALSE);
621 srv.cli.bind();
622 srv.cli.set_url(argv[optind]);
624 debug_printf("URI = %s\n", srv.get_uri().c_str());
626 string data_buf, type_buf;
627 osc_inline_typed_strstream data;
628 data << srv.get_uri();
629 if (!srv.cli.send("/update", data))
631 g_error("Could not send the initial update message via OSC to %s", argv[optind]);
632 return 1;
635 g_main_loop_run(mainloop);
636 if (srv.source_id)
637 g_source_remove(srv.source_id);
639 srv.set_osc_update(false);
640 debug_printf("exited\n");
642 return 0;