first vaguely working version using PresentationInfo
[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 using namespace std;
32 using namespace PBD;
33 using namespace ARDOUR;
34 using namespace ArdourSurface;
35
36 OSCRouteObserver::OSCRouteObserver (boost::shared_ptr<Route> r, lo_address a)
37         : _route (r)
38 {
39         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
40
41         _route->PropertyChanged.connect (name_changed_connection, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::name_changed, this, boost::lambda::_1), OSC::instance());
42
43         if (boost::dynamic_pointer_cast<AudioTrack>(_route) || boost::dynamic_pointer_cast<MidiTrack>(_route)) {
44
45                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track>(r);
46                 boost::shared_ptr<Controllable> rec_controllable = boost::dynamic_pointer_cast<Controllable>(track->rec_enable_control());
47
48                 rec_controllable->Changed.connect (rec_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/rec"), track->rec_enable_control()), OSC::instance());
49         }
50
51         boost::shared_ptr<Controllable> mute_controllable = boost::dynamic_pointer_cast<Controllable>(_route->mute_control());
52         mute_controllable->Changed.connect (mute_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/mute"), _route->mute_control()), OSC::instance());
53
54         boost::shared_ptr<Controllable> solo_controllable = boost::dynamic_pointer_cast<Controllable>(_route->solo_control());
55         solo_controllable->Changed.connect (solo_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/solo"), _route->solo_control()), OSC::instance());
56
57         boost::shared_ptr<Controllable> gain_controllable = boost::dynamic_pointer_cast<Controllable>(_route->gain_control());
58         gain_controllable->Changed.connect (gain_changed_connection, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/route/gain"), _route->gain_control()), OSC::instance());
59 }
60
61 OSCRouteObserver::~OSCRouteObserver ()
62 {
63         name_changed_connection.disconnect();
64         rec_changed_connection.disconnect();
65         mute_changed_connection.disconnect();
66         solo_changed_connection.disconnect();
67         gain_changed_connection.disconnect();
68
69         lo_address_free (addr);
70 }
71
72 void
73 OSCRouteObserver::name_changed (const PBD::PropertyChange& what_changed)
74 {
75         if (!what_changed.contains (ARDOUR::Properties::name)) {
76             return;
77         }
78
79         if (!_route) {
80                 return;
81         }
82
83         lo_message msg = lo_message_new ();
84
85         /* XXX can only use group part of ID at present */
86         lo_message_add_int32 (msg, _route->presentation_info().group_order());
87         lo_message_add_string (msg, _route->name().c_str());
88
89         lo_send_message (addr, "/route/name", msg);
90         lo_message_free (msg);
91 }
92
93 void
94 OSCRouteObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
95 {
96         lo_message msg = lo_message_new ();
97
98         /* XXX can only use group part of ID at present */
99         lo_message_add_int32 (msg, _route->presentation_info().group_order());
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 }