Reenable the correct sort column and type when redisplaying regions
[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         , _threshold_value (-60)
55         , _minimum_length_value (1000)
56         , _fade_length_value (64)
57 {
58         set_session (s);
59
60         for (list<RegionView*>::const_iterator r = v.begin(); r != v.end(); ++r) {
61                 views.push_back (ViewInterval (*r));
62         }
63
64         Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
65
66         Gtk::Table* table = Gtk::manage (new Gtk::Table (3, 3));
67         table->set_spacings (6);
68
69         //get the last used settings for this
70         XMLNode* node = _session->extra_xml(X_("StripSilence"));
71         if (node) {
72                 node->get_property(X_("threshold"), _threshold_value);
73                 node->get_property(X_("min-length"), _minimum_length_value);
74                 node->get_property(X_("fade-length"), _fade_length_value);
75         }
76
77         int n = 0;
78
79         table->attach (*Gtk::manage (new Gtk::Label (_("Threshold"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
80         table->attach (_threshold, 1, 2, n, n + 1, Gtk::FILL);
81         table->attach (*Gtk::manage (new Gtk::Label (_("dBFS"))), 2, 3, n, n + 1, Gtk::FILL);
82         ++n;
83
84         _threshold.set_digits (1);
85         _threshold.set_increments (1, 10);
86         _threshold.set_range (-120, 0);
87         _threshold.set_value (_threshold_value);
88         _threshold.set_activates_default ();
89
90         table->attach (*Gtk::manage (new Gtk::Label (_("Minimum length"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
91         table->attach (*_minimum_length, 1, 2, n, n + 1, Gtk::FILL);
92         ++n;
93
94         _minimum_length->set_session (s);
95         _minimum_length->set_mode (AudioClock::Samples);
96         _minimum_length->set (_minimum_length_value, true);
97
98         table->attach (*Gtk::manage (new Gtk::Label (_("Fade length"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
99         table->attach (*_fade_length, 1, 2, n, n + 1, Gtk::FILL);
100         ++n;
101
102         _fade_length->set_session (s);
103         _fade_length->set_mode (AudioClock::Samples);
104         _fade_length->set (_fade_length_value, true);
105
106         hbox->pack_start (*table);
107
108         get_vbox()->pack_start (*hbox, false, false);
109
110         cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
111         apply_button = add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
112         set_default_response (Gtk::RESPONSE_OK);
113
114         get_vbox()->pack_start (_progress_bar, true, true, 12);
115
116         show_all ();
117
118         _threshold.get_adjustment()->signal_value_changed().connect (sigc::mem_fun (*this, &StripSilenceDialog::threshold_changed));
119         _minimum_length->ValueChanged.connect (sigc::mem_fun (*this, &StripSilenceDialog::restart_thread));
120         _fade_length->ValueChanged.connect (sigc::mem_fun (*this, &StripSilenceDialog::restart_thread));
121
122         update_silence_rects ();
123         update_threshold_line ();
124
125         _progress_bar.set_text (_("Analyzing"));
126         update_progress_gui (0);
127         apply_button->set_sensitive (false);
128         progress_idle_connection = Glib::signal_idle().connect (sigc::mem_fun (*this, &StripSilenceDialog::idle_update_progress));
129
130         /* Create a thread which runs while the dialogue is open to compute the silence regions */
131         Completed.connect (_completed_connection, invalidator(*this), boost::bind (&StripSilenceDialog::update, this), gui_context ());
132         _thread_should_finish = false;
133         pthread_create (&_thread, 0, StripSilenceDialog::_detection_thread_work, this);
134
135         signal_response().connect(sigc::mem_fun (*this, &StripSilenceDialog::finished));
136 }
137
138
139 StripSilenceDialog::~StripSilenceDialog ()
140 {
141         _destroying = true;
142         progress_idle_connection.disconnect();
143
144         /* Terminate our thread */
145         _interthread_info.cancel = true;
146         _lock.lock ();
147         _thread_should_finish = true;
148         _lock.unlock ();
149
150         _run_cond.signal ();
151         pthread_join (_thread, 0);
152
153         delete _minimum_length;
154         delete _fade_length;
155 }
156
157 bool
158 StripSilenceDialog::idle_update_progress()
159 {
160         if (analysis_progress_max > 0) {
161                 // AudioRegion::find_silence() has
162                 // itt.progress = (end - pos) / length
163                 // not sure if that's intentional, but let's use (1. - val)
164                 float rp = std::min(1.f, std::max (0.f, (1.f - _interthread_info.progress)));
165                 float p = analysis_progress_cur / (float) analysis_progress_max
166                         + rp / (float) analysis_progress_max;
167                 update_progress_gui (p);
168         }
169         return !_destroying;
170 }
171
172 void
173 StripSilenceDialog::silences (AudioIntervalMap& m)
174 {
175         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
176                 pair<boost::shared_ptr<Region>,AudioIntervalResult> newpair (v->view->region(), v->intervals);
177                 m.insert (newpair);
178         }
179 }
180
181 void
182 StripSilenceDialog::drop_rects ()
183 {
184         // called by parent when starting to progess (dialog::run returned),
185         // but before the dialog is destoyed.
186
187         _interthread_info.cancel = true;
188
189         /* Block until the thread is idle */
190         _lock.lock ();
191         _lock.unlock ();
192
193         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
194                 v->view->drop_silent_frames ();
195         }
196
197         cancel_button->set_sensitive (false);
198         apply_button->set_sensitive (false);
199 }
200
201 void
202 StripSilenceDialog::update_threshold_line ()
203 {
204 #if 0
205         int n = 0;
206
207         /* Don't need to lock here as we're not reading the _waves silence details */
208
209         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
210                 (*i)->threshold_line->property_x1() = 0;
211                 (*i)->threshold_line->property_x2() = _wave_width;
212
213                 double const y = alt_log_meter (_threshold.get_value());
214
215                 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
216                 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
217         }
218
219         ++n;
220 #endif
221 }
222
223 void
224 StripSilenceDialog::update ()
225 {
226         update_threshold_line ();
227         update_silence_rects ();
228         _progress_bar.set_text ("");
229         update_progress_gui (0);
230         apply_button->set_sensitive(true);
231 }
232
233 void
234 StripSilenceDialog::update_silence_rects ()
235 {
236         /* Lock so that we don't contend with the detection thread for access to the silence regions */
237         Glib::Threads::Mutex::Lock lm (_lock);
238         double const y = _threshold.get_value();
239
240         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
241                 v->view->set_silent_frames (v->intervals, y);
242         }
243 }
244
245 void *
246 StripSilenceDialog::_detection_thread_work (void* arg)
247 {
248         StripSilenceDialog* d = reinterpret_cast<StripSilenceDialog*> (arg);
249         return d->detection_thread_work ();
250 }
251
252 /** Body of our silence detection thread */
253 void *
254 StripSilenceDialog::detection_thread_work ()
255 {
256         /* Do not register with all UIs, but do register with the GUI,
257            because we will need to queue some GUI (only) requests
258         */
259         ARDOUR_UI::instance()->register_thread (pthread_self(), "silence", 32);
260
261         /* Hold this lock when we are doing work */
262         _lock.lock ();
263
264         while (1) {
265                 analysis_progress_cur = 0;
266                 analysis_progress_max = views.size();
267                 for (list<ViewInterval>::iterator i = views.begin(); i != views.end(); ++i) {
268                         boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> ((*i).view->region());
269
270                         if (ar) {
271                                 i->intervals = ar->find_silence (dB_to_coefficient (threshold ()), minimum_length (), fade_length(), _interthread_info);
272                         }
273
274                         if (_interthread_info.cancel) {
275                                 break;
276                         }
277                         ++analysis_progress_cur;
278                         _interthread_info.progress = 1.0;
279                         ARDOUR::GUIIdle ();
280                 }
281
282                 analysis_progress_max = 0;
283
284                 if (!_interthread_info.cancel) {
285                         Completed (); /* EMIT SIGNAL */
286                 }
287
288                 /* Our work is done; sleep until there is more to do.
289                  * The lock is released while we are waiting.
290                  */
291                 _run_cond.wait (_lock);
292
293                 if (_thread_should_finish) {
294                         _lock.unlock ();
295                         return 0;
296                 }
297         }
298
299         return 0;
300 }
301
302 void
303 StripSilenceDialog::restart_thread ()
304 {
305         if (_destroying) {
306                 /* I don't know how this happens, but it seems to be possible for this
307                    method to be called after our destructor has finished executing.
308                    If this happens, bad things follow; _lock cannot be locked and
309                    Ardour hangs.  So if we are destroying, just bail early.
310                    */
311                 return;
312         }
313
314         _progress_bar.set_text (_("Analyzing"));
315         update_progress_gui (0);
316         apply_button->set_sensitive (false);
317
318         /* Cancel any current run */
319         _interthread_info.cancel = true;
320
321         /* Block until the thread waits() */
322         _lock.lock ();
323         /* Reset the flag */
324         _interthread_info.cancel = false;
325         _lock.unlock ();
326
327         /* And re-awake the thread */
328         _run_cond.signal ();
329 }
330
331 void
332 StripSilenceDialog::threshold_changed ()
333 {
334         update_threshold_line ();
335         restart_thread ();
336 }
337
338 samplecnt_t
339 StripSilenceDialog::minimum_length () const
340 {
341         return std::max((samplecnt_t)1, _minimum_length->current_duration (views.front().view->region()->position()));
342 }
343
344 samplecnt_t
345 StripSilenceDialog::fade_length () const
346 {
347         return std::max((samplecnt_t)0, _fade_length->current_duration (views.front().view->region()->position()));
348 }
349
350 void
351 StripSilenceDialog::update_progress_gui (float p)
352 {
353         _progress_bar.set_fraction (p);
354 }
355
356 XMLNode&
357 StripSilenceDialog::get_state ()
358 {
359         XMLNode* node = new XMLNode(X_("StripSilence"));
360         node->set_property(X_("threshold"), threshold());
361         node->set_property(X_("min-length"), minimum_length());
362         node->set_property(X_("fade-length"), fade_length());
363         return *node;
364 }
365
366 void
367 StripSilenceDialog::set_state (const XMLNode &)
368 {
369 }
370
371 void
372 StripSilenceDialog::finished(int response)
373 {
374         if(response == Gtk::RESPONSE_OK) {
375                 _session->add_extra_xml(get_state());
376         }
377 }