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