Reset PanControllable StreamPanners when they change.
[ardour.git] / libs / ardour / ardour / panner.h
1 /*
2     Copyright (C) 2004 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_panner_h__
21 #define __ardour_panner_h__
22
23 #include <cmath>
24 #include <cassert>
25 #include <vector>
26 #include <string>
27 #include <iostream>
28
29 #include "pbd/stateful.h"
30 #include "pbd/controllable.h"
31 #include "pbd/cartesian.h"
32
33 #include "ardour/types.h"
34 #include "ardour/automation_control.h"
35 #include "ardour/processor.h"
36
37 namespace ARDOUR {
38
39 class Session;
40 class Panner;
41 class BufferSet;
42 class AudioBuffer;
43 class Speakers;
44
45 class StreamPanner : public PBD::Stateful
46 {
47   public:
48         StreamPanner (Panner& p, Evoral::Parameter param);
49         ~StreamPanner ();
50
51         void set_muted (bool yn);
52         bool muted() const { return _muted; }
53
54         const PBD::AngularVector& get_position() const { return _angles; }
55         const PBD::AngularVector& get_effective_position() const { return _effective_angles; }
56         void set_position (const PBD::AngularVector&, bool link_call = false);
57         void set_diffusion (double);
58
59         void distribute (AudioBuffer &, BufferSet &, gain_t, pframes_t);
60         void distribute_automated (AudioBuffer &, BufferSet &, framepos_t, framepos_t, pframes_t, pan_t **);
61
62         /* the basic StreamPanner API */
63
64         /**
65          *  Pan some input samples to a number of output buffers.
66          *
67          *  @param src Input buffer.
68          *  @param obufs Output buffers (one per panner output).
69          *  @param gain_coeff Gain coefficient to apply to output samples.
70          *  @param nframes Number of frames in the input.
71          */
72         virtual void do_distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, pframes_t nframes) = 0;
73         virtual void do_distribute_automated (AudioBuffer& src, BufferSet& obufs,
74                                               framepos_t start, framepos_t end, pframes_t nframes,
75                                               pan_t** buffers) = 0;
76
77         boost::shared_ptr<AutomationControl> pan_control()  { return _control; }
78
79         PBD::Signal0<void> Changed;      /* for position or diffusion */
80         PBD::Signal0<void> StateChanged; /* for mute, mono */
81
82         int set_state (const XMLNode&, int version);
83         virtual XMLNode& state (bool full_state) = 0;
84
85         Panner & get_parent() { return parent; }
86
87         /* old school automation loading */
88         virtual int load (std::istream&, std::string path, uint32_t&) = 0;
89
90         struct PanControllable : public AutomationControl {
91                 PanControllable (Session& s, std::string name, StreamPanner* p, Evoral::Parameter param)
92                         : AutomationControl (s, param,
93                                              boost::shared_ptr<AutomationList>(new AutomationList(param)), name)
94                         , streampanner (p)
95                 { assert (param.type() == PanAutomation); }
96                 
97                 AutomationList* alist() { return (AutomationList*)_list.get(); }
98                 StreamPanner* streampanner;
99
100                 void set_value (double);
101                 double get_value (void) const;
102                 double lower () const;
103         };
104
105   protected:
106         friend class Panner;
107         Panner& parent;
108
109         void set_mono (bool);
110         
111         PBD::AngularVector _angles;
112         PBD::AngularVector _effective_angles;
113         double        _diffusion; 
114
115         bool _muted;
116         bool _mono;
117         
118         boost::shared_ptr<AutomationControl> _control;
119
120         XMLNode& get_state ();
121
122         /* Update internal parameters based on this.angles */
123         virtual void update () = 0;
124 };
125
126 class BaseStereoPanner : public StreamPanner
127 {
128   public:
129         BaseStereoPanner (Panner&, Evoral::Parameter param);
130         ~BaseStereoPanner ();
131
132         /* this class just leaves the pan law itself to be defined
133            by the update(), do_distribute_automated()
134            methods. derived classes also need a factory method
135            and a type name. See EqualPowerStereoPanner as an example.
136         */
137
138         void do_distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, pframes_t nframes);
139
140         static double azimuth_to_lr_fract (double azi) { 
141                 /* 180.0 degrees=> left => 0.0 */
142                 /* 0.0 degrees => right => 1.0 */
143                 return 1.0 - (azi/180.0);
144         }
145
146         static double lr_fract_to_azimuth (double fract) { 
147                 /* fract = 0.0 => degrees = 180.0 => left */
148                 /* fract = 1.0 => degrees = 0.0 => right */
149                 return 180.0 - (fract * 180.0);
150         }
151         
152         /* old school automation loading */
153
154         int load (std::istream&, std::string path, uint32_t&);
155
156   protected:
157         float left;
158         float right;
159         float desired_left;
160         float desired_right;
161         float left_interp;
162         float right_interp;
163 };
164
165 class EqualPowerStereoPanner : public BaseStereoPanner
166 {
167   public:
168         EqualPowerStereoPanner (Panner&, Evoral::Parameter param);
169         ~EqualPowerStereoPanner ();
170
171         void do_distribute_automated (AudioBuffer& src, BufferSet& obufs,
172                                       framepos_t start, framepos_t end, pframes_t nframes,
173                                       pan_t** buffers);
174
175         void get_current_coefficients (pan_t*) const;
176         void get_desired_coefficients (pan_t*) const;
177
178         static StreamPanner* factory (Panner&, Evoral::Parameter param, Speakers&);
179         static std::string name;
180
181         XMLNode& state (bool full_state); 
182         XMLNode& get_state (void); 
183         int      set_state (const XMLNode&, int version);
184
185   private:
186         void update ();
187 };
188
189 /** Class to pan from some number of inputs to some number of outputs.
190  *  This class has a number of StreamPanners, one for each input.
191  */
192 class Panner : public SessionObject, public Automatable
193 {
194 public:
195         struct Output {
196             PBD::AngularVector position;
197             pan_t current_pan;
198             pan_t desired_pan;
199             
200             Output (const PBD::AngularVector& a) 
201             : position (a), current_pan (0), desired_pan (0) {}
202
203         };
204
205         Panner (std::string name, Session&);
206         virtual ~Panner ();
207
208         void clear_panners ();
209         bool empty() const { return _streampanners.empty(); }
210
211         void set_automation_state (AutoState);
212         AutoState automation_state() const;
213         void set_automation_style (AutoStyle);
214         AutoStyle automation_style() const;
215         bool touching() const;
216
217         std::string describe_parameter (Evoral::Parameter param);
218
219         bool can_support_io_configuration (const ChanCount& /*in*/, ChanCount& /*out*/) const { return true; };
220
221         /// The fundamental Panner function
222         void run (BufferSet& src, BufferSet& dest, framepos_t start_frame, framepos_t end_frames, pframes_t nframes);
223
224         bool bypassed() const { return _bypassed; }
225         void set_bypassed (bool yn);
226         bool mono () const { return _mono; }
227         void set_mono (bool);
228
229         StreamPanner* add ();
230         void remove (uint32_t which);
231         void reset (uint32_t noutputs, uint32_t npans);
232         void reset_streampanner (uint32_t which_panner);
233         void reset_to_default ();
234
235         XMLNode& get_state (void);
236         XMLNode& state (bool full);
237         int      set_state (const XMLNode&, int version);
238
239         static bool equivalent (pan_t a, pan_t b) {
240                 return fabsf (a - b) < 0.002; // about 1 degree of arc for a stereo panner
241         }
242         static bool equivalent (const PBD::AngularVector& a, const PBD::AngularVector& b) {
243                 /* XXX azimuth only, at present */
244                 return fabs (a.azi - b.azi) < 1.0;
245         }
246
247         void move_output (uint32_t, float x, float y);
248         uint32_t nouts() const { return outputs.size(); }
249         Output& output (uint32_t n) { return outputs[n]; }
250
251         enum LinkDirection {
252                 SameDirection,
253                 OppositeDirection
254         };
255
256         LinkDirection link_direction() const { return _link_direction; }
257         void set_link_direction (LinkDirection);
258
259         bool linked() const { return _linked; }
260         void set_linked (bool yn);
261
262         StreamPanner &streampanner( uint32_t n ) const { assert( n < _streampanners.size() ); return *_streampanners[n]; }
263         uint32_t npanners() const { return _streampanners.size(); }
264
265         PBD::Signal0<void> Changed; /* panner and/or outputs count changed */
266         PBD::Signal0<void> LinkStateChanged;
267         PBD::Signal0<void> StateChanged; /* for bypass */
268
269         /* only StreamPanner should call these */
270
271         void set_position (const PBD::AngularVector&, StreamPanner& orig);
272
273         /* old school automation */
274
275         int load ();
276
277         boost::shared_ptr<AutomationControl> pan_control (int id, uint32_t chan=0) {
278                 return automation_control (Evoral::Parameter (PanAutomation, chan, id));
279         }
280
281         boost::shared_ptr<const AutomationControl> pan_control (int id, uint32_t chan=0) const {
282                 return automation_control (Evoral::Parameter (PanAutomation, chan, id));
283         }
284
285         boost::shared_ptr<AutomationControl> direction_control () {
286                 return automation_control (Evoral::Parameter (PanAutomation, 0, 100));
287         }
288
289         boost::shared_ptr<AutomationControl> width_control () {
290                 return automation_control (Evoral::Parameter (PanAutomation, 0, 200));
291         }
292
293         void set_stereo_position (double);
294         void set_stereo_width (double);
295         bool set_stereo_pan (double pos, double width);
296         
297         static std::string value_as_string (double);
298         
299   private:
300         /* disallow copy construction */
301         Panner (Panner const &);
302
303         void distribute_no_automation(BufferSet& src, BufferSet& dest, pframes_t nframes, gain_t gain_coeff);
304         std::vector<StreamPanner*> _streampanners; ///< one StreamPanner per input
305         std::vector<Output> outputs;
306         uint32_t     current_outs;
307         bool             _linked;
308         bool             _bypassed;
309         bool             _mono;
310         LinkDirection    _link_direction;
311
312         static float current_automation_version_number;
313
314         void setup_speakers (uint32_t nouts);
315         void setup_meta_controls ();
316
317         /* old school automation handling */
318
319         std::string automation_path;
320 };
321
322 } // namespace ARDOUR
323
324 #endif /*__ardour_panner_h__ */