b22ca9790fc87d511f94e3cb4fff4dffb2d75b85
[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();
78         nframes_t frames_per_cycle();
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 transport_frame () const {
98                 const jack_client_t* _priv_jack = _jack;
99                 if (!_running || !_priv_jack) return 0;
100                 return jack_get_current_transport_frame (_priv_jack);
101         }
102
103         int request_buffer_size (nframes_t);
104
105         nframes_t set_monitor_check_interval (nframes_t);
106         nframes_t processed_frames() const { return _processed_frames; }
107
108         float get_cpu_load() {
109                 jack_client_t* _priv_jack = _jack;
110                 if (!_running || !_priv_jack) return 0;
111                 return jack_cpu_load (_priv_jack);
112         }
113
114         void set_session (Session *);
115         void remove_session (); // not a replacement for SessionHandle::session_going_away()
116
117         class PortRegistrationFailure : public std::exception {
118         public:
119                 PortRegistrationFailure (const char* why = "") {
120                         reason = why;
121                 }
122                 virtual const char *what() const throw() { return reason; }
123
124         private:
125                 const char* reason;
126         };
127
128         class NoBackendAvailable : public std::exception {
129         public:
130                 virtual const char *what() const throw() { return "could not connect to engine backend"; }
131         };
132
133         Port *register_input_port (DataType, const std::string& portname);
134         Port *register_output_port (DataType, const std::string& portname);
135         int   unregister_port (Port &);
136
137         void split_cycle (nframes_t offset);
138
139         int connect (const std::string& source, const std::string& destination);
140         int disconnect (const std::string& source, const std::string& destination);
141         int disconnect (Port &);
142
143         const char ** get_ports (const std::string& port_name_pattern, const std::string& type_name_pattern, uint32_t flags);
144
145         bool can_request_hardware_monitoring ();
146
147         uint32_t n_physical_outputs (DataType type) const;
148         uint32_t n_physical_inputs (DataType type) const;
149
150         void get_physical_outputs (DataType type, std::vector<std::string>&);
151         void get_physical_inputs (DataType type, std::vector<std::string>&);
152
153         std::string get_nth_physical_output (DataType type, uint32_t n) {
154                 return get_nth_physical (type, n, JackPortIsInput);
155         }
156
157         std::string get_nth_physical_input (DataType type, uint32_t n) {
158                 return get_nth_physical (type, n, JackPortIsOutput);
159         }
160
161         void update_total_latencies ();
162         void update_total_latency (const Port&);
163
164         Port *get_port_by_name (const std::string &);
165         Port *get_port_by_name_locked (const std::string &);
166
167         enum TransportState {
168                 TransportStopped = JackTransportStopped,
169                 TransportRolling = JackTransportRolling,
170                 TransportLooping = JackTransportLooping,
171                 TransportStarting = JackTransportStarting
172         };
173
174         void transport_start ();
175         void transport_stop ();
176         void transport_locate (nframes_t);
177         TransportState transport_state ();
178
179         int  reset_timebase ();
180
181         /* start/stop freewheeling */
182
183         int freewheel (bool onoff);
184         bool freewheeling() const { return _freewheeling; }
185
186         /* this signal is sent for every process() cycle while freewheeling.
187 _          the regular process() call to session->process() is not made.
188         */
189
190         PBD::Signal1<int,nframes_t> Freewheel;
191
192         PBD::Signal0<void> Xrun;
193
194         /* this signal is if JACK notifies us of a graph order event */
195
196         PBD::Signal0<void> GraphReordered;
197
198         /* this signal is emitted if the sample rate changes */
199
200         PBD::Signal1<void,nframes_t> SampleRateChanged;
201
202         /* this signal is sent if JACK ever disconnects us */
203
204         PBD::Signal0<void> Halted;
205
206         /* these two are emitted when the engine itself is
207            started and stopped
208         */
209
210         PBD::Signal0<void> Running;
211         PBD::Signal0<void> Stopped;
212
213         /* this signal is emitted if a JACK port is registered or unregistered */
214
215         PBD::Signal0<void> PortRegisteredOrUnregistered;
216
217         std::string make_port_name_relative (std::string);
218         std::string make_port_name_non_relative (std::string);
219
220         static AudioEngine* instance() { return _instance; }
221         void died ();
222
223   private:
224         static AudioEngine*       _instance;
225
226         jack_client_t* volatile   _jack; /* could be reset to null by SIGPIPE or another thread */
227         std::string                jack_client_name;
228         Glib::Mutex               _process_lock;
229         Glib::Cond                 session_removed;
230         bool                       session_remove_pending;
231         bool                      _running;
232         bool                      _has_run;
233         nframes_t                 _buffer_size;
234         std::map<DataType,size_t> _raw_buffer_sizes;
235         nframes_t                 _frame_rate;
236         /// number of frames between each check for changes in monitor input
237         nframes_t                  monitor_check_interval;
238         /// time of the last monitor check in frames
239         nframes_t                  last_monitor_check;
240         /// the number of frames processed since start() was called
241         nframes_t                 _processed_frames;
242         bool                      _freewheeling;
243         bool                      _freewheel_pending;
244         bool                      _freewheel_thread_registered;
245         boost::function<int(nframes_t)>  freewheel_action;
246         bool                       reconnect_on_halt;
247         int                       _usecs_per_cycle;
248
249         SerializedRCUManager<Ports> ports;
250
251         Port *register_port (DataType type, const std::string& portname, bool input);
252
253         int    process_callback (nframes_t nframes);
254         void   remove_all_ports ();
255
256         std::string get_nth_physical (DataType type, uint32_t n, int flags);
257
258         void port_registration_failure (const std::string& portname);
259
260         static int  _xrun_callback (void *arg);
261         static int  _graph_order_callback (void *arg);
262         static int  _process_callback (nframes_t nframes, void *arg);
263         static int  _sample_rate_callback (nframes_t nframes, void *arg);
264         static int  _bufsize_callback (nframes_t nframes, void *arg);
265         static void _jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int, void*);
266         static int  _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
267         static void _freewheel_callback (int , void *arg);
268         static void _registration_callback (jack_port_id_t, int, void *);
269
270         void jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int);
271         int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
272         int  jack_bufsize_callback (nframes_t);
273         int  jack_sample_rate_callback (nframes_t);
274
275         int connect_to_jack (std::string client_name);
276
277         static void halted (void *);
278
279         void meter_thread ();
280         void start_metering_thread ();
281         void stop_metering_thread ();
282
283         Glib::Thread*    m_meter_thread;
284         static gint      m_meter_exit;
285 };
286
287 } // namespace ARDOUR
288
289 #endif /* __ardour_audioengine_h__ */