fixes for destructive track offsets of various kinds; move from jack_nframes_t -...
[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         virtual int load (istream&, string path, uint32_t&) = 0;
87
88         virtual int save (ostream&) const = 0;
89
90         sigc::signal<void> Changed;      /* for position */
91         sigc::signal<void> StateChanged; /* for mute */
92
93         int set_state (const XMLNode&);
94         virtual XMLNode& state (bool full_state) = 0;
95
96         Panner & get_parent() { return parent; }
97         
98   protected:
99         friend class Panner;
100         Panner& parent;
101
102         float x;
103         float y;
104         float z;
105         
106         /* these are for automation. they store the last value
107            used by the most recent process() cycle.
108         */
109
110         float effective_x;
111         float effective_y;
112         float effective_z;
113
114         bool             _muted;
115
116         struct PanControllable : public PBD::Controllable {
117             PanControllable (StreamPanner& p) : panner (p) {}
118             
119             StreamPanner& panner;
120             
121             void set_value (float);
122             float get_value (void) const;
123             bool can_send_feedback() const;
124         };
125
126         PanControllable  _control;
127
128         void add_state (XMLNode&);
129         virtual void update () = 0;
130 };
131
132 class BaseStereoPanner : public StreamPanner
133 {
134   public:
135         BaseStereoPanner (Panner&);
136         ~BaseStereoPanner ();
137
138         /* this class just leaves the pan law itself to be defined
139            by the update(), distribute_automated() 
140            methods. derived classes also need a factory method
141            and a type name. See EqualPowerStereoPanner as an example.
142         */
143
144         void distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nframes_t nframes);
145
146         int load (istream&, string path, uint32_t&);
147         int save (ostream&) const;
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   protected:
156         float left;
157         float right;
158         float desired_left;
159         float desired_right;
160         float left_interp;
161         float right_interp;
162
163         Curve  _automation;
164 };
165
166 class EqualPowerStereoPanner : public BaseStereoPanner
167 {
168   public:
169         EqualPowerStereoPanner (Panner&);
170         ~EqualPowerStereoPanner ();
171
172         void distribute_automated (Sample* src, Sample** obufs, 
173                              nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers);
174
175         void get_current_coefficients (pan_t*) const;
176         void get_desired_coefficients (pan_t*) const;
177
178         static StreamPanner* factory (Panner&);
179         static string name;
180
181         XMLNode& state (bool full_state); 
182         XMLNode& get_state (void); 
183         int      set_state (const XMLNode&);
184
185   private:
186         void update ();
187 };
188
189 class Multi2dPanner : public StreamPanner
190 {
191   public:
192         Multi2dPanner (Panner& parent);
193         ~Multi2dPanner ();
194
195         void snapshot (nframes_t now);
196         void transport_stopped (nframes_t frame);
197         void set_automation_state (AutoState);
198         void set_automation_style (AutoStyle);
199
200         /* XXX this is wrong. for multi-dimensional panners, there
201            must surely be more than 1 automation curve.
202         */
203
204         Curve& automation() { return _automation; }
205
206         void distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nframes_t nframes);
207         void distribute_automated (Sample* src, Sample** obufs, 
208                              nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers);
209
210         int load (istream&, string path, uint32_t&);
211         int save (ostream&) const;
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   private:
221         Curve _automation;
222         void update ();
223 };
224
225 class Panner : public std::vector<StreamPanner*>, public Stateful, public sigc::trackable
226 {
227   public:
228         struct Output {
229             float x;
230             float y;
231             pan_t current_pan;
232             pan_t desired_pan;
233
234             Output (float xp, float yp) 
235                     : x (xp), y (yp), current_pan (0.0f), desired_pan (0.f) {}
236                     
237         };
238
239         Panner (string name, Session&);
240         virtual ~Panner ();
241
242         void set_name (string);
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         int load ();
264         int save () const;
265
266         XMLNode& get_state (void);
267         XMLNode& state (bool full);
268         int      set_state (const XMLNode&);
269
270         sigc::signal<void> Changed;
271         
272         static bool equivalent (pan_t a, pan_t b) {
273                 return fabsf (a - b) < 0.002; // about 1 degree of arc for a stereo panner
274         }
275
276         void move_output (uint32_t, float x, float y);
277         uint32_t nouts() const { return outputs.size(); }
278         Output& output (uint32_t n) { return outputs[n]; }
279
280         std::vector<Output> outputs;
281         Session& session() const { return _session; }
282
283         enum LinkDirection {
284                 SameDirection,
285                 OppositeDirection
286         };
287
288         LinkDirection link_direction() const { return _link_direction; }
289         void set_link_direction (LinkDirection);
290         
291         bool linked() const { return _linked; }
292         void set_linked (bool yn);
293
294         sigc::signal<void> LinkStateChanged;
295         sigc::signal<void> StateChanged; /* for bypass */
296
297         /* only StreamPanner should call these */
298         
299         void set_position (float x, StreamPanner& orig);
300         void set_position (float x, float y, StreamPanner& orig);
301         void set_position (float x, float y, float z, StreamPanner& orig);
302         
303   private:
304
305         string            automation_path;
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
315 } // namespace ARDOUR
316
317 #endif /*__ardour_panner_h__ */