Merge branch 'master' into exportvis
[ardour.git] / libs / ardour / ardour / panner.h
1 /*
2     Copyright (C) 2004-2011 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/cartesian.h"
30 #include "pbd/signals.h"
31 #include "pbd/stateful.h"
32
33 #include "ardour/libardour_visibility.h"
34 #include "ardour/types.h"
35 #include "ardour/automation_control.h"
36 #include "ardour/automatable.h"
37
38 namespace ARDOUR {
39
40 class Session;
41 class Pannable;
42 class BufferSet;
43 class AudioBuffer;
44 class Speakers;
45
46 class LIBARDOUR_API Panner : public PBD::Stateful, public PBD::ScopedConnectionList
47 {
48 public:
49         Panner (boost::shared_ptr<Pannable>);
50         ~Panner ();
51
52         virtual boost::shared_ptr<Speakers> get_speakers() const { return boost::shared_ptr<Speakers>(); }
53
54         virtual ChanCount in() const = 0;
55         virtual ChanCount out() const = 0;
56
57         virtual void configure_io (ARDOUR::ChanCount /*in*/, ARDOUR::ChanCount /*out*/) {}
58
59         /* derived implementations of these methods must indicate
60            whether it is legal for a Controllable to use the
61            value of the argument (post-call) in a call to
62            Controllable::set_value().
63
64            they have a choice of:
65
66            * return true, leave argument unchanged
67            * return true, modify argument
68            * return false
69         */
70
71         virtual bool clamp_position (double&) { return true; }
72         virtual bool clamp_width (double&) { return true; }
73         virtual bool clamp_elevation (double&) { return true; }
74
75         virtual std::pair<double, double> position_range () const { return std::make_pair (-DBL_MAX, DBL_MAX); }
76         virtual std::pair<double, double> width_range () const { return std::make_pair (-DBL_MAX, DBL_MAX); }
77         virtual std::pair<double, double> elevation_range () const { return std::make_pair (-DBL_MAX, DBL_MAX); }
78
79         virtual void set_position (double) { }
80         virtual void set_width (double) { }
81         virtual void set_elevation (double) { }
82
83         virtual double position () const { return 0.0; }
84         virtual double width () const { return 0.0; }
85         virtual double elevation () const { return 0.0; }
86
87         virtual PBD::AngularVector signal_position (uint32_t) const { return PBD::AngularVector(); }
88
89         virtual void reset () = 0;
90
91         void      set_automation_state (AutoState);
92         AutoState automation_state() const;
93         void      set_automation_style (AutoStyle);
94         AutoStyle automation_style() const;
95
96         virtual std::set<Evoral::Parameter> what_can_be_automated() const;
97         virtual std::string describe_parameter (Evoral::Parameter);
98         virtual std::string value_as_string (boost::shared_ptr<AutomationControl>) const;
99
100         bool touching() const;
101
102         static double azimuth_to_lr_fract (double azi) {
103                 /* 180.0 degrees=> left => 0.0 */
104                 /* 0.0 degrees => right => 1.0 */
105
106                 /* humans can only distinguish 1 degree of arc between two positions,
107                    so force azi back to an integral value before computing
108                 */
109
110                 return 1.0 - (rint(azi)/180.0);
111         }
112
113         static double lr_fract_to_azimuth (double fract) {
114                 /* fract = 0.0 => degrees = 180.0 => left */
115                 /* fract = 1.0 => degrees = 0.0 => right */
116
117                 /* humans can only distinguish 1 degree of arc between two positions,
118                    so force azi back to an integral value after computing
119                 */
120
121                 return rint (180.0 - (fract * 180.0));
122         }
123
124         /**
125          *  Pan some input buffers to a number of output buffers.
126          *
127          *  @param ibufs Input buffers (one per panner input)
128          *  @param obufs Output buffers (one per panner output).
129          *  @param gain_coeff fixed, additional gain coefficient to apply to output samples.
130          *  @param nframes Number of frames in the input.
131          *
132          *  Derived panners can choose to implement these if they need to gain more
133          *  control over the panning algorithm.  The default is to call
134          *  distribute_one() or distribute_one_automated() on each input buffer to
135          *  deliver it to each output buffer.
136          *
137          *  If a panner does not need to override this default behaviour, it can
138          *  just implement distribute_one() and distribute_one_automated() (below).
139          */
140         virtual void distribute (BufferSet& ibufs, BufferSet& obufs, gain_t gain_coeff, pframes_t nframes);
141         virtual void distribute_automated (BufferSet& ibufs, BufferSet& obufs,
142                                            framepos_t start, framepos_t end, pframes_t nframes,
143                                            pan_t** buffers);
144
145         int set_state (const XMLNode&, int version);
146         XMLNode& get_state ();
147         
148         boost::shared_ptr<Pannable> pannable() const { return _pannable; }
149
150         static bool equivalent (pan_t a, pan_t b) {
151                 return fabsf (a - b) < 0.002; // about 1 degree of arc for a stereo panner
152         }
153
154         static bool equivalent (const PBD::AngularVector& a, const PBD::AngularVector& b) {
155                 /* XXX azimuth only, at present */
156                 return fabs (a.azi - b.azi) < 1.0;
157         }
158
159         virtual void freeze ();
160         virtual void thaw ();
161
162 protected:
163         boost::shared_ptr<Pannable> _pannable;
164
165         virtual void distribute_one (AudioBuffer&, BufferSet& obufs, gain_t gain_coeff, pframes_t nframes, uint32_t which) = 0;
166         virtual void distribute_one_automated (AudioBuffer&, BufferSet& obufs,
167                                                framepos_t start, framepos_t end, pframes_t nframes,
168                                                pan_t** buffers, uint32_t which) = 0;
169
170         int32_t _frozen;
171 };
172
173 } // namespace
174
175 extern "C" {
176 struct LIBARDOUR_API PanPluginDescriptor {
177         std::string name;
178         int32_t in;
179         int32_t out;
180         ARDOUR::Panner* (*factory)(boost::shared_ptr<ARDOUR::Pannable>, boost::shared_ptr<ARDOUR::Speakers>);
181 };
182 }
183
184 #endif /* __ardour_panner_h__ */