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