Reading of MIDI CC from MIDI regions (MidiModel). UI still needs work though..
[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         typedef std::set<Port*> Ports;
50
51         AudioEngine (std::string client_name);
52         virtual ~AudioEngine ();
53         
54         jack_client_t* jack() const { return _jack; }
55         bool connected() const { return _jack != 0; }
56
57         bool is_realtime () const;
58
59         std::string client_name() const { return jack_client_name; }
60
61         int reconnect_to_jack ();
62         int disconnect_from_jack();
63
64         bool will_reconnect_at_halt ();
65         void set_reconnect_at_halt (bool);
66
67         int stop (bool forever = false);
68         int start ();
69         bool running() const { return _running; }
70
71         Glib::Mutex& process_lock() { return _process_lock; }
72
73         nframes_t frame_rate();
74         nframes_t frames_per_cycle();
75
76         int usecs_per_cycle () const { return _usecs_per_cycle; }
77
78         bool get_sync_offset (nframes_t& offset) const;
79
80         nframes_t frames_since_cycle_start () {
81                 if (!_running || !_jack) return 0;
82                 return jack_frames_since_cycle_start (_jack);
83         }
84         nframes_t frame_time () {
85                 if (!_running || !_jack) return 0;
86                 return jack_frame_time (_jack);
87         }
88
89         nframes_t transport_frame () const {
90                 if (!_running || !_jack) return 0;
91                 return jack_get_current_transport_frame (_jack);
92         }
93         
94         int request_buffer_size (nframes_t);
95         
96         nframes_t set_monitor_check_interval (nframes_t);
97
98         float get_cpu_load() { 
99                 if (!_running || !_jack) return 0;
100                 return jack_cpu_load (_jack);
101         }
102
103         void set_session (Session *);
104         void remove_session ();
105
106         class PortRegistrationFailure : public std::exception {
107           public:
108                 virtual const char *what() const throw() { return "failed port registration"; }
109         };
110
111         class NoBackendAvailable : public std::exception {
112           public:
113                 virtual const char *what() const throw() { return "could not connect to engine backend"; }
114         };
115
116         Port *register_input_port (DataType type, const std::string& portname);
117         Port *register_output_port (DataType type, const std::string& portname);
118         int   unregister_port (Port &);
119         
120         int connect (const std::string& source, const std::string& destination);
121         int disconnect (const std::string& source, const std::string& destination);
122         int disconnect (Port &);
123         
124         const char ** get_ports (const std::string& port_name_pattern, const std::string& type_name_pattern, uint32_t flags);
125
126         uint32_t n_physical_outputs () const;
127         uint32_t n_physical_inputs () const;
128
129         void get_physical_outputs (std::vector<std::string>&);
130         void get_physical_inputs (std::vector<std::string>&);
131
132         std::string get_nth_physical_output (DataType type, uint32_t n) {
133                 return get_nth_physical (type, n, JackPortIsInput);
134         }
135
136         std::string get_nth_physical_input (DataType type, uint32_t n) {
137                 return get_nth_physical (type, n, JackPortIsOutput);
138         }
139
140         nframes_t get_port_total_latency (const Port&);
141         void update_total_latencies ();
142         void update_total_latency (const Port&);
143
144         /** Caller may not delete the object pointed to by the return value
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         /// number of frames between each check for changes in monitor input
209         nframes_t monitor_check_interval;
210         /// time of the last monitor check in frames
211         nframes_t last_monitor_check;
212         /// the number of frames processed since start() was called
213         nframes_t _processed_frames;
214         bool _freewheeling;
215         bool _freewheel_thread_registered;
216         sigc::slot<int,nframes_t> freewheel_action;
217         bool reconnect_on_halt;
218         int _usecs_per_cycle;
219
220         SerializedRCUManager<Ports> ports;
221
222         int    process_callback (nframes_t nframes);
223         void   remove_all_ports ();
224
225         typedef std::pair<std::string,std::string> PortConnection;
226         typedef std::list<PortConnection> PortConnections;
227
228         PortConnections port_connections;
229         void   remove_connections_for (Port&);
230
231         std::string get_nth_physical (DataType type, uint32_t n, int flags);
232
233         static int  _xrun_callback (void *arg);
234         static int  _graph_order_callback (void *arg);
235         static int  _process_callback (nframes_t nframes, void *arg);
236         static int  _sample_rate_callback (nframes_t nframes, void *arg);
237         static int  _bufsize_callback (nframes_t nframes, void *arg);
238         static void _jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int, void*);
239         static int  _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
240         static void _freewheel_callback (int , void *arg);
241
242         void jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int);
243         int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
244         int  jack_bufsize_callback (nframes_t);
245         int  jack_sample_rate_callback (nframes_t);
246
247         static void halted (void *);
248
249         int connect_to_jack (std::string client_name);
250
251         void meter_thread ();
252         void start_metering_thread ();
253         void stop_metering_thread ();
254
255         Glib::Thread*    m_meter_thread;
256         static gint      m_meter_exit;
257 };
258
259 } // namespace ARDOUR
260
261 #endif /* __ardour_audioengine_h__ */