Reading of MIDI CC from MIDI regions (MidiModel). UI still needs work though..
[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 #include <sigc++/signal.h>
29
30 #include <pbd/stateful.h> 
31 #include <pbd/controllable.h>
32
33 #include <ardour/types.h>
34 #include <ardour/curve.h>
35 #include <ardour/automation_control.h>
36
37 using std::istream;
38 using std::ostream;
39
40 namespace ARDOUR {
41
42 class Session;
43 class Panner;
44 class BufferSet;
45 class AudioBuffer;
46
47 class StreamPanner : public sigc::trackable, public PBD::Stateful
48 {
49   public:
50         StreamPanner (Panner& p, Parameter param);
51         ~StreamPanner ();
52
53         void set_muted (bool yn);
54         bool muted() const { return _muted; }
55
56         void set_position (float x, bool link_call = false);
57         void set_position (float x, float y, bool link_call = false);
58         void set_position (float x, float y, float z, bool link_call = false);
59
60         void get_position (float& xpos) const { xpos = x; }
61         void get_position (float& xpos, float& ypos) const { xpos = x; ypos = y; }
62         void get_position (float& xpos, float& ypos, float& zpos) const { xpos = x; ypos = y; zpos = z; }
63
64         void get_effective_position (float& xpos) const { xpos = effective_x; }
65         void get_effective_position (float& xpos, float& ypos) const { xpos = effective_x; ypos = effective_y; }
66         void get_effective_position (float& xpos, float& ypos, float& zpos) const { xpos = effective_x; ypos = effective_y; zpos = effective_z; }
67
68         /* the basic StreamPanner API */
69
70         virtual void distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes) = 0;
71         virtual void distribute_automated (AudioBuffer& src, BufferSet& obufs,
72                                      nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers) = 0;
73
74         boost::shared_ptr<AutomationControl> pan_control()  { return _control; }
75         
76         sigc::signal<void> Changed;      /* for position */
77         sigc::signal<void> StateChanged; /* for mute */
78
79         int set_state (const XMLNode&);
80         virtual XMLNode& state (bool full_state) = 0;
81         
82         Panner & get_parent() { return parent; }
83
84         /* old school automation loading */
85
86         virtual int load (istream&, string path, uint32_t&) = 0;
87
88   protected:
89         friend class Panner;
90         Panner& parent;
91
92         float x;
93         float y;
94         float z;
95         
96         /* these are for automation. they store the last value
97            used by the most recent process() cycle.
98         */
99
100         float effective_x;
101         float effective_y;
102         float effective_z;
103
104         bool             _muted;
105
106         struct PanControllable : public AutomationControl {
107             PanControllable (Session& s, std::string name, StreamPanner& p, Parameter param)
108                         : AutomationControl (s, boost::shared_ptr<AutomationList>(new AutomationList(
109                                         param, 0.0, 1.0, 0.5)), name)
110                         , panner (p) { assert(param.type() != NullAutomation); }
111             
112             StreamPanner& panner;
113             
114             void set_value (float);
115             float get_value (void) const;
116             bool can_send_feedback() const;
117         };
118
119         boost::shared_ptr<PanControllable> _control;
120
121         void add_state (XMLNode&);
122         virtual void update () = 0;
123 };
124
125 class BaseStereoPanner : public StreamPanner
126 {
127   public:
128         BaseStereoPanner (Panner&, Parameter param);
129         ~BaseStereoPanner ();
130
131         /* this class just leaves the pan law itself to be defined
132            by the update(), distribute_automated() 
133            methods. derived classes also need a factory method
134            and a type name. See EqualPowerStereoPanner as an example.
135         */
136
137         void distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes);
138
139         /* old school automation loading */
140
141         int load (istream&, string path, uint32_t&);
142
143   protected:
144         float left;
145         float right;
146         float desired_left;
147         float desired_right;
148         float left_interp;
149         float right_interp;
150 };
151
152 class EqualPowerStereoPanner : public BaseStereoPanner
153 {
154   public:
155         EqualPowerStereoPanner (Panner&, Parameter param);
156         ~EqualPowerStereoPanner ();
157
158         void distribute_automated (AudioBuffer& src, BufferSet& obufs, 
159                              nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers);
160
161         void get_current_coefficients (pan_t*) const;
162         void get_desired_coefficients (pan_t*) const;
163
164         static StreamPanner* factory (Panner&, Parameter param);
165         static string name;
166
167         XMLNode& state (bool full_state); 
168         XMLNode& get_state (void); 
169         int      set_state (const XMLNode&);
170
171   private:
172         void update ();
173 };
174
175 class Multi2dPanner : public StreamPanner
176 {
177   public:
178         Multi2dPanner (Panner& parent, Parameter);
179         ~Multi2dPanner ();
180
181         void distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes);
182         void distribute_automated (AudioBuffer& src, BufferSet& obufs,
183                                    nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers);
184
185         static StreamPanner* factory (Panner&, Parameter);
186         static string name;
187
188         XMLNode& state (bool full_state); 
189         XMLNode& get_state (void);
190         int set_state (const XMLNode&);
191
192         /* old school automation loading */
193
194         int load (istream&, string path, uint32_t&);
195
196   private:
197         void update ();
198 };
199
200 class Panner : public std::vector<StreamPanner*>, public PBD::Stateful, public sigc::trackable
201 {
202   public:
203         struct Output {
204             float x;
205             float y;
206             pan_t current_pan;
207             pan_t desired_pan;
208
209             Output (float xp, float yp) 
210                     : x (xp), y (yp), current_pan (0.0f), desired_pan (0.f) {}
211                     
212         };
213
214         Panner (string name, Session&);
215         virtual ~Panner ();
216
217         /// The fundamental Panner function
218         void distribute(BufferSet& src, BufferSet& dest, nframes_t start_frame, nframes_t end_frames, nframes_t nframes, nframes_t offset);
219
220         bool bypassed() const { return _bypassed; }
221         void set_bypassed (bool yn);
222
223         StreamPanner* add ();
224         void remove (uint32_t which);
225         void clear ();
226         void reset (uint32_t noutputs, uint32_t npans);
227
228         void snapshot (nframes_t now);
229         void transport_stopped (nframes_t frame);
230         
231         void clear_automation ();
232
233         void set_automation_state (AutoState);
234         AutoState automation_state() const;
235         void set_automation_style (AutoStyle);
236         AutoStyle automation_style() const;
237         bool touching() const;
238
239         XMLNode& get_state (void);
240         XMLNode& state (bool full);
241         int      set_state (const XMLNode&);
242
243         sigc::signal<void> Changed;
244         
245         static bool equivalent (pan_t a, pan_t b) {
246                 return fabsf (a - b) < 0.002; // about 1 degree of arc for a stereo panner
247         }
248
249         void move_output (uint32_t, float x, float y);
250         uint32_t nouts() const { return outputs.size(); }
251         Output& output (uint32_t n) { return outputs[n]; }
252
253         std::vector<Output> outputs;
254         Session& session() const { return _session; }
255
256         enum LinkDirection {
257                 SameDirection,
258                 OppositeDirection
259         };
260
261         LinkDirection link_direction() const { return _link_direction; }
262         void set_link_direction (LinkDirection);
263         
264         bool linked() const { return _linked; }
265         void set_linked (bool yn);
266
267         sigc::signal<void> LinkStateChanged;
268         sigc::signal<void> StateChanged; /* for bypass */
269
270         /* only StreamPanner should call these */
271         
272         void set_position (float x, StreamPanner& orig);
273         void set_position (float x, float y, StreamPanner& orig);
274         void set_position (float x, float y, float z, StreamPanner& orig);
275
276         /* old school automation */
277
278         int load ();
279
280   private:
281         void distribute_no_automation(BufferSet& src, BufferSet& dest, nframes_t nframes, nframes_t offset, gain_t gain_coeff);
282
283         Session&         _session;
284         uint32_t     current_outs;
285         bool             _linked;
286         bool             _bypassed;
287         LinkDirection    _link_direction;
288
289         static float current_automation_version_number;
290
291         /* old school automation handling */
292
293         std::string automation_path;
294         void set_name (std::string);
295 };
296
297 } // namespace ARDOUR
298
299 #endif /*__ardour_panner_h__ */