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