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