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