jack_audiobackend.cc finally compiles
[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
65 class AudioEngine : public SessionHandlePtr, public PortManager
66 {
67 public:
68
69     static AudioEngine* create (const std::string&  client_name, const std::string& session_uuid);
70
71     virtual ~AudioEngine ();
72     
73     int discover_backends();
74     std::vector<std::string> available_backends() const;
75     std::string current_backend_name () const;
76     int set_backend (const std::string&);
77
78     ProcessThread* main_thread() const { return _main_thread; }
79     
80     std::string client_name() const { return backend_client_name; }
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 ();
89     int            stop ();
90     int            pause ();
91     int            freewheel (bool start_stop);
92     float          get_cpu_load() const ;
93     void           transport_start ();
94     void           transport_stop ();
95     TransportState transport_state ();
96     void           transport_locate (framepos_t pos);
97     framepos_t     transport_frame();
98     framecnt_t     sample_rate () const;
99     pframes_t      samples_per_cycle () const;
100     int            usecs_per_cycle () const;
101     size_t         raw_buffer_size (DataType t);
102     pframes_t      sample_time ();
103     pframes_t      sample_time_at_cycle_start ();
104     pframes_t      samples_since_cycle_start ();
105     bool           get_sync_offset (pframes_t& offset) const;
106     int            create_process_thread (boost::function<void()> func, pthread_t*, size_t stacksize);
107     bool           is_realtime() const;
108
109     /* END BACKEND PROXY API */
110
111     bool freewheeling() const { return _freewheeling; }
112     bool running() const { return _running; }
113
114     Glib::Threads::Mutex& process_lock() { return _process_lock; }
115
116     int request_buffer_size (pframes_t);
117
118     framecnt_t processed_frames() const { return _processed_frames; }
119     
120     void set_session (Session *);
121     void remove_session (); // not a replacement for SessionHandle::session_going_away()
122     
123     class NoBackendAvailable : public std::exception {
124       public:
125         virtual const char *what() const throw() { return "could not connect to engine backend"; }
126     };
127     
128     void split_cycle (pframes_t offset);
129     
130     int  reset_timebase ();
131     
132     void update_latencies ();
133
134     
135     /* this signal is sent for every process() cycle while freewheeling.
136        (the regular process() call to session->process() is not made)
137     */
138     
139     PBD::Signal1<int, pframes_t> Freewheel;
140     
141     PBD::Signal0<void> Xrun;
142     
143     /* this signal is if the backend notifies us of a graph order event */
144     
145     PBD::Signal0<void> GraphReordered;
146     
147 #ifdef HAVE_JACK_SESSION
148     PBD::Signal1<void,jack_session_event_t *> JackSessionEvent;
149 #endif
150     
151     /* this signal is emitted if the sample rate changes */
152     
153     PBD::Signal1<void, framecnt_t> SampleRateChanged;
154     
155     /* this signal is sent if the backend ever disconnects us */
156     
157     PBD::Signal1<void,const char*> Halted;
158     
159     /* these two are emitted when the engine itself is
160        started and stopped
161     */
162     
163     PBD::Signal0<void> Running;
164     PBD::Signal0<void> Stopped;
165     
166     /** Emitted if a Port is registered or unregistered */
167     PBD::Signal0<void> PortRegisteredOrUnregistered;
168     
169     /** Emitted if a Port is connected or disconnected.
170      *  The Port parameters are the ports being connected / disconnected, or 0 if they are not known to Ardour.
171      *  The std::string parameters are the (long) port names.
172      *  The bool parameter is true if ports were connected, or false for disconnected.
173      */
174     PBD::Signal5<void, boost::weak_ptr<Port>, std::string, boost::weak_ptr<Port>, std::string, bool> PortConnectedOrDisconnected;
175     
176     std::string make_port_name_relative (std::string) const;
177     std::string make_port_name_non_relative (std::string) const;
178     bool port_is_mine (const std::string&) const;
179     
180     static AudioEngine* instance() { return _instance; }
181     static void destroy();
182     void died ();
183     
184     /* The backend will cause these at the appropriate time(s)
185      */
186     int  process_callback (pframes_t nframes);
187     int  buffer_size_change (pframes_t nframes);
188     int  sample_rate_change (pframes_t nframes);
189     void freewheel_callback (bool);
190     void timebase_callback (TransportState state, pframes_t nframes, framepos_t pos, int new_position);
191     int  sync_callback (TransportState state, framepos_t position);
192     int  port_registration_callback ();
193     void latency_callback (bool for_playback);
194     void halted_callback (const char* reason);
195
196     /* sets up the process callback thread */
197     static void thread_init_callback (void *);
198
199   private:
200     AudioEngine (const std::string&  client_name, const std::string& session_uuid);
201
202     static AudioEngine*       _instance;
203
204     AudioBackend*             _backend;
205     Glib::Threads::Mutex      _process_lock;
206     Glib::Threads::Cond        session_removed;
207     bool                       session_remove_pending;
208     frameoffset_t              session_removal_countdown;
209     gain_t                     session_removal_gain;
210     gain_t                     session_removal_gain_step;
211     bool                      _running;
212     bool                      _has_run;
213     mutable framecnt_t        _buffer_size;
214     std::map<DataType,size_t> _raw_buffer_sizes;
215     mutable framecnt_t        _frame_rate;
216     /// number of frames between each check for changes in monitor input
217     framecnt_t                 monitor_check_interval;
218     /// time of the last monitor check in frames
219     framecnt_t                 last_monitor_check;
220     /// the number of frames processed since start() was called
221     framecnt_t                _processed_frames;
222     bool                      _freewheeling;
223     bool                      _pre_freewheel_mmc_enabled;
224     int                       _usecs_per_cycle;
225     bool                       port_remove_in_progress;
226     Glib::Threads::Thread*     m_meter_thread;
227     ProcessThread*            _main_thread;
228     
229     std::string               backend_client_name;
230     std::string               backend_session_uuid;
231     
232     void meter_thread ();
233     void start_metering_thread ();
234     void stop_metering_thread ();
235     
236     static gint      m_meter_exit;
237     
238     void parameter_changed (const std::string&);
239     PBD::ScopedConnection config_connection;
240
241     typedef std::map<std::string,AudioBackend*> BackendMap;
242     BackendMap _backends;
243     AudioBackend* backend_discover (const std::string&);
244     void drop_backend ();
245 };
246         
247 } // namespace ARDOUR
248
249 #endif /* __ardour_audioengine_h__ */