Abstract definition of rt-scheduler policy
[ardour.git] / libs / ardour / automation_watch.cc
1 /*
2     Copyright (C) 2012 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 #include <iostream>
21
22 #include <glibmm/timer.h>
23
24 #include "pbd/compose.h"
25 #include "pbd/pthread_utils.h"
26
27 #include "ardour/automation_control.h"
28 #include "ardour/automation_watch.h"
29 #include "ardour/debug.h"
30 #include "ardour/session.h"
31
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 AutomationWatch* AutomationWatch::_instance = 0;
36
37 AutomationWatch&
38 AutomationWatch::instance ()
39 {
40         if (_instance == 0) {
41                 _instance = new AutomationWatch;
42         }
43         return *_instance;
44 }
45
46 AutomationWatch::AutomationWatch ()
47         : _thread (0)
48         , _last_time (0)
49         , _run_thread (false)
50 {
51
52 }
53
54 AutomationWatch::~AutomationWatch ()
55 {
56         if (_thread) {
57                 _run_thread = false;
58                 _thread->join ();
59                 _thread = 0;
60         }
61
62         Glib::Threads::Mutex::Lock lm (automation_watch_lock);
63         automation_watches.clear ();
64         automation_connections.clear ();
65 }
66
67 void
68 AutomationWatch::add_automation_watch (boost::shared_ptr<AutomationControl> ac)
69 {
70         Glib::Threads::Mutex::Lock lm (automation_watch_lock);
71         DEBUG_TRACE (DEBUG::Automation, string_compose ("now watching control %1 for automation, astate = %2\n", ac->name(), enum_2_string (ac->automation_state())));
72         std::pair<AutomationWatches::iterator, bool> r = automation_watches.insert (ac);
73
74         if (!r.second) {
75                 return;
76         }
77
78         /* if an automation control is added here while the transport is
79          * rolling, make sure that it knows that there is a write pass going
80          * on, rather than waiting for the transport to start.
81          */
82
83         if (_session && _session->transport_rolling() && ac->alist()->automation_write()) {
84                 DEBUG_TRACE (DEBUG::Automation, string_compose ("\ttransport is rolling @ %1, audible = %2so enter write pass\n",
85                                                                 _session->transport_speed(), _session->audible_frame()));
86                 /* add a guard point since we are already moving */
87                 ac->list()->set_in_write_pass (true, true, _session->audible_frame());
88         }
89
90         /* we can't store shared_ptr<Destructible> in connections because it
91          * creates reference cycles. we don't need to make the weak_ptr<>
92          * explicit here, but it helps to remind us what is going on.
93          */
94
95         boost::weak_ptr<AutomationControl> wac (ac);
96         ac->DropReferences.connect_same_thread (automation_connections[ac], boost::bind (&AutomationWatch::remove_weak_automation_watch, this, wac));
97 }
98
99 void
100 AutomationWatch::remove_weak_automation_watch (boost::weak_ptr<AutomationControl> wac)
101 {
102         boost::shared_ptr<AutomationControl> ac = wac.lock();
103
104         if (!ac) {
105                 return;
106         }
107
108         remove_automation_watch (ac);
109 }
110
111 void
112 AutomationWatch::remove_automation_watch (boost::shared_ptr<AutomationControl> ac)
113 {
114         Glib::Threads::Mutex::Lock lm (automation_watch_lock);
115         DEBUG_TRACE (DEBUG::Automation, string_compose ("remove control %1 from automation watch\n", ac->name()));
116         automation_watches.erase (ac);
117         automation_connections.erase (ac);
118         ac->list()->set_in_write_pass (false);
119 }
120
121 void
122 AutomationWatch::transport_stop_automation_watches (framepos_t when)
123 {
124         DEBUG_TRACE (DEBUG::Automation, "clear all automation watches\n");
125
126         AutomationWatches tmp;
127
128         {
129                 Glib::Threads::Mutex::Lock lm (automation_watch_lock);
130                 /* copy automation watches */
131                 tmp = automation_watches;
132                 /* clear existing container so that each
133                    ::remove_automation_watch() call from
134                    AutomationControl::stop_touch() is faster.
135                 */
136
137                 automation_watches.clear ();
138                 automation_connections.clear ();
139         }
140
141         for (AutomationWatches::iterator i = tmp.begin(); i != tmp.end(); ++i) {
142                 (*i)->stop_touch (when);
143         }
144 }
145
146 gint
147 AutomationWatch::timer ()
148 {
149         if (!_session || !_session->transport_rolling()) {
150                 return TRUE;
151         }
152
153         {
154                 Glib::Threads::Mutex::Lock lm (automation_watch_lock);
155
156                 framepos_t time = _session->audible_frame ();
157                 if (time > _last_time) {  //we only write automation in the forward direction; this fixes automation-recording in a loop
158                         for (AutomationWatches::iterator aw = automation_watches.begin(); aw != automation_watches.end(); ++aw) {
159                                 if ((*aw)->alist()->automation_write()) {
160                                         double val = (*aw)->user_double();
161                                         boost::shared_ptr<SlavableAutomationControl> sc = boost::dynamic_pointer_cast<SlavableAutomationControl> (*aw);
162                                         if (sc) {
163                                                 val = sc->reduce_by_masters (val, true);
164                                         }
165                                         (*aw)->list()->add (time, val, true);
166                                 }
167                         }
168                 } else if (time != _last_time) {  //transport stopped or reversed.  stop the automation pass and start a new one (for bonus points, someday store the previous pass in an undo record)
169                         for (AutomationWatches::iterator aw = automation_watches.begin(); aw != automation_watches.end(); ++aw) {
170                                 DEBUG_TRACE (DEBUG::Automation, string_compose ("%1: transport in rewind, speed %2, in write pass ? %3 writing ? %4\n",
171                                                                                 (*aw)->name(), _session->transport_speed(), _session->transport_rolling(),
172                                                                                 (*aw)->alist()->automation_write()));
173                                 (*aw)->list()->set_in_write_pass (false);
174                                 if ( (*aw)->alist()->automation_write() ) {
175                                         (*aw)->list()->set_in_write_pass (true, time);
176                                 }
177                         }
178                 }
179
180                 _last_time = time;
181         }
182
183         return TRUE;
184 }
185
186 void
187 AutomationWatch::thread ()
188 {
189         pbd_set_thread_priority (pthread_self(), PBD_SCHED_FIFO, -25);
190         while (_run_thread) {
191                 Glib::usleep ((gulong) floor (Config->get_automation_interval_msecs() * 1000));
192                 timer ();
193         }
194 }
195
196 void
197 AutomationWatch::set_session (Session* s)
198 {
199         transport_connection.disconnect ();
200
201         if (_thread) {
202                 _run_thread = false;
203                 _thread->join ();
204                 _thread = 0;
205         }
206
207         SessionHandlePtr::set_session (s);
208
209         if (_session) {
210                 _run_thread = true;
211                 _thread = Glib::Threads::Thread::create (boost::bind (&AutomationWatch::thread, this));
212
213                 _session->TransportStateChange.connect_same_thread (transport_connection, boost::bind (&AutomationWatch::transport_state_change, this));
214         }
215 }
216
217 void
218 AutomationWatch::transport_state_change ()
219 {
220         if (!_session) {
221                 return;
222         }
223
224         bool rolling = _session->transport_rolling();
225
226         _last_time = _session->audible_frame ();
227
228         {
229                 Glib::Threads::Mutex::Lock lm (automation_watch_lock);
230
231                 for (AutomationWatches::iterator aw = automation_watches.begin(); aw != automation_watches.end(); ++aw) {
232                         DEBUG_TRACE (DEBUG::Automation, string_compose ("%1: transport state changed, speed %2, in write pass ? %3 writing ? %4\n",
233                                                                         (*aw)->name(), _session->transport_speed(), rolling,
234                                                                         (*aw)->alist()->automation_write()));
235                         if (rolling && (*aw)->alist()->automation_write()) {
236                                 (*aw)->list()->set_in_write_pass (true);
237                         } else {
238                                 (*aw)->list()->set_in_write_pass (false);
239                         }
240                 }
241         }
242 }