Optimize plugin-processing for non-automated params
[ardour.git] / libs / ardour / ardour / session_event.h
1 /*
2     Copyright (C) 2012 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_session_event_h__
21 #define __ardour_session_event_h__
22
23 #include <list>
24 #include <boost/function.hpp>
25 #include <boost/shared_ptr.hpp>
26
27 #include "pbd/pool.h"
28 #include "pbd/ringbuffer.h"
29 #include "pbd/event_loop.h"
30
31 #include "ardour/libardour_visibility.h"
32 #include "ardour/types.h"
33
34 namespace ARDOUR {
35
36 class TransportMaster;
37 class Region;
38
39 class LIBARDOUR_API SessionEvent {
40 public:
41         enum Type {
42                 SetTransportSpeed,
43                 Locate,
44                 LocateRoll,
45                 LocateRollLocate,
46                 SetLoop,
47                 PunchIn,
48                 PunchOut,
49                 RangeStop,
50                 RangeLocate,
51                 Overwrite,
52                 Audition,
53                 SetPlayAudioRange,
54                 CancelPlayAudioRange,
55                 RealTimeOperation,
56                 AdjustPlaybackBuffering,
57                 AdjustCaptureBuffering,
58                 SetTimecodeTransmission,
59                 Skip,
60                 SetTransportMaster,
61
62                 /* only one of each of these events can be queued at any one time */
63
64                 StopOnce,
65                 AutoLoop,
66         };
67
68         enum Action {
69                 Add,
70                 Remove,
71                 Replace,
72                 Clear
73         };
74
75         Type       type;
76         Action     action;
77         samplepos_t action_sample;
78         samplepos_t target_sample;
79         double     speed;
80
81         union {
82                 void*            ptr;
83                 bool             yes_or_no;
84                 samplepos_t      target2_sample;
85                 Route*           route;
86         };
87
88         union {
89                 bool second_yes_or_no;
90                 double control_value;
91         };
92
93         union {
94                 bool third_yes_or_no;
95         };
96
97         /* 5 members to handle a multi-group event handled in RT context */
98
99         typedef boost::function<void (SessionEvent*)> RTeventCallback;
100
101         boost::shared_ptr<ControlList> controls; /* apply to */
102         boost::shared_ptr<RouteList> routes;     /* apply to */
103         boost::function<void (void)> rt_slot;    /* what to call in RT context */
104         RTeventCallback              rt_return;  /* called after rt_slot, with this event as an argument */
105         PBD::EventLoop*              event_loop;
106
107         std::list<AudioRange> audio_range;
108         std::list<MusicRange> music_range;
109
110         boost::shared_ptr<Region> region;
111         boost::shared_ptr<TransportMaster> transport_master;
112
113         SessionEvent (Type t, Action a, samplepos_t when, samplepos_t where, double spd, bool yn = false, bool yn2 = false, bool yn3 = false);
114
115         void set_ptr (void* p) {
116                 ptr = p;
117         }
118
119         bool before (const SessionEvent& other) const {
120                 return action_sample < other.action_sample;
121         }
122
123         bool after (const SessionEvent& other) const {
124                 return action_sample > other.action_sample;
125         }
126
127         static bool compare (const SessionEvent *e1, const SessionEvent *e2) {
128                 return e1->before (*e2);
129         }
130
131         void* operator new (size_t);
132         void  operator delete (void *ptr, size_t /*size*/);
133
134         static const samplepos_t Immediate = -1;
135
136         static bool has_per_thread_pool ();
137         static void create_per_thread_pool (const std::string& n, uint32_t nitems);
138         static void init_event_pool ();
139
140         CrossThreadPool* event_pool() const { return own_pool; }
141
142 private:
143         static PerThreadPool* pool;
144         CrossThreadPool* own_pool;
145
146         friend class Butler;
147 };
148
149 class SessionEventManager {
150 public:
151         SessionEventManager () : pending_events (2048),
152                                  auto_loop_event(0), punch_out_event(0), punch_in_event(0) {}
153         virtual ~SessionEventManager() {}
154
155         virtual void queue_event (SessionEvent *ev) = 0;
156         void clear_events (SessionEvent::Type type);
157         void clear_events (SessionEvent::Type type, boost::function<void (void)> after);
158
159 protected:
160         PBD::RingBuffer<SessionEvent*> pending_events;
161         typedef std::list<SessionEvent *> Events;
162         Events           events;
163         Events           immediate_events;
164         Events::iterator next_event;
165
166         Glib::Threads::Mutex rb_write_lock;
167
168         /* there can only ever be one of each of these */
169
170         SessionEvent *auto_loop_event;
171         SessionEvent *punch_out_event;
172         SessionEvent *punch_in_event;
173
174         void dump_events () const;
175         void merge_event (SessionEvent*);
176         void replace_event (SessionEvent::Type, samplepos_t action_sample, samplepos_t target = 0);
177         bool _replace_event (SessionEvent*);
178         bool _remove_event (SessionEvent *);
179         void _clear_event_type (SessionEvent::Type);
180
181         void add_event (samplepos_t action_sample, SessionEvent::Type type, samplepos_t target_sample = 0);
182         void remove_event (samplepos_t sample, SessionEvent::Type type);
183
184         virtual void process_event(SessionEvent*) = 0;
185         virtual void set_next_event () = 0;
186 };
187
188 } /* namespace */
189
190 #endif /* __ardour_session_event_h__ */