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)
54 , _threshold_value (-60)
55 , _minimum_length_value (1000)
56 , _fade_length_value (64)
60 for (list<RegionView*>::const_iterator r = v.begin(); r != v.end(); ++r) {
61 views.push_back (ViewInterval (*r));
64 Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
66 Gtk::Table* table = Gtk::manage (new Gtk::Table (3, 3));
67 table->set_spacings (6);
69 //get the last used settings for this
70 XMLNode* node = _session->extra_xml(X_("StripSilence"));
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);
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);
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 ();
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);
94 _minimum_length->set_session (s);
95 _minimum_length->set_mode (AudioClock::Samples);
96 _minimum_length->set (_minimum_length_value, true);
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);
102 _fade_length->set_session (s);
103 _fade_length->set_mode (AudioClock::Samples);
104 _fade_length->set (_fade_length_value, true);
106 hbox->pack_start (*table);
108 get_vbox()->pack_start (*hbox, false, false);
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);
114 get_vbox()->pack_start (_progress_bar, true, true, 12);
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));
122 update_silence_rects ();
123 update_threshold_line ();
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));
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);
135 signal_response().connect(sigc::mem_fun (*this, &StripSilenceDialog::finished));
139 StripSilenceDialog::~StripSilenceDialog ()
142 progress_idle_connection.disconnect();
144 /* Terminate our thread */
145 _interthread_info.cancel = true;
147 _thread_should_finish = true;
151 pthread_join (_thread, 0);
153 delete _minimum_length;
158 StripSilenceDialog::idle_update_progress()
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);
173 StripSilenceDialog::silences (AudioIntervalMap& m)
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);
182 StripSilenceDialog::drop_rects ()
184 // called by parent when starting to progess (dialog::run returned),
185 // but before the dialog is destoyed.
187 _interthread_info.cancel = true;
189 /* Block until the thread is idle */
193 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
194 v->view->drop_silent_frames ();
197 cancel_button->set_sensitive (false);
198 apply_button->set_sensitive (false);
202 StripSilenceDialog::update_threshold_line ()
207 /* Don't need to lock here as we're not reading the _waves silence details */
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;
213 double const y = alt_log_meter (_threshold.get_value());
215 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
216 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
224 StripSilenceDialog::update ()
226 update_threshold_line ();
227 update_silence_rects ();
228 _progress_bar.set_text ("");
229 update_progress_gui (0);
230 apply_button->set_sensitive(true);
234 StripSilenceDialog::update_silence_rects ()
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();
240 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
241 v->view->set_silent_frames (v->intervals, y);
246 StripSilenceDialog::_detection_thread_work (void* arg)
248 StripSilenceDialog* d = reinterpret_cast<StripSilenceDialog*> (arg);
249 return d->detection_thread_work ();
252 /** Body of our silence detection thread */
254 StripSilenceDialog::detection_thread_work ()
256 /* Do not register with all UIs, but do register with the GUI,
257 because we will need to queue some GUI (only) requests
259 ARDOUR_UI::instance()->register_thread (pthread_self(), "silence", 32);
261 /* Hold this lock when we are doing work */
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());
271 i->intervals = ar->find_silence (dB_to_coefficient (threshold ()), minimum_length (), fade_length(), _interthread_info);
274 if (_interthread_info.cancel) {
277 ++analysis_progress_cur;
278 _interthread_info.progress = 1.0;
282 analysis_progress_max = 0;
284 if (!_interthread_info.cancel) {
285 Completed (); /* EMIT SIGNAL */
288 /* Our work is done; sleep until there is more to do.
289 * The lock is released while we are waiting.
291 _run_cond.wait (_lock);
293 if (_thread_should_finish) {
303 StripSilenceDialog::restart_thread ()
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.
314 _progress_bar.set_text (_("Analyzing"));
315 update_progress_gui (0);
316 apply_button->set_sensitive (false);
318 /* Cancel any current run */
319 _interthread_info.cancel = true;
321 /* Block until the thread waits() */
324 _interthread_info.cancel = false;
327 /* And re-awake the thread */
332 StripSilenceDialog::threshold_changed ()
334 update_threshold_line ();
339 StripSilenceDialog::minimum_length () const
341 return std::max((samplecnt_t)1, _minimum_length->current_duration (views.front().view->region()->position()));
345 StripSilenceDialog::fade_length () const
347 return std::max((samplecnt_t)0, _fade_length->current_duration (views.front().view->region()->position()));
351 StripSilenceDialog::update_progress_gui (float p)
353 _progress_bar.set_fraction (p);
357 StripSilenceDialog::get_state ()
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());
367 StripSilenceDialog::set_state (const XMLNode &)
372 StripSilenceDialog::finished(int response)
374 if(response == Gtk::RESPONSE_OK) {
375 _session->add_extra_xml(get_state());