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