d8ff311486e8fbda02e653efae5defebcdfeaa60
[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 <sys/poll.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30
31 #include <glibmm/miscutils.h>
32
33 #include <pbd/pthread_utils.h>
34 #include <pbd/file_utils.h>
35 #include <pbd/filesystem.h>
36 #include <pbd/failed_constructor.h>
37
38 #include <ardour/session.h>
39 #include <ardour/route.h>
40 #include <ardour/audio_track.h>
41 #include <ardour/midi_track.h>
42 #include <ardour/dB.h>
43 #include <ardour/filesystem_paths.h>
44
45 #include "osc.h"
46 #include "osc_controllable.h"
47 #include "i18n.h"
48
49 using namespace ARDOUR;
50 using namespace sigc;
51 using namespace std;
52
53
54 static void error_callback(int num, const char *m, const char *path)
55 {
56 #ifdef DEBUG
57         fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
58 #endif
59 }
60
61 OSC::OSC (Session& s, uint32_t port)
62         : ControlProtocol (s, "OSC")
63         , _port(port)
64 {
65         _shutdown = false;
66         _osc_server = 0;
67         _osc_unix_server = 0;
68         _osc_thread = 0;
69         _namespace_root = "/ardour";
70         _send_route_changes = true;
71
72         // "Application Hooks"
73         session_loaded (s);
74         session->Exported.connect( mem_fun( *this, &OSC::session_exported ) );
75
76         /* catch up with existing routes */
77
78         boost::shared_ptr<RouteList> rl = session->get_routes ();
79         route_added (*(rl.get()));
80
81         // session->RouteAdded.connect (mem_fun (*this, &OSC::route_added));
82 }
83
84 OSC::~OSC()
85 {
86         stop ();
87 }
88
89 int
90 OSC::set_active (bool yn)
91 {
92         if (yn) {
93                 return start ();
94         } else {
95                 return stop ();
96         }
97 }
98
99 bool
100 OSC::get_active () const
101 {
102         return _osc_server != 0;
103 }
104
105 int 
106 OSC::set_feedback (bool yn)
107 {
108         _send_route_changes = yn;
109         return 0;
110 }
111
112 bool
113 OSC::get_feedback () const
114 {
115         return _send_route_changes;
116 }
117
118 int
119 OSC::start ()
120 {
121         char tmpstr[255];
122
123         if (_osc_server) {
124                 /* already started */
125                 return 0;
126         }
127         
128         for (int j=0; j < 20; ++j) {
129                 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
130                 
131                 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
132                         break;
133                 }
134 #ifdef DEBUG            
135                 cerr << "can't get osc at port: " << _port << endl;
136 #endif
137                 _port++;
138                 continue;
139         }
140         
141 #ifdef ARDOUR_OSC_UNIX_SERVER
142         
143         // APPEARS sluggish for now
144         
145         // attempt to create unix socket server too
146         
147         snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
148         int fd = mkstemp(tmpstr);
149         
150         if (fd >= 0 ) {
151                 unlink (tmpstr);
152                 close (fd);
153                 
154                 _osc_unix_server = lo_server_new (tmpstr, error_callback);
155                 
156                 if (_osc_unix_server) {
157                         _osc_unix_socket_path = tmpstr;
158                 }
159         }
160 #endif
161         
162         cerr << "OSC @ " << get_server_url () << endl;
163
164         PBD::sys::path url_file;
165
166         if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
167                                       "osc_url", url_file)) {
168                 _osc_url_file = url_file.to_string();
169                 ofstream urlfile;
170                 urlfile.open(_osc_url_file.c_str(), ios::trunc);
171                 if ( urlfile )
172                 {
173                         urlfile << get_server_url () << endl;
174                         urlfile.close();
175                 }
176                 else
177                 {  
178                         cerr << "Couldn't write '" <<  _osc_url_file << "'" <<endl;
179                 }
180         }
181         
182         register_callbacks();
183         
184         // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
185                 
186         if (!init_osc_thread()) {
187                 return -1;
188         }
189         return 0;
190 }
191
192 int
193 OSC::stop ()
194 {       
195         if (_osc_server == 0) {
196                 /* already stopped */
197                 return 0;
198         }
199
200         // stop server thread
201         terminate_osc_thread();
202
203         lo_server_free (_osc_server);
204         _osc_server = 0;
205         
206         if (!_osc_unix_socket_path.empty()) {
207                 // unlink it
208                 unlink(_osc_unix_socket_path.c_str());
209         }
210         
211         if (!  _osc_url_file.empty() ) {
212                 unlink(_osc_url_file.c_str() );
213         }
214         return 0;
215 }
216
217 void
218 OSC::register_callbacks()
219 {
220         lo_server srvs[2];
221         lo_server serv;
222
223         srvs[0] = _osc_server;
224         srvs[1] = _osc_unix_server;
225         
226         for (size_t i = 0; i < 2; ++i) {
227
228                 if (!srvs[i]) {
229                         continue;
230                 }
231
232                 serv = srvs[i];
233                 
234                 /* this is a special catchall handler */
235                 
236                 lo_server_add_method (serv, 0, 0, _catchall, this);
237
238 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
239                 
240                 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
241                 REGISTER_CALLBACK (serv, "/ardour/access_action", "s", access_action);
242                 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
243                 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
244                 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
245                 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
246                 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
247                 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
248                 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
249                 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
250                 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
251                 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
252                 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
253                 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
254                 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
255                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
256                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
257                 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
258                 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
259
260                 REGISTER_CALLBACK (serv, "/ardour/routes/mute", "ii", route_mute);
261                 REGISTER_CALLBACK (serv, "/ardour/routes/solo", "ii", route_solo);
262                 REGISTER_CALLBACK (serv, "/ardour/routes/recenable", "ii", route_recenable);
263                 REGISTER_CALLBACK (serv, "/ardour/routes/gainabs", "if", route_set_gain_abs);
264                 REGISTER_CALLBACK (serv, "/ardour/routes/gaindB", "if", route_set_gain_dB);
265
266 #if 0
267                 REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
268                 REGISTER_CALLBACK (serv, "/ardour/set", "", set);
269 #endif
270
271 #if 0
272                 // un/register_update args= s:ctrl s:returl s:retpath
273                 lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
274                 lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
275                 lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
276                 lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
277 #endif
278         }
279 }
280
281 bool
282 OSC::init_osc_thread ()
283 {
284         // create new thread to run server
285         if (pipe (_request_pipe)) {
286                 cerr << "Cannot create osc request signal pipe" <<  strerror (errno) << endl;
287                 return false;
288         }
289
290         if (fcntl (_request_pipe[0], F_SETFL, O_NONBLOCK)) {
291                 cerr << "osc: cannot set O_NONBLOCK on signal read pipe " << strerror (errno) << endl;
292                 return false;
293         }
294
295         if (fcntl (_request_pipe[1], F_SETFL, O_NONBLOCK)) {
296                 cerr << "osc: cannot set O_NONBLOCK on signal write pipe " << strerror (errno) << endl;
297                 return false;
298         }
299         
300         pthread_attr_t attr;
301         pthread_attr_init(&attr);
302         pthread_attr_setstacksize(&attr, 500000);
303
304         pthread_create (&_osc_thread, &attr, &OSC::_osc_receiver, this);
305         if (!_osc_thread) {
306                 return false;
307         }
308         pthread_attr_destroy(&attr);
309
310         //pthread_detach (_osc_thread);
311         return true;
312 }
313
314 void
315 OSC::terminate_osc_thread ()
316 {
317         void* status;
318
319         _shutdown = true;
320         
321         poke_osc_thread ();
322
323         pthread_join (_osc_thread, &status);
324 }
325
326 void
327 OSC::poke_osc_thread ()
328 {
329         char c;
330
331         if (write (_request_pipe[1], &c, 1) != 1) {
332                 cerr << "cannot send signal to osc thread! " <<  strerror (errno) << endl;
333         }
334 }
335
336 std::string
337 OSC::get_server_url()
338 {
339         string url;
340         char * urlstr;
341
342         if (_osc_server) {
343                 urlstr = lo_server_get_url (_osc_server);
344                 url = urlstr;
345                 free (urlstr);
346         }
347         
348         return url;
349 }
350
351 std::string
352 OSC::get_unix_server_url()
353 {
354         string url;
355         char * urlstr;
356
357         if (_osc_unix_server) {
358                 urlstr = lo_server_get_url (_osc_unix_server);
359                 url = urlstr;
360                 free (urlstr);
361         }
362         
363         return url;
364 }
365
366
367 /* server thread */
368
369 void *
370 OSC::_osc_receiver(void * arg)
371 {
372         PBD::notify_gui_about_thread_creation (pthread_self(), X_("OSC"));
373
374         static_cast<OSC*> (arg)->osc_receiver();
375         return 0;
376 }
377
378 void
379 OSC::osc_receiver()
380 {
381         struct pollfd pfd[3];
382         int fds[3];
383         lo_server srvs[3];
384         int nfds = 0;
385         int timeout = -1;
386         int ret;
387         
388         fds[0] = _request_pipe[0];
389         nfds++;
390         
391         if (_osc_server && lo_server_get_socket_fd(_osc_server) >= 0) {
392                 fds[nfds] = lo_server_get_socket_fd(_osc_server);
393                 srvs[nfds] = _osc_server;
394                 nfds++;
395         }
396
397         if (_osc_unix_server && lo_server_get_socket_fd(_osc_unix_server) >= 0) {
398                 fds[nfds] = lo_server_get_socket_fd(_osc_unix_server);
399                 srvs[nfds] = _osc_unix_server;
400                 nfds++;
401         }
402         
403         
404         while (!_shutdown) {
405
406                 for (int i=0; i < nfds; ++i) {
407                         pfd[i].fd = fds[i];
408                         pfd[i].events = POLLIN|POLLPRI|POLLHUP|POLLERR;
409                         pfd[i].revents = 0;
410                 }
411                 
412         again:
413                 //cerr << "poll on " << nfds << " for " << timeout << endl;
414                 if ((ret = poll (pfd, nfds, timeout)) < 0) {
415                         if (errno == EINTR) {
416                                 /* gdb at work, perhaps */
417                                 goto again;
418                         }
419                         
420                         cerr << "OSC thread poll failed: " <<  strerror (errno) << endl;
421                         
422                         break;
423                 }
424
425                 //cerr << "poll returned " << ret << "  pfd[0].revents = " << pfd[0].revents << "  pfd[1].revents = " << pfd[1].revents << endl;
426                 
427                 if (_shutdown) {
428                         break;
429                 }
430                 
431                 if ((pfd[0].revents & ~POLLIN)) {
432                         cerr << "OSC: error polling extra port" << endl;
433                         break;
434                 }
435                 
436                 for (int i=1; i < nfds; ++i) {
437                         if (pfd[i].revents & POLLIN)
438                         {
439                                 // this invokes callbacks
440                                 // cerr << "invoking recv on " << pfd[i].fd << endl;
441                                 lo_server_recv(srvs[i]);
442                         }
443                 }
444
445         }
446
447         //cerr << "SL engine shutdown" << endl;
448         
449         if (_osc_server) {
450                 int fd = lo_server_get_socket_fd(_osc_server);
451                 if (fd >=0) {
452                         // hack around
453                         close(fd);
454                 }
455                 lo_server_free (_osc_server);
456                 _osc_server = 0;
457         }
458
459         if (_osc_unix_server) {
460                 cerr << "freeing unix server" << endl;
461                 lo_server_free (_osc_unix_server);
462                 _osc_unix_server = 0;
463         }
464         
465         close(_request_pipe[0]);
466         close(_request_pipe[1]);
467 }
468
469 void
470 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
471 {
472         char* subpath;
473         
474         subpath = (char*) malloc (len-15+1);
475         memcpy (subpath, path, len-15);
476         subpath[len-15] = '\0';
477         
478         send_current_value (subpath, argv, argc, msg);
479         
480         free (subpath);
481 }
482
483 void
484 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
485 {
486         if (!session) {
487                 return;
488         }
489
490         lo_message reply = lo_message_new ();
491         boost::shared_ptr<Route> r;
492         int id;
493
494         lo_message_add_string (reply, path);
495         
496         if (argc == 0) {
497                 lo_message_add_string (reply, "bad syntax");
498         } else {
499                 id = argv[0]->i;
500                 r = session->route_by_remote_id (id);
501
502                 if (!r) {
503                         lo_message_add_string (reply, "not found");
504                 } else {
505
506                         if (strcmp (path, "/routes/state") == 0) {
507                                 
508                                 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
509                                         lo_message_add_string (reply, "AT");
510                                 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
511                                         lo_message_add_string (reply, "MT");
512                                 } else {
513                                         lo_message_add_string (reply, "B");
514                                 }
515                                 
516                                 lo_message_add_string (reply, r->name().c_str());
517                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
518                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
519                                 lo_message_add_int32 (reply, r->muted());
520                                 lo_message_add_int32 (reply, r->soloed());
521                                 
522                         } else if (strcmp (path, "/routes/mute") == 0) {
523                                 
524                                 lo_message_add_int32 (reply, (float) r->muted());
525                                 
526                         } else if (strcmp (path, "/routes/solo") == 0) {
527                                 
528                                 lo_message_add_int32 (reply, r->soloed());
529                         }
530                 }
531         }
532
533         lo_send_message (lo_message_get_source (msg), "#reply", reply);
534         lo_message_free (reply);
535 }
536         
537 int
538 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) 
539 {
540         return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
541 }
542
543 int
544 OSC::catchall (const char *path, const char *types, lo_arg **argv, int argc, lo_message msg) 
545 {
546         size_t len;
547         int ret = 1; /* unhandled */
548
549         cerr << "Received a message, path = " << path << " types = \"" 
550              << (types ? types : "NULL") << '"' << endl;
551
552         /* 15 for /#current_value plus 2 for /<path> */
553
554         len = strlen (path);
555
556         if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
557                 current_value_query (path, len, argv, argc, msg);
558                 ret = 0;
559
560         } else if (strcmp (path, "/routes/listen") == 0) {
561                 
562                 cerr << "set up listener\n";
563
564                 lo_message reply = lo_message_new ();
565
566                 if (argc <= 0) {
567                         lo_message_add_string (reply, "syntax error");
568                 } else {
569                         for (int n = 0; n < argc; ++n) {
570
571                                 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
572                                 
573                                 if (!r) {
574                                         lo_message_add_string (reply, "not found");
575                                         cerr << "no such route\n";
576                                         break;
577                                 } else {                        
578                                         cerr << "add listener\n";
579                                         listen_to_route (r, lo_message_get_source (msg));
580                                         lo_message_add_int32 (reply, argv[n]->i);
581                                 }
582                         }
583                 }
584
585                 lo_send_message (lo_message_get_source (msg), "#reply", reply);
586                 lo_message_free (reply);
587
588         } else if (strcmp (path, "/routes/ignore") == 0) {
589
590                 for (int n = 0; n < argc; ++n) {
591
592                         boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
593                         
594                         if (r) {
595                                 end_listen (r, lo_message_get_source (msg));
596                         }
597                 }
598         }
599
600         return ret;
601 }
602
603 void
604 OSC::route_added (RouteList& rl)
605 {
606 }
607
608 void
609 OSC::listen_to_route (boost::shared_ptr<Route> route, lo_address addr)
610 {
611         Controllables::iterator x;
612         bool route_exists = false;
613
614         cerr << "listen to route\n";
615
616         /* avoid duplicate listens */
617         
618         for (x = controllables.begin(); x != controllables.end(); ++x) {
619                 
620                 OSCRouteControllable* rc;
621
622                 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
623                         
624                         if (rc->route() == route) {
625                                 route_exists = true;
626                                 
627                                 /* XXX NEED lo_address_equal() */
628                                 
629                                 if (rc->address() == addr) {
630                                         return;
631                                 }
632                         }
633                 }
634         }
635
636         cerr << "listener binding to signals\n";
637
638         OSCControllable* c;
639         string path;
640
641         path = X_("/route/solo");
642         c = new OSCRouteControllable (addr, path, route->solo_control(), route);
643         controllables.push_back (c);
644
645         path = X_("/route/mute");
646         c = new OSCRouteControllable (addr, path, route->mute_control(), route);
647         controllables.push_back (c);
648
649         path = X_("/route/gain");
650         c = new OSCRouteControllable (addr, path, route->gain_control(), route);
651         controllables.push_back (c);
652
653         cerr << "Now have " << controllables.size() << " controllables\n";
654
655         /* if there is no existing controllable related to this route, make sure we clean up
656            if it is ever deleted.
657         */
658         
659         if (!route_exists) {
660                 route->GoingAway.connect (bind (mem_fun (*this, &OSC::drop_route), boost::weak_ptr<Route> (route)));
661         }
662 }
663
664 void
665 OSC::drop_route (boost::weak_ptr<Route> wr)
666 {
667         boost::shared_ptr<Route> r = wr.lock ();
668
669         if (!r) {
670                 return;
671         }
672
673         for (Controllables::iterator x = controllables.begin(); x != controllables.end();) {
674
675                 OSCRouteControllable* rc;
676                 
677                 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
678                         if (rc->route() == r) {
679                                 delete *x;
680                                 x = controllables.erase (x);
681                         } else {
682                                 ++x;
683                         }
684                 } else {
685                         ++x;
686                 }
687         }
688 }
689
690 void
691 OSC::end_listen (boost::shared_ptr<Route> r, lo_address addr)
692 {
693         Controllables::iterator x;
694
695         for (x = controllables.begin(); x != controllables.end(); ++x) {
696
697                 OSCRouteControllable* rc;
698                 
699                 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
700
701                         /* XXX NEED lo_address_equal () */
702
703                         if (rc->route() == r && rc->address() == addr) {
704                                 controllables.erase (x);
705                                 return;
706                         }
707                 }
708         }
709 }
710
711 // "Application Hook" Handlers //
712 void
713 OSC::session_loaded( Session& s ) {
714         lo_address listener = lo_address_new( NULL, "7770" );
715         lo_send( listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str() );
716 }
717
718 void
719 OSC::session_exported( std::string path, std::string name ) {
720         lo_address listener = lo_address_new( NULL, "7770" );
721         lo_send( listener, "/session/exported", "ss", path.c_str(), name.c_str() );
722 }
723
724 // end "Application Hook" Handlers //
725
726 /* path callbacks */
727
728 int 
729 OSC::current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void* user_data) 
730
731 #if 0
732         const char* returl;
733
734         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
735                 return 1;
736         }
737
738         const char *returl = argv[1]->s;
739         lo_address addr = find_or_cache_addr (returl);
740
741         const char *retpath = argv[2]->s;
742
743         
744         if (strcmp (argv[0]->s, "transport_frame") == 0) {
745
746                 if (session) {
747                         lo_send (addr, retpath, "i", session->transport_frame());
748                 }
749
750         } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
751                 
752                 if (session) {
753                         lo_send (addr, retpath, "i", session->transport_frame());
754                 }
755                 
756         } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
757                 
758                 if (session) {
759                         lo_send (addr, retpath, "i", session->transport_frame());
760                 }
761                 
762         } else if (strcmp (argv[0]->s, "punch_in") == 0) {
763                 
764                 if (session) {
765                         lo_send (addr, retpath, "i", session->transport_frame());
766                 }
767                 
768         } else if (strcmp (argv[0]->s, "punch_out") == 0) {
769
770                 if (session) {
771                         lo_send (addr, retpath, "i", session->transport_frame());
772                 }
773                 
774         } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
775                         
776                 if (session) {
777                         lo_send (addr, retpath, "i", session->transport_frame());
778                 }
779
780         } else {
781
782                 /* error */
783         }
784 #endif
785         return 0;
786 }
787
788 int
789 OSC::route_mute (int rid, int yn)
790 {
791         if (!session) return -1;
792
793         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
794
795         if (r) {
796                 r->set_mute (yn, this);
797         }
798         return 0;
799 }
800
801 int
802 OSC::route_solo (int rid, int yn)
803 {
804         if (!session) return -1;
805
806         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
807
808         if (r) {
809                 r->set_solo (yn, this);
810         }
811         return 0;
812 }
813
814 int
815 OSC::route_recenable (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_record_enable (yn, this);
823         }
824         return 0;
825 }
826
827 int
828 OSC::route_set_gain_abs (int rid, float level)
829 {
830         if (!session) return -1;
831
832         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
833
834         if (r) {
835                 r->set_gain (level, this);
836         }
837
838         return 0;
839 }
840
841 int
842 OSC::route_set_gain_dB (int rid, float dB)
843 {
844         if (!session) return -1;
845
846         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
847
848         if (r) {
849                 r->set_gain (dB_to_coefficient (dB), this);
850         }
851         
852         return 0;
853 }
854
855 XMLNode& 
856 OSC::get_state () 
857 {
858         return *(new XMLNode ("OSC"));
859 }
860                 
861 int 
862 OSC::set_state (const XMLNode&)
863 {
864         return 0;
865 }