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