Make DnD copy processors using their XML representations. Remove unused
[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
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, nframes_t offset);
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
233         
234
235
236         XMLNode& get_state (void);
237         XMLNode& state (bool full);
238         int      set_state (const XMLNode&);
239
240         static bool equivalent (pan_t a, pan_t b) {
241                 return fabsf (a - b) < 0.002; // about 1 degree of arc for a stereo panner
242         }
243
244         void move_output (uint32_t, float x, float y);
245         uint32_t nouts() const { return outputs.size(); }
246         Output& output (uint32_t n) { return outputs[n]; }
247
248         std::vector<Output> outputs;
249
250         enum LinkDirection {
251                 SameDirection,
252                 OppositeDirection
253         };
254
255         LinkDirection link_direction() const { return _link_direction; }
256         void set_link_direction (LinkDirection);
257         
258         bool linked() const { return _linked; }
259         void set_linked (bool yn);
260
261         StreamPanner &streampanner( uint32_t n ) const { assert( n < _streampanners.size() ); return *_streampanners[n]; }
262         uint32_t npanners() const { return _streampanners.size(); }
263
264         sigc::signal<void> Changed;
265         sigc::signal<void> LinkStateChanged;
266         sigc::signal<void> StateChanged; /* for bypass */
267
268         /* only StreamPanner should call these */
269         
270         void set_position (float x, StreamPanner& orig);
271         void set_position (float x, float y, StreamPanner& orig);
272         void set_position (float x, float y, float z, StreamPanner& orig);
273
274         /* old school automation */
275
276         int load ();
277
278         struct PanControllable : public AutomationControl {
279             PanControllable (Session& s, std::string name, Panner& p, Evoral::Parameter param)
280                         : AutomationControl (s, param,
281                                         boost::shared_ptr<AutomationList>(new AutomationList(param)), name)
282                         , panner (p)
283                 { assert(param.type() != NullAutomation); }
284             
285                 AutomationList* alist() { return (AutomationList*)_list.get(); }
286             Panner& panner;
287             
288             void set_value (float);
289             float get_value (void) const;
290         };
291
292         boost::shared_ptr<AutomationControl> pan_control (int id, int chan=0) {
293             return automation_control(Evoral::Parameter (PanAutomation, chan, id));
294         }
295
296         boost::shared_ptr<const AutomationControl> pan_control (int id, int chan=0) const {
297             return automation_control(Evoral::Parameter (PanAutomation, chan, id));
298         }
299
300   private:
301         /* disallow copy construction */
302         Panner (Panner const &);
303         
304         void distribute_no_automation(BufferSet& src, BufferSet& dest, nframes_t nframes, nframes_t offset, gain_t gain_coeff);
305         std::vector<StreamPanner*> _streampanners;
306         uint32_t     current_outs;
307         bool             _linked;
308         bool             _bypassed;
309         LinkDirection    _link_direction;
310
311         static float current_automation_version_number;
312
313         /* old school automation handling */
314
315         std::string automation_path;
316 };
317
318 } // namespace ARDOUR
319
320 #endif /*__ardour_panner_h__ */