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