806f350e03b915b4fe054531d932c7fa40fea2eb
[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     $Id$
19 */
20
21 #ifndef __ardour_panner_h__
22 #define __ardour_panner_h__
23
24 #include <cmath>
25 #include <vector>
26 #include <string>
27 #include <iostream>
28 #include <sigc++/signal.h>
29
30 #include <midi++/controllable.h>
31
32 #include <ardour/types.h>
33 #include <ardour/stateful.h>
34 #include <ardour/curve.h>
35
36 using std::istream;
37 using std::ostream;
38
39 namespace ARDOUR {
40
41 class Session;
42 class Panner;
43
44 class StreamPanner : public sigc::trackable, public Stateful
45 {
46   public:
47         StreamPanner (Panner& p);
48         ~StreamPanner ();
49
50         void set_muted (bool yn);
51         bool muted() const { return _muted; }
52
53         void set_position (float x, bool link_call = false);
54         void set_position (float x, float y, bool link_call = false);
55         void set_position (float x, float y, float z, bool link_call = false);
56
57         void get_position (float& xpos) const { xpos = x; }
58         void get_position (float& xpos, float& ypos) const { xpos = x; ypos = y; }
59         void get_position (float& xpos, float& ypos, float& zpos) const { xpos = x; ypos = y; zpos = z; }
60
61         void get_effective_position (float& xpos) const { xpos = effective_x; }
62         void get_effective_position (float& xpos, float& ypos) const { xpos = effective_x; ypos = effective_y; }
63         void get_effective_position (float& xpos, float& ypos, float& zpos) const { xpos = effective_x; ypos = effective_y; zpos = effective_z; }
64
65         /* the basic panner API */
66
67         virtual void distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes) = 0;
68         virtual void distribute_automated (Sample* src, Sample** obufs, 
69                                      jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes, pan_t** buffers) = 0;
70
71         /* automation */
72
73         virtual void snapshot (jack_nframes_t now) = 0;
74         virtual void transport_stopped (jack_nframes_t frame) = 0;
75         virtual void set_automation_state (AutoState) = 0;
76         virtual void set_automation_style (AutoStyle) = 0;
77         
78         /* MIDI control */
79
80         struct MIDIControl : public MIDI::Controllable {
81             MIDIControl (StreamPanner&, MIDI::Port *);
82             void set_value (float);
83             void send_feedback (gain_t);
84             MIDI::byte* write_feedback (MIDI::byte* buf, int32_t& bufsize, gain_t val, bool force = false);
85
86             pan_t (*midi_to_pan)(double val);
87             double (*pan_to_midi)(pan_t p);
88
89             StreamPanner& sp;
90             bool setting;
91             gain_t last_written;
92         };
93
94         MIDIControl& midi_control()  { return _midi_control; }
95         void reset_midi_control (MIDI::Port *, bool);
96         
97         /* XXX this is wrong. for multi-dimensional panners, there
98            must surely be more than 1 automation curve.
99         */
100
101         virtual Curve& automation() = 0;
102
103
104         virtual int load (istream&, string path, uint32_t&) = 0;
105
106         virtual int save (ostream&) const = 0;
107
108         sigc::signal<void> Changed;      /* for position */
109         sigc::signal<void> StateChanged; /* for mute */
110
111         int set_state (const XMLNode&);
112         virtual XMLNode& state (bool full_state) = 0;
113
114         Panner & get_parent() { return parent; }
115         
116   protected:
117         friend class Panner;
118         Panner& parent;
119
120         float x;
121         float y;
122         float z;
123         
124         /* these are for automation. they store the last value
125            used by the most recent process() cycle.
126         */
127
128         float effective_x;
129         float effective_y;
130         float effective_z;
131
132         bool             _muted;
133         MIDIControl _midi_control;
134
135         void add_state (XMLNode&);
136         bool get_midi_node_info (XMLNode * node, MIDI::eventType & ev, MIDI::channel_t & chan, MIDI::byte & additional);
137         bool set_midi_node_info (XMLNode * node, MIDI::eventType ev, MIDI::channel_t chan, MIDI::byte additional);
138
139         virtual void update () = 0;
140 };
141
142 class BaseStereoPanner : public StreamPanner
143 {
144   public:
145         BaseStereoPanner (Panner&);
146         ~BaseStereoPanner ();
147
148         /* this class just leaves the pan law itself to be defined
149            by the update(), distribute_automated() 
150            methods. derived classes also need a factory method
151            and a type name. See EqualPowerStereoPanner as an example.
152         */
153
154         void distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes);
155
156         int load (istream&, string path, uint32_t&);
157         int save (ostream&) const;
158         void snapshot (jack_nframes_t now);
159         void transport_stopped (jack_nframes_t frame);
160         void set_automation_state (AutoState);
161         void set_automation_style (AutoStyle);
162
163         Curve& automation() { return _automation; }
164
165   protected:
166         float left;
167         float right;
168         float desired_left;
169         float desired_right;
170         float left_interp;
171         float right_interp;
172
173         Curve  _automation;
174 };
175
176 class EqualPowerStereoPanner : public BaseStereoPanner
177 {
178   public:
179         EqualPowerStereoPanner (Panner&);
180         ~EqualPowerStereoPanner ();
181
182         void distribute_automated (Sample* src, Sample** obufs, 
183                              jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes, pan_t** buffers);
184
185         void get_current_coefficients (pan_t*) const;
186         void get_desired_coefficients (pan_t*) const;
187
188         static StreamPanner* factory (Panner&);
189         static string name;
190
191         XMLNode& state (bool full_state); 
192         XMLNode& get_state (void); 
193         int      set_state (const XMLNode&);
194
195   private:
196         void update ();
197 };
198
199 class Multi2dPanner : public StreamPanner
200 {
201   public:
202         Multi2dPanner (Panner& parent);
203         ~Multi2dPanner ();
204
205         void snapshot (jack_nframes_t now);
206         void transport_stopped (jack_nframes_t frame);
207         void set_automation_state (AutoState);
208         void set_automation_style (AutoStyle);
209
210         /* XXX this is wrong. for multi-dimensional panners, there
211            must surely be more than 1 automation curve.
212         */
213
214         Curve& automation() { return _automation; }
215
216         void distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes);
217         void distribute_automated (Sample* src, Sample** obufs, 
218                              jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes, pan_t** buffers);
219
220         int load (istream&, string path, uint32_t&);
221         int save (ostream&) const;
222
223         static StreamPanner* factory (Panner&);
224         static string name;
225
226         XMLNode& state (bool full_state); 
227         XMLNode& get_state (void);
228         int set_state (const XMLNode&);
229
230   private:
231         Curve _automation;
232         void update ();
233 };
234
235 class Panner : public std::vector<StreamPanner*>, public Stateful, public sigc::trackable
236 {
237   public:
238         struct Output {
239             float x;
240             float y;
241             pan_t current_pan;
242             pan_t desired_pan;
243
244             Output (float xp, float yp) 
245                     : x (xp), y (yp), current_pan (0.0f), desired_pan (0.f) {}
246                     
247         };
248
249         Panner (string name, Session&);
250         virtual ~Panner ();
251
252         void set_name (string);
253
254         bool bypassed() const { return _bypassed; }
255         void set_bypassed (bool yn);
256
257         StreamPanner* add ();
258         void remove (uint32_t which);
259         void clear ();
260         void reset (uint32_t noutputs, uint32_t npans);
261
262         void snapshot (jack_nframes_t now);
263         void transport_stopped (jack_nframes_t frame);
264         
265         void clear_automation ();
266
267         void set_automation_state (AutoState);
268         AutoState automation_state() const;
269         void set_automation_style (AutoStyle);
270         AutoStyle automation_style() const;
271         bool touching() const;
272
273         int load ();
274         int save () const;
275
276         XMLNode& get_state (void);
277         XMLNode& state (bool full);
278         int      set_state (const XMLNode&);
279
280         sigc::signal<void> Changed;
281         
282         static bool equivalent (pan_t a, pan_t b) {
283                 return fabsf (a - b) < 0.002; // about 1 degree of arc for a stereo panner
284         }
285
286         void move_output (uint32_t, float x, float y);
287         uint32_t nouts() const { return outputs.size(); }
288         Output& output (uint32_t n) { return outputs[n]; }
289
290         std::vector<Output> outputs;
291         Session& session() const { return _session; }
292
293         void reset_midi_control (MIDI::Port *, bool);
294         void send_all_midi_feedback ();
295         MIDI::byte* write_midi_feedback (MIDI::byte*, int32_t& bufsize);
296
297         enum LinkDirection {
298                 SameDirection,
299                 OppositeDirection
300         };
301
302         LinkDirection link_direction() const { return _link_direction; }
303         void set_link_direction (LinkDirection);
304         
305         bool linked() const { return _linked; }
306         void set_linked (bool yn);
307
308         sigc::signal<void> LinkStateChanged;
309         sigc::signal<void> StateChanged; /* for bypass */
310
311         /* only StreamPanner should call these */
312         
313         void set_position (float x, StreamPanner& orig);
314         void set_position (float x, float y, StreamPanner& orig);
315         void set_position (float x, float y, float z, StreamPanner& orig);
316         
317   private:
318
319         string            automation_path;
320         Session&         _session;
321         uint32_t     current_outs;
322         bool             _linked;
323         bool             _bypassed;
324         LinkDirection    _link_direction;
325
326         static float current_automation_version_number;
327 };
328
329 }; /* namespace ARDOUR */
330
331 #endif /*__ardour_panner_h__ */