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