Lincoln's OSC clean-ups (#3716).
[ardour.git] / libs / surfaces / osc / osc_route_observer.cc
1 /*
2     Copyright (C) 2009 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "boost/lambda/lambda.hpp"
21
22 #include "ardour/route.h"
23 #include "ardour/audio_track.h"
24 #include "ardour/midi_track.h"
25
26 #include "osc.h"
27 #include "osc_route_observer.h"
28
29 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
30
31 using namespace std;
32 using namespace sigc;
33 using namespace PBD;
34 using namespace ARDOUR;
35 using namespace boost;
36
37
38 OSCRouteObserver::OSCRouteObserver (shared_ptr<Route> r, lo_address a)
39         : _route (r)
40 {
41         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
42         
43         _route->PropertyChanged.connect (name_changed_connection, MISSING_INVALIDATOR, ui_bind (&OSCRouteObserver::name_changed, this, boost::lambda::_1), OSC::instance());
44
45         if (dynamic_pointer_cast<AudioTrack>(_route) || dynamic_pointer_cast<MidiTrack>(_route)) {
46
47                 shared_ptr<Track> track = dynamic_pointer_cast<Track>(r);
48                 shared_ptr<Controllable> rec_controllable = dynamic_pointer_cast<Controllable>(track->rec_enable_control());
49
50                 rec_controllable->Changed.connect (rec_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/rec"), track->rec_enable_control()), OSC::instance());
51         }
52         
53         shared_ptr<Controllable> mute_controllable = dynamic_pointer_cast<Controllable>(_route->mute_control());
54         mute_controllable->Changed.connect (mute_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/mute"), _route->mute_control()), OSC::instance());
55
56         shared_ptr<Controllable> solo_controllable = dynamic_pointer_cast<Controllable>(_route->solo_control());
57         solo_controllable->Changed.connect (solo_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/solo"), _route->solo_control()), OSC::instance());
58
59         shared_ptr<Controllable> gain_controllable = dynamic_pointer_cast<Controllable>(_route->gain_control());
60         gain_controllable->Changed.connect (gain_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/gain"), _route->gain_control()), OSC::instance());
61 }
62
63 OSCRouteObserver::~OSCRouteObserver ()
64 {
65         name_changed_connection.disconnect();
66         rec_changed_connection.disconnect();
67         mute_changed_connection.disconnect();
68         solo_changed_connection.disconnect();
69         gain_changed_connection.disconnect();
70
71         lo_address_free (addr);
72 }
73
74 void
75 OSCRouteObserver::name_changed (const PBD::PropertyChange& what_changed)
76 {
77         if (!what_changed.contains (ARDOUR::Properties::name)) {
78             return;
79         }
80         
81         if (!_route) {
82                 return;
83         }
84         
85         lo_message msg = lo_message_new ();
86
87         lo_message_add_int32 (msg, _route->remote_control_id());
88         lo_message_add_string (msg, _route->name().c_str());
89
90         lo_send_message (addr, "/route/name", msg);
91         lo_message_free (msg);
92 }
93
94 void
95 OSCRouteObserver::send_change_message (string path, shared_ptr<Controllable> controllable)
96 {
97         lo_message msg = lo_message_new ();
98
99         lo_message_add_int32 (msg, _route->remote_control_id());
100         lo_message_add_float (msg, (float) controllable->get_value());
101
102         /* XXX thread issues */
103
104         //std::cerr << "ORC: send " << path << " = " << controllable->get_value() << std::endl;
105
106         lo_send_message (addr, path.c_str(), msg);
107         lo_message_free (msg);
108 }