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