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