likely fixes for most remaining issues with data in automation/control lists, but...
[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 "pbd/compose.h"
23
24 #include "ardour/automation_control.h"
25 #include "ardour/automation_watch.h"
26 #include "ardour/debug.h"
27 #include "ardour/session.h"
28
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 AutomationWatch* AutomationWatch::_instance = 0;
33
34 AutomationWatch&
35 AutomationWatch::instance ()
36 {
37         if (_instance == 0) {
38                 _instance = new AutomationWatch;
39         }
40         return *_instance;
41 }
42
43 AutomationWatch::AutomationWatch ()
44         : _thread (0)
45         , _run_thread (false)
46 {
47         
48 }
49
50 AutomationWatch::~AutomationWatch ()
51 {
52         if (_thread) {
53                 _run_thread = false;
54                 _thread->join ();
55                 _thread = 0;
56         }
57
58         Glib::Threads::Mutex::Lock lm (automation_watch_lock);
59         automation_watches.clear ();
60 }
61
62 void
63 AutomationWatch::add_automation_watch (boost::shared_ptr<AutomationControl> ac)
64 {
65         Glib::Threads::Mutex::Lock lm (automation_watch_lock);
66         DEBUG_TRACE (DEBUG::Automation, string_compose ("now watching control %1 for automation, astate = %2\n", ac->name(), enum_2_string (ac->automation_state())));
67         automation_watches.insert (ac);
68
69         /* if an automation control is added here while the transport is
70          * rolling, make sure that it knows that there is a write pass going
71          * on, rather than waiting for the transport to start.
72          */
73
74         if (_session && _session->transport_rolling() && ac->alist()->automation_write()) {
75                 DEBUG_TRACE (DEBUG::Automation, string_compose ("\ttransport is rolling @ %1, so enter write pass\n", _session->transport_speed()));
76                 ac->list()->set_in_write_pass (true);
77         }
78
79         /* we can't store shared_ptr<Destructible> in connections because it
80          * creates reference cycles. we don't need to make the weak_ptr<>
81          * explicit here, but it helps to remind us what is going on.
82          */
83         
84         boost::weak_ptr<AutomationControl> wac (ac);
85         ac->DropReferences.connect_same_thread (*this, boost::bind (&AutomationWatch::remove_weak_automation_watch, this, wac));
86 }
87
88 void
89 AutomationWatch::remove_weak_automation_watch (boost::weak_ptr<AutomationControl> wac)
90 {
91         boost::shared_ptr<AutomationControl> ac = wac.lock();
92
93         if (!ac) {
94                 return;
95         }
96
97         remove_automation_watch (ac);
98 }
99
100 void
101 AutomationWatch::remove_automation_watch (boost::shared_ptr<AutomationControl> ac)
102 {
103         Glib::Threads::Mutex::Lock lm (automation_watch_lock);
104         DEBUG_TRACE (DEBUG::Automation, string_compose ("remove control %1 from automation watch\n", ac->name()));
105         automation_watches.erase (ac);
106         ac->list()->set_in_write_pass (false);
107 }
108
109 gint
110 AutomationWatch::timer ()
111 {
112         if (!_session || !_session->transport_rolling()) {
113                 return TRUE;
114         }
115
116         {
117                 Glib::Threads::Mutex::Lock lm (automation_watch_lock);
118
119                 framepos_t time = _session->audible_frame ();
120
121                 for (AutomationWatches::iterator aw = automation_watches.begin(); aw != automation_watches.end(); ++aw) {
122                         if ((*aw)->alist()->automation_write()) {
123                                 (*aw)->list()->add (time, (*aw)->user_double());
124                         }
125                 }
126         }
127
128         return TRUE;
129 }
130
131 void
132 AutomationWatch::thread ()
133 {
134         while (_run_thread) {
135                 usleep (100000); // Config->get_automation_interval() * 10);
136                 timer ();
137         }
138 }
139
140 void
141 AutomationWatch::set_session (Session* s)
142 {
143         transport_connection.disconnect ();
144
145         if (_thread) {
146                 _run_thread = false;
147                 _thread->join ();
148                 _thread = 0;
149         }
150
151         SessionHandlePtr::set_session (s);
152
153         if (_session) {
154                 _run_thread = true;
155                 _thread = Glib::Threads::Thread::create (boost::bind (&AutomationWatch::thread, this));
156                 
157                 _session->TransportStateChange.connect_same_thread (transport_connection, boost::bind (&AutomationWatch::transport_state_change, this));
158         }
159 }
160
161 void
162 AutomationWatch::transport_state_change ()
163 {
164         if (!_session) {
165                 return;
166         }
167
168         bool rolling = _session->transport_rolling();
169
170         {
171                 Glib::Threads::Mutex::Lock lm (automation_watch_lock);
172
173                 for (AutomationWatches::iterator aw = automation_watches.begin(); aw != automation_watches.end(); ++aw) {
174                         DEBUG_TRACE (DEBUG::Automation, string_compose ("%1: transport state changed, speed %2, in write pass ? %3 writing ? %4\n", 
175                                                                         (*aw)->name(), _session->transport_speed(), rolling,
176                                                                         (*aw)->alist()->automation_write()));
177                         if (rolling && (*aw)->alist()->automation_write()) {
178                                 (*aw)->list()->set_in_write_pass (true);
179                         } else {
180                                 (*aw)->list()->set_in_write_pass (false);
181                         }
182                 }
183         }
184 }