dynamic playback & capture buffer resizing (though transport is stopped first)
[ardour.git] / libs / ardour / ardour / session_event.h
1 #ifndef __ardour_session_event_h__
2 #define __ardour_session_event_h__
3
4 #include <list>
5 #include <boost/function.hpp>
6 #include <boost/shared_ptr.hpp>
7
8 #include "pbd/pool.h"
9 #include "pbd/ringbuffer.h"
10 #include "pbd/event_loop.h"
11
12 #include "ardour/types.h"
13
14 namespace ARDOUR {
15
16 class Slave;
17 class Region;
18
19 struct SessionEvent {
20     enum Type {
21             SetTransportSpeed,
22             SetTrackSpeed,
23             Locate,
24             LocateRoll,
25             LocateRollLocate,
26             SetLoop,
27             PunchIn,
28             PunchOut,
29             RangeStop,
30             RangeLocate,
31             Overwrite,
32             SetSyncSource,
33             Audition,
34             InputConfigurationChange,
35             SetPlayAudioRange,
36             RealTimeOperation,
37             AdjustPlaybackBuffering,
38             AdjustCaptureBuffering,
39
40             /* only one of each of these events can be queued at any one time */
41             
42             StopOnce,
43             AutoLoop
44     };
45     
46     enum Action {
47             Add,
48             Remove,
49             Replace,
50             Clear
51     };
52     
53     Type             type;
54     Action           action;
55     nframes64_t      action_frame;
56     nframes64_t      target_frame;
57     double           speed;
58     
59     union {
60         void*        ptr;
61         bool         yes_or_no;
62         nframes64_t  target2_frame;
63         Slave*       slave;
64         Route*       route;
65     };
66     
67     union {
68         bool second_yes_or_no;
69     };
70
71     /* 4 members to handle a multi-group event handled in RT context */
72
73     typedef boost::function<void (SessionEvent*)> RTeventCallback;
74
75     boost::shared_ptr<RouteList> routes;    /* apply to */
76     boost::function<void (void)> rt_slot;   /* what to call in RT context */
77     RTeventCallback              rt_return; /* called after rt_slot, with this event as an argument */
78     PBD::EventLoop*              event_loop;
79
80     std::list<AudioRange> audio_range;
81     std::list<MusicRange> music_range;
82     
83     boost::shared_ptr<Region> region;
84
85     SessionEvent (Type t, Action a, nframes_t when, nframes_t where, double spd, bool yn = false, bool yn2 = false)
86             : type (t)
87             , action (a)
88             , action_frame (when)
89             , target_frame (where)
90             , speed (spd)
91             , yes_or_no (yn)
92             , second_yes_or_no (yn2)
93             , event_loop (0) {}
94
95     void set_ptr (void* p) {
96             ptr = p;
97     }
98     
99     bool before (const SessionEvent& other) const {
100             return action_frame < other.action_frame;
101     }
102     
103     bool after (const SessionEvent& other) const {
104             return action_frame > other.action_frame;
105     }
106     
107     static bool compare (const SessionEvent *e1, const SessionEvent *e2) {
108             return e1->before (*e2);
109     }
110     
111     void* operator new (size_t);
112     void  operator delete (void *ptr, size_t /*size*/);
113     
114     static const nframes_t Immediate = 0;
115     
116     static void create_per_thread_pool (const std::string& n, unsigned long nitems);
117     static void init_event_pool ();
118
119 private:
120     static PerThreadPool* pool;
121     CrossThreadPool* own_pool;
122
123     friend class Butler;
124 };
125
126 class SessionEventManager {
127 public:
128         SessionEventManager () : pending_events (2048),
129                 auto_loop_event(0), punch_out_event(0), punch_in_event(0) {}
130         virtual ~SessionEventManager() {}
131
132         virtual void queue_event (SessionEvent *ev) = 0;
133         void clear_events (SessionEvent::Type type);
134
135 protected:
136         RingBuffer<SessionEvent*> pending_events;
137         typedef std::list<SessionEvent *> Events;
138         Events           events;
139         Events           immediate_events;
140         Events::iterator next_event;
141
142         /* there can only ever be one of each of these */
143
144         SessionEvent *auto_loop_event;
145         SessionEvent *punch_out_event;
146         SessionEvent *punch_in_event;
147
148         void dump_events () const;
149         void merge_event (SessionEvent*);
150         void replace_event (SessionEvent::Type, nframes64_t action_frame, nframes64_t target = 0);
151         bool _replace_event (SessionEvent*);
152         bool _remove_event (SessionEvent *);
153         void _clear_event_type (SessionEvent::Type);
154
155         void add_event (nframes64_t action_frame, SessionEvent::Type type, nframes64_t target_frame = 0);
156         void remove_event (nframes64_t frame, SessionEvent::Type type);
157
158         virtual void process_event(SessionEvent*) = 0;
159         virtual void set_next_event () = 0;
160 };
161
162 } /* namespace */
163
164 #endif /* __ardour_session_event_h__ */