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