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