strip silence: wait for analysis to complete
[ardour.git] / gtk2_ardour / strip_silence_dialog.cc
1 /*
2     Copyright (C) 2009 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 <gtkmm/table.h>
23 #include <gtkmm/label.h>
24 #include <gtkmm/stock.h>
25
26 #include "ardour/audioregion.h"
27 #include "ardour/dB.h"
28 #include "ardour_ui.h"
29
30 #include "audio_clock.h"
31 #include "gui_thread.h"
32 #include "strip_silence_dialog.h"
33 #include "region_view.h"
34 #include "rgb_macros.h"
35 #include "i18n.h"
36 #include "logmeter.h"
37
38 using namespace ARDOUR;
39 using namespace std;
40 using namespace ArdourCanvas;
41
42 /** Construct Strip silence dialog box */
43 StripSilenceDialog::StripSilenceDialog (Session* s, list<RegionView*> const & v)
44         : ArdourDialog (_("Strip Silence"))
45         , ProgressReporter ()
46         , _minimum_length (new AudioClock (X_("silence duration"), true, "", true, false, true, false))
47         , _fade_length (new AudioClock (X_("silence duration"), true, "", true, false, true, false))
48         , _destroying (false)
49 {
50         set_session (s);
51
52         for (list<RegionView*>::const_iterator r = v.begin(); r != v.end(); ++r) {
53                 views.push_back (ViewInterval (*r));
54         }
55
56         Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
57
58         Gtk::Table* table = Gtk::manage (new Gtk::Table (3, 3));
59         table->set_spacings (6);
60
61         int n = 0;
62
63         table->attach (*Gtk::manage (new Gtk::Label (_("Threshold"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
64         table->attach (_threshold, 1, 2, n, n + 1, Gtk::FILL);
65         table->attach (*Gtk::manage (new Gtk::Label (_("dbFS"))), 2, 3, n, n + 1, Gtk::FILL);
66         ++n;
67
68         _threshold.set_digits (1);
69         _threshold.set_increments (1, 10);
70         _threshold.set_range (-120, 0);
71         _threshold.set_value (-60);
72         _threshold.set_activates_default ();
73
74         table->attach (*Gtk::manage (new Gtk::Label (_("Minimum length"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
75         table->attach (*_minimum_length, 1, 2, n, n + 1, Gtk::FILL);
76         ++n;
77
78         _minimum_length->set_session (s);
79         _minimum_length->set_mode (AudioClock::Frames);
80         _minimum_length->set (1000, true);
81
82         table->attach (*Gtk::manage (new Gtk::Label (_("Fade length"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
83         table->attach (*_fade_length, 1, 2, n, n + 1, Gtk::FILL);
84         ++n;
85
86         _fade_length->set_session (s);
87         _fade_length->set_mode (AudioClock::Frames);
88         _fade_length->set (64, true);
89
90         hbox->pack_start (*table);
91
92         get_vbox()->pack_start (*hbox, false, false);
93
94         cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
95         apply_button = add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
96         set_default_response (Gtk::RESPONSE_OK);
97
98         get_vbox()->pack_start (_progress_bar, true, true, 12);
99
100         show_all ();
101
102         _threshold.get_adjustment()->signal_value_changed().connect (sigc::mem_fun (*this, &StripSilenceDialog::threshold_changed));
103         _minimum_length->ValueChanged.connect (sigc::mem_fun (*this, &StripSilenceDialog::restart_thread));
104         _fade_length->ValueChanged.connect (sigc::mem_fun (*this, &StripSilenceDialog::restart_thread));
105
106         update_silence_rects ();
107         update_threshold_line ();
108
109         _progress_bar.set_text (_("Analyzing"));
110         apply_button->set_sensitive (false);
111
112         /* Create a thread which runs while the dialogue is open to compute the silence regions */
113         Completed.connect (_completed_connection, invalidator(*this), boost::bind (&StripSilenceDialog::update, this), gui_context ());
114         _thread_should_finish = false;
115         pthread_create (&_thread, 0, StripSilenceDialog::_detection_thread_work, this);
116 }
117
118
119 StripSilenceDialog::~StripSilenceDialog ()
120 {
121         _destroying = true;
122
123         /* Terminate our thread */
124         _lock.lock ();
125         _interthread_info.cancel = true;
126         _thread_should_finish = true;
127         _lock.unlock ();
128
129         _run_cond.signal ();
130         pthread_join (_thread, 0);
131
132         delete _minimum_length;
133         delete _fade_length;
134 }
135
136 void
137 StripSilenceDialog::silences (AudioIntervalMap& m)
138 {
139         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
140                 pair<boost::shared_ptr<Region>,AudioIntervalResult> newpair (v->view->region(), v->intervals);
141                 m.insert (newpair);
142         }
143 }
144
145 void
146 StripSilenceDialog::drop_rects ()
147 {
148         // called by parent when starting to progess (dialog::run returned),
149         // but before the dialog is destoyed.
150
151         _interthread_info.cancel = true;
152
153         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
154                 v->view->drop_silent_frames ();
155         }
156
157         cancel_button->set_sensitive (false);
158         apply_button->set_sensitive (false);
159 }
160
161 void
162 StripSilenceDialog::update_threshold_line ()
163 {
164 #if 0
165         int n = 0;
166
167         /* Don't need to lock here as we're not reading the _waves silence details */
168
169         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
170                 (*i)->threshold_line->property_x1() = 0;
171                 (*i)->threshold_line->property_x2() = _wave_width;
172
173                 double const y = alt_log_meter (_threshold.get_value());
174
175                 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
176                 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
177         }
178
179         ++n;
180 #endif
181 }
182
183 void
184 StripSilenceDialog::update ()
185 {
186         update_threshold_line ();
187         update_silence_rects ();
188         _progress_bar.set_text ("");
189         apply_button->set_sensitive(true);
190 }
191
192 void
193 StripSilenceDialog::update_silence_rects ()
194 {
195         /* Lock so that we don't contend with the detection thread for access to the silence regions */
196         Glib::Threads::Mutex::Lock lm (_lock);
197         double const y = _threshold.get_value();
198
199         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
200                 v->view->set_silent_frames (v->intervals, y);
201         }
202 }
203
204 void *
205 StripSilenceDialog::_detection_thread_work (void* arg)
206 {
207         StripSilenceDialog* d = reinterpret_cast<StripSilenceDialog*> (arg);
208         return d->detection_thread_work ();
209 }
210
211 /** Body of our silence detection thread */
212 void *
213 StripSilenceDialog::detection_thread_work ()
214 {
215         ARDOUR_UI::instance()->register_thread ("gui", pthread_self(), "silence", 32);
216
217         /* Hold this lock when we are doing work */
218         _lock.lock ();
219
220         while (1) {
221                 for (list<ViewInterval>::iterator i = views.begin(); i != views.end(); ++i) {
222                         boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> ((*i).view->region());
223
224                         if (ar) {
225                                 i->intervals = ar->find_silence (dB_to_coefficient (threshold ()), minimum_length (), fade_length(), _interthread_info);
226                         }
227
228                         if (_interthread_info.cancel) {
229                                 break;
230                         }
231                 }
232
233                 if (!_interthread_info.cancel) {
234                         Completed (); /* EMIT SIGNAL */
235                 }
236
237                 /* Our work is done; sleep until there is more to do.
238                  * The lock is released while we are waiting.
239                  */
240                 _run_cond.wait (_lock);
241
242                 if (_thread_should_finish) {
243                         _lock.unlock ();
244                         return 0;
245                 }
246         }
247
248         return 0;
249 }
250
251 void
252 StripSilenceDialog::restart_thread ()
253 {
254         if (_destroying) {
255                 /* I don't know how this happens, but it seems to be possible for this
256                    method to be called after our destructor has finished executing.
257                    If this happens, bad things follow; _lock cannot be locked and
258                    Ardour hangs.  So if we are destroying, just bail early.
259                    */
260                 return;
261         }
262
263         _progress_bar.set_text (_("Analyzing"));
264         apply_button->set_sensitive (false);
265
266         /* Cancel any current run */
267         _interthread_info.cancel = true;
268
269         /* Block until the thread waits() */
270         _lock.lock ();
271         /* Reset the flag */
272         _interthread_info.cancel = false;
273         _lock.unlock ();
274
275         /* And re-awake the thread */
276         _run_cond.signal ();
277 }
278
279 void
280 StripSilenceDialog::threshold_changed ()
281 {
282         update_threshold_line ();
283         restart_thread ();
284 }
285
286 framecnt_t
287 StripSilenceDialog::minimum_length () const
288 {
289         return std::max((framecnt_t)1, _minimum_length->current_duration (views.front().view->region()->position()));
290 }
291
292 framecnt_t
293 StripSilenceDialog::fade_length () const
294 {
295         return std::max((framecnt_t)0, _fade_length->current_duration (views.front().view->region()->position()));
296 }
297
298 void
299 StripSilenceDialog::update_progress_gui (float p)
300 {
301         _progress_bar.set_fraction (p);
302 }