2 * Copyright (C) 2006 Paul Davis
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.
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.
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.
28 #include "pbd/gstdio_compat.h"
29 #include <glibmm/miscutils.h>
31 #include <pbd/convert.h>
32 #include <pbd/pthread_utils.h>
33 #include <pbd/file_utils.h>
34 #include <pbd/failed_constructor.h>
36 #include "ardour/amp.h"
37 #include "ardour/session.h"
38 #include "ardour/route.h"
39 #include "ardour/audio_track.h"
40 #include "ardour/midi_track.h"
41 #include "ardour/dB.h"
42 #include "ardour/filesystem_paths.h"
43 #include "ardour/panner.h"
44 #include "ardour/plugin.h"
45 #include "ardour/plugin_insert.h"
46 #include "ardour/send.h"
49 #include "osc_controllable.h"
50 #include "osc_route_observer.h"
53 using namespace ARDOUR;
56 using namespace ArdourSurface;
58 #include "pbd/abstract_ui.cc" // instantiate template
60 OSC* OSC::_instance = 0;
63 static void error_callback(int num, const char *m, const char *path)
65 fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
68 static void error_callback(int, const char *, const char *)
74 OSC::OSC (Session& s, uint32_t port)
75 : ControlProtocol (s, X_("Open Sound Control (OSC)"))
76 , AbstractUI<OSCUIRequest> (name())
83 , _osc_unix_server (0)
84 , _namespace_root ("/ardour")
85 , _send_route_changes (true)
92 session->Exported.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::session_exported, this, _1, _2), this);
102 OSC::request_factory (uint32_t num_requests)
104 /* AbstractUI<T>::request_buffer_factory() is a template method only
105 instantiated in this source module. To provide something visible for
106 use in the interface/descriptor, we have this static method that is
109 return request_buffer_factory (num_requests);
113 OSC::do_request (OSCUIRequest* req)
115 if (req->type == CallSlot) {
117 call_slot (MISSING_INVALIDATOR, req->the_slot);
119 } else if (req->type == Quit) {
126 OSC::set_active (bool yn)
128 if (yn != active()) {
142 return ControlProtocol::set_active (yn);
146 OSC::get_active () const
148 return _osc_server != 0;
152 OSC::set_feedback (bool yn)
154 _send_route_changes = yn;
159 OSC::get_feedback () const
161 return _send_route_changes;
170 /* already started */
174 for (int j=0; j < 20; ++j) {
175 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
177 //if ((_osc_server = lo_server_new_with_proto (tmpstr, LO_TCP, error_callback))) {
181 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
186 cerr << "can't get osc at port: " << _port << endl;
196 #ifdef ARDOUR_OSC_UNIX_SERVER
198 // APPEARS sluggish for now
200 // attempt to create unix socket server too
202 snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
203 int fd = mkstemp(tmpstr);
209 _osc_unix_server = lo_server_new (tmpstr, error_callback);
211 if (_osc_unix_server) {
212 _osc_unix_socket_path = tmpstr;
217 PBD::info << "OSC @ " << get_server_url () << endmsg;
219 std::string url_file;
221 if (find_file (ardour_config_search_path(), "osc_url", url_file)) {
222 _osc_url_file = url_file;
223 if (g_file_set_contents (_osc_url_file.c_str(), get_server_url().c_str(), -1, NULL)) {
224 cerr << "Couldn't write '" << _osc_url_file << "'" <<endl;
228 register_callbacks();
230 // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
232 /* startup the event loop thread */
242 pthread_set_name (event_loop_name().c_str());
244 if (_osc_unix_server) {
245 Glib::RefPtr<IOSource> src = IOSource::create (lo_server_get_socket_fd (_osc_unix_server), IO_IN|IO_HUP|IO_ERR);
246 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_unix_server));
247 src->attach (_main_loop->get_context());
248 local_server = src->gobj();
249 g_source_ref (local_server);
253 Glib::RefPtr<IOSource> src = IOSource::create (lo_server_get_socket_fd (_osc_server), IO_IN|IO_HUP|IO_ERR);
254 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_server));
255 src->attach (_main_loop->get_context());
256 remote_server = src->gobj();
257 g_source_ref (remote_server);
260 PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
261 SessionEvent::create_per_thread_pool (event_loop_name(), 128);
270 g_source_destroy (local_server);
271 g_source_unref (local_server);
276 g_source_destroy (remote_server);
277 g_source_unref (remote_server);
284 lo_server_free (_osc_server);
288 if (_osc_unix_server) {
289 lo_server_free (_osc_unix_server);
290 _osc_unix_server = 0;
293 if (!_osc_unix_socket_path.empty()) {
294 ::g_unlink (_osc_unix_socket_path.c_str());
297 if (!_osc_url_file.empty() ) {
298 ::g_unlink (_osc_url_file.c_str() );
301 // Delete any active route observers
302 for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
304 OSCRouteObserver* rc;
306 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
308 x = route_observers.erase (x);
318 OSC::register_callbacks()
323 srvs[0] = _osc_server;
324 srvs[1] = _osc_unix_server;
326 for (size_t i = 0; i < 2; ++i) {
335 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
337 REGISTER_CALLBACK (serv, "/routes/list", "", routes_list);
338 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
339 REGISTER_CALLBACK (serv, "/ardour/access_action", "s", access_action);
340 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
341 REGISTER_CALLBACK (serv, "/ardour/loop_location", "ii", loop_location);
342 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
343 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
344 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
345 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
346 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
347 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
348 REGISTER_CALLBACK (serv, "/ardour/transport_frame", "", transport_frame);
349 REGISTER_CALLBACK (serv, "/ardour/transport_speed", "", transport_speed);
350 REGISTER_CALLBACK (serv, "/ardour/record_enabled", "", record_enabled);
351 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
352 REGISTER_CALLBACK (serv, "/ardour/locate", "ii", locate);
353 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
354 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
355 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
356 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
357 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
358 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
359 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
360 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
361 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
362 REGISTER_CALLBACK (serv, "/ardour/remove_marker", "", remove_marker_at_playhead);
363 REGISTER_CALLBACK (serv, "/ardour/jump_bars", "f", jump_by_bars);
364 REGISTER_CALLBACK (serv, "/ardour/jump_seconds", "f", jump_by_seconds);
365 REGISTER_CALLBACK (serv, "/ardour/mark_in", "", mark_in);
366 REGISTER_CALLBACK (serv, "/ardour/mark_out", "", mark_out);
367 REGISTER_CALLBACK (serv, "/ardour/toggle_click", "", toggle_click);
368 REGISTER_CALLBACK (serv, "/ardour/midi_panic", "", midi_panic);
369 REGISTER_CALLBACK (serv, "/ardour/toggle_roll", "", toggle_roll);
370 REGISTER_CALLBACK (serv, "/ardour/stop_forget", "", stop_forget);
371 REGISTER_CALLBACK (serv, "/ardour/set_punch_range", "", set_punch_range);
372 REGISTER_CALLBACK (serv, "/ardour/set_loop_range", "", set_loop_range);
373 REGISTER_CALLBACK (serv, "/ardour/set_session_range", "", set_session_range);
374 REGISTER_CALLBACK (serv, "/ardour/toggle_monitor_mute", "", toggle_monitor_mute);
375 REGISTER_CALLBACK (serv, "/ardour/toggle_monitor_dim", "", toggle_monitor_dim);
376 REGISTER_CALLBACK (serv, "/ardour/toggle_monitor_mono", "", toggle_monitor_mono);
377 REGISTER_CALLBACK (serv, "/ardour/quick_snapshot_switch", "", quick_snapshot_switch);
378 REGISTER_CALLBACK (serv, "/ardour/quick_snapshot_stay", "", quick_snapshot_stay);
379 REGISTER_CALLBACK (serv, "/ardour/fit_1_track", "", fit_1_track);
380 REGISTER_CALLBACK (serv, "/ardour/fit_2_tracks", "", fit_2_tracks);
381 REGISTER_CALLBACK (serv, "/ardour/fit_4_tracks", "", fit_4_tracks);
382 REGISTER_CALLBACK (serv, "/ardour/fit_8_tracks", "", fit_8_tracks);
383 REGISTER_CALLBACK (serv, "/ardour/fit_16_tracks", "", fit_16_tracks);
384 REGISTER_CALLBACK (serv, "/ardour/fit_32_tracks", "", fit_32_tracks);
385 REGISTER_CALLBACK (serv, "/ardour/fit_all_tracks", "", fit_all_tracks);
386 REGISTER_CALLBACK (serv, "/ardour/zoom_100_ms", "", zoom_100_ms);
387 REGISTER_CALLBACK (serv, "/ardour/zoom_1_sec", "", zoom_1_sec);
388 REGISTER_CALLBACK (serv, "/ardour/zoom_10_sec", "", zoom_10_sec);
389 REGISTER_CALLBACK (serv, "/ardour/zoom_1_min", "", zoom_1_min);
390 REGISTER_CALLBACK (serv, "/ardour/zoom_5_min", "", zoom_5_min);
391 REGISTER_CALLBACK (serv, "/ardour/zoom_10_min", "", zoom_10_min);
392 REGISTER_CALLBACK (serv, "/ardour/zoom_to_session", "", zoom_to_session);
393 REGISTER_CALLBACK (serv, "/ardour/temporal_zoom_in", "f", temporal_zoom_in);
394 REGISTER_CALLBACK (serv, "/ardour/temporal_zoom_out", "f", temporal_zoom_out);
395 REGISTER_CALLBACK (serv, "/ardour/scroll_up_1_track", "f", scroll_up_1_track);
396 REGISTER_CALLBACK (serv, "/ardour/scroll_dn_1_track", "f", scroll_dn_1_track);
397 REGISTER_CALLBACK (serv, "/ardour/scroll_up_1_page", "f", scroll_up_1_page);
398 REGISTER_CALLBACK (serv, "/ardour/scroll_dn_1_page", "f", scroll_dn_1_page);
402 * NOTE: these messages are provided for (arguably broken) apps
403 * that MUST send float args ( TouchOSC and Lemur ).
404 * Normally these ardour transport messages don't require an argument,
405 * so we're providing redundant calls with vestigial "float" args.
407 * These controls are active on 1.0 only (to prevent duplicate action on
408 * press "/button 1", and release "/button 0")
409 * http://hexler.net/docs/touchosc-controls-reference
411 REGISTER_CALLBACK (serv, "/ardour/pushbutton/loop_toggle", "f", loop_toggle);
412 REGISTER_CALLBACK (serv, "/ardour/pushbutton/add_marker", "f", add_marker);
413 REGISTER_CALLBACK (serv, "/ardour/pushbutton/goto_start", "f", goto_start);
414 REGISTER_CALLBACK (serv, "/ardour/pushbutton/goto_end", "f", goto_end);
415 REGISTER_CALLBACK (serv, "/ardour/pushbutton/rewind", "f", rewind);
416 REGISTER_CALLBACK (serv, "/ardour/pushbutton/ffwd", "f", ffwd);
417 REGISTER_CALLBACK (serv, "/ardour/pushbutton/transport_stop", "f", transport_stop);
418 REGISTER_CALLBACK (serv, "/ardour/pushbutton/transport_play", "f", transport_play);
419 REGISTER_CALLBACK (serv, "/ardour/pushbutton/save_state", "f", save_state);
420 REGISTER_CALLBACK (serv, "/ardour/pushbutton/prev_marker", "f", prev_marker);
421 REGISTER_CALLBACK (serv, "/ardour/pushbutton/next_marker", "f", next_marker);
422 REGISTER_CALLBACK (serv, "/ardour/pushbutton/undo", "f", undo);
423 REGISTER_CALLBACK (serv, "/ardour/pushbutton/redo", "f", redo);
424 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_punch_in", "f", toggle_punch_in);
425 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_punch_out", "f", toggle_punch_out);
426 REGISTER_CALLBACK (serv, "/ardour/pushbutton/rec_enable_toggle", "f", rec_enable_toggle);
427 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_all_rec_enables", "f", toggle_all_rec_enables);
428 REGISTER_CALLBACK (serv, "/ardour/pushbutton/remove_marker", "f", remove_marker_at_playhead);
429 REGISTER_CALLBACK (serv, "/ardour/pushbutton/mark_in", "f", mark_in);
430 REGISTER_CALLBACK (serv, "/ardour/pushbutton/mark_out", "f", mark_out);
431 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_click", "f", toggle_click);
432 REGISTER_CALLBACK (serv, "/ardour/pushbutton/midi_panic", "f", midi_panic);
433 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_roll", "f", toggle_roll);
434 REGISTER_CALLBACK (serv, "/ardour/pushbutton/stop_forget", "f", stop_forget);
435 REGISTER_CALLBACK (serv, "/ardour/pushbutton/set_punch_range", "f", set_punch_range);
436 REGISTER_CALLBACK (serv, "/ardour/pushbutton/set_loop_range", "f", set_loop_range);
437 REGISTER_CALLBACK (serv, "/ardour/pushbutton/set_session_range", "f", set_session_range);
438 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_monitor_mute", "f", toggle_monitor_mute);
439 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_monitor_dim", "f", toggle_monitor_dim);
440 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_monitor_mono", "f", toggle_monitor_mono);
441 REGISTER_CALLBACK (serv, "/ardour/pushbutton/quick_snapshot_switch", "f", quick_snapshot_switch);
442 REGISTER_CALLBACK (serv, "/ardour/pushbutton/quick_snapshot_stay", "f", quick_snapshot_stay);
443 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_1_track", "f", fit_1_track);
444 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_2_tracks", "f", fit_2_tracks);
445 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_4_tracks", "f", fit_4_tracks);
446 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_8_tracks", "f", fit_8_tracks);
447 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_16_tracks", "f", fit_16_tracks);
448 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_32_tracks", "f", fit_32_tracks);
449 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_all_tracks", "f", fit_all_tracks);
450 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_100_ms", "f", zoom_100_ms);
451 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_1_sec", "f", zoom_1_sec);
452 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_10_sec", "f", zoom_10_sec);
453 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_1_min", "f", zoom_1_min);
454 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_5_min", "f", zoom_5_min);
455 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_10_min", "f", zoom_10_min);
456 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_to_session", "f", zoom_to_session);
457 REGISTER_CALLBACK (serv, "/ardour/pushbutton/temporal_zoom_in", "f", temporal_zoom_in);
458 REGISTER_CALLBACK (serv, "/ardour/pushbutton/temporal_zoom_out", "f", temporal_zoom_out);
459 REGISTER_CALLBACK (serv, "/ardour/pushbutton/scroll_up_1_track", "f", scroll_up_1_track);
460 REGISTER_CALLBACK (serv, "/ardour/pushbutton/scroll_dn_1_track", "f", scroll_dn_1_track);
461 REGISTER_CALLBACK (serv, "/ardour/pushbutton/scroll_up_1_page", "f", scroll_up_1_page);
462 REGISTER_CALLBACK (serv, "/ardour/pushbutton/scroll_dn_1_page", "f", scroll_dn_1_page);
464 /* These commands require the route index in addition to the arg; TouchOSC (et al) can't use these */
465 REGISTER_CALLBACK (serv, "/ardour/routes/mute", "ii", route_mute);
466 REGISTER_CALLBACK (serv, "/ardour/routes/solo", "ii", route_solo);
467 REGISTER_CALLBACK (serv, "/ardour/routes/recenable", "ii", route_recenable);
468 REGISTER_CALLBACK (serv, "/ardour/routes/gainabs", "if", route_set_gain_abs);
469 REGISTER_CALLBACK (serv, "/ardour/routes/gaindB", "if", route_set_gain_dB);
470 REGISTER_CALLBACK (serv, "/ardour/routes/trimabs", "if", route_set_trim_abs);
471 REGISTER_CALLBACK (serv, "/ardour/routes/trimdB", "if", route_set_trim_dB);
472 REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_position", "if", route_set_pan_stereo_position);
473 REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_width", "if", route_set_pan_stereo_width);
474 REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter", "iiif", route_plugin_parameter);
475 REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter/print", "iii", route_plugin_parameter_print);
476 REGISTER_CALLBACK (serv, "/ardour/routes/send/gainabs", "iif", route_set_send_gain_abs);
477 REGISTER_CALLBACK (serv, "/ardour/routes/send/gaindB", "iif", route_set_send_gain_dB);
479 /* still not-really-standardized query interface */
480 //REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
481 //REGISTER_CALLBACK (serv, "/ardour/set", "", set);
483 // un/register_update args= s:ctrl s:returl s:retpath
484 //lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
485 //lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
486 //lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
487 //lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
489 /* this is a special catchall handler,
490 * register at the end so this is only called if no
491 * other handler matches (used for debug) */
492 lo_server_add_method (serv, 0, 0, _catchall, this);
497 OSC::osc_input_handler (IOCondition ioc, lo_server srv)
504 lo_server_recv (srv);
511 OSC::get_server_url()
517 urlstr = lo_server_get_url (_osc_server);
526 OSC::get_unix_server_url()
531 if (_osc_unix_server) {
532 urlstr = lo_server_get_url (_osc_unix_server);
541 OSC::listen_to_route (boost::shared_ptr<Route> route, lo_address addr)
543 /* avoid duplicate listens */
545 for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); ++x) {
547 OSCRouteObserver* ro;
549 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
551 int res = strcmp(lo_address_get_hostname(ro->address()), lo_address_get_hostname(addr));
553 if (ro->route() == route && res == 0) {
559 OSCRouteObserver* o = new OSCRouteObserver (route, addr);
560 route_observers.push_back (o);
562 route->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::drop_route, this, boost::weak_ptr<Route> (route)), this);
566 OSC::drop_route (boost::weak_ptr<Route> wr)
568 boost::shared_ptr<Route> r = wr.lock ();
574 for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
576 OSCRouteObserver* rc;
578 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
580 if (rc->route() == r) {
582 x = route_observers.erase (x);
593 OSC::end_listen (boost::shared_ptr<Route> r, lo_address addr)
595 RouteObservers::iterator x;
597 // Remove the route observers
598 for (x = route_observers.begin(); x != route_observers.end();) {
600 OSCRouteObserver* ro;
602 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
604 int res = strcmp(lo_address_get_hostname(ro->address()), lo_address_get_hostname(addr));
606 if (ro->route() == r && res == 0) {
608 x = route_observers.erase (x);
621 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
625 subpath = (char*) malloc (len-15+1);
626 memcpy (subpath, path, len-15);
627 subpath[len-15] = '\0';
629 send_current_value (subpath, argv, argc, msg);
635 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
641 lo_message reply = lo_message_new ();
642 boost::shared_ptr<Route> r;
645 lo_message_add_string (reply, path);
648 lo_message_add_string (reply, "bad syntax");
651 r = session->route_by_remote_id (id);
654 lo_message_add_string (reply, "not found");
657 if (strcmp (path, "/routes/state") == 0) {
659 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
660 lo_message_add_string (reply, "AT");
661 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
662 lo_message_add_string (reply, "MT");
664 lo_message_add_string (reply, "B");
667 lo_message_add_string (reply, r->name().c_str());
668 lo_message_add_int32 (reply, r->n_inputs().n_audio());
669 lo_message_add_int32 (reply, r->n_outputs().n_audio());
670 lo_message_add_int32 (reply, r->muted());
671 lo_message_add_int32 (reply, r->soloed());
673 } else if (strcmp (path, "/routes/mute") == 0) {
675 lo_message_add_int32 (reply, (float) r->muted());
677 } else if (strcmp (path, "/routes/solo") == 0) {
679 lo_message_add_int32 (reply, r->soloed());
684 lo_send_message (lo_message_get_source (msg), "#reply", reply);
685 lo_message_free (reply);
689 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
691 return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
695 OSC::catchall (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
698 int ret = 1; /* unhandled */
700 //cerr << "Received a message, path = " << path << " types = \""
701 // << (types ? types : "NULL") << '"' << endl;
703 /* 15 for /#current_value plus 2 for /<path> */
707 if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
708 current_value_query (path, len, argv, argc, msg);
711 } else if (strcmp (path, "/routes/listen") == 0) {
713 cerr << "set up listener\n";
715 lo_message reply = lo_message_new ();
718 lo_message_add_string (reply, "syntax error");
720 for (int n = 0; n < argc; ++n) {
722 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
725 lo_message_add_string (reply, "not found");
726 cerr << "no such route\n";
729 cerr << "add listener\n";
730 listen_to_route (r, lo_message_get_source (msg));
731 lo_message_add_int32 (reply, argv[n]->i);
736 lo_send_message (lo_message_get_source (msg), "#reply", reply);
737 lo_message_free (reply);
741 } else if (strcmp (path, "/routes/ignore") == 0) {
743 for (int n = 0; n < argc; ++n) {
745 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
748 end_listen (r, lo_message_get_source (msg));
753 } else if (argc == 1 && types[0] == 'f') { // single float -- probably TouchOSC
754 if (!strncmp (path, "/ardour/routes/gainabs/", 23) && strlen (path) > 23) {
755 int rid = atoi (&path[23]);
756 // use some power-scale mapping??
757 route_set_gain_abs (rid, argv[0]->f);
760 else if (!strncmp (path, "/ardour/routes/trimabs/", 23) && strlen (path) > 23) {
761 int rid = atoi (&path[23]);
763 route_set_trim_abs (rid, argv[0]->f);
766 else if (!strncmp (path, "/ardour/routes/mute/", 20) && strlen (path) > 20) {
767 int rid = atoi (&path[20]);
768 route_mute (rid, argv[0]->f == 1.0);
771 else if (!strncmp (path, "/ardour/routes/solo/", 20) && strlen (path) > 20) {
772 int rid = atoi (&path[20]);
773 route_solo (rid, argv[0]->f == 1.0);
776 else if (!strncmp (path, "/ardour/routes/recenable/", 25) && strlen (path) > 25) {
777 int rid = atoi (&path[25]);
778 route_recenable (rid, argv[0]->f == 1.0);
783 if ((ret && _debugmode == Unhandled)) {
784 debugmsg (_("Unhandled OSC message"), path, types, argv, argc);
791 OSC::debugmsg (const char *prefix, const char *path, const char* types, lo_arg **argv, int argc)
793 std::stringstream ss;
794 for (int i = 0; i < argc; ++i) {
795 lo_type type = (lo_type)types[i];
799 ss << "i:" << argv[i]->i;
802 ss << "f:" << argv[i]->f;
805 ss << "d:" << argv[i]->d;
808 ss << "s:" << &argv[i]->s;
811 ss << "h:" << argv[i]->h;
814 ss << "c:" << argv[i]->s;
845 PBD::info << prefix << ": " << path << ss.str() << endmsg;
854 // "Application Hook" Handlers //
856 OSC::session_loaded (Session& s)
858 lo_address listener = lo_address_new (NULL, "7770");
859 lo_send (listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str());
863 OSC::session_exported (std::string path, std::string name)
865 lo_address listener = lo_address_new (NULL, "7770");
866 lo_send (listener, "/session/exported", "ss", path.c_str(), name.c_str());
869 // end "Application Hook" Handlers //
874 OSC::current_value (const char */*path*/, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, void */*data*/, void* /*user_data*/)
879 if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
883 const char *returl = argv[1]->s;
884 lo_address addr = find_or_cache_addr (returl);
886 const char *retpath = argv[2]->s;
889 if (strcmp (argv[0]->s, "transport_frame") == 0) {
892 lo_send (addr, retpath, "i", session->transport_frame());
895 } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
898 lo_send (addr, retpath, "i", session->transport_frame());
901 } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
904 lo_send (addr, retpath, "i", session->transport_frame());
907 } else if (strcmp (argv[0]->s, "punch_in") == 0) {
910 lo_send (addr, retpath, "i", session->transport_frame());
913 } else if (strcmp (argv[0]->s, "punch_out") == 0) {
916 lo_send (addr, retpath, "i", session->transport_frame());
919 } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
922 lo_send (addr, retpath, "i", session->transport_frame());
934 OSC::routes_list (lo_message msg)
939 for (int n = 0; n < (int) session->nroutes(); ++n) {
941 boost::shared_ptr<Route> r = session->route_by_remote_id (n);
945 lo_message reply = lo_message_new ();
947 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
948 lo_message_add_string (reply, "AT");
949 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
950 lo_message_add_string (reply, "MT");
952 lo_message_add_string (reply, "B");
955 lo_message_add_string (reply, r->name().c_str());
956 lo_message_add_int32 (reply, r->n_inputs().n_audio());
957 lo_message_add_int32 (reply, r->n_outputs().n_audio());
958 lo_message_add_int32 (reply, r->muted());
959 lo_message_add_int32 (reply, r->soloed());
960 lo_message_add_int32 (reply, r->remote_control_id());
962 if (boost::dynamic_pointer_cast<AudioTrack>(r)
963 || boost::dynamic_pointer_cast<MidiTrack>(r)) {
965 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(r);
966 lo_message_add_int32 (reply, t->record_enabled());
969 //Automatically listen to routes listed
970 listen_to_route(r, lo_message_get_source (msg));
972 lo_send_message (lo_message_get_source (msg), "#reply", reply);
973 lo_message_free (reply);
977 // Send end of listing message
978 lo_message reply = lo_message_new ();
980 lo_message_add_string (reply, "end_route_list");
981 lo_message_add_int64 (reply, session->frame_rate());
982 lo_message_add_int64 (reply, session->current_end_frame());
984 lo_send_message (lo_message_get_source (msg), "#reply", reply);
986 lo_message_free (reply);
990 OSC::transport_frame (lo_message msg)
995 framepos_t pos = session->transport_frame ();
997 lo_message reply = lo_message_new ();
998 lo_message_add_int64 (reply, pos);
1000 lo_send_message (lo_message_get_source (msg), "/ardour/transport_frame", reply);
1002 lo_message_free (reply);
1006 OSC::transport_speed (lo_message msg)
1011 double ts = session->transport_speed ();
1013 lo_message reply = lo_message_new ();
1014 lo_message_add_double (reply, ts);
1016 lo_send_message (lo_message_get_source (msg), "/ardour/transport_speed", reply);
1018 lo_message_free (reply);
1022 OSC::record_enabled (lo_message msg)
1027 int re = (int)session->get_record_enabled ();
1029 lo_message reply = lo_message_new ();
1030 lo_message_add_int32 (reply, re);
1032 lo_send_message (lo_message_get_source (msg), "/ardour/record_enabled", reply);
1034 lo_message_free (reply);
1039 OSC::route_mute (int rid, int yn)
1041 if (!session) return -1;
1043 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1046 r->set_mute (yn, this);
1053 OSC::route_solo (int rid, int yn)
1055 if (!session) return -1;
1057 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1060 boost::shared_ptr<RouteList> rl (new RouteList);
1062 session->set_solo (rl, yn);
1069 OSC::route_recenable (int rid, int yn)
1071 if (!session) return -1;
1073 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1076 r->set_record_enabled (yn, this);
1083 OSC::route_set_gain_abs (int rid, float level)
1085 if (!session) return -1;
1087 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1090 r->set_gain (level, PBD::Controllable::NoGroup);
1097 OSC::route_set_gain_dB (int rid, float dB)
1099 return route_set_gain_abs (rid, dB_to_coefficient (dB));
1104 OSC::route_set_trim_abs (int rid, float level)
1106 if (!session) return -1;
1108 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1111 r->set_trim (level, this);
1118 OSC::route_set_trim_dB (int rid, float dB)
1120 return route_set_trim_abs(rid, dB_to_coefficient (dB));
1125 OSC::route_set_pan_stereo_position (int rid, float pos)
1127 if (!session) return -1;
1129 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1132 boost::shared_ptr<Panner> panner = r->panner();
1134 panner->set_position (pos);
1143 OSC::route_set_pan_stereo_width (int rid, float pos)
1145 if (!session) return -1;
1147 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1150 boost::shared_ptr<Panner> panner = r->panner();
1152 panner->set_width (pos);
1161 OSC::route_set_send_gain_abs (int rid, int sid, float val)
1167 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1173 /* revert to zero-based counting */
1179 boost::shared_ptr<Processor> p = r->nth_send (sid);
1182 boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
1183 boost::shared_ptr<Amp> a = s->amp();
1186 a->gain_control()->set_value (val, PBD::Controllable::NoGroup);
1193 OSC::route_set_send_gain_dB (int rid, int sid, float val)
1199 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1205 /* revert to zero-based counting */
1211 boost::shared_ptr<Processor> p = r->nth_send (sid);
1214 boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
1215 boost::shared_ptr<Amp> a = s->amp();
1218 a->gain_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
1225 OSC::route_plugin_parameter (int rid, int piid, int par, float val)
1230 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1233 PBD::error << "OSC: Invalid Remote Control ID '" << rid << "'" << endmsg;
1237 boost::shared_ptr<Processor> redi=r->nth_plugin (piid);
1240 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << rid << "'" << endmsg;
1244 boost::shared_ptr<PluginInsert> pi;
1246 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
1247 PBD::error << "OSC: given processor # " << piid << " on RID '" << rid << "' is not a Plugin." << endmsg;
1251 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
1254 uint32_t controlid = pip->nth_parameter (par,ok);
1257 PBD::error << "OSC: Cannot find parameter # " << par << " for plugin # " << piid << " on RID '" << rid << "'" << endmsg;
1261 if (!pip->parameter_is_input(controlid)) {
1262 PBD::error << "OSC: Parameter # " << par << " for plugin # " << piid << " on RID '" << rid << "' is not a control input" << endmsg;
1266 ParameterDescriptor pd;
1267 pi->plugin()->get_parameter_descriptor (controlid,pd);
1269 if (val >= pd.lower && val < pd.upper) {
1271 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
1272 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
1273 c->set_value (val, PBD::Controllable::NoGroup);
1275 PBD::warning << "OSC: Parameter # " << par << " for plugin # " << piid << " on RID '" << rid << "' is out of range" << endmsg;
1276 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
1283 OSC::route_plugin_parameter_print (int rid, int piid, int par)
1289 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1295 boost::shared_ptr<Processor> redi=r->nth_processor (piid);
1301 boost::shared_ptr<PluginInsert> pi;
1303 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
1307 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
1310 uint32_t controlid = pip->nth_parameter (par,ok);
1316 ParameterDescriptor pd;
1318 if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
1319 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
1321 cerr << "parameter: " << redi->describe_parameter(controlid) << "\n";
1322 cerr << "current value: " << c->get_value ();
1323 cerr << "lower value: " << pd.lower << "\n";
1324 cerr << "upper value: " << pd.upper << "\n";
1333 XMLNode& node (ControlProtocol::get_state());
1334 node.add_property("debugmode", (int) _debugmode); // TODO: enum2str
1339 OSC::set_state (const XMLNode& node, int version)
1341 if (ControlProtocol::set_state (node, version)) {
1344 XMLProperty const * p = node.property (X_("debugmode"));
1346 _debugmode = OSCDebugMode (PBD::atoi(p->value ()));