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