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