more miscellaneous changes for audioengine, all of this is still far from actually...
[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
51 #ifdef HAVE_JACK_SESSION
52 #include <jack/session.h>
53 #endif
54
55 namespace ARDOUR {
56
57 class InternalPort;
58 class MidiPort;
59 class Port;
60 class Session;
61 class ProcessThread;
62 class AudioBackend;
63
64 class AudioEngine : public SessionHandlePtr
65 {
66 public:
67     typedef std::map<std::string,boost::shared_ptr<Port> > Ports;
68     
69     AudioEngine (std::string client_name, std::string session_uuid);
70     virtual ~AudioEngine ();
71     
72     static int discover_backends();
73     std::vector<std::string> available_backends() const;
74     std::string current_backend_name () const;
75
76     ProcessThread* main_thread() const { return _main_thread; }
77     
78     std::string client_name() const { return backend_client_name; }
79     
80     int stop (bool forever = false);
81     int start ();
82     int pause ();
83     int freewheel (bool onoff);
84     bool freewheeling() const { return _freewheeling; }
85    
86     bool running() const { return _running; }
87     Glib::Threads::Mutex& process_lock() { return _process_lock; }
88
89     int request_buffer_size (pframes_t);
90
91     framecnt_t processed_frames() const { return _processed_frames; }
92     
93     float get_cpu_load();
94     
95     void set_session (Session *);
96     void remove_session (); // not a replacement for SessionHandle::session_going_away()
97     
98     class NoBackendAvailable : public std::exception {
99       public:
100         virtual const char *what() const throw() { return "could not connect to engine backend"; }
101     };
102     
103     void split_cycle (pframes_t offset);
104     
105     int  reset_timebase ();
106     
107     void update_latencies ();
108
109     
110     /* this signal is sent for every process() cycle while freewheeling.
111        (the regular process() call to session->process() is not made)
112     */
113     
114     PBD::Signal1<int, pframes_t> Freewheel;
115     
116     PBD::Signal0<void> Xrun;
117     
118     /* this signal is if the backend notifies us of a graph order event */
119     
120     PBD::Signal0<void> GraphReordered;
121     
122 #ifdef HAVE_JACK_SESSION
123     PBD::Signal1<void,jack_session_event_t *> JackSessionEvent;
124 #endif
125     
126     /* this signal is emitted if the sample rate changes */
127     
128     PBD::Signal1<void, framecnt_t> SampleRateChanged;
129     
130     /* this signal is sent if the backend ever disconnects us */
131     
132     PBD::Signal1<void,const char*> Halted;
133     
134     /* these two are emitted when the engine itself is
135        started and stopped
136     */
137     
138     PBD::Signal0<void> Running;
139     PBD::Signal0<void> Stopped;
140     
141     /** Emitted if a Port is registered or unregistered */
142     PBD::Signal0<void> PortRegisteredOrUnregistered;
143     
144     /** Emitted if a Port is connected or disconnected.
145      *  The Port parameters are the ports being connected / disconnected, or 0 if they are not known to Ardour.
146      *  The std::string parameters are the (long) port names.
147      *  The bool parameter is true if ports were connected, or false for disconnected.
148      */
149     PBD::Signal5<void, boost::weak_ptr<Port>, std::string, boost::weak_ptr<Port>, std::string, bool> PortConnectedOrDisconnected;
150     
151     std::string make_port_name_relative (std::string) const;
152     std::string make_port_name_non_relative (std::string) const;
153     bool port_is_mine (const std::string&) const;
154     
155     static AudioEngine* instance() { return _instance; }
156     static void destroy();
157     void died ();
158     
159     /* The backend will cause this at the appropriate time(s)
160      */
161     int    process_callback (pframes_t nframes);
162     
163   private:
164     static AudioEngine*       _instance;
165     
166     Glib::Threads::Mutex      _process_lock;
167     Glib::Threads::Cond        session_removed;
168     bool                       session_remove_pending;
169     frameoffset_t              session_removal_countdown;
170     gain_t                     session_removal_gain;
171     gain_t                     session_removal_gain_step;
172     bool                      _running;
173     bool                      _has_run;
174     mutable framecnt_t        _buffer_size;
175     std::map<DataType,size_t> _raw_buffer_sizes;
176     mutable framecnt_t        _frame_rate;
177     /// number of frames between each check for changes in monitor input
178     framecnt_t                 monitor_check_interval;
179     /// time of the last monitor check in frames
180     framecnt_t                 last_monitor_check;
181     /// the number of frames processed since start() was called
182     framecnt_t                _processed_frames;
183     bool                      _freewheeling;
184     bool                      _pre_freewheel_mmc_enabled;
185     int                       _usecs_per_cycle;
186     bool                       port_remove_in_progress;
187     Glib::Threads::Thread*     m_meter_thread;
188     ProcessThread*            _main_thread;
189     
190     
191     void meter_thread ();
192     void start_metering_thread ();
193     void stop_metering_thread ();
194     
195     static gint      m_meter_exit;
196     
197     void parameter_changed (const std::string&);
198     PBD::ScopedConnection config_connection;
199 };
200         
201 } // namespace ARDOUR
202
203 #endif /* __ardour_audioengine_h__ */