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>
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.
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.
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.
24 #include <gtkmm/table.h>
25 #include <gtkmm/label.h>
26 #include <gtkmm/progressbar.h>
27 #include <gtkmm/stock.h>
29 #include "ardour/audioregion.h"
30 #include "ardour/dB.h"
31 #include "ardour/logmeter.h"
32 #include "ardour_ui.h"
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"
41 using namespace ARDOUR;
43 using namespace ArdourCanvas;
45 /** Construct Strip silence dialog box */
46 StripSilenceDialog::StripSilenceDialog (Session* s, list<RegionView*> const & v)
47 : ArdourDialog (_("Strip Silence"))
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))
52 , analysis_progress_cur (0)
53 , analysis_progress_max (0)
57 for (list<RegionView*>::const_iterator r = v.begin(); r != v.end(); ++r) {
58 views.push_back (ViewInterval (*r));
61 Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
63 Gtk::Table* table = Gtk::manage (new Gtk::Table (3, 3));
64 table->set_spacings (6);
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);
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 ();
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);
83 _minimum_length->set_session (s);
84 _minimum_length->set_mode (AudioClock::Samples);
85 _minimum_length->set (1000, true);
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);
91 _fade_length->set_session (s);
92 _fade_length->set_mode (AudioClock::Samples);
93 _fade_length->set (64, true);
95 hbox->pack_start (*table);
97 get_vbox()->pack_start (*hbox, false, false);
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);
103 get_vbox()->pack_start (_progress_bar, true, true, 12);
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));
111 update_silence_rects ();
112 update_threshold_line ();
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));
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);
126 StripSilenceDialog::~StripSilenceDialog ()
129 progress_idle_connection.disconnect();
131 /* Terminate our thread */
132 _interthread_info.cancel = true;
134 _thread_should_finish = true;
138 pthread_join (_thread, 0);
140 delete _minimum_length;
145 StripSilenceDialog::idle_update_progress()
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);
160 StripSilenceDialog::silences (AudioIntervalMap& m)
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);
169 StripSilenceDialog::drop_rects ()
171 // called by parent when starting to progess (dialog::run returned),
172 // but before the dialog is destoyed.
174 _interthread_info.cancel = true;
176 /* Block until the thread is idle */
180 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
181 v->view->drop_silent_frames ();
184 cancel_button->set_sensitive (false);
185 apply_button->set_sensitive (false);
189 StripSilenceDialog::update_threshold_line ()
194 /* Don't need to lock here as we're not reading the _waves silence details */
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;
200 double const y = alt_log_meter (_threshold.get_value());
202 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
203 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
211 StripSilenceDialog::update ()
213 update_threshold_line ();
214 update_silence_rects ();
215 _progress_bar.set_text ("");
216 update_progress_gui (0);
217 apply_button->set_sensitive(true);
221 StripSilenceDialog::update_silence_rects ()
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();
227 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
228 v->view->set_silent_frames (v->intervals, y);
233 StripSilenceDialog::_detection_thread_work (void* arg)
235 StripSilenceDialog* d = reinterpret_cast<StripSilenceDialog*> (arg);
236 return d->detection_thread_work ();
239 /** Body of our silence detection thread */
241 StripSilenceDialog::detection_thread_work ()
243 /* Do not register with all UIs, but do register with the GUI,
244 because we will need to queue some GUI (only) requests
246 ARDOUR_UI::instance()->register_thread (pthread_self(), "silence", 32);
248 /* Hold this lock when we are doing work */
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());
258 i->intervals = ar->find_silence (dB_to_coefficient (threshold ()), minimum_length (), fade_length(), _interthread_info);
261 if (_interthread_info.cancel) {
264 ++analysis_progress_cur;
265 _interthread_info.progress = 1.0;
269 analysis_progress_max = 0;
271 if (!_interthread_info.cancel) {
272 Completed (); /* EMIT SIGNAL */
275 /* Our work is done; sleep until there is more to do.
276 * The lock is released while we are waiting.
278 _run_cond.wait (_lock);
280 if (_thread_should_finish) {
290 StripSilenceDialog::restart_thread ()
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.
301 _progress_bar.set_text (_("Analyzing"));
302 update_progress_gui (0);
303 apply_button->set_sensitive (false);
305 /* Cancel any current run */
306 _interthread_info.cancel = true;
308 /* Block until the thread waits() */
311 _interthread_info.cancel = false;
314 /* And re-awake the thread */
319 StripSilenceDialog::threshold_changed ()
321 update_threshold_line ();
326 StripSilenceDialog::minimum_length () const
328 return std::max((samplecnt_t)1, _minimum_length->current_duration (views.front().view->region()->position()));
332 StripSilenceDialog::fade_length () const
334 return std::max((samplecnt_t)0, _fade_length->current_duration (views.front().view->region()->position()));
338 StripSilenceDialog::update_progress_gui (float p)
340 _progress_bar.set_fraction (p);