split up session code that uses parts of the JACK API (timebase + session event handl...
[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 "midi++/mmc.h"
40
41 #include "ardour/ardour.h"
42 #include "ardour/data_type.h"
43 #include "ardour/session_handle.h"
44 #include "ardour/types.h"
45 #include "ardour/chan_count.h"
46 #include "ardour/port_manager.h"
47
48 #ifdef HAVE_JACK_SESSION
49 #include <jack/session.h>
50 #endif
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     int            create_process_thread (boost::function<void()> func, pthread_t*, size_t stacksize);
104     bool           is_realtime() const;
105     bool           connected() const;
106
107     int set_device_name (const std::string&);
108     int set_sample_rate (float);
109     int set_buffer_size (uint32_t);
110     int set_sample_format (SampleFormat);
111     int set_interleaved (bool yn);
112     int set_input_channels (uint32_t);
113     int set_output_channels (uint32_t);
114     int set_systemic_input_latency (uint32_t);
115     int set_systemic_output_latency (uint32_t);
116
117     /* END BACKEND PROXY API */
118
119     bool freewheeling() const { return _freewheeling; }
120     bool running() const { return _running; }
121
122     Glib::Threads::Mutex& process_lock() { return _process_lock; }
123
124     int request_buffer_size (pframes_t samples) {
125             return set_buffer_size (samples);
126     }
127
128     framecnt_t processed_frames() const { return _processed_frames; }
129     
130     void set_session (Session *);
131     void remove_session (); // not a replacement for SessionHandle::session_going_away()
132     Session* session() const { return _session; }
133
134     class NoBackendAvailable : public std::exception {
135       public:
136         virtual const char *what() const throw() { return "could not connect to engine backend"; }
137     };
138     
139     void split_cycle (pframes_t offset);
140     
141     int  reset_timebase ();
142     
143     void update_latencies ();
144     
145     /* this signal is sent for every process() cycle while freewheeling.
146        (the regular process() call to session->process() is not made)
147     */
148     
149     PBD::Signal1<int, pframes_t> Freewheel;
150     
151     PBD::Signal0<void> Xrun;
152
153     /* this signal is emitted if the sample rate changes */
154     
155     PBD::Signal1<void, framecnt_t> SampleRateChanged;
156     
157     /* this signal is sent if the backend ever disconnects us */
158     
159     PBD::Signal1<void,const char*> Halted;
160     
161     /* these two are emitted when the engine itself is
162        started and stopped
163     */
164     
165     PBD::Signal0<void> Running;
166     PBD::Signal0<void> Stopped;
167
168     static AudioEngine* instance() { return _instance; }
169     static void destroy();
170     void died ();
171     
172     /* The backend will cause these at the appropriate time(s)
173      */
174     int  process_callback (pframes_t nframes);
175     int  buffer_size_change (pframes_t nframes);
176     int  sample_rate_change (pframes_t nframes);
177     void freewheel_callback (bool);
178     void timebase_callback (TransportState state, pframes_t nframes, framepos_t pos, int new_position);
179     int  sync_callback (TransportState state, framepos_t position);
180     int  port_registration_callback ();
181     void latency_callback (bool for_playback);
182     void halted_callback (const char* reason);
183
184     /* sets up the process callback thread */
185     static void thread_init_callback (void *);
186
187     MIDI::MachineControl& mmc() { return _mmc; }
188
189   private:
190     AudioEngine ();
191
192     static AudioEngine*       _instance;
193
194     boost::shared_ptr<AudioBackend> _backend;
195
196     Glib::Threads::Mutex      _process_lock;
197     Glib::Threads::Cond        session_removed;
198     bool                       session_remove_pending;
199     frameoffset_t              session_removal_countdown;
200     gain_t                     session_removal_gain;
201     gain_t                     session_removal_gain_step;
202     bool                      _running;
203     bool                      _freewheeling;
204     /// number of frames between each check for changes in monitor input
205     framecnt_t                 monitor_check_interval;
206     /// time of the last monitor check in frames
207     framecnt_t                 last_monitor_check;
208     /// the number of frames processed since start() was called
209     framecnt_t                _processed_frames;
210     bool                      _pre_freewheel_mmc_enabled;
211     Glib::Threads::Thread*     m_meter_thread;
212     ProcessThread*            _main_thread;
213     MIDI::MachineControl      _mmc;
214     
215     void meter_thread ();
216     void start_metering_thread ();
217     void stop_metering_thread ();
218     
219     static gint      m_meter_exit;
220     
221     void parameter_changed (const std::string&);
222     PBD::ScopedConnection config_connection;
223
224     typedef std::map<std::string,AudioBackendInfo*> BackendMap;
225     BackendMap _backends;
226     AudioBackendInfo* backend_discover (const std::string&);
227     void drop_backend ();
228 };
229         
230 } // namespace ARDOUR
231
232 #endif /* __ardour_audioengine_h__ */