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