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