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