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