ae91576c9f5ae1ea5880986f7c73ee95fbe8a754
[ardour.git] / libs / surfaces / osc / osc.cc
1 /*
2  * Copyright (C) 2006 Paul Davis
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19
20 #include <iostream>
21 #include <fstream>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <cerrno>
25 #include <algorithm>
26
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include <glib/gstdio.h>
31 #include <glibmm/miscutils.h>
32
33 #include <pbd/convert.h>
34 #include <pbd/pthread_utils.h>
35 #include <pbd/file_utils.h>
36 #include <pbd/failed_constructor.h>
37
38 #include "ardour/amp.h"
39 #include "ardour/session.h"
40 #include "ardour/route.h"
41 #include "ardour/audio_track.h"
42 #include "ardour/midi_track.h"
43 #include "ardour/dB.h"
44 #include "ardour/filesystem_paths.h"
45 #include "ardour/panner.h"
46 #include "ardour/plugin.h"
47 #include "ardour/plugin_insert.h"
48 #include "ardour/send.h"
49
50 #include "osc.h"
51 #include "osc_controllable.h"
52 #include "osc_route_observer.h"
53 #include "i18n.h"
54
55 using namespace ARDOUR;
56 using namespace std;
57 using namespace Glib;
58 using namespace ArdourSurface;
59
60 #include "pbd/abstract_ui.cc" // instantiate template
61
62 OSC* OSC::_instance = 0;
63
64 #ifdef DEBUG
65 static void error_callback(int num, const char *m, const char *path)
66 {
67         fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
68 }
69 #else
70 static void error_callback(int, const char *, const char *)
71 {
72
73 }
74 #endif
75
76 OSC::OSC (Session& s, uint32_t port)
77         : ControlProtocol (s, X_("Open Sound Control (OSC)"))
78         , AbstractUI<OSCUIRequest> ("osc")
79         , local_server (0)
80         , remote_server (0)
81         , _port(port)
82         , _ok (true)
83         , _shutdown (false)
84         , _osc_server (0)
85         , _osc_unix_server (0)
86         , _namespace_root ("/ardour")
87         , _send_route_changes (true)
88 {
89         _instance = this;
90
91         session_loaded (s);
92         session->Exported.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::session_exported, this, _1, _2), this);
93 }
94
95 OSC::~OSC()
96 {
97         stop ();
98         _instance = 0;
99 }
100
101 void
102 OSC::do_request (OSCUIRequest* req)
103 {
104         if (req->type == CallSlot) {
105
106                 call_slot (MISSING_INVALIDATOR, req->the_slot);
107
108         } else if (req->type == Quit) {
109
110                 stop ();
111         }
112 }
113
114 int
115 OSC::set_active (bool yn)
116 {
117         if (yn != active()) {
118
119                 if (yn) {
120                         if (start ()) {
121                                 return -1;
122                         }
123                 } else {
124                         if (stop ()) {
125                                 return -1;
126                         }
127                 }
128
129         }
130
131         return ControlProtocol::set_active (yn);
132 }
133
134 bool
135 OSC::get_active () const
136 {
137         return _osc_server != 0;
138 }
139
140 int
141 OSC::set_feedback (bool yn)
142 {
143         _send_route_changes = yn;
144         return 0;
145 }
146
147 bool
148 OSC::get_feedback () const
149 {
150         return _send_route_changes;
151 }
152
153 int
154 OSC::start ()
155 {
156         char tmpstr[255];
157
158         if (_osc_server) {
159                 /* already started */
160                 return 0;
161         }
162
163         for (int j=0; j < 20; ++j) {
164                 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
165
166                 //if ((_osc_server = lo_server_new_with_proto (tmpstr, LO_TCP, error_callback))) {
167                 //      break;
168                 //}
169
170                 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
171                         break;
172                 }
173
174 #ifdef DEBUG
175                 cerr << "can't get osc at port: " << _port << endl;
176 #endif
177                 _port++;
178                 continue;
179         }
180
181         if (!_osc_server) {
182                 return 1;
183         }
184
185 #ifdef ARDOUR_OSC_UNIX_SERVER
186
187         // APPEARS sluggish for now
188
189         // attempt to create unix socket server too
190
191         snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
192         int fd = mkstemp(tmpstr);
193
194         if (fd >= 0 ) {
195                 ::g_unlink (tmpstr);
196                 close (fd);
197
198                 _osc_unix_server = lo_server_new (tmpstr, error_callback);
199
200                 if (_osc_unix_server) {
201                         _osc_unix_socket_path = tmpstr;
202                 }
203         }
204 #endif
205
206         PBD::info << "OSC @ " << get_server_url () << endmsg;
207
208         std::string url_file;
209
210         if (find_file (ardour_config_search_path(), "osc_url", url_file)) {
211
212                 _osc_url_file = url_file;
213                 ofstream urlfile;
214                 urlfile.open(_osc_url_file.c_str(), ios::trunc);
215
216                 if (urlfile) {
217                         urlfile << get_server_url () << endl;
218                         urlfile.close();
219                 } else {
220                         cerr << "Couldn't write '" <<  _osc_url_file << "'" <<endl;
221                 }
222         }
223
224         register_callbacks();
225
226         // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
227
228         /* startup the event loop thread */
229
230         BaseUI::run ();
231
232         return 0;
233 }
234
235 void
236 OSC::thread_init ()
237 {
238         pthread_set_name (X_("OSC"));
239
240         if (_osc_unix_server) {
241                 Glib::RefPtr<IOSource> src = IOSource::create (lo_server_get_socket_fd (_osc_unix_server), IO_IN|IO_HUP|IO_ERR);
242                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_unix_server));
243                 src->attach (_main_loop->get_context());
244                 local_server = src->gobj();
245                 g_source_ref (local_server);
246         }
247
248         if (_osc_server) {
249                 Glib::RefPtr<IOSource> src  = IOSource::create (lo_server_get_socket_fd (_osc_server), IO_IN|IO_HUP|IO_ERR);
250                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_server));
251                 src->attach (_main_loop->get_context());
252                 remote_server = src->gobj();
253                 g_source_ref (remote_server);
254         }
255
256         PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("OSC"), 2048);
257         SessionEvent::create_per_thread_pool (X_("OSC"), 128);
258 }
259
260 int
261 OSC::stop ()
262 {
263         /* stop main loop */
264
265         if (local_server) {
266                 g_source_destroy (local_server);
267                 g_source_unref (local_server);
268                 local_server = 0;
269         }
270
271         if (remote_server) {
272                 g_source_destroy (remote_server);
273                 g_source_unref (remote_server);
274                 remote_server = 0;
275         }
276
277         BaseUI::quit ();
278
279         if (_osc_server) {
280                 int fd = lo_server_get_socket_fd(_osc_server);
281                 if (fd >=0) {
282                         close(fd);
283                 }
284                 lo_server_free (_osc_server);
285                 _osc_server = 0;
286         }
287
288         if (_osc_unix_server) {
289                 int fd = lo_server_get_socket_fd(_osc_unix_server);
290                 if (fd >=0) {
291                         close(fd);
292                 }
293                 lo_server_free (_osc_unix_server);
294                 _osc_unix_server = 0;
295         }
296
297         if (!_osc_unix_socket_path.empty()) {
298                 ::g_unlink (_osc_unix_socket_path.c_str());
299         }
300
301         if (!_osc_url_file.empty() ) {
302                 ::g_unlink (_osc_url_file.c_str() );
303         }
304
305         // Delete any active route observers
306         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
307
308                 OSCRouteObserver* rc;
309
310                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
311                         delete *x;
312                         x = route_observers.erase (x);
313                 } else {
314                         ++x;
315                 }
316         }
317
318         return 0;
319 }
320
321 void
322 OSC::register_callbacks()
323 {
324         lo_server srvs[2];
325         lo_server serv;
326
327         srvs[0] = _osc_server;
328         srvs[1] = _osc_unix_server;
329
330         for (size_t i = 0; i < 2; ++i) {
331
332                 if (!srvs[i]) {
333                         continue;
334                 }
335
336                 serv = srvs[i];
337
338                 /* this is a special catchall handler */
339
340                 lo_server_add_method (serv, 0, 0, _catchall, this);
341
342 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
343
344                 REGISTER_CALLBACK (serv, "/routes/list", "", routes_list);
345                 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
346                 REGISTER_CALLBACK (serv, "/ardour/access_action", "s", access_action);
347                 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
348                 REGISTER_CALLBACK (serv, "/ardour/loop_location", "ii", loop_location);
349                 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
350                 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
351                 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
352                 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
353                 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
354                 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
355                 REGISTER_CALLBACK (serv, "/ardour/transport_frame", "", transport_frame);
356                 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
357                 REGISTER_CALLBACK (serv, "/ardour/locate", "ii", locate);
358                 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
359                 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
360                 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
361                 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
362                 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
363                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
364                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
365                 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
366                 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
367
368                 REGISTER_CALLBACK (serv, "/ardour/routes/mute", "ii", route_mute);
369                 REGISTER_CALLBACK (serv, "/ardour/routes/solo", "ii", route_solo);
370                 REGISTER_CALLBACK (serv, "/ardour/routes/recenable", "ii", route_recenable);
371                 REGISTER_CALLBACK (serv, "/ardour/routes/gainabs", "if", route_set_gain_abs);
372                 REGISTER_CALLBACK (serv, "/ardour/routes/gaindB", "if", route_set_gain_dB);
373                 REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_position", "if", route_set_pan_stereo_position);
374                 REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_width", "if", route_set_pan_stereo_width);
375                 REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter", "iiif", route_plugin_parameter);
376                 REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter/print", "iii", route_plugin_parameter_print);
377                 REGISTER_CALLBACK (serv, "/ardour/routes/send/gainabs", "iif", route_set_send_gain_abs);
378                 REGISTER_CALLBACK (serv, "/ardour/routes/send/gaindB", "iif", route_set_send_gain_dB);
379
380                 /* still not-really-standardized query interface */
381                 //REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
382                 //REGISTER_CALLBACK (serv, "/ardour/set", "", set);
383
384                 // un/register_update args= s:ctrl s:returl s:retpath
385                 //lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
386                 //lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
387                 //lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
388                 //lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
389
390         }
391 }
392
393 bool
394 OSC::osc_input_handler (IOCondition ioc, lo_server srv)
395 {
396         if (ioc & ~IO_IN) {
397                 return false;
398         }
399
400         if (ioc & IO_IN) {
401                 lo_server_recv (srv);
402         }
403
404         return true;
405 }
406
407 std::string
408 OSC::get_server_url()
409 {
410         string url;
411         char * urlstr;
412
413         if (_osc_server) {
414                 urlstr = lo_server_get_url (_osc_server);
415                 url = urlstr;
416                 free (urlstr);
417         }
418
419         return url;
420 }
421
422 std::string
423 OSC::get_unix_server_url()
424 {
425         string url;
426         char * urlstr;
427
428         if (_osc_unix_server) {
429                 urlstr = lo_server_get_url (_osc_unix_server);
430                 url = urlstr;
431                 free (urlstr);
432         }
433
434         return url;
435 }
436
437 void
438 OSC::listen_to_route (boost::shared_ptr<Route> route, lo_address addr)
439 {
440         /* avoid duplicate listens */
441
442         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); ++x) {
443
444                 OSCRouteObserver* ro;
445
446                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
447
448                         int res = strcmp(lo_address_get_hostname(ro->address()), lo_address_get_hostname(addr));
449
450                         if (ro->route() == route && res == 0) {
451                                 return;
452                         }
453                 }
454         }
455
456         OSCRouteObserver* o = new OSCRouteObserver (route, addr);
457         route_observers.push_back (o);
458
459         route->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::drop_route, this, boost::weak_ptr<Route> (route)), this);
460 }
461
462 void
463 OSC::drop_route (boost::weak_ptr<Route> wr)
464 {
465         boost::shared_ptr<Route> r = wr.lock ();
466
467         if (!r) {
468                 return;
469         }
470
471         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
472
473                 OSCRouteObserver* rc;
474
475                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
476
477                         if (rc->route() == r) {
478                                 delete *x;
479                                 x = route_observers.erase (x);
480                         } else {
481                                 ++x;
482                         }
483                 } else {
484                         ++x;
485                 }
486         }
487 }
488
489 void
490 OSC::end_listen (boost::shared_ptr<Route> r, lo_address addr)
491 {
492         RouteObservers::iterator x;
493
494         // Remove the route observers
495         for (x = route_observers.begin(); x != route_observers.end();) {
496
497                 OSCRouteObserver* ro;
498
499                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
500
501                         int res = strcmp(lo_address_get_hostname(ro->address()), lo_address_get_hostname(addr));
502
503                         if (ro->route() == r && res == 0) {
504                                 delete *x;
505                                 x = route_observers.erase (x);
506                         }
507                         else {
508                                 ++x;
509                         }
510                 }
511                 else {
512                         ++x;
513                 }
514         }
515 }
516
517 void
518 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
519 {
520         char* subpath;
521
522         subpath = (char*) malloc (len-15+1);
523         memcpy (subpath, path, len-15);
524         subpath[len-15] = '\0';
525
526         send_current_value (subpath, argv, argc, msg);
527
528         free (subpath);
529 }
530
531 void
532 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
533 {
534         if (!session) {
535                 return;
536         }
537
538         lo_message reply = lo_message_new ();
539         boost::shared_ptr<Route> r;
540         int id;
541
542         lo_message_add_string (reply, path);
543
544         if (argc == 0) {
545                 lo_message_add_string (reply, "bad syntax");
546         } else {
547                 id = argv[0]->i;
548                 r = session->route_by_remote_id (id);
549
550                 if (!r) {
551                         lo_message_add_string (reply, "not found");
552                 } else {
553
554                         if (strcmp (path, "/routes/state") == 0) {
555
556                                 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
557                                         lo_message_add_string (reply, "AT");
558                                 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
559                                         lo_message_add_string (reply, "MT");
560                                 } else {
561                                         lo_message_add_string (reply, "B");
562                                 }
563
564                                 lo_message_add_string (reply, r->name().c_str());
565                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
566                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
567                                 lo_message_add_int32 (reply, r->muted());
568                                 lo_message_add_int32 (reply, r->soloed());
569
570                         } else if (strcmp (path, "/routes/mute") == 0) {
571
572                                 lo_message_add_int32 (reply, (float) r->muted());
573
574                         } else if (strcmp (path, "/routes/solo") == 0) {
575
576                                 lo_message_add_int32 (reply, r->soloed());
577                         }
578                 }
579         }
580
581         lo_send_message (lo_message_get_source (msg), "#reply", reply);
582         lo_message_free (reply);
583 }
584
585 int
586 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
587 {
588         return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
589 }
590
591 int
592 OSC::catchall (const char *path, const char* /*types*/, lo_arg **argv, int argc, lo_message msg)
593 {
594         size_t len;
595         int ret = 1; /* unhandled */
596
597         //cerr << "Received a message, path = " << path << " types = \""
598         //     << (types ? types : "NULL") << '"' << endl;
599
600         /* 15 for /#current_value plus 2 for /<path> */
601
602         len = strlen (path);
603
604         if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
605                 current_value_query (path, len, argv, argc, msg);
606                 ret = 0;
607
608         } else if (strcmp (path, "/routes/listen") == 0) {
609
610                 cerr << "set up listener\n";
611
612                 lo_message reply = lo_message_new ();
613
614                 if (argc <= 0) {
615                         lo_message_add_string (reply, "syntax error");
616                 } else {
617                         for (int n = 0; n < argc; ++n) {
618
619                                 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
620
621                                 if (!r) {
622                                         lo_message_add_string (reply, "not found");
623                                         cerr << "no such route\n";
624                                         break;
625                                 } else {
626                                         cerr << "add listener\n";
627                                         listen_to_route (r, lo_message_get_source (msg));
628                                         lo_message_add_int32 (reply, argv[n]->i);
629                                 }
630                         }
631                 }
632
633                 lo_send_message (lo_message_get_source (msg), "#reply", reply);
634                 lo_message_free (reply);
635
636                 ret = 0;
637
638         } else if (strcmp (path, "/routes/ignore") == 0) {
639
640                 for (int n = 0; n < argc; ++n) {
641
642                         boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
643
644                         if (r) {
645                                 end_listen (r, lo_message_get_source (msg));
646                         }
647                 }
648
649                 ret = 0;
650         }
651
652         return ret;
653 }
654
655 void
656 OSC::update_clock ()
657 {
658
659 }
660
661 // "Application Hook" Handlers //
662 void
663 OSC::session_loaded (Session& s)
664 {
665         lo_address listener = lo_address_new (NULL, "7770");
666         lo_send (listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str());
667 }
668
669 void
670 OSC::session_exported (std::string path, std::string name)
671 {
672         lo_address listener = lo_address_new (NULL, "7770");
673         lo_send (listener, "/session/exported", "ss", path.c_str(), name.c_str());
674 }
675
676 // end "Application Hook" Handlers //
677
678 /* path callbacks */
679
680 int
681 OSC::current_value (const char */*path*/, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, void */*data*/, void* /*user_data*/)
682 {
683 #if 0
684         const char* returl;
685
686         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
687                 return 1;
688         }
689
690         const char *returl = argv[1]->s;
691         lo_address addr = find_or_cache_addr (returl);
692
693         const char *retpath = argv[2]->s;
694
695
696         if (strcmp (argv[0]->s, "transport_frame") == 0) {
697
698                 if (session) {
699                         lo_send (addr, retpath, "i", session->transport_frame());
700                 }
701
702         } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
703
704                 if (session) {
705                         lo_send (addr, retpath, "i", session->transport_frame());
706                 }
707
708         } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
709
710                 if (session) {
711                         lo_send (addr, retpath, "i", session->transport_frame());
712                 }
713
714         } else if (strcmp (argv[0]->s, "punch_in") == 0) {
715
716                 if (session) {
717                         lo_send (addr, retpath, "i", session->transport_frame());
718                 }
719
720         } else if (strcmp (argv[0]->s, "punch_out") == 0) {
721
722                 if (session) {
723                         lo_send (addr, retpath, "i", session->transport_frame());
724                 }
725
726         } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
727
728                 if (session) {
729                         lo_send (addr, retpath, "i", session->transport_frame());
730                 }
731
732         } else {
733
734                 /* error */
735         }
736 #endif
737         return 0;
738 }
739
740 void
741 OSC::routes_list (lo_message msg)
742 {
743         for (int n = 0; n < (int) session->nroutes(); ++n) {
744
745                 boost::shared_ptr<Route> r = session->route_by_remote_id (n);
746
747                 if (r) {
748
749                         lo_message reply = lo_message_new ();
750
751                         if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
752                                 lo_message_add_string (reply, "AT");
753                         } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
754                                 lo_message_add_string (reply, "MT");
755                         } else {
756                                 lo_message_add_string (reply, "B");
757                         }
758
759                         lo_message_add_string (reply, r->name().c_str());
760                         lo_message_add_int32 (reply, r->n_inputs().n_audio());
761                         lo_message_add_int32 (reply, r->n_outputs().n_audio());
762                         lo_message_add_int32 (reply, r->muted());
763                         lo_message_add_int32 (reply, r->soloed());
764                         lo_message_add_int32 (reply, r->remote_control_id());
765
766                         if (boost::dynamic_pointer_cast<AudioTrack>(r)
767                                         || boost::dynamic_pointer_cast<MidiTrack>(r)) {
768
769                                 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(r);
770                                 lo_message_add_int32 (reply, t->record_enabled());
771                         }
772
773                         //Automatically listen to routes listed
774                         listen_to_route(r, lo_message_get_source (msg));
775
776                         lo_send_message (lo_message_get_source (msg), "#reply", reply);
777                         lo_message_free (reply);
778                 }
779         }
780
781         // Send end of listing message
782         lo_message reply = lo_message_new ();
783
784         lo_message_add_string (reply, "end_route_list");
785         lo_message_add_int64 (reply, session->frame_rate());
786         lo_message_add_int64 (reply, session->current_end_frame());
787
788         lo_send_message (lo_message_get_source (msg), "#reply", reply);
789
790         lo_message_free (reply);
791 }
792
793 void
794 OSC::transport_frame (lo_message msg)
795 {
796         framepos_t pos = session->transport_frame ();
797
798         lo_message reply = lo_message_new ();
799         lo_message_add_int64 (reply, pos);
800
801         lo_send_message (lo_message_get_source (msg), "/ardour/transport_frame", reply);
802
803         lo_message_free (reply);
804 }
805
806 int
807 OSC::route_mute (int rid, int yn)
808 {
809         if (!session) return -1;
810
811         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
812
813         if (r) {
814                 r->set_mute (yn, this);
815         }
816
817         return 0;
818 }
819
820 int
821 OSC::route_solo (int rid, int yn)
822 {
823         if (!session) return -1;
824
825         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
826
827         if (r) {
828                 r->set_solo (yn, this);
829         }
830
831         return 0;
832 }
833
834 int
835 OSC::route_recenable (int rid, int yn)
836 {
837         if (!session) return -1;
838
839         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
840
841         if (r) {
842                 r->set_record_enabled (yn, this);
843         }
844
845         return 0;
846 }
847
848 int
849 OSC::route_set_gain_abs (int rid, float level)
850 {
851         if (!session) return -1;
852
853         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
854
855         if (r) {
856                 r->set_gain (level, this);
857         }
858
859         return 0;
860 }
861
862 int
863 OSC::route_set_gain_dB (int rid, float dB)
864 {
865         if (!session) return -1;
866
867         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
868
869         if (r) {
870                 r->set_gain (dB_to_coefficient (dB), this);
871         }
872
873         return 0;
874 }
875
876 int
877 OSC::route_set_pan_stereo_position (int rid, float pos)
878 {
879         if (!session) return -1;
880
881         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
882
883         if (r) {
884                 boost::shared_ptr<Panner> panner = r->panner();
885                 if (panner) {
886                         panner->set_position (pos);
887                 }
888         }
889
890         return 0;
891
892 }
893
894 int
895 OSC::route_set_pan_stereo_width (int rid, float pos)
896 {
897         if (!session) return -1;
898
899         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
900
901         if (r) {
902                 boost::shared_ptr<Panner> panner = r->panner();
903                 if (panner) {
904                         panner->set_width (pos);
905                 }
906         }
907
908         return 0;
909
910 }
911
912 int
913 OSC::route_set_send_gain_abs (int rid, int sid, float val)
914 {
915         if (!session) {
916                 return -1;
917         }
918
919         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
920
921         if (!r) {
922                 return -1;
923         }
924
925         /* revert to zero-based counting */
926
927         if (sid > 0) {
928                 --sid;
929         }
930
931         boost::shared_ptr<Processor> p = r->nth_send (sid);
932
933         if (p) {
934                 boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
935                 boost::shared_ptr<Amp> a = s->amp();
936
937                 if (a) {
938                         a->set_gain (val, this);
939                 }
940         }
941         return 0;
942 }
943
944 int
945 OSC::route_set_send_gain_dB (int rid, int sid, float val)
946 {
947         if (!session) {
948                 return -1;
949         }
950
951         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
952
953         if (!r) {
954                 return -1;
955         }
956
957         /* revert to zero-based counting */
958
959         if (sid > 0) {
960                 --sid;
961         }
962
963         boost::shared_ptr<Processor> p = r->nth_send (sid);
964
965         if (p) {
966                 boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
967                 boost::shared_ptr<Amp> a = s->amp();
968
969                 if (a) {
970                         a->set_gain (dB_to_coefficient (val), this);
971                 }
972         }
973         return 0;
974 }
975
976 int
977 OSC::route_plugin_parameter (int rid, int piid, int par, float val)
978 {
979         if (!session)
980                 return -1;
981
982         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
983
984         if (!r) {
985                 PBD::error << "OSC: Invalid Remote Control ID '" << rid << "'" << endmsg;
986                 return -1;
987         }
988
989         boost::shared_ptr<Processor> redi=r->nth_plugin (piid);
990
991         if (!redi) {
992                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << rid << "'" << endmsg;
993                 return -1;
994         }
995
996         boost::shared_ptr<PluginInsert> pi;
997
998         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
999                 PBD::error << "OSC: given processor # " << piid << " on RID '" << rid << "' is not a Plugin." << endmsg;
1000                 return -1;
1001         }
1002
1003         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
1004         bool ok=false;
1005
1006         uint32_t controlid = pip->nth_parameter (par,ok);
1007
1008         if (!ok) {
1009                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << rid << "'" << endmsg;
1010                 return -1;
1011         }
1012
1013         if (!pip->parameter_is_input(controlid)) {
1014                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << rid << "' is not a control input" << endmsg;
1015                 return -1;
1016         }
1017
1018         ParameterDescriptor pd;
1019         pi->plugin()->get_parameter_descriptor (controlid,pd);
1020
1021         if (val >= pd.lower && val < pd.upper) {
1022
1023                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
1024                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
1025                 c->set_value (val);
1026         } else {
1027                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << rid << "' is out of range" << endmsg;
1028                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
1029         }
1030
1031         return 0;
1032 }
1033
1034 int
1035 OSC::route_plugin_parameter_print (int rid, int piid, int par)
1036 {
1037         if (!session) {
1038                 return -1;
1039         }
1040
1041         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1042
1043         if (!r) {
1044                 return -1;
1045         }
1046
1047         boost::shared_ptr<Processor> redi=r->nth_processor (piid);
1048
1049         if (!redi) {
1050                 return -1;
1051         }
1052
1053         boost::shared_ptr<PluginInsert> pi;
1054
1055         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
1056                 return -1;
1057         }
1058
1059         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
1060         bool ok=false;
1061
1062         uint32_t controlid = pip->nth_parameter (par,ok);
1063
1064         if (!ok) {
1065                 return -1;
1066         }
1067
1068         ParameterDescriptor pd;
1069
1070         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
1071                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
1072
1073                 cerr << "parameter:     " << redi->describe_parameter(controlid)  << "\n";
1074                 cerr << "current value: " << c->get_value ();
1075                 cerr << "lower value:   " << pd.lower << "\n";
1076                 cerr << "upper value:   " << pd.upper << "\n";
1077         }
1078
1079         return 0;
1080 }
1081
1082 XMLNode&
1083 OSC::get_state ()
1084 {
1085         XMLNode& node (ControlProtocol::get_state());
1086
1087         node.add_property (X_("feedback"), _send_route_changes ? "1" : "0");
1088         return node;
1089 }
1090
1091 int
1092 OSC::set_state (const XMLNode& node, int /*version*/)
1093 {
1094         const XMLProperty* prop = node.property (X_("feedback"));
1095
1096         if (prop) {
1097                 if (PBD::string_is_affirmative (prop->value())) {
1098                         _send_route_changes = true;
1099                 } else {
1100                         _send_route_changes = false;
1101                 }
1102         } else {
1103                 /* leave it alone */
1104         }
1105
1106         return 0;
1107 }