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.
20 #ifndef __ardour_audioengine_h__
21 #define __ardour_audioengine_h__
29 #include <sigc++/signal.h>
31 #include <glibmm/thread.h>
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>
47 class AudioEngine : public sigc::trackable
50 typedef std::set<Port*> Ports;
52 AudioEngine (std::string client_name);
53 virtual ~AudioEngine ();
55 jack_client_t* jack() const;
56 bool connected() const { return _jack != 0; }
58 bool is_realtime () const;
60 std::string client_name() const { return jack_client_name; }
62 int reconnect_to_jack ();
63 int disconnect_from_jack();
65 bool will_reconnect_at_halt ();
66 void set_reconnect_at_halt (bool);
68 int stop (bool forever = false);
70 bool running() const { return _running; }
72 Glib::Mutex& process_lock() { return _process_lock; }
74 nframes_t frame_rate();
75 nframes_t frames_per_cycle();
77 int usecs_per_cycle () const { return _usecs_per_cycle; }
79 bool get_sync_offset (nframes_t& offset) const;
81 nframes_t frames_since_cycle_start () {
82 if (!_running || !_jack) return 0;
83 return jack_frames_since_cycle_start (_jack);
85 nframes_t frame_time () {
86 if (!_running || !_jack) return 0;
87 return jack_frame_time (_jack);
90 nframes_t transport_frame () const {
91 if (!_running || !_jack) return 0;
92 return jack_get_current_transport_frame (_jack);
95 int request_buffer_size (nframes_t);
97 nframes_t set_monitor_check_interval (nframes_t);
99 float get_cpu_load() {
100 if (!_running || !_jack) return 0;
101 return jack_cpu_load (_jack);
104 void set_session (Session *);
105 void remove_session ();
107 class PortRegistrationFailure : public std::exception {
109 virtual const char *what() const throw() { return "failed port registration"; }
112 class NoBackendAvailable : public std::exception {
114 virtual const char *what() const throw() { return "could not connect to engine backend"; }
117 Port *register_input_port (PortType, DataType, const std::string& portname);
118 Port *register_output_port (PortType, DataType, const std::string& portname);
119 int unregister_port (Port &);
121 int connect (const std::string& source, const std::string& destination);
122 int disconnect (const std::string& source, const std::string& destination);
123 int disconnect (Port &);
125 const char ** get_ports (const std::string& port_name_pattern, const std::string& type_name_pattern, uint32_t flags);
127 uint32_t n_physical_outputs () const;
128 uint32_t n_physical_inputs () const;
130 void get_physical_outputs (std::vector<std::string>&);
131 void get_physical_inputs (std::vector<std::string>&);
133 std::string get_nth_physical_output (DataType type, uint32_t n) {
134 return get_nth_physical (type, n, JackPortIsInput);
137 std::string get_nth_physical_input (DataType type, uint32_t n) {
138 return get_nth_physical (type, n, JackPortIsOutput);
141 nframes_t get_port_total_latency (const Port&);
142 void update_total_latencies ();
143 void update_total_latency (const Port&);
145 /** Caller may not delete the object pointed to by the return value
147 Port *get_port_by_name (const std::string& name, bool keep = true) const;
149 enum TransportState {
150 TransportStopped = JackTransportStopped,
151 TransportRolling = JackTransportRolling,
152 TransportLooping = JackTransportLooping,
153 TransportStarting = JackTransportStarting
156 void transport_start ();
157 void transport_stop ();
158 void transport_locate (nframes_t);
159 TransportState transport_state ();
161 int reset_timebase ();
163 /* start/stop freewheeling */
165 int freewheel (bool onoff);
166 bool freewheeling() const { return _freewheeling; }
168 /* this signal is sent for every process() cycle while freewheeling.
169 the regular process() call to session->process() is not made.
172 sigc::signal<int,nframes_t> Freewheel;
174 sigc::signal<void> Xrun;
176 /* this signal is if JACK notifies us of a graph order event */
178 sigc::signal<void> GraphReordered;
180 /* this signal is emitted if the sample rate changes */
182 sigc::signal<void,nframes_t> SampleRateChanged;
184 /* this signal is sent if JACK ever disconnects us */
186 sigc::signal<void> Halted;
188 /* these two are emitted when the engine itself is
192 sigc::signal<void> Running;
193 sigc::signal<void> Stopped;
195 std::string make_port_name_relative (std::string);
196 std::string make_port_name_non_relative (std::string);
199 ARDOUR::Session *session;
200 jack_client_t *_jack;
201 std::string jack_client_name;
202 mutable Glib::Mutex _process_lock;
203 Glib::Cond session_removed;
204 bool session_remove_pending;
207 nframes_t _buffer_size;
208 nframes_t _frame_rate;
209 /// number of frames between each check for changes in monitor input
210 nframes_t monitor_check_interval;
211 /// time of the last monitor check in frames
212 nframes_t last_monitor_check;
213 /// the number of frames processed since start() was called
214 nframes_t _processed_frames;
216 bool _freewheel_thread_registered;
217 sigc::slot<int,nframes_t> freewheel_action;
218 bool reconnect_on_halt;
219 int _usecs_per_cycle;
221 SerializedRCUManager<Ports> ports;
223 Port *register_port (PortType ptype, DataType type, const std::string& portname, bool input);
225 int process_callback (nframes_t nframes);
226 void remove_all_ports ();
228 InternalPort* get_internal_port (const std::string& short_name);
230 typedef std::pair<std::string,std::string> PortConnection;
231 typedef std::list<PortConnection> PortConnections;
233 PortConnections port_connections;
234 void remove_connections_for (Port&);
236 std::string get_nth_physical (DataType type, uint32_t n, int flags);
238 static int _xrun_callback (void *arg);
239 static int _graph_order_callback (void *arg);
240 static int _process_callback (nframes_t nframes, void *arg);
241 static int _sample_rate_callback (nframes_t nframes, void *arg);
242 static int _bufsize_callback (nframes_t nframes, void *arg);
243 static void _jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int, void*);
244 static int _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
245 static void _freewheel_callback (int , void *arg);
247 void jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int);
248 int jack_sync_callback (jack_transport_state_t, jack_position_t*);
249 int jack_bufsize_callback (nframes_t);
250 int jack_sample_rate_callback (nframes_t);
252 static void halted (void *);
254 int connect_to_jack (std::string client_name);
256 void meter_thread ();
257 void start_metering_thread ();
258 void stop_metering_thread ();
260 Glib::Thread* m_meter_thread;
261 static gint m_meter_exit;
264 } // namespace ARDOUR
266 #endif /* __ardour_audioengine_h__ */