Get the _note_mode set up correctly in a MidiDiskstream
[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 #include "i18n.h"
30
31 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
32
33 using namespace std;
34 using namespace sigc;
35 using namespace PBD;
36 using namespace ARDOUR;
37 using namespace boost;
38
39
40 OSCRouteObserver::OSCRouteObserver (boost::shared_ptr<Route> r, lo_address a)
41         : _route (r)
42 {
43         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
44         
45         _route->PropertyChanged.connect (name_changed_connection, MISSING_INVALIDATOR, ui_bind (&OSCRouteObserver::name_changed, this, boost::lambda::_1), OSC::instance());
46
47         if (dynamic_pointer_cast<AudioTrack>(_route) || dynamic_pointer_cast<MidiTrack>(_route)) {
48
49                 boost::shared_ptr<Track> track = dynamic_pointer_cast<Track>(r);
50                 boost::shared_ptr<Controllable> rec_controllable = dynamic_pointer_cast<Controllable>(track->rec_enable_control());
51
52                 rec_controllable->Changed.connect (rec_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/rec"), track->rec_enable_control()), OSC::instance());
53         }
54         
55         boost::shared_ptr<Controllable> mute_controllable = dynamic_pointer_cast<Controllable>(_route->mute_control());
56         mute_controllable->Changed.connect (mute_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/mute"), _route->mute_control()), OSC::instance());
57
58         boost::shared_ptr<Controllable> solo_controllable = dynamic_pointer_cast<Controllable>(_route->solo_control());
59         solo_controllable->Changed.connect (solo_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/solo"), _route->solo_control()), OSC::instance());
60
61         boost::shared_ptr<Controllable> gain_controllable = dynamic_pointer_cast<Controllable>(_route->gain_control());
62         gain_controllable->Changed.connect (gain_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/gain"), _route->gain_control()), OSC::instance());
63 }
64
65 OSCRouteObserver::~OSCRouteObserver ()
66 {
67         name_changed_connection.disconnect();
68         rec_changed_connection.disconnect();
69         mute_changed_connection.disconnect();
70         solo_changed_connection.disconnect();
71         gain_changed_connection.disconnect();
72
73         lo_address_free (addr);
74 }
75
76 void
77 OSCRouteObserver::name_changed (const PBD::PropertyChange& what_changed)
78 {
79         if (!what_changed.contains (ARDOUR::Properties::name)) {
80             return;
81         }
82         
83         if (!_route) {
84                 return;
85         }
86         
87         lo_message msg = lo_message_new ();
88
89         lo_message_add_int32 (msg, _route->remote_control_id());
90         lo_message_add_string (msg, _route->name().c_str());
91
92         lo_send_message (addr, "/route/name", msg);
93         lo_message_free (msg);
94 }
95
96 void
97 OSCRouteObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
98 {
99         lo_message msg = lo_message_new ();
100
101         lo_message_add_int32 (msg, _route->remote_control_id());
102         lo_message_add_float (msg, (float) controllable->get_value());
103
104         /* XXX thread issues */
105
106         //std::cerr << "ORC: send " << path << " = " << controllable->get_value() << std::endl;
107
108         lo_send_message (addr, path.c_str(), msg);
109         lo_message_free (msg);
110 }