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