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