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