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