Move Stateful class declared in pbd/stateful.h into the PBD namespace
[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 <vector>
25 #include <string>
26 #include <iostream>
27 #include <sigc++/signal.h>
28
29 #include <pbd/stateful.h> 
30 #include <pbd/controllable.h>
31
32 #include <ardour/types.h>
33 #include <ardour/curve.h>
34
35 using std::istream;
36 using std::ostream;
37
38 namespace ARDOUR {
39
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);
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         /* automation */
73
74         virtual void snapshot (nframes_t now) = 0;
75         virtual void transport_stopped (nframes_t frame) = 0;
76         virtual void set_automation_state (AutoState) = 0;
77         virtual void set_automation_style (AutoStyle) = 0;
78         
79         PBD::Controllable& control()  { return _control; }
80         
81         /* XXX this is wrong. for multi-dimensional panners, there
82            must surely be more than 1 automation curve.
83         */
84
85         virtual Curve& automation() = 0;
86
87         sigc::signal<void> Changed;      /* for position */
88         sigc::signal<void> StateChanged; /* for mute */
89
90         int set_state (const XMLNode&);
91         virtual XMLNode& state (bool full_state) = 0;
92         
93         Panner & get_parent() { return parent; }
94         
95         /* old school automation loading */
96
97         virtual int load (istream&, string path, uint32_t&) = 0;
98
99   protected:
100         friend class Panner;
101         Panner& parent;
102
103         float x;
104         float y;
105         float z;
106         
107         /* these are for automation. they store the last value
108            used by the most recent process() cycle.
109         */
110
111         float effective_x;
112         float effective_y;
113         float effective_z;
114
115         bool             _muted;
116
117         struct PanControllable : public PBD::Controllable {
118             PanControllable (std::string name, StreamPanner& p) : Controllable (name), panner (p) {}
119             
120             StreamPanner& panner;
121             
122             void set_value (float);
123             float get_value (void) const;
124             bool can_send_feedback() const;
125         };
126
127         PanControllable  _control;
128
129         void add_state (XMLNode&);
130         virtual void update () = 0;
131 };
132
133 class BaseStereoPanner : public StreamPanner
134 {
135   public:
136         BaseStereoPanner (Panner&);
137         ~BaseStereoPanner ();
138
139         /* this class just leaves the pan law itself to be defined
140            by the update(), distribute_automated() 
141            methods. derived classes also need a factory method
142            and a type name. See EqualPowerStereoPanner as an example.
143         */
144
145         void distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes);
146
147         void snapshot (nframes_t now);
148         void transport_stopped (nframes_t frame);
149         void set_automation_state (AutoState);
150         void set_automation_style (AutoStyle);
151
152         Curve& automation() { return _automation; }
153
154         /* old school automation loading */
155
156         int load (istream&, string path, uint32_t&);
157
158   protected:
159         float left;
160         float right;
161         float desired_left;
162         float desired_right;
163         float left_interp;
164         float right_interp;
165
166         Curve  _automation;
167 };
168
169 class EqualPowerStereoPanner : public BaseStereoPanner
170 {
171   public:
172         EqualPowerStereoPanner (Panner&);
173         ~EqualPowerStereoPanner ();
174
175         void distribute_automated (AudioBuffer& src, BufferSet& obufs, 
176                              nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers);
177
178         void get_current_coefficients (pan_t*) const;
179         void get_desired_coefficients (pan_t*) const;
180
181         static StreamPanner* factory (Panner&);
182         static string name;
183
184         XMLNode& state (bool full_state); 
185         XMLNode& get_state (void); 
186         int      set_state (const XMLNode&);
187
188   private:
189         void update ();
190 };
191
192 class Multi2dPanner : public StreamPanner
193 {
194   public:
195         Multi2dPanner (Panner& parent);
196         ~Multi2dPanner ();
197
198         void snapshot (nframes_t now);
199         void transport_stopped (nframes_t frame);
200         void set_automation_state (AutoState);
201         void set_automation_style (AutoStyle);
202
203         /* XXX this is wrong. for multi-dimensional panners, there
204            must surely be more than 1 automation curve.
205         */
206
207         Curve& automation() { return _automation; }
208
209         void distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes);
210         void distribute_automated (AudioBuffer& src, BufferSet& obufs,
211                                    nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers);
212
213         static StreamPanner* factory (Panner&);
214         static string name;
215
216         XMLNode& state (bool full_state); 
217         XMLNode& get_state (void);
218         int set_state (const XMLNode&);
219
220         /* old school automation loading */
221
222         int load (istream&, string path, uint32_t&);
223
224   private:
225         Curve _automation;
226         void update ();
227 };
228
229 class Panner : public std::vector<StreamPanner*>, public PBD::Stateful, public sigc::trackable
230 {
231   public:
232         struct Output {
233             float x;
234             float y;
235             pan_t current_pan;
236             pan_t desired_pan;
237
238             Output (float xp, float yp) 
239                     : x (xp), y (yp), current_pan (0.0f), desired_pan (0.f) {}
240                     
241         };
242
243         Panner (string name, Session&);
244         virtual ~Panner ();
245
246         /// The fundamental Panner function
247         void distribute(BufferSet& src, BufferSet& dest, nframes_t start_frame, nframes_t end_frames, nframes_t nframes, nframes_t offset);
248
249         bool bypassed() const { return _bypassed; }
250         void set_bypassed (bool yn);
251
252         StreamPanner* add ();
253         void remove (uint32_t which);
254         void clear ();
255         void reset (uint32_t noutputs, uint32_t npans);
256
257         void snapshot (nframes_t now);
258         void transport_stopped (nframes_t frame);
259         
260         void clear_automation ();
261
262         void set_automation_state (AutoState);
263         AutoState automation_state() const;
264         void set_automation_style (AutoStyle);
265         AutoStyle automation_style() const;
266         bool touching() const;
267
268         XMLNode& get_state (void);
269         XMLNode& state (bool full);
270         int      set_state (const XMLNode&);
271
272         sigc::signal<void> Changed;
273         
274         static bool equivalent (pan_t a, pan_t b) {
275                 return fabsf (a - b) < 0.002; // about 1 degree of arc for a stereo panner
276         }
277
278         void move_output (uint32_t, float x, float y);
279         uint32_t nouts() const { return outputs.size(); }
280         Output& output (uint32_t n) { return outputs[n]; }
281
282         std::vector<Output> outputs;
283         Session& session() const { return _session; }
284
285         enum LinkDirection {
286                 SameDirection,
287                 OppositeDirection
288         };
289
290         LinkDirection link_direction() const { return _link_direction; }
291         void set_link_direction (LinkDirection);
292         
293         bool linked() const { return _linked; }
294         void set_linked (bool yn);
295
296         sigc::signal<void> LinkStateChanged;
297         sigc::signal<void> StateChanged; /* for bypass */
298
299         /* only StreamPanner should call these */
300         
301         void set_position (float x, StreamPanner& orig);
302         void set_position (float x, float y, StreamPanner& orig);
303         void set_position (float x, float y, float z, StreamPanner& orig);
304
305         /* old school automation */
306
307         int load ();
308
309   private:
310         void distribute_no_automation(BufferSet& src, BufferSet& dest, nframes_t nframes, nframes_t offset, gain_t gain_coeff);
311
312         Session&         _session;
313         uint32_t     current_outs;
314         bool             _linked;
315         bool             _bypassed;
316         LinkDirection    _link_direction;
317
318         static float current_automation_version_number;
319
320         /* old school automation handling */
321
322         std::string automation_path;
323         void set_name (std::string);
324 };
325
326 } // namespace ARDOUR
327
328 #endif /*__ardour_panner_h__ */