Add session MIDI bundles to enable connection of MIDI tracks from the right-click...
[ardour.git] / libs / ardour / ardour / audioengine.h
1 /*
2     Copyright (C) 2002-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_audioengine_h__
21 #define __ardour_audioengine_h__
22
23 #ifdef WAF_BUILD
24 #include "libardour-config.h"
25 #endif
26
27 #include <iostream>
28 #include <list>
29 #include <set>
30 #include <cmath>
31 #include <exception>
32 #include <string>
33
34
35 #include <glibmm/thread.h>
36
37 #include "pbd/rcu.h"
38 #include "pbd/signals.h"
39
40 #include "ardour/ardour.h"
41 #include <jack/jack.h>
42 #include <jack/transport.h>
43
44 #include "ardour/data_type.h"
45 #include "ardour/session_handle.h"
46 #include "ardour/types.h"
47 #include "ardour/chan_count.h"
48
49 #ifdef HAVE_JACK_SESSION
50 #include <jack/session.h>
51 #endif
52
53 namespace ARDOUR {
54
55 class InternalPort;
56 class MidiPort;
57 class Port;
58 class Session;
59 class ProcessThread;
60
61 class AudioEngine : public SessionHandlePtr
62 {
63    public:
64         typedef std::set<Port*> Ports;
65
66         AudioEngine (std::string client_name, std::string session_uuid);
67         virtual ~AudioEngine ();
68
69         jack_client_t* jack() const;
70         bool connected() const { return _jack != 0; }
71
72         bool is_realtime () const;
73
74         ProcessThread* main_thread() const { return _main_thread; }
75
76         std::string client_name() const { return jack_client_name; }
77
78         int reconnect_to_jack ();
79         int disconnect_from_jack();
80
81         bool will_reconnect_at_halt ();
82         void set_reconnect_at_halt (bool);
83
84         int stop (bool forever = false);
85         int start ();
86         bool running() const { return _running; }
87
88         Glib::Mutex& process_lock() { return _process_lock; }
89
90         nframes_t frame_rate() const;
91         nframes_t frames_per_cycle() const;
92
93         size_t raw_buffer_size(DataType t);
94
95         int usecs_per_cycle () const { return _usecs_per_cycle; }
96
97         bool get_sync_offset (nframes_t& offset) const;
98
99         nframes_t frames_since_cycle_start () {
100                 jack_client_t* _priv_jack = _jack;
101                 if (!_running || !_priv_jack) return 0;
102                 return jack_frames_since_cycle_start (_priv_jack);
103         }
104         nframes_t frame_time () {
105                 jack_client_t* _priv_jack = _jack;
106                 if (!_running || !_priv_jack) return 0;
107                 return jack_frame_time (_priv_jack);
108         }
109
110         nframes_t frame_time_at_cycle_start () {
111                 jack_client_t* _priv_jack = _jack;
112                 if (!_running || !_priv_jack) return 0;
113                 return jack_last_frame_time (_priv_jack);
114         }
115
116         nframes_t transport_frame () const {
117                 const jack_client_t* _priv_jack = _jack;
118                 if (!_running || !_priv_jack) return 0;
119                 return jack_get_current_transport_frame (_priv_jack);
120         }
121
122         int request_buffer_size (nframes_t);
123
124         nframes_t set_monitor_check_interval (nframes_t);
125         nframes_t processed_frames() const { return _processed_frames; }
126
127         float get_cpu_load() {
128                 jack_client_t* _priv_jack = _jack;
129                 if (!_running || !_priv_jack) return 0;
130                 return jack_cpu_load (_priv_jack);
131         }
132
133         void set_session (Session *);
134         void remove_session (); // not a replacement for SessionHandle::session_going_away()
135
136         class PortRegistrationFailure : public std::exception {
137         public:
138                 PortRegistrationFailure (const char* why = "") {
139                         reason = why;
140                 }
141                 virtual const char *what() const throw() { return reason; }
142
143         private:
144                 const char* reason;
145         };
146
147         class NoBackendAvailable : public std::exception {
148         public:
149                 virtual const char *what() const throw() { return "could not connect to engine backend"; }
150         };
151
152         Port *register_input_port (DataType, const std::string& portname);
153         Port *register_output_port (DataType, const std::string& portname);
154         int   unregister_port (Port &);
155
156         void split_cycle (nframes_t offset);
157
158         int connect (const std::string& source, const std::string& destination);
159         int disconnect (const std::string& source, const std::string& destination);
160         int disconnect (Port &);
161
162         const char ** get_ports (const std::string& port_name_pattern, const std::string& type_name_pattern, uint32_t flags);
163
164         bool can_request_hardware_monitoring ();
165
166         ChanCount n_physical_outputs () const;
167         ChanCount n_physical_inputs () const;
168
169         void get_physical_outputs (DataType type, std::vector<std::string>&);
170         void get_physical_inputs (DataType type, std::vector<std::string>&);
171
172         std::string get_nth_physical_output (DataType type, uint32_t n) {
173                 return get_nth_physical (type, n, JackPortIsInput);
174         }
175
176         std::string get_nth_physical_input (DataType type, uint32_t n) {
177                 return get_nth_physical (type, n, JackPortIsOutput);
178         }
179
180         void update_total_latencies ();
181         void update_total_latency (const Port&);
182
183         Port *get_port_by_name (const std::string &);
184         Port *get_port_by_name_locked (const std::string &);
185
186         enum TransportState {
187                 TransportStopped = JackTransportStopped,
188                 TransportRolling = JackTransportRolling,
189                 TransportLooping = JackTransportLooping,
190                 TransportStarting = JackTransportStarting
191         };
192
193         void transport_start ();
194         void transport_stop ();
195         void transport_locate (nframes_t);
196         TransportState transport_state ();
197
198         int  reset_timebase ();
199
200         /* start/stop freewheeling */
201
202         int freewheel (bool onoff);
203         bool freewheeling() const { return _freewheeling; }
204
205         /* this signal is sent for every process() cycle while freewheeling.
206 _          the regular process() call to session->process() is not made.
207         */
208
209         PBD::Signal1<int,nframes_t> Freewheel;
210
211         PBD::Signal0<void> Xrun;
212
213         /* this signal is if JACK notifies us of a graph order event */
214
215         PBD::Signal0<void> GraphReordered;
216
217 #ifdef HAVE_JACK_SESSION
218         PBD::Signal1<void,jack_session_event_t *> JackSessionEvent;
219 #endif
220
221
222         /* this signal is emitted if the sample rate changes */
223
224         PBD::Signal1<void,nframes_t> SampleRateChanged;
225
226         /* this signal is sent if JACK ever disconnects us */
227
228         PBD::Signal1<void,const char*> Halted;
229
230         /* these two are emitted when the engine itself is
231            started and stopped
232         */
233
234         PBD::Signal0<void> Running;
235         PBD::Signal0<void> Stopped;
236
237         /** Emitted if a JACK port is registered or unregistered */
238         PBD::Signal0<void> PortRegisteredOrUnregistered;
239
240         /** Emitted if a JACK port is connected or disconnected */
241         PBD::Signal0<void> PortConnectedOrDisconnected;
242
243         std::string make_port_name_relative (std::string);
244         std::string make_port_name_non_relative (std::string);
245
246         static AudioEngine* instance() { return _instance; }
247         void died ();
248
249         pthread_t create_process_thread (boost::function<void()>, size_t stacksize);
250
251   private:
252         static AudioEngine*       _instance;
253
254         jack_client_t* volatile   _jack; /* could be reset to null by SIGPIPE or another thread */
255         std::string                jack_client_name;
256         Glib::Mutex               _process_lock;
257         Glib::Cond                 session_removed;
258         bool                       session_remove_pending;
259         bool                      _running;
260         bool                      _has_run;
261         mutable nframes_t         _buffer_size;
262         std::map<DataType,size_t> _raw_buffer_sizes;
263         mutable nframes_t         _frame_rate;
264         /// number of frames between each check for changes in monitor input
265         nframes_t                  monitor_check_interval;
266         /// time of the last monitor check in frames
267         nframes_t                  last_monitor_check;
268         /// the number of frames processed since start() was called
269         nframes_t                 _processed_frames;
270         bool                      _freewheeling;
271         bool                      _freewheel_pending;
272         boost::function<int(nframes_t)>  freewheel_action;
273         bool                       reconnect_on_halt;
274         int                       _usecs_per_cycle;
275
276         SerializedRCUManager<Ports> ports;
277
278         Port *register_port (DataType type, const std::string& portname, bool input);
279
280         int    process_callback (nframes_t nframes);
281         void*  process_thread ();
282         void   finish_process_cycle (int status);
283         void   remove_all_ports ();
284
285         std::string get_nth_physical (DataType type, uint32_t n, int flags);
286         ChanCount n_physical (unsigned long) const;
287         void get_physical (DataType, unsigned long, std::vector<std::string> &);
288
289         void port_registration_failure (const std::string& portname);
290
291         static int  _xrun_callback (void *arg);
292 #ifdef HAVE_JACK_SESSION
293         static void _session_callback (jack_session_event_t *event, void *arg);
294 #endif
295         static int  _graph_order_callback (void *arg);
296         static int  _process_callback (nframes_t nframes, void *arg);
297         static void* _process_thread (void *arg);
298         static int  _sample_rate_callback (nframes_t nframes, void *arg);
299         static int  _bufsize_callback (nframes_t nframes, void *arg);
300         static void _jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int, void*);
301         static int  _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
302         static void _freewheel_callback (int , void *arg);
303         static void _registration_callback (jack_port_id_t, int, void *);
304         static void _connect_callback (jack_port_id_t, jack_port_id_t, int, void *);
305
306         void jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int);
307         int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
308         int  jack_bufsize_callback (nframes_t);
309         int  jack_sample_rate_callback (nframes_t);
310
311         int connect_to_jack (std::string client_name, std::string session_uuid);
312
313         static void halted (void *);
314         static void halted_info (jack_status_t,const char*,void *);
315
316         void meter_thread ();
317         void start_metering_thread ();
318         void stop_metering_thread ();
319
320         Glib::Thread*    m_meter_thread;
321         static gint      m_meter_exit;
322
323         ProcessThread* _main_thread;
324
325         struct ThreadData {
326             AudioEngine* engine;
327             boost::function<void()> f;
328             size_t stacksize;
329             
330             ThreadData (AudioEngine* ae, boost::function<void()> fp, size_t stacksz) 
331             : engine (ae) , f (fp) , stacksize (stacksz) {}
332         };
333         
334         static void* _start_process_thread (void*);
335 };
336
337 } // namespace ARDOUR
338
339 #endif /* __ardour_audioengine_h__ */