Further automation refactoring - bring in the concept of Controllable, work towards
[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         boost::shared_ptr<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         /* TODO: Panner is-a Automation solves this */
85
86         virtual boost::shared_ptr<AutomationList> 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         boost::shared_ptr<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         /* TODO: StreamPanner is-a Automatable? */
154         boost::shared_ptr<AutomationList> automation() { return _automation; }
155
156         /* old school automation loading */
157
158         int load (istream&, string path, uint32_t&);
159
160   protected:
161         float left;
162         float right;
163         float desired_left;
164         float desired_right;
165         float left_interp;
166         float right_interp;
167
168         boost::shared_ptr<AutomationList> _automation;
169 };
170
171 class EqualPowerStereoPanner : public BaseStereoPanner
172 {
173   public:
174         EqualPowerStereoPanner (Panner&);
175         ~EqualPowerStereoPanner ();
176
177         void distribute_automated (AudioBuffer& src, BufferSet& obufs, 
178                              nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers);
179
180         void get_current_coefficients (pan_t*) const;
181         void get_desired_coefficients (pan_t*) const;
182
183         static StreamPanner* factory (Panner&);
184         static string name;
185
186         XMLNode& state (bool full_state); 
187         XMLNode& get_state (void); 
188         int      set_state (const XMLNode&);
189
190   private:
191         void update ();
192 };
193
194 class Multi2dPanner : public StreamPanner
195 {
196   public:
197         Multi2dPanner (Panner& parent);
198         ~Multi2dPanner ();
199
200         void snapshot (nframes_t now);
201         void transport_stopped (nframes_t frame);
202         void set_automation_state (AutoState);
203         void set_automation_style (AutoStyle);
204
205         /* XXX this is wrong. for multi-dimensional panners, there
206            must surely be more than 1 automation curve.
207         */
208         
209         /* TODO: StreamPanner is-a Automatable? */
210
211         boost::shared_ptr<AutomationList> automation() { return _automation; }
212
213         void distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes);
214         void distribute_automated (AudioBuffer& src, BufferSet& obufs,
215                                    nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers);
216
217         static StreamPanner* factory (Panner&);
218         static string name;
219
220         XMLNode& state (bool full_state); 
221         XMLNode& get_state (void);
222         int set_state (const XMLNode&);
223
224         /* old school automation loading */
225
226         int load (istream&, string path, uint32_t&);
227
228   private:
229         boost::shared_ptr<AutomationList> _automation;
230         void update ();
231 };
232
233 class Panner : public std::vector<StreamPanner*>, public PBD::Stateful, public sigc::trackable
234 {
235   public:
236         struct Output {
237             float x;
238             float y;
239             pan_t current_pan;
240             pan_t desired_pan;
241
242             Output (float xp, float yp) 
243                     : x (xp), y (yp), current_pan (0.0f), desired_pan (0.f) {}
244                     
245         };
246
247         Panner (string name, Session&);
248         virtual ~Panner ();
249
250         /// The fundamental Panner function
251         void distribute(BufferSet& src, BufferSet& dest, nframes_t start_frame, nframes_t end_frames, nframes_t nframes, nframes_t offset);
252
253         bool bypassed() const { return _bypassed; }
254         void set_bypassed (bool yn);
255
256         StreamPanner* add ();
257         void remove (uint32_t which);
258         void clear ();
259         void reset (uint32_t noutputs, uint32_t npans);
260
261         void snapshot (nframes_t now);
262         void transport_stopped (nframes_t frame);
263         
264         void clear_automation ();
265
266         void set_automation_state (AutoState);
267         AutoState automation_state() const;
268         void set_automation_style (AutoStyle);
269         AutoStyle automation_style() const;
270         bool touching() const;
271
272         XMLNode& get_state (void);
273         XMLNode& state (bool full);
274         int      set_state (const XMLNode&);
275
276         sigc::signal<void> Changed;
277         
278         static bool equivalent (pan_t a, pan_t b) {
279                 return fabsf (a - b) < 0.002; // about 1 degree of arc for a stereo panner
280         }
281
282         void move_output (uint32_t, float x, float y);
283         uint32_t nouts() const { return outputs.size(); }
284         Output& output (uint32_t n) { return outputs[n]; }
285
286         std::vector<Output> outputs;
287         Session& session() const { return _session; }
288
289         enum LinkDirection {
290                 SameDirection,
291                 OppositeDirection
292         };
293
294         LinkDirection link_direction() const { return _link_direction; }
295         void set_link_direction (LinkDirection);
296         
297         bool linked() const { return _linked; }
298         void set_linked (bool yn);
299
300         sigc::signal<void> LinkStateChanged;
301         sigc::signal<void> StateChanged; /* for bypass */
302
303         /* only StreamPanner should call these */
304         
305         void set_position (float x, StreamPanner& orig);
306         void set_position (float x, float y, StreamPanner& orig);
307         void set_position (float x, float y, float z, StreamPanner& orig);
308
309         /* old school automation */
310
311         int load ();
312
313   private:
314         void distribute_no_automation(BufferSet& src, BufferSet& dest, nframes_t nframes, nframes_t offset, gain_t gain_coeff);
315
316         Session&         _session;
317         uint32_t     current_outs;
318         bool             _linked;
319         bool             _bypassed;
320         LinkDirection    _link_direction;
321
322         static float current_automation_version_number;
323
324         /* old school automation handling */
325
326         std::string automation_path;
327         void set_name (std::string);
328 };
329
330 } // namespace ARDOUR
331
332 #endif /*__ardour_panner_h__ */