switch to using boost::signals2 instead of sigc++, at least for libardour. not finish...
[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
29 #include "pbd/stateful.h"
30 #include "pbd/controllable.h"
31
32 #include "ardour/types.h"
33 #include "ardour/automation_control.h"
34 #include "ardour/processor.h"
35
36 namespace ARDOUR {
37
38 class Session;
39 class Panner;
40 class BufferSet;
41 class AudioBuffer;
42
43 class StreamPanner : public PBD::Stateful
44 {
45   public:
46         StreamPanner (Panner& p, Evoral::Parameter param);
47         ~StreamPanner ();
48
49         void set_muted (bool yn);
50         bool muted() const { return _muted; }
51         void set_mono (bool);
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         void distribute (AudioBuffer &, BufferSet &, gain_t, nframes_t);
66         void distribute_automated (AudioBuffer &, BufferSet &, nframes_t, nframes_t, nframes_t, pan_t **);
67
68         /* the basic StreamPanner API */
69
70         /**
71          *  Pan some input samples to a number of output buffers.
72          *
73          *  @param src Input buffer.
74          *  @param obufs Output buffers (one per panner output).
75          *  @param gain_coeff Gain coefficient to apply to output samples.
76          *  @param nframes Numbner of frames in the input.
77          */
78         virtual void do_distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes) = 0;
79         virtual void do_distribute_automated (AudioBuffer& src, BufferSet& obufs,
80                                               nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers) = 0;
81
82         boost::shared_ptr<AutomationControl> pan_control()  { return _control; }
83
84         boost::signals2::signal<void()> Changed;      /* for position */
85         boost::signals2::signal<void()> StateChanged; /* for mute */
86
87         int set_state (const XMLNode&, int version);
88         virtual XMLNode& state (bool full_state) = 0;
89
90         Panner & get_parent() { return parent; }
91
92         /* old school automation loading */
93
94         virtual int load (std::istream&, std::string path, uint32_t&) = 0;
95
96   protected:
97         friend class Panner;
98         Panner& parent;
99
100         float x;
101         float y;
102         float z;
103
104         /* these are for automation. they store the last value
105            used by the most recent process() cycle.
106         */
107
108         float effective_x;
109         float effective_y;
110         float effective_z;
111
112         bool _muted;
113         bool _mono;
114
115         boost::shared_ptr<AutomationControl> _control;
116
117         void add_state (XMLNode&);
118         virtual void update () = 0;
119 };
120
121 class BaseStereoPanner : public StreamPanner
122 {
123   public:
124         BaseStereoPanner (Panner&, Evoral::Parameter param);
125         ~BaseStereoPanner ();
126
127         /* this class just leaves the pan law itself to be defined
128            by the update(), do_distribute_automated()
129            methods. derived classes also need a factory method
130            and a type name. See EqualPowerStereoPanner as an example.
131         */
132
133         void do_distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes);
134
135         /* old school automation loading */
136
137         int load (std::istream&, std::string path, uint32_t&);
138
139   protected:
140         float left;
141         float right;
142         float desired_left;
143         float desired_right;
144         float left_interp;
145         float right_interp;
146 };
147
148 class EqualPowerStereoPanner : public BaseStereoPanner
149 {
150   public:
151         EqualPowerStereoPanner (Panner&, Evoral::Parameter param);
152         ~EqualPowerStereoPanner ();
153
154         void do_distribute_automated (AudioBuffer& src, BufferSet& obufs,
155                                       nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers);
156
157         void get_current_coefficients (pan_t*) const;
158         void get_desired_coefficients (pan_t*) const;
159
160         static StreamPanner* factory (Panner&, Evoral::Parameter param);
161         static std::string name;
162
163         XMLNode& state (bool full_state); 
164         XMLNode& get_state (void); 
165         int      set_state (const XMLNode&, int version);
166
167   private:
168         void update ();
169 };
170
171 class Multi2dPanner : public StreamPanner
172 {
173   public:
174         Multi2dPanner (Panner& parent, Evoral::Parameter);
175         ~Multi2dPanner ();
176
177         void do_distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes);
178         void do_distribute_automated (AudioBuffer& src, BufferSet& obufs,
179                                       nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers);
180
181         static StreamPanner* factory (Panner&, Evoral::Parameter);
182         static std::string name;
183
184         XMLNode& state (bool full_state);
185         XMLNode& get_state (void);
186         int set_state (const XMLNode&, int version);
187
188         /* old school automation loading */
189
190         int load (std::istream&, std::string path, uint32_t&);
191
192   private:
193         void update ();
194 };
195
196
197 ///< Class to pan from some number of inputs to some number of outputs
198
199 class Panner : public SessionObject, public AutomatableControls
200 {
201 public:
202         struct Output {
203                 float x;
204                 float y;
205                 pan_t current_pan;
206                 pan_t desired_pan;
207
208                 Output (float xp, float yp)
209                         : x (xp), y (yp), current_pan (0.0f), desired_pan (0.f) {}
210
211         };
212
213         //Panner (std::string name, Session&, int _num_bufs);
214         Panner (std::string name, Session&);
215         virtual ~Panner ();
216
217         void clear_panners ();
218         bool empty() const { return _streampanners.empty(); }
219
220         void set_automation_state (AutoState);
221         AutoState automation_state() const;
222         void set_automation_style (AutoStyle);
223         AutoStyle automation_style() const;
224         bool touching() const;
225
226         bool can_support_io_configuration (const ChanCount& /*in*/, ChanCount& /*out*/) const { return true; };
227
228         /// The fundamental Panner function
229         void run (BufferSet& src, BufferSet& dest, sframes_t start_frame, sframes_t end_frames, nframes_t nframes);
230
231         //void* get_inline_gui() const = 0;
232         //void* get_full_gui() const = 0;
233
234         bool bypassed() const { return _bypassed; }
235         void set_bypassed (bool yn);
236         bool mono () const { return _mono; }
237         void set_mono (bool);
238
239         StreamPanner* add ();
240         void remove (uint32_t which);
241         void reset (uint32_t noutputs, uint32_t npans);
242         void reset_streampanner (uint32_t which_panner);
243         void reset_to_default ();
244
245         XMLNode& get_state (void);
246         XMLNode& state (bool full);
247         int      set_state (const XMLNode&, int version);
248
249         static bool equivalent (pan_t a, pan_t b) {
250                 return fabsf (a - b) < 0.002; // about 1 degree of arc for a stereo panner
251         }
252
253         void move_output (uint32_t, float x, float y);
254         uint32_t nouts() const { return outputs.size(); }
255         Output& output (uint32_t n) { return outputs[n]; }
256
257         std::vector<Output> outputs;
258
259         enum LinkDirection {
260                 SameDirection,
261                 OppositeDirection
262         };
263
264         LinkDirection link_direction() const { return _link_direction; }
265         void set_link_direction (LinkDirection);
266
267         bool linked() const { return _linked; }
268         void set_linked (bool yn);
269
270         StreamPanner &streampanner( uint32_t n ) const { assert( n < _streampanners.size() ); return *_streampanners[n]; }
271         uint32_t npanners() const { return _streampanners.size(); }
272
273         boost::signals2::signal<void()> Changed;
274         boost::signals2::signal<void()> LinkStateChanged;
275         boost::signals2::signal<void()> StateChanged; /* for bypass */
276
277         /* only StreamPanner should call these */
278
279         void set_position (float x, StreamPanner& orig);
280         void set_position (float x, float y, StreamPanner& orig);
281         void set_position (float x, float y, float z, StreamPanner& orig);
282
283         /* old school automation */
284
285         int load ();
286
287         struct PanControllable : public AutomationControl {
288                 PanControllable (Session& s, std::string name, Panner& p, Evoral::Parameter param)
289                         : AutomationControl (s, param,
290                                         boost::shared_ptr<AutomationList>(new AutomationList(param)), name)
291                         , panner (p)
292                 { assert(param.type() != NullAutomation); }
293
294                 AutomationList* alist() { return (AutomationList*)_list.get(); }
295                 Panner& panner;
296
297                 void set_value (float);
298                 float get_value (void) const;
299         };
300
301         boost::shared_ptr<AutomationControl> pan_control (int id, int chan=0) {
302                 return automation_control(Evoral::Parameter (PanAutomation, chan, id));
303         }
304
305         boost::shared_ptr<const AutomationControl> pan_control (int id, int chan=0) const {
306                 return automation_control(Evoral::Parameter (PanAutomation, chan, id));
307         }
308
309   private:
310         /* disallow copy construction */
311         Panner (Panner const &);
312
313         void distribute_no_automation(BufferSet& src, BufferSet& dest, nframes_t nframes, gain_t gain_coeff);
314         std::vector<StreamPanner*> _streampanners; ///< one StreamPanner per input
315         uint32_t     current_outs;
316         bool             _linked;
317         bool             _bypassed;
318         bool             _mono;
319         LinkDirection    _link_direction;
320
321         static float current_automation_version_number;
322
323         /* old school automation handling */
324
325         std::string automation_path;
326 };
327
328 } // namespace ARDOUR
329
330 #endif /*__ardour_panner_h__ */