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