Merge branch 'master' into windows
[ardour.git] / libs / ardour / ardour / monitor_processor.h
1 /*
2     Copyright (C) 2010 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 #ifndef __ardour_monitor_processor_h__
21 #define __ardour_monitor_processor_h__
22
23 #include <algorithm>
24 #include <iostream>
25 #include <vector>
26
27 #include "pbd/signals.h"
28 #include "pbd/compose.h"
29 #include "pbd/controllable.h"
30
31 #include "ardour/types.h"
32 #include "ardour/processor.h"
33
34 class XMLNode;
35
36 namespace ARDOUR {
37
38 class Session;
39
40 template<typename T>
41 class MPControl : public PBD::Controllable {
42 public:
43         MPControl (T initial, const std::string& name, PBD::Controllable::Flag flag,
44                    float lower = 0.0f, float upper = 1.0f)
45                 : PBD::Controllable (name, flag)
46                 , _value (initial)
47                 , _lower (lower)
48                 , _upper (upper)
49         {}
50
51         /* Controllable API */
52
53         void set_value (double v) {
54                 T newval = (T) v;
55                 if (newval != _value) {
56                         _value = std::max (_lower, std::min (_upper, newval));
57                         Changed(); /* EMIT SIGNAL */
58                 }
59         }
60
61         double get_value () const {
62                 return (float) _value;
63         }
64
65         double lower () const { return _lower; }
66         double upper () const { return _upper; }
67
68         /* "access as T" API */
69
70         MPControl& operator=(const T& v) {
71                 if (v != _value) {
72                         _value = std::max (_lower, std::min (_upper, v));
73                         Changed (); /* EMIT SIGNAL */
74                 }
75                 return *this;
76         }
77
78         bool operator==(const T& v) const {
79                 return _value == v;
80         }
81
82         bool operator<(const T& v) const {
83                 return _value < v;
84         }
85
86         bool operator<=(const T& v) const {
87                 return _value <= v;
88         }
89
90         bool operator>(const T& v) const {
91                 return _value > v;
92         }
93
94         bool operator>=(const T& v) const {
95                 return _value >= v;
96         }
97
98         operator T() const { return _value; }
99         T val() const { return _value; }
100
101 protected:
102         T _value;
103         T _lower;
104         T _upper;
105 };
106
107 class MonitorProcessor : public Processor
108 {
109 public:
110         MonitorProcessor (Session&);
111         ~MonitorProcessor ();
112
113         bool display_to_user() const;
114
115         void run (BufferSet& /*bufs*/, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t /*nframes*/, bool /*result_required*/);
116
117         XMLNode& state (bool full);
118         int set_state (const XMLNode&, int /* version */);
119
120         bool configure_io (ChanCount in, ChanCount out);
121         bool can_support_io_configuration (const ChanCount& in, ChanCount& out);
122
123         void set_cut_all (bool);
124         void set_dim_all (bool);
125         void set_polarity (uint32_t, bool invert);
126         void set_cut (uint32_t, bool cut);
127         void set_dim (uint32_t, bool dim);
128         void set_solo (uint32_t, bool);
129         void set_mono (bool);
130
131         gain_t dim_level() const { return _dim_level; }
132         gain_t solo_boost_level() const { return _solo_boost_level; }
133
134         bool dimmed (uint32_t chn) const;
135         bool soloed (uint32_t chn) const;
136         bool inverted (uint32_t chn) const;
137         bool cut (uint32_t chn) const;
138         bool cut_all () const;
139         bool dim_all () const;
140         bool mono () const;
141
142         PBD::Signal0<void> Changed;
143
144         boost::shared_ptr<PBD::Controllable> channel_cut_control (uint32_t) const;
145         boost::shared_ptr<PBD::Controllable> channel_dim_control (uint32_t) const;
146         boost::shared_ptr<PBD::Controllable> channel_polarity_control (uint32_t) const;
147         boost::shared_ptr<PBD::Controllable> channel_solo_control (uint32_t) const;
148
149         boost::shared_ptr<PBD::Controllable> dim_control () const { return _dim_all_control; }
150         boost::shared_ptr<PBD::Controllable> cut_control () const { return _cut_all_control; }
151         boost::shared_ptr<PBD::Controllable> mono_control () const { return _mono_control; }
152         boost::shared_ptr<PBD::Controllable> dim_level_control () const { return _dim_level_control; }
153         boost::shared_ptr<PBD::Controllable> solo_boost_control () const { return _solo_boost_level_control; }
154
155 private:
156         struct ChannelRecord {
157                 gain_t current_gain;
158
159                 /* pointers - created first, but managed by boost::shared_ptr<> */
160
161                 MPControl<gain_t>* cut_ptr;
162                 MPControl<bool>*   dim_ptr;
163                 MPControl<gain_t>* polarity_ptr;
164                 MPControl<bool>*   soloed_ptr;
165
166                 /* shared ptr access and lifetime management, for external users */
167
168                 boost::shared_ptr<PBD::Controllable> cut_control;
169                 boost::shared_ptr<PBD::Controllable> dim_control;
170                 boost::shared_ptr<PBD::Controllable> polarity_control;
171                 boost::shared_ptr<PBD::Controllable> soloed_control;
172
173                 /* typed controllables for internal use */
174
175                 MPControl<gain_t>& cut;
176                 MPControl<bool>&   dim;
177                 MPControl<gain_t>& polarity;
178                 MPControl<bool>&   soloed;
179
180                 ChannelRecord (uint32_t);
181         };
182
183         std::vector<ChannelRecord*> _channels;
184
185         uint32_t             solo_cnt;
186
187         /* pointers - created first, but managed by boost::shared_ptr<> */
188
189         MPControl<bool>*            _dim_all_ptr;
190         MPControl<bool>*            _cut_all_ptr;
191         MPControl<bool>*            _mono_ptr;
192         MPControl<volatile gain_t>* _dim_level_ptr;
193         MPControl<volatile gain_t>* _solo_boost_level_ptr;
194
195         /* shared ptr access and lifetime management, for external users */
196
197         boost::shared_ptr<PBD::Controllable> _dim_all_control;
198         boost::shared_ptr<PBD::Controllable> _cut_all_control;
199         boost::shared_ptr<PBD::Controllable> _mono_control;
200         boost::shared_ptr<PBD::Controllable> _dim_level_control;
201         boost::shared_ptr<PBD::Controllable> _solo_boost_level_control;
202
203         /* typed controllables for internal use */
204
205         MPControl<bool>&            _dim_all;
206         MPControl<bool>&            _cut_all;
207         MPControl<bool>&            _mono;
208         MPControl<volatile gain_t>& _dim_level;
209         MPControl<volatile gain_t>& _solo_boost_level;
210
211         void allocate_channels (uint32_t);
212 };
213
214 } /* namespace */
215
216 #endif /* __ardour_monitor_processor_h__ */