2 Copyright (C) 2002-2004 Paul Davis
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.
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.
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.
21 #ifndef __ardour_audioengine_h__
22 #define __ardour_audioengine_h__
30 #include <sigc++/signal.h>
32 #include <glibmm/thread.h>
36 #include <ardour/ardour.h>
37 #include <jack/jack.h>
38 #include <jack/transport.h>
39 #include <ardour/types.h>
40 #include <ardour/data_type.h>
47 class AudioEngine : public sigc::trackable
50 AudioEngine (std::string client_name);
51 virtual ~AudioEngine ();
53 jack_client_t* jack() const { return _jack; }
54 bool connected() const { return _jack != 0; }
56 std::string client_name() const { return jack_client_name; }
58 int reconnect_to_jack ();
59 int disconnect_from_jack();
61 bool will_reconnect_at_halt ();
62 void set_reconnect_at_halt (bool);
64 int stop (bool forever = false);
66 bool running() const { return _running; }
68 Glib::Mutex& process_lock() { return _process_lock; }
70 nframes_t frame_rate();
71 nframes_t frames_per_cycle();
73 int usecs_per_cycle () const { return _usecs_per_cycle; }
75 bool get_sync_offset (nframes_t& offset) const;
77 nframes_t frames_since_cycle_start () {
78 if (!_running || !_jack) return 0;
79 return jack_frames_since_cycle_start (_jack);
81 nframes_t frame_time () {
82 if (!_running || !_jack) return 0;
83 return jack_frame_time (_jack);
86 nframes_t transport_frame () const {
87 if (!_running || !_jack) return 0;
88 return jack_get_current_transport_frame (_jack);
91 int request_buffer_size (nframes_t);
93 nframes_t set_monitor_check_interval (nframes_t);
95 float get_cpu_load() {
96 if (!_running || !_jack) return 0;
97 return jack_cpu_load (_jack);
100 void set_session (Session *);
101 void remove_session ();
103 class PortRegistrationFailure : public std::exception {
105 virtual const char *what() const throw() { return "failed port registration"; }
108 class NoBackendAvailable : public std::exception {
110 virtual const char *what() const throw() { return "could not connect to engine backend"; }
113 Port *register_input_port (DataType type, const std::string& portname);
114 Port *register_output_port (DataType type, const std::string& portname);
115 int unregister_port (Port &);
117 int connect (const std::string& source, const std::string& destination);
118 int disconnect (const std::string& source, const std::string& destination);
119 int disconnect (Port &);
121 const char ** get_ports (const std::string& port_name_pattern, const std::string& type_name_pattern, uint32_t flags);
123 uint32_t n_physical_outputs () const;
124 uint32_t n_physical_inputs () const;
126 void get_physical_outputs (std::vector<std::string>&);
127 void get_physical_inputs (std::vector<std::string>&);
129 std::string get_nth_physical_output (DataType type, uint32_t n) {
130 return get_nth_physical (type, n, JackPortIsInput);
133 std::string get_nth_physical_input (DataType type, uint32_t n) {
134 return get_nth_physical (type, n, JackPortIsOutput);
137 nframes_t get_port_total_latency (const Port&);
138 void update_total_latencies ();
140 /** Caller may not delete the object pointed to by the return value
142 Port *get_port_by_name (const std::string& name, bool keep = true);
144 enum TransportState {
145 TransportStopped = JackTransportStopped,
146 TransportRolling = JackTransportRolling,
147 TransportLooping = JackTransportLooping,
148 TransportStarting = JackTransportStarting
151 void transport_start ();
152 void transport_stop ();
153 void transport_locate (nframes_t);
154 TransportState transport_state ();
156 int reset_timebase ();
158 /* start/stop freewheeling */
160 int freewheel (bool onoff);
161 bool freewheeling() const { return _freewheeling; }
163 /* this signal is sent for every process() cycle while freewheeling.
164 the regular process() call to session->process() is not made.
167 sigc::signal<int,nframes_t> Freewheel;
169 sigc::signal<void> Xrun;
171 /* this signal is if JACK notifies us of a graph order event */
173 sigc::signal<void> GraphReordered;
175 /* this signal is emitted if the sample rate changes */
177 sigc::signal<void,nframes_t> SampleRateChanged;
179 /* this signal is sent if JACK ever disconnects us */
181 sigc::signal<void> Halted;
183 /* these two are emitted when the engine itself is
187 sigc::signal<void> Running;
188 sigc::signal<void> Stopped;
190 std::string make_port_name_relative (std::string);
191 std::string make_port_name_non_relative (std::string);
194 ARDOUR::Session *session;
195 jack_client_t *_jack;
196 std::string jack_client_name;
197 Glib::Mutex _process_lock;
198 Glib::Cond session_removed;
199 bool session_remove_pending;
202 nframes_t _buffer_size;
203 nframes_t _frame_rate;
204 nframes_t monitor_check_interval;
205 nframes_t last_monitor_check;
206 nframes_t _processed_frames;
208 bool _freewheel_thread_registered;
209 sigc::slot<int,nframes_t> freewheel_action;
210 bool reconnect_on_halt;
211 int _usecs_per_cycle;
213 typedef std::set<Port*> Ports;
214 SerializedRCUManager<Ports> ports;
216 int process_callback (nframes_t nframes);
217 void remove_all_ports ();
219 typedef std::pair<std::string,std::string> PortConnection;
220 typedef std::list<PortConnection> PortConnections;
222 PortConnections port_connections;
223 void remove_connections_for (Port&);
225 std::string get_nth_physical (DataType type, uint32_t n, int flags);
227 static int _xrun_callback (void *arg);
228 static int _graph_order_callback (void *arg);
229 static int _process_callback (nframes_t nframes, void *arg);
230 static int _sample_rate_callback (nframes_t nframes, void *arg);
231 static int _bufsize_callback (nframes_t nframes, void *arg);
232 static void _jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int, void*);
233 static int _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
234 static void _freewheel_callback (int , void *arg);
236 void jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int);
237 int jack_sync_callback (jack_transport_state_t, jack_position_t*);
238 int jack_bufsize_callback (nframes_t);
239 int jack_sample_rate_callback (nframes_t);
241 static void halted (void *);
243 int connect_to_jack (std::string client_name);
245 void meter_thread ();
246 void start_metering_thread ();
247 void stop_metering_thread ();
249 Glib::Thread* m_meter_thread;
250 static gint m_meter_exit;
253 } // namespace ARDOUR
255 #endif /* __ardour_audioengine_h__ */