7eeae8f205c03c5e66bee2c5125f9d55c91b5f96
[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 #ifdef WAF_BUILD
24 #include "libardour-config.h"
25 #endif
26
27 #include <iostream>
28 #include <list>
29 #include <set>
30 #include <cmath>
31 #include <exception>
32 #include <string>
33
34 #include <glibmm/threads.h>
35
36 #include "pbd/signals.h"
37 #include "pbd/stacktrace.h"
38
39 #include "ardour/ardour.h"
40 #include "ardour/data_type.h"
41 #include "ardour/session_handle.h"
42 #include "ardour/libardour_visibility.h"
43 #include "ardour/types.h"
44 #include "ardour/chan_count.h"
45 #include "ardour/port_manager.h"
46
47 #ifdef HAVE_JACK_SESSION
48 #include <jack/session.h>
49 #endif
50
51 class MTDM;
52
53 namespace ARDOUR {
54
55 class InternalPort;
56 class MidiPort;
57 class MIDIDM;
58 class Port;
59 class Session;
60 class ProcessThread;
61 class AudioBackend;
62 class AudioBackendInfo;
63
64 class LIBARDOUR_API AudioEngine : public SessionHandlePtr, public PortManager
65 {
66 public:
67
68     static AudioEngine* create ();
69
70     virtual ~AudioEngine ();
71
72     int discover_backends();
73     std::vector<const AudioBackendInfo*> available_backends() const;
74     std::string current_backend_name () const;
75     boost::shared_ptr<AudioBackend> set_default_backend ();
76     boost::shared_ptr<AudioBackend> set_backend (const std::string&, const std::string& arg1, const std::string& arg2);
77     boost::shared_ptr<AudioBackend> current_backend() const { return _backend; }
78     bool setup_required () const;
79
80     ProcessThread* main_thread() const { return _main_thread; }
81     
82     /* START BACKEND PROXY API 
83      *
84      * See audio_backend.h for full documentation and semantics. These wrappers
85      * just forward to a backend implementation.
86      */
87
88     int            start (bool for_latency_measurement=false);
89     int            stop (bool for_latency_measurement=false);
90     int            freewheel (bool start_stop);
91     float          get_dsp_load() const ;
92     void           transport_start ();
93     void           transport_stop ();
94     TransportState transport_state ();
95     void           transport_locate (framepos_t pos);
96     framepos_t     transport_frame();
97     framecnt_t     sample_rate () const;
98     pframes_t      samples_per_cycle () const;
99     int            usecs_per_cycle () const;
100     size_t         raw_buffer_size (DataType t);
101     pframes_t      sample_time ();
102     pframes_t      sample_time_at_cycle_start ();
103     pframes_t      samples_since_cycle_start ();
104     bool           get_sync_offset (pframes_t& offset) const;
105
106     int            create_process_thread (boost::function<void()> func);
107     int            join_process_threads ();
108     bool           in_process_thread ();
109     uint32_t       process_thread_count ();
110
111     bool           is_realtime() const;
112     bool           connected() const;
113
114     int set_device_name (const std::string&);
115     int set_sample_rate (float);
116     int set_buffer_size (uint32_t);
117     int set_interleaved (bool yn);
118     int set_input_channels (uint32_t);
119     int set_output_channels (uint32_t);
120     int set_systemic_input_latency (uint32_t);
121     int set_systemic_output_latency (uint32_t);
122
123     /* END BACKEND PROXY API */
124
125     bool freewheeling() const { return _freewheeling; }
126     bool running() const { return _running; }
127
128     Glib::Threads::Mutex& process_lock() { return _process_lock; }
129
130     int request_buffer_size (pframes_t samples) {
131             return set_buffer_size (samples);
132     }
133
134     framecnt_t processed_frames() const { return _processed_frames; }
135     
136     void set_session (Session *);
137     void remove_session (); // not a replacement for SessionHandle::session_going_away()
138     Session* session() const { return _session; }
139
140     class NoBackendAvailable : public std::exception {
141       public:
142         virtual const char *what() const throw() { return "could not connect to engine backend"; }
143     };
144     
145     void split_cycle (pframes_t offset);
146     
147     int  reset_timebase ();
148     
149     void update_latencies ();
150     
151     /* this signal is sent for every process() cycle while freewheeling.
152        (the regular process() call to session->process() is not made)
153     */
154     
155     PBD::Signal1<int, pframes_t> Freewheel;
156     
157     PBD::Signal0<void> Xrun;
158
159     /* this signal is emitted if the sample rate changes */
160     
161     PBD::Signal1<void, framecnt_t> SampleRateChanged;
162     
163     /* this signal is sent if the backend ever disconnects us */
164     
165     PBD::Signal1<void,const char*> Halted;
166     
167     /* these two are emitted when the engine itself is
168        started and stopped
169     */
170     
171     PBD::Signal0<void> Running;
172     PBD::Signal0<void> Stopped;
173
174     static AudioEngine* instance() { return _instance; }
175     static void destroy();
176     void died ();
177     
178     /* The backend will cause these at the appropriate time(s)
179      */
180     int  process_callback (pframes_t nframes);
181     int  buffer_size_change (pframes_t nframes);
182     int  sample_rate_change (pframes_t nframes);
183     void freewheel_callback (bool);
184     void timebase_callback (TransportState state, pframes_t nframes, framepos_t pos, int new_position);
185     int  sync_callback (TransportState state, framepos_t position);
186     int  port_registration_callback ();
187     void latency_callback (bool for_playback);
188     void halted_callback (const char* reason);
189
190     /* sets up the process callback thread */
191     static void thread_init_callback (void *);
192
193     /* latency measurement */
194
195     MTDM* mtdm() { return _mtdm; }
196     MIDIDM* mididm() { return _mididm; }
197
198     int  prepare_for_latency_measurement ();
199     int  start_latency_detection (bool);
200     void stop_latency_detection ();
201     void set_latency_input_port (const std::string&);
202     void set_latency_output_port (const std::string&);
203     uint32_t latency_signal_delay () const { return _latency_signal_latency; }
204
205                 enum LatencyMeasurement {
206                         MeasureNone,
207                         MeasureAudio,
208                         MeasureMIDI
209                 };
210
211                 LatencyMeasurement measuring_latency () const { return _measuring_latency; }
212
213   private:
214     AudioEngine ();
215
216     static AudioEngine*       _instance;
217
218     Glib::Threads::Mutex      _process_lock;
219     Glib::Threads::Cond        session_removed;
220     bool                       session_remove_pending;
221     frameoffset_t              session_removal_countdown;
222     gain_t                     session_removal_gain;
223     gain_t                     session_removal_gain_step;
224     bool                      _running;
225     bool                      _freewheeling;
226     /// number of frames between each check for changes in monitor input
227     framecnt_t                 monitor_check_interval;
228     /// time of the last monitor check in frames
229     framecnt_t                 last_monitor_check;
230     /// the number of frames processed since start() was called
231     framecnt_t                _processed_frames;
232     Glib::Threads::Thread*     m_meter_thread;
233     ProcessThread*            _main_thread;
234     MTDM*                     _mtdm;
235     MIDIDM*                   _mididm;
236                 LatencyMeasurement        _measuring_latency;
237     PortEngine::PortHandle    _latency_input_port;
238     PortEngine::PortHandle    _latency_output_port;
239     framecnt_t                _latency_flush_frames;
240     std::string               _latency_input_name;
241     std::string               _latency_output_name;
242     framecnt_t                _latency_signal_latency;
243     bool                      _stopped_for_latency;
244     bool                      _started_for_latency;
245     bool                      _in_destructor;
246
247     void meter_thread ();
248     void start_metering_thread ();
249     void stop_metering_thread ();
250     
251     static gint      m_meter_exit;
252     
253     typedef std::map<std::string,AudioBackendInfo*> BackendMap;
254     BackendMap _backends;
255     AudioBackendInfo* backend_discover (const std::string&);
256     void drop_backend ();
257 };
258         
259 } // namespace ARDOUR
260
261 #endif /* __ardour_audioengine_h__ */