Fix processor box for no crash on click
[ardour.git] / gtk2_ardour / strip_silence_dialog.cc
1 /*
2  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <iostream>
23
24 #include <gtkmm/table.h>
25 #include <gtkmm/label.h>
26 #include <gtkmm/progressbar.h>
27 #include <gtkmm/stock.h>
28
29 #include "ardour/audioregion.h"
30 #include "ardour/dB.h"
31 #include "ardour/logmeter.h"
32 #include "ardour_ui.h"
33
34 #include "audio_clock.h"
35 #include "gui_thread.h"
36 #include "strip_silence_dialog.h"
37 #include "region_view.h"
38 #include "rgb_macros.h"
39 #include "pbd/i18n.h"
40
41 using namespace ARDOUR;
42 using namespace std;
43 using namespace ArdourCanvas;
44
45 /** Construct Strip silence dialog box */
46 StripSilenceDialog::StripSilenceDialog (Session* s, list<RegionView*> const & v)
47         : ArdourDialog (_("Strip Silence"))
48         , ProgressReporter ()
49         , _minimum_length (new AudioClock (X_("silence duration"), true, "", true, false, true, false))
50         , _fade_length (new AudioClock (X_("silence duration"), true, "", true, false, true, false))
51         , _destroying (false)
52         , analysis_progress_cur (0)
53         , analysis_progress_max (0)
54 {
55         set_session (s);
56
57         for (list<RegionView*>::const_iterator r = v.begin(); r != v.end(); ++r) {
58                 views.push_back (ViewInterval (*r));
59         }
60
61         Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
62
63         Gtk::Table* table = Gtk::manage (new Gtk::Table (3, 3));
64         table->set_spacings (6);
65
66         int n = 0;
67
68         table->attach (*Gtk::manage (new Gtk::Label (_("Threshold"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
69         table->attach (_threshold, 1, 2, n, n + 1, Gtk::FILL);
70         table->attach (*Gtk::manage (new Gtk::Label (_("dBFS"))), 2, 3, n, n + 1, Gtk::FILL);
71         ++n;
72
73         _threshold.set_digits (1);
74         _threshold.set_increments (1, 10);
75         _threshold.set_range (-120, 0);
76         _threshold.set_value (-60);
77         _threshold.set_activates_default ();
78
79         table->attach (*Gtk::manage (new Gtk::Label (_("Minimum length"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
80         table->attach (*_minimum_length, 1, 2, n, n + 1, Gtk::FILL);
81         ++n;
82
83         _minimum_length->set_session (s);
84         _minimum_length->set_mode (AudioClock::Samples);
85         _minimum_length->set (1000, true);
86
87         table->attach (*Gtk::manage (new Gtk::Label (_("Fade length"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
88         table->attach (*_fade_length, 1, 2, n, n + 1, Gtk::FILL);
89         ++n;
90
91         _fade_length->set_session (s);
92         _fade_length->set_mode (AudioClock::Samples);
93         _fade_length->set (64, true);
94
95         hbox->pack_start (*table);
96
97         get_vbox()->pack_start (*hbox, false, false);
98
99         cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
100         apply_button = add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
101         set_default_response (Gtk::RESPONSE_OK);
102
103         get_vbox()->pack_start (_progress_bar, true, true, 12);
104
105         show_all ();
106
107         _threshold.get_adjustment()->signal_value_changed().connect (sigc::mem_fun (*this, &StripSilenceDialog::threshold_changed));
108         _minimum_length->ValueChanged.connect (sigc::mem_fun (*this, &StripSilenceDialog::restart_thread));
109         _fade_length->ValueChanged.connect (sigc::mem_fun (*this, &StripSilenceDialog::restart_thread));
110
111         update_silence_rects ();
112         update_threshold_line ();
113
114         _progress_bar.set_text (_("Analyzing"));
115         update_progress_gui (0);
116         apply_button->set_sensitive (false);
117         progress_idle_connection = Glib::signal_idle().connect (sigc::mem_fun (*this, &StripSilenceDialog::idle_update_progress));
118
119         /* Create a thread which runs while the dialogue is open to compute the silence regions */
120         Completed.connect (_completed_connection, invalidator(*this), boost::bind (&StripSilenceDialog::update, this), gui_context ());
121         _thread_should_finish = false;
122         pthread_create (&_thread, 0, StripSilenceDialog::_detection_thread_work, this);
123 }
124
125
126 StripSilenceDialog::~StripSilenceDialog ()
127 {
128         _destroying = true;
129         progress_idle_connection.disconnect();
130
131         /* Terminate our thread */
132         _interthread_info.cancel = true;
133         _lock.lock ();
134         _thread_should_finish = true;
135         _lock.unlock ();
136
137         _run_cond.signal ();
138         pthread_join (_thread, 0);
139
140         delete _minimum_length;
141         delete _fade_length;
142 }
143
144 bool
145 StripSilenceDialog::idle_update_progress()
146 {
147         if (analysis_progress_max > 0) {
148                 // AudioRegion::find_silence() has
149                 // itt.progress = (end - pos) / length
150                 // not sure if that's intentional, but let's use (1. - val)
151                 float rp = std::min(1.f, std::max (0.f, (1.f - _interthread_info.progress)));
152                 float p = analysis_progress_cur / (float) analysis_progress_max
153                         + rp / (float) analysis_progress_max;
154                 update_progress_gui (p);
155         }
156         return !_destroying;
157 }
158
159 void
160 StripSilenceDialog::silences (AudioIntervalMap& m)
161 {
162         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
163                 pair<boost::shared_ptr<Region>,AudioIntervalResult> newpair (v->view->region(), v->intervals);
164                 m.insert (newpair);
165         }
166 }
167
168 void
169 StripSilenceDialog::drop_rects ()
170 {
171         // called by parent when starting to progess (dialog::run returned),
172         // but before the dialog is destoyed.
173
174         _interthread_info.cancel = true;
175
176         /* Block until the thread is idle */
177         _lock.lock ();
178         _lock.unlock ();
179
180         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
181                 v->view->drop_silent_frames ();
182         }
183
184         cancel_button->set_sensitive (false);
185         apply_button->set_sensitive (false);
186 }
187
188 void
189 StripSilenceDialog::update_threshold_line ()
190 {
191 #if 0
192         int n = 0;
193
194         /* Don't need to lock here as we're not reading the _waves silence details */
195
196         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
197                 (*i)->threshold_line->property_x1() = 0;
198                 (*i)->threshold_line->property_x2() = _wave_width;
199
200                 double const y = alt_log_meter (_threshold.get_value());
201
202                 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
203                 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
204         }
205
206         ++n;
207 #endif
208 }
209
210 void
211 StripSilenceDialog::update ()
212 {
213         update_threshold_line ();
214         update_silence_rects ();
215         _progress_bar.set_text ("");
216         update_progress_gui (0);
217         apply_button->set_sensitive(true);
218 }
219
220 void
221 StripSilenceDialog::update_silence_rects ()
222 {
223         /* Lock so that we don't contend with the detection thread for access to the silence regions */
224         Glib::Threads::Mutex::Lock lm (_lock);
225         double const y = _threshold.get_value();
226
227         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
228                 v->view->set_silent_frames (v->intervals, y);
229         }
230 }
231
232 void *
233 StripSilenceDialog::_detection_thread_work (void* arg)
234 {
235         StripSilenceDialog* d = reinterpret_cast<StripSilenceDialog*> (arg);
236         return d->detection_thread_work ();
237 }
238
239 /** Body of our silence detection thread */
240 void *
241 StripSilenceDialog::detection_thread_work ()
242 {
243         /* Do not register with all UIs, but do register with the GUI,
244            because we will need to queue some GUI (only) requests
245         */
246         ARDOUR_UI::instance()->register_thread (pthread_self(), "silence", 32);
247
248         /* Hold this lock when we are doing work */
249         _lock.lock ();
250
251         while (1) {
252                 analysis_progress_cur = 0;
253                 analysis_progress_max = views.size();
254                 for (list<ViewInterval>::iterator i = views.begin(); i != views.end(); ++i) {
255                         boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> ((*i).view->region());
256
257                         if (ar) {
258                                 i->intervals = ar->find_silence (dB_to_coefficient (threshold ()), minimum_length (), fade_length(), _interthread_info);
259                         }
260
261                         if (_interthread_info.cancel) {
262                                 break;
263                         }
264                         ++analysis_progress_cur;
265                         _interthread_info.progress = 1.0;
266                         ARDOUR::GUIIdle ();
267                 }
268
269                 analysis_progress_max = 0;
270
271                 if (!_interthread_info.cancel) {
272                         Completed (); /* EMIT SIGNAL */
273                 }
274
275                 /* Our work is done; sleep until there is more to do.
276                  * The lock is released while we are waiting.
277                  */
278                 _run_cond.wait (_lock);
279
280                 if (_thread_should_finish) {
281                         _lock.unlock ();
282                         return 0;
283                 }
284         }
285
286         return 0;
287 }
288
289 void
290 StripSilenceDialog::restart_thread ()
291 {
292         if (_destroying) {
293                 /* I don't know how this happens, but it seems to be possible for this
294                    method to be called after our destructor has finished executing.
295                    If this happens, bad things follow; _lock cannot be locked and
296                    Ardour hangs.  So if we are destroying, just bail early.
297                    */
298                 return;
299         }
300
301         _progress_bar.set_text (_("Analyzing"));
302         update_progress_gui (0);
303         apply_button->set_sensitive (false);
304
305         /* Cancel any current run */
306         _interthread_info.cancel = true;
307
308         /* Block until the thread waits() */
309         _lock.lock ();
310         /* Reset the flag */
311         _interthread_info.cancel = false;
312         _lock.unlock ();
313
314         /* And re-awake the thread */
315         _run_cond.signal ();
316 }
317
318 void
319 StripSilenceDialog::threshold_changed ()
320 {
321         update_threshold_line ();
322         restart_thread ();
323 }
324
325 samplecnt_t
326 StripSilenceDialog::minimum_length () const
327 {
328         return std::max((samplecnt_t)1, _minimum_length->current_duration (views.front().view->region()->position()));
329 }
330
331 samplecnt_t
332 StripSilenceDialog::fade_length () const
333 {
334         return std::max((samplecnt_t)0, _fade_length->current_duration (views.front().view->region()->position()));
335 }
336
337 void
338 StripSilenceDialog::update_progress_gui (float p)
339 {
340         _progress_bar.set_fraction (p);
341 }