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