laying the groundwork for adding/removing transport masters
[ardour.git] / libs / ardour / ardour / transport_master_manager.h
1 /*
2  * Copyright (C) 2018 Paul Davis (paul@linuxaudiosystems.com)
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, or (at your option)
7  * 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 Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #ifndef __ardour_transport_master_manager_h__
20 #define __ardour_transport_master_manager_h__
21
22 #include <string>
23
24 #include <boost/noncopyable.hpp>
25
26 #include "ardour/transport_master.h"
27 #include "ardour/types.h"
28
29 namespace ARDOUR {
30
31 class UI_TransportMaster;
32
33 class LIBARDOUR_API TransportMasterManager : public boost::noncopyable
34 {
35   public:
36         ~TransportMasterManager ();
37
38         int init ();
39
40         static TransportMasterManager& instance();
41
42         typedef std::list<boost::shared_ptr<TransportMaster> > TransportMasters;
43
44         int add (SyncSource type, std::string const & name, bool removeable = true);
45         int remove (std::string const & name);
46         void clear ();
47
48         PBD::Signal1<void,boost::shared_ptr<TransportMaster> > Added;
49         PBD::Signal1<void,boost::shared_ptr<TransportMaster> > Removed; // null argument means "clear"
50
51         double pre_process_transport_masters (pframes_t, samplepos_t session_transport_position);
52
53         double get_current_speed_in_process_context() const { return _master_speed; }
54         samplepos_t get_current_position_in_process_context() const { return _master_position; }
55
56         boost::shared_ptr<TransportMaster> current() const { return _current_master; }
57         int set_current (boost::shared_ptr<TransportMaster>);
58         int set_current (SyncSource);
59         int set_current (std::string const &);
60
61         PBD::Signal2<void,boost::shared_ptr<TransportMaster>, boost::shared_ptr<TransportMaster> > CurrentChanged;
62
63         int set_state (XMLNode const &, int);
64         XMLNode& get_state();
65
66         void set_session (Session*);
67         Session* session() const { return _session; }
68
69         bool master_invalid_this_cycle() const { return _master_invalid_this_cycle; }
70
71         boost::shared_ptr<TransportMaster> master_by_type (SyncSource src) const;
72         boost::shared_ptr<TransportMaster> master_by_name (std::string const &) const;
73
74         TransportMasters const & transport_masters() const { return _transport_masters; }
75
76         static const std::string state_node_name;
77
78   private:
79         TransportMasterManager();
80
81         TransportMasters      _transport_masters;
82         mutable Glib::Threads::RWLock  lock;
83         double                _master_speed;
84         samplepos_t           _master_position;
85         boost::shared_ptr<TransportMaster>    _current_master;
86         Session* _session;
87
88         bool _master_invalid_this_cycle;
89
90         // a DLL to chase the transport master
91
92         int    transport_dll_initstate;
93         double t0; /// time at the beginning of ???
94         double t1; /// calculated end of the ???
95         double e2; /// second order loop error
96         double bandwidth; /// DLL filter bandwidth
97         double b, c, omega; /// DLL filter coefficients
98
99         void init_transport_master_dll (double speed, samplepos_t pos);
100         int master_dll_initstate;
101
102         static TransportMasterManager* _instance;
103
104         int add_locked (boost::shared_ptr<TransportMaster>);
105         double compute_matching_master_speed (pframes_t nframes, samplepos_t, bool& locate_required);
106         int set_current_locked (boost::shared_ptr<TransportMaster>);
107
108         PBD::ScopedConnection config_connection;
109         void parameter_changed (std::string const & what);
110 };
111
112 } // namespace ARDOUR
113
114 #endif /* __ardour_transport_master_manager_h__ */