ardour
audioengine.h
Go to the documentation of this file.
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"
43 #include "ardour/types.h"
44 #include "ardour/chan_count.h"
45 #include "ardour/port_manager.h"
46 
47 class MTDM;
48 
49 namespace ARDOUR {
50 
51 class InternalPort;
52 class MidiPort;
53 class MIDIDM;
54 class Port;
55 class Session;
56 class ProcessThread;
57 class AudioBackend;
58 struct AudioBackendInfo;
59 
61 {
62  public:
63 
64  static AudioEngine* create ();
65 
66  virtual ~AudioEngine ();
67 
68  int discover_backends();
69  std::vector<const AudioBackendInfo*> available_backends() const;
70  std::string current_backend_name () const;
71  boost::shared_ptr<AudioBackend> set_default_backend ();
72  boost::shared_ptr<AudioBackend> set_backend (const std::string&, const std::string& arg1, const std::string& arg2);
73  boost::shared_ptr<AudioBackend> current_backend() const { return _backend; }
74  bool setup_required () const;
75 
76  ProcessThread* main_thread() const { return _main_thread; }
77 
78  /* START BACKEND PROXY API
79  *
80  * See audio_backend.h for full documentation and semantics. These wrappers
81  * just forward to a backend implementation.
82  */
83 
84  int start (bool for_latency_measurement=false);
85  int stop (bool for_latency_measurement=false);
86  int freewheel (bool start_stop);
87  float get_dsp_load() const ;
88  void transport_start ();
89  void transport_stop ();
90  TransportState transport_state ();
91  void transport_locate (framepos_t pos);
92  framepos_t transport_frame();
93  framecnt_t sample_rate () const;
94  pframes_t samples_per_cycle () const;
95  int usecs_per_cycle () const;
96  size_t raw_buffer_size (DataType t);
97  framepos_t sample_time ();
98  framepos_t sample_time_at_cycle_start ();
99  pframes_t samples_since_cycle_start ();
100  bool get_sync_offset (pframes_t& offset) const;
101 
102  int create_process_thread (boost::function<void()> func);
103  int join_process_threads ();
104  bool in_process_thread ();
105  uint32_t process_thread_count ();
106 
107  int backend_reset_requested();
108  void request_backend_reset();
109  void request_device_list_update();
110  void launch_device_control_app();
111 
112  bool is_realtime() const;
113  bool connected() const;
114 
115  // for the user which hold state_lock to check if reset operation is pending
116  bool is_reset_requested() const { return g_atomic_int_get(const_cast<gint*>(&_hw_reset_request_count)); }
117 
118  int set_device_name (const std::string&);
119  int set_sample_rate (float);
120  int set_buffer_size (uint32_t);
121  int set_interleaved (bool yn);
122  int set_input_channels (uint32_t);
123  int set_output_channels (uint32_t);
124  int set_systemic_input_latency (uint32_t);
125  int set_systemic_output_latency (uint32_t);
126 
127  /* END BACKEND PROXY API */
128 
129  bool freewheeling() const { return _freewheeling; }
130  bool running() const { return _running; }
131 
132  Glib::Threads::Mutex& process_lock() { return _process_lock; }
133  Glib::Threads::RecMutex& state_lock() { return _state_lock; }
134 
136  return set_buffer_size (samples);
137  }
138 
139  framecnt_t processed_frames() const { return _processed_frames; }
140 
141  void set_session (Session *);
142  void remove_session (); // not a replacement for SessionHandle::session_going_away()
143  Session* session() const { return _session; }
144 
145  void reconnect_session_routes (bool reconnect_inputs = true, bool reconnect_outputs = true);
146 
147  class NoBackendAvailable : public std::exception {
148  public:
149  virtual const char *what() const throw() { return "could not connect to engine backend"; }
150  };
151 
152  void split_cycle (pframes_t offset);
153 
154  int reset_timebase ();
155 
156  void update_latencies ();
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  PBD::Signal1<int, pframes_t> Freewheel;
163 
164  PBD::Signal0<void> Xrun;
165 
167  PBD::Signal1<void, framecnt_t> SampleRateChanged;
168 
170  PBD::Signal1<void, pframes_t> BufferSizeChanged;
171 
173  PBD::Signal0<void> DeviceError;
174 
175  /* this signal is emitted if the device list changed */
176 
177  PBD::Signal0<void> DeviceListChanged;
178 
179  /* this signal is sent if the backend ever disconnects us */
180 
181  PBD::Signal1<void,const char*> Halted;
182 
183  /* these two are emitted when the engine itself is
184  started and stopped
185  */
186 
187  PBD::Signal0<void> Running;
188  PBD::Signal0<void> Stopped;
189 
190  /* these two are emitted when a device reset is initiated/finished
191  */
192 
193  PBD::Signal0<void> DeviceResetStarted;
194  PBD::Signal0<void> DeviceResetFinished;
195 
196  static AudioEngine* instance() { return _instance; }
197  static void destroy();
198  void died ();
199 
200  /* The backend will cause these at the appropriate time(s)
201  */
202  int process_callback (pframes_t nframes);
203  int buffer_size_change (pframes_t nframes);
204  int sample_rate_change (pframes_t nframes);
205  void freewheel_callback (bool);
206  void timebase_callback (TransportState state, pframes_t nframes, framepos_t pos, int new_position);
207  int sync_callback (TransportState state, framepos_t position);
208  int port_registration_callback ();
209  void latency_callback (bool for_playback);
210  void halted_callback (const char* reason);
211 
212  /* sets up the process callback thread */
213  static void thread_init_callback (void *);
214 
215  /* latency measurement */
216 
217  MTDM* mtdm() { return _mtdm; }
218  MIDIDM* mididm() { return _mididm; }
219 
220  int prepare_for_latency_measurement ();
221  int start_latency_detection (bool);
222  void stop_latency_detection ();
223  void set_latency_input_port (const std::string&);
224  void set_latency_output_port (const std::string&);
225  uint32_t latency_signal_delay () const { return _latency_signal_latency; }
226 
230  MeasureMIDI
231  };
232 
233  LatencyMeasurement measuring_latency () const { return _measuring_latency; }
234 
235  /* These two are used only in builds where SILENCE_AFTER_SECONDS was
236  * set. BecameSilent will be emitted when the audioengine goes silent.
237  * reset_silence_countdown() can be used to reset the silence
238  * countdown, whose duration will be reduced to half of its previous
239  * value.
240  */
241 
242  PBD::Signal0<void> BecameSilent;
243  void reset_silence_countdown ();
244 
245  private:
246  AudioEngine ();
247 
249 
250  Glib::Threads::Mutex _process_lock;
251  Glib::Threads::RecMutex _state_lock;
252  Glib::Threads::Cond session_removed;
257  bool _running;
265  Glib::Threads::Thread* m_meter_thread;
273  std::string _latency_input_name;
274  std::string _latency_output_name;
279 
280  Glib::Threads::Thread* _hw_reset_event_thread;
282  Glib::Threads::Cond _hw_reset_condition;
283  Glib::Threads::Mutex _reset_request_lock;
285  Glib::Threads::Thread* _hw_devicelist_update_thread;
287  Glib::Threads::Cond _hw_devicelist_update_condition;
288  Glib::Threads::Mutex _devicelist_update_lock;
290 
291  void start_hw_event_processing();
292  void stop_hw_event_processing();
293  void do_reset_backend();
294  void do_devicelist_update();
295 
296  typedef std::map<std::string,AudioBackendInfo*> BackendMap;
297  BackendMap _backends;
298  AudioBackendInfo* backend_discover (const std::string&);
299  void drop_backend ();
300 
301 #ifdef SILENCE_AFTER
302  framecnt_t _silence_countdown;
303  uint32_t _silence_hit_cnt;
304 #endif
305 
306 };
307 
308 } // namespace ARDOUR
309 
310 #endif /* __ardour_audioengine_h__ */
framecnt_t _latency_signal_latency
Definition: audioengine.h:275
gain_t session_removal_gain
Definition: audioengine.h:255
Glib::Threads::Mutex _devicelist_update_lock
Definition: audioengine.h:288
Glib::Threads::Cond session_removed
Definition: audioengine.h:252
PBD::Signal1< int, pframes_t > Freewheel
Definition: audioengine.h:162
PBD::Signal0< void > Xrun
Definition: audioengine.h:164
int request_buffer_size(pframes_t samples)
Definition: audioengine.h:135
bool running() const
Definition: audioengine.h:130
Definition: mtdm.h:26
Glib::Threads::Thread * _hw_reset_event_thread
Definition: audioengine.h:280
Glib::Threads::RecMutex _state_lock
Definition: audioengine.h:251
Glib::Threads::Thread * m_meter_thread
Definition: audioengine.h:265
std::string _latency_output_name
Definition: audioengine.h:274
boost::shared_ptr< AudioBackend > current_backend() const
Definition: audioengine.h:73
gint _stop_hw_devicelist_processing
Definition: audioengine.h:289
framecnt_t last_monitor_check
time of the last monitor check in frames
Definition: audioengine.h:262
uint32_t pframes_t
Definition: types.h:61
framecnt_t _processed_frames
the number of frames processed since start() was called
Definition: audioengine.h:264
PBD::Signal0< void > DeviceListChanged
Definition: audioengine.h:177
float gain_t
Definition: types.h:58
static AudioEngine * _instance
Definition: audioengine.h:248
LatencyMeasurement measuring_latency() const
Definition: audioengine.h:233
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > start
Definition: region.cc:63
framecnt_t monitor_check_interval
number of frames between each check for changes in monitor input
Definition: audioengine.h:260
static AudioEngine * instance()
Definition: audioengine.h:196
bool is_reset_requested() const
Definition: audioengine.h:116
PBD::Signal0< void > DeviceResetFinished
Definition: audioengine.h:194
gint _hw_devicelist_update_count
Definition: audioengine.h:286
frameoffset_t session_removal_countdown
Definition: audioengine.h:254
ProcessThread * main_thread() const
Definition: audioengine.h:76
framecnt_t processed_frames() const
Definition: audioengine.h:139
ProcessThread * _main_thread
Definition: audioengine.h:266
framecnt_t _latency_flush_frames
Definition: audioengine.h:272
std::map< std::string, AudioBackendInfo * > BackendMap
Definition: audioengine.h:296
Glib::Threads::Cond _hw_reset_condition
Definition: audioengine.h:282
int64_t framecnt_t
Definition: types.h:76
Glib::Threads::Thread * _hw_devicelist_update_thread
Definition: audioengine.h:285
gint _stop_hw_reset_processing
Definition: audioengine.h:284
Glib::Threads::Mutex _process_lock
Definition: audioengine.h:250
Definition: amp.h:29
PortEngine::PortHandle _latency_input_port
Definition: audioengine.h:270
PBD::Signal1< void, framecnt_t > SampleRateChanged
Definition: audioengine.h:167
int64_t framepos_t
Definition: types.h:66
BackendMap _backends
Definition: audioengine.h:297
PBD::Signal0< void > BecameSilent
Definition: audioengine.h:242
Glib::Threads::Mutex _reset_request_lock
Definition: audioengine.h:283
int64_t frameoffset_t
Definition: types.h:71
PBD::Signal0< void > Running
Definition: audioengine.h:187
PBD::Signal0< void > DeviceResetStarted
Definition: audioengine.h:193
#define LIBARDOUR_API
Session * session() const
Definition: audioengine.h:143
Glib::Threads::Mutex & process_lock()
Definition: audioengine.h:132
bool freewheeling() const
Definition: audioengine.h:129
PortEngine::PortHandle _latency_output_port
Definition: audioengine.h:271
Glib::Threads::RecMutex & state_lock()
Definition: audioengine.h:133
Glib::Threads::Cond _hw_devicelist_update_condition
Definition: audioengine.h:287
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > position
Definition: region.cc:65
PBD::Signal0< void > DeviceError
Definition: audioengine.h:173
PBD::Signal1< void, pframes_t > BufferSizeChanged
Definition: audioengine.h:170
virtual const char * what() const
Definition: audioengine.h:149
std::string _latency_input_name
Definition: audioengine.h:273
PBD::Signal1< void, const char * > Halted
Definition: audioengine.h:181
PBD::Signal0< void > Stopped
Definition: audioengine.h:188
LIBARDOUR_API uint64_t AudioEngine
Definition: session.h:982
LatencyMeasurement _measuring_latency
Definition: audioengine.h:269
TransportState
Definition: types.h:600
uint32_t latency_signal_delay() const
Definition: audioengine.h:225
gain_t session_removal_gain_step
Definition: audioengine.h:256