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