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