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