Merge branch 'uk-english'
[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/goto_start", "", goto_start);
348                 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
349                 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
350                 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
351                 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
352                 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
353                 REGISTER_CALLBACK (serv, "/ardour/transport_frame", "", transport_frame);
354                 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
355                 REGISTER_CALLBACK (serv, "/ardour/locate", "ii", locate);
356                 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
357                 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
358                 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
359                 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
360                 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
361                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
362                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
363                 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
364                 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
365
366                 REGISTER_CALLBACK (serv, "/ardour/routes/mute", "ii", route_mute);
367                 REGISTER_CALLBACK (serv, "/ardour/routes/solo", "ii", route_solo);
368                 REGISTER_CALLBACK (serv, "/ardour/routes/recenable", "ii", route_recenable);
369                 REGISTER_CALLBACK (serv, "/ardour/routes/gainabs", "if", route_set_gain_abs);
370                 REGISTER_CALLBACK (serv, "/ardour/routes/gaindB", "if", route_set_gain_dB);
371                 REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_position", "if", route_set_pan_stereo_position);
372                 REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_width", "if", route_set_pan_stereo_width);
373                 REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter", "iiif", route_plugin_parameter);
374                 REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter/print", "iii", route_plugin_parameter_print);
375                 REGISTER_CALLBACK (serv, "/ardour/routes/send/gainabs", "iif", route_set_send_gain_abs);
376                 REGISTER_CALLBACK (serv, "/ardour/routes/send/gaindB", "iif", route_set_send_gain_dB);
377
378                 /* still not-really-standardized query interface */
379                 //REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
380                 //REGISTER_CALLBACK (serv, "/ardour/set", "", set);
381
382                 // un/register_update args= s:ctrl s:returl s:retpath
383                 //lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
384                 //lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
385                 //lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
386                 //lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
387
388         }
389 }
390
391 bool
392 OSC::osc_input_handler (IOCondition ioc, lo_server srv)
393 {
394         if (ioc & ~IO_IN) {
395                 return false;
396         }
397
398         if (ioc & IO_IN) {
399                 lo_server_recv (srv);
400         }
401
402         return true;
403 }
404
405 std::string
406 OSC::get_server_url()
407 {
408         string url;
409         char * urlstr;
410
411         if (_osc_server) {
412                 urlstr = lo_server_get_url (_osc_server);
413                 url = urlstr;
414                 free (urlstr);
415         }
416
417         return url;
418 }
419
420 std::string
421 OSC::get_unix_server_url()
422 {
423         string url;
424         char * urlstr;
425
426         if (_osc_unix_server) {
427                 urlstr = lo_server_get_url (_osc_unix_server);
428                 url = urlstr;
429                 free (urlstr);
430         }
431
432         return url;
433 }
434
435 void
436 OSC::listen_to_route (boost::shared_ptr<Route> route, lo_address addr)
437 {
438         /* avoid duplicate listens */
439
440         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); ++x) {
441
442                 OSCRouteObserver* ro;
443
444                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
445
446                         int res = strcmp(lo_address_get_hostname(ro->address()), lo_address_get_hostname(addr));
447
448                         if (ro->route() == route && res == 0) {
449                                 return;
450                         }
451                 }
452         }
453
454         OSCRouteObserver* o = new OSCRouteObserver (route, addr);
455         route_observers.push_back (o);
456
457         route->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::drop_route, this, boost::weak_ptr<Route> (route)), this);
458 }
459
460 void
461 OSC::drop_route (boost::weak_ptr<Route> wr)
462 {
463         boost::shared_ptr<Route> r = wr.lock ();
464
465         if (!r) {
466                 return;
467         }
468
469         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
470
471                 OSCRouteObserver* rc;
472
473                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
474
475                         if (rc->route() == r) {
476                                 delete *x;
477                                 x = route_observers.erase (x);
478                         } else {
479                                 ++x;
480                         }
481                 } else {
482                         ++x;
483                 }
484         }
485 }
486
487 void
488 OSC::end_listen (boost::shared_ptr<Route> r, lo_address addr)
489 {
490         RouteObservers::iterator x;
491
492         // Remove the route observers
493         for (x = route_observers.begin(); x != route_observers.end();) {
494
495                 OSCRouteObserver* ro;
496
497                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
498
499                         int res = strcmp(lo_address_get_hostname(ro->address()), lo_address_get_hostname(addr));
500
501                         if (ro->route() == r && res == 0) {
502                                 delete *x;
503                                 x = route_observers.erase (x);
504                         }
505                         else {
506                                 ++x;
507                         }
508                 }
509                 else {
510                         ++x;
511                 }
512         }
513 }
514
515 void
516 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
517 {
518         char* subpath;
519
520         subpath = (char*) malloc (len-15+1);
521         memcpy (subpath, path, len-15);
522         subpath[len-15] = '\0';
523
524         send_current_value (subpath, argv, argc, msg);
525
526         free (subpath);
527 }
528
529 void
530 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
531 {
532         if (!session) {
533                 return;
534         }
535
536         lo_message reply = lo_message_new ();
537         boost::shared_ptr<Route> r;
538         int id;
539
540         lo_message_add_string (reply, path);
541
542         if (argc == 0) {
543                 lo_message_add_string (reply, "bad syntax");
544         } else {
545                 id = argv[0]->i;
546                 r = session->route_by_remote_id (id);
547
548                 if (!r) {
549                         lo_message_add_string (reply, "not found");
550                 } else {
551
552                         if (strcmp (path, "/routes/state") == 0) {
553
554                                 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
555                                         lo_message_add_string (reply, "AT");
556                                 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
557                                         lo_message_add_string (reply, "MT");
558                                 } else {
559                                         lo_message_add_string (reply, "B");
560                                 }
561
562                                 lo_message_add_string (reply, r->name().c_str());
563                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
564                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
565                                 lo_message_add_int32 (reply, r->muted());
566                                 lo_message_add_int32 (reply, r->soloed());
567
568                         } else if (strcmp (path, "/routes/mute") == 0) {
569
570                                 lo_message_add_int32 (reply, (float) r->muted());
571
572                         } else if (strcmp (path, "/routes/solo") == 0) {
573
574                                 lo_message_add_int32 (reply, r->soloed());
575                         }
576                 }
577         }
578
579         lo_send_message (lo_message_get_source (msg), "#reply", reply);
580         lo_message_free (reply);
581 }
582
583 int
584 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
585 {
586         return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
587 }
588
589 int
590 OSC::catchall (const char *path, const char* /*types*/, lo_arg **argv, int argc, lo_message msg)
591 {
592         size_t len;
593         int ret = 1; /* unhandled */
594
595         //cerr << "Received a message, path = " << path << " types = \""
596         //     << (types ? types : "NULL") << '"' << endl;
597
598         /* 15 for /#current_value plus 2 for /<path> */
599
600         len = strlen (path);
601
602         if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
603                 current_value_query (path, len, argv, argc, msg);
604                 ret = 0;
605
606         } else if (strcmp (path, "/routes/listen") == 0) {
607
608                 cerr << "set up listener\n";
609
610                 lo_message reply = lo_message_new ();
611
612                 if (argc <= 0) {
613                         lo_message_add_string (reply, "syntax error");
614                 } else {
615                         for (int n = 0; n < argc; ++n) {
616
617                                 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
618
619                                 if (!r) {
620                                         lo_message_add_string (reply, "not found");
621                                         cerr << "no such route\n";
622                                         break;
623                                 } else {
624                                         cerr << "add listener\n";
625                                         listen_to_route (r, lo_message_get_source (msg));
626                                         lo_message_add_int32 (reply, argv[n]->i);
627                                 }
628                         }
629                 }
630
631                 lo_send_message (lo_message_get_source (msg), "#reply", reply);
632                 lo_message_free (reply);
633
634                 ret = 0;
635
636         } else if (strcmp (path, "/routes/ignore") == 0) {
637
638                 for (int n = 0; n < argc; ++n) {
639
640                         boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
641
642                         if (r) {
643                                 end_listen (r, lo_message_get_source (msg));
644                         }
645                 }
646
647                 ret = 0;
648         }
649
650         return ret;
651 }
652
653 void
654 OSC::update_clock ()
655 {
656
657 }
658
659 // "Application Hook" Handlers //
660 void
661 OSC::session_loaded (Session& s)
662 {
663         lo_address listener = lo_address_new (NULL, "7770");
664         lo_send (listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str());
665 }
666
667 void
668 OSC::session_exported (std::string path, std::string name)
669 {
670         lo_address listener = lo_address_new (NULL, "7770");
671         lo_send (listener, "/session/exported", "ss", path.c_str(), name.c_str());
672 }
673
674 // end "Application Hook" Handlers //
675
676 /* path callbacks */
677
678 int
679 OSC::current_value (const char */*path*/, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, void */*data*/, void* /*user_data*/)
680 {
681 #if 0
682         const char* returl;
683
684         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
685                 return 1;
686         }
687
688         const char *returl = argv[1]->s;
689         lo_address addr = find_or_cache_addr (returl);
690
691         const char *retpath = argv[2]->s;
692
693
694         if (strcmp (argv[0]->s, "transport_frame") == 0) {
695
696                 if (session) {
697                         lo_send (addr, retpath, "i", session->transport_frame());
698                 }
699
700         } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
701
702                 if (session) {
703                         lo_send (addr, retpath, "i", session->transport_frame());
704                 }
705
706         } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
707
708                 if (session) {
709                         lo_send (addr, retpath, "i", session->transport_frame());
710                 }
711
712         } else if (strcmp (argv[0]->s, "punch_in") == 0) {
713
714                 if (session) {
715                         lo_send (addr, retpath, "i", session->transport_frame());
716                 }
717
718         } else if (strcmp (argv[0]->s, "punch_out") == 0) {
719
720                 if (session) {
721                         lo_send (addr, retpath, "i", session->transport_frame());
722                 }
723
724         } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
725
726                 if (session) {
727                         lo_send (addr, retpath, "i", session->transport_frame());
728                 }
729
730         } else {
731
732                 /* error */
733         }
734 #endif
735         return 0;
736 }
737
738 void
739 OSC::routes_list (lo_message msg)
740 {
741         for (int n = 0; n < (int) session->nroutes(); ++n) {
742
743                 boost::shared_ptr<Route> r = session->route_by_remote_id (n);
744
745                 if (r) {
746
747                         lo_message reply = lo_message_new ();
748
749                         if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
750                                 lo_message_add_string (reply, "AT");
751                         } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
752                                 lo_message_add_string (reply, "MT");
753                         } else {
754                                 lo_message_add_string (reply, "B");
755                         }
756
757                         lo_message_add_string (reply, r->name().c_str());
758                         lo_message_add_int32 (reply, r->n_inputs().n_audio());
759                         lo_message_add_int32 (reply, r->n_outputs().n_audio());
760                         lo_message_add_int32 (reply, r->muted());
761                         lo_message_add_int32 (reply, r->soloed());
762                         lo_message_add_int32 (reply, r->remote_control_id());
763
764                         if (boost::dynamic_pointer_cast<AudioTrack>(r)
765                                         || boost::dynamic_pointer_cast<MidiTrack>(r)) {
766
767                                 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(r);
768                                 lo_message_add_int32 (reply, t->record_enabled());
769                         }
770
771                         //Automatically listen to routes listed
772                         listen_to_route(r, lo_message_get_source (msg));
773
774                         lo_send_message (lo_message_get_source (msg), "#reply", reply);
775                         lo_message_free (reply);
776                 }
777         }
778
779         // Send end of listing message
780         lo_message reply = lo_message_new ();
781
782         lo_message_add_string (reply, "end_route_list");
783         lo_message_add_int64 (reply, session->frame_rate());
784         lo_message_add_int64 (reply, session->current_end_frame());
785
786         lo_send_message (lo_message_get_source (msg), "#reply", reply);
787
788         lo_message_free (reply);
789 }
790
791 void
792 OSC::transport_frame (lo_message msg)
793 {
794         framepos_t pos = session->transport_frame ();
795
796         lo_message reply = lo_message_new ();
797         lo_message_add_int64 (reply, pos);
798
799         lo_send_message (lo_message_get_source (msg), "/ardour/transport_frame", reply);
800
801         lo_message_free (reply);
802 }
803
804 int
805 OSC::route_mute (int rid, int yn)
806 {
807         if (!session) return -1;
808
809         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
810
811         if (r) {
812                 r->set_mute (yn, this);
813         }
814
815         return 0;
816 }
817
818 int
819 OSC::route_solo (int rid, int yn)
820 {
821         if (!session) return -1;
822
823         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
824
825         if (r) {
826                 r->set_solo (yn, this);
827         }
828
829         return 0;
830 }
831
832 int
833 OSC::route_recenable (int rid, int yn)
834 {
835         if (!session) return -1;
836
837         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
838
839         if (r) {
840                 r->set_record_enabled (yn, this);
841         }
842
843         return 0;
844 }
845
846 int
847 OSC::route_set_gain_abs (int rid, float level)
848 {
849         if (!session) return -1;
850
851         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
852
853         if (r) {
854                 r->set_gain (level, this);
855         }
856
857         return 0;
858 }
859
860 int
861 OSC::route_set_gain_dB (int rid, float dB)
862 {
863         if (!session) return -1;
864
865         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
866
867         if (r) {
868                 r->set_gain (dB_to_coefficient (dB), this);
869         }
870
871         return 0;
872 }
873
874 int
875 OSC::route_set_pan_stereo_position (int rid, float pos)
876 {
877         if (!session) return -1;
878
879         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
880
881         if (r) {
882                 boost::shared_ptr<Panner> panner = r->panner();
883                 if (panner) {
884                         panner->set_position (pos);
885                 }
886         }
887
888         return 0;
889
890 }
891
892 int
893 OSC::route_set_pan_stereo_width (int rid, float pos)
894 {
895         if (!session) return -1;
896
897         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
898
899         if (r) {
900                 boost::shared_ptr<Panner> panner = r->panner();
901                 if (panner) {
902                         panner->set_width (pos);
903                 }
904         }
905
906         return 0;
907
908 }
909
910 int
911 OSC::route_set_send_gain_abs (int rid, int sid, float val)
912 {
913         if (!session) {
914                 return -1;
915         }
916
917         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
918
919         if (!r) {
920                 return -1;
921         }
922
923         /* revert to zero-based counting */
924
925         if (sid > 0) {
926                 --sid;
927         }
928
929         boost::shared_ptr<Processor> p = r->nth_send (sid);
930
931         if (p) {
932                 boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
933                 boost::shared_ptr<Amp> a = s->amp();
934
935                 if (a) {
936                         a->set_gain (val, this);
937                 }
938         }
939         return 0;
940 }
941
942 int
943 OSC::route_set_send_gain_dB (int rid, int sid, float val)
944 {
945         if (!session) {
946                 return -1;
947         }
948
949         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
950
951         if (!r) {
952                 return -1;
953         }
954
955         /* revert to zero-based counting */
956
957         if (sid > 0) {
958                 --sid;
959         }
960
961         boost::shared_ptr<Processor> p = r->nth_send (sid);
962
963         if (p) {
964                 boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
965                 boost::shared_ptr<Amp> a = s->amp();
966
967                 if (a) {
968                         a->set_gain (dB_to_coefficient (val), this);
969                 }
970         }
971         return 0;
972 }
973
974 int
975 OSC::route_plugin_parameter (int rid, int piid, int par, float val)
976 {
977         if (!session)
978                 return -1;
979
980         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
981
982         if (!r) {
983                 PBD::error << "OSC: Invalid Remote Control ID '" << rid << "'" << endmsg;
984                 return -1;
985         }
986
987         boost::shared_ptr<Processor> redi=r->nth_plugin (piid);
988
989         if (!redi) {
990                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << rid << "'" << endmsg;
991                 return -1;
992         }
993
994         boost::shared_ptr<PluginInsert> pi;
995
996         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
997                 PBD::error << "OSC: given processor # " << piid << " on RID '" << rid << "' is not a Plugin." << endmsg;
998                 return -1;
999         }
1000
1001         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
1002         bool ok=false;
1003
1004         uint32_t controlid = pip->nth_parameter (par,ok);
1005
1006         if (!ok) {
1007                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << rid << "'" << endmsg;
1008                 return -1;
1009         }
1010
1011         if (!pip->parameter_is_input(controlid)) {
1012                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << rid << "' is not a control input" << endmsg;
1013                 return -1;
1014         }
1015
1016         ParameterDescriptor pd;
1017         pi->plugin()->get_parameter_descriptor (controlid,pd);
1018
1019         if (val >= pd.lower && val < pd.upper) {
1020
1021                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
1022                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
1023                 c->set_value (val);
1024         } else {
1025                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << rid << "' is out of range" << endmsg;
1026                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
1027         }
1028
1029         return 0;
1030 }
1031
1032 int
1033 OSC::route_plugin_parameter_print (int rid, int piid, int par)
1034 {
1035         if (!session) {
1036                 return -1;
1037         }
1038
1039         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1040
1041         if (!r) {
1042                 return -1;
1043         }
1044
1045         boost::shared_ptr<Processor> redi=r->nth_processor (piid);
1046
1047         if (!redi) {
1048                 return -1;
1049         }
1050
1051         boost::shared_ptr<PluginInsert> pi;
1052
1053         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
1054                 return -1;
1055         }
1056
1057         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
1058         bool ok=false;
1059
1060         uint32_t controlid = pip->nth_parameter (par,ok);
1061
1062         if (!ok) {
1063                 return -1;
1064         }
1065
1066         ParameterDescriptor pd;
1067
1068         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
1069                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
1070
1071                 cerr << "parameter:     " << redi->describe_parameter(controlid)  << "\n";
1072                 cerr << "current value: " << c->get_value ();
1073                 cerr << "lower value:   " << pd.lower << "\n";
1074                 cerr << "upper value:   " << pd.upper << "\n";
1075         }
1076
1077         return 0;
1078 }
1079
1080 XMLNode&
1081 OSC::get_state ()
1082 {
1083         XMLNode& node (ControlProtocol::get_state());
1084
1085         node.add_property (X_("feedback"), _send_route_changes ? "1" : "0");
1086         return node;
1087 }
1088
1089 int
1090 OSC::set_state (const XMLNode& node, int /*version*/)
1091 {
1092         const XMLProperty* prop = node.property (X_("feedback"));
1093
1094         if (prop) {
1095                 if (PBD::string_is_affirmative (prop->value())) {
1096                         _send_route_changes = true;
1097                 } else {
1098                         _send_route_changes = false;
1099                 }
1100         } else {
1101                 /* leave it alone */
1102         }
1103
1104         return 0;
1105 }