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