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