2 Copyright (C) 2009 Paul Davis
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.
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.
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.
22 #include <gtkmm/table.h>
23 #include <gtkmm/label.h>
24 #include <gtkmm/progressbar.h>
25 #include <gtkmm/stock.h>
27 #include "ardour/audioregion.h"
28 #include "ardour/dB.h"
29 #include "ardour/logmeter.h"
30 #include "ardour_ui.h"
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"
39 using namespace ARDOUR;
41 using namespace ArdourCanvas;
43 /** Construct Strip silence dialog box */
44 StripSilenceDialog::StripSilenceDialog (Session* s, list<RegionView*> const & v)
45 : ArdourDialog (_("Strip Silence"))
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))
50 , analysis_progress_cur (0)
51 , analysis_progress_max (0)
55 for (list<RegionView*>::const_iterator r = v.begin(); r != v.end(); ++r) {
56 views.push_back (ViewInterval (*r));
59 Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
61 Gtk::Table* table = Gtk::manage (new Gtk::Table (3, 3));
62 table->set_spacings (6);
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);
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 ();
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);
81 _minimum_length->set_session (s);
82 _minimum_length->set_mode (AudioClock::Frames);
83 _minimum_length->set (1000, true);
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);
89 _fade_length->set_session (s);
90 _fade_length->set_mode (AudioClock::Frames);
91 _fade_length->set (64, true);
93 hbox->pack_start (*table);
95 get_vbox()->pack_start (*hbox, false, false);
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);
101 get_vbox()->pack_start (_progress_bar, true, true, 12);
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));
109 update_silence_rects ();
110 update_threshold_line ();
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));
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);
124 StripSilenceDialog::~StripSilenceDialog ()
127 progress_idle_connection.disconnect();
129 /* Terminate our thread */
130 _interthread_info.cancel = true;
132 _thread_should_finish = true;
136 pthread_join (_thread, 0);
138 delete _minimum_length;
143 StripSilenceDialog::idle_update_progress()
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);
158 StripSilenceDialog::silences (AudioIntervalMap& m)
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);
167 StripSilenceDialog::drop_rects ()
169 // called by parent when starting to progess (dialog::run returned),
170 // but before the dialog is destoyed.
172 _interthread_info.cancel = true;
174 /* Block until the thread is idle */
178 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
179 v->view->drop_silent_frames ();
182 cancel_button->set_sensitive (false);
183 apply_button->set_sensitive (false);
187 StripSilenceDialog::update_threshold_line ()
192 /* Don't need to lock here as we're not reading the _waves silence details */
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;
198 double const y = alt_log_meter (_threshold.get_value());
200 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
201 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
209 StripSilenceDialog::update ()
211 update_threshold_line ();
212 update_silence_rects ();
213 _progress_bar.set_text ("");
214 update_progress_gui (0);
215 apply_button->set_sensitive(true);
219 StripSilenceDialog::update_silence_rects ()
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();
225 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
226 v->view->set_silent_frames (v->intervals, y);
231 StripSilenceDialog::_detection_thread_work (void* arg)
233 StripSilenceDialog* d = reinterpret_cast<StripSilenceDialog*> (arg);
234 return d->detection_thread_work ();
237 /** Body of our silence detection thread */
239 StripSilenceDialog::detection_thread_work ()
241 /* Do not register with all UIs, but do register with the GUI,
242 because we will need to queue some GUI (only) requests
244 ARDOUR_UI::instance()->register_thread (pthread_self(), "silence", 32);
246 /* Hold this lock when we are doing work */
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());
256 i->intervals = ar->find_silence (dB_to_coefficient (threshold ()), minimum_length (), fade_length(), _interthread_info);
259 if (_interthread_info.cancel) {
262 ++analysis_progress_cur;
263 _interthread_info.progress = 1.0;
267 analysis_progress_max = 0;
269 if (!_interthread_info.cancel) {
270 Completed (); /* EMIT SIGNAL */
273 /* Our work is done; sleep until there is more to do.
274 * The lock is released while we are waiting.
276 _run_cond.wait (_lock);
278 if (_thread_should_finish) {
288 StripSilenceDialog::restart_thread ()
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.
299 _progress_bar.set_text (_("Analyzing"));
300 update_progress_gui (0);
301 apply_button->set_sensitive (false);
303 /* Cancel any current run */
304 _interthread_info.cancel = true;
306 /* Block until the thread waits() */
309 _interthread_info.cancel = false;
312 /* And re-awake the thread */
317 StripSilenceDialog::threshold_changed ()
319 update_threshold_line ();
324 StripSilenceDialog::minimum_length () const
326 return std::max((framecnt_t)1, _minimum_length->current_duration (views.front().view->region()->position()));
330 StripSilenceDialog::fade_length () const
332 return std::max((framecnt_t)0, _fade_length->current_duration (views.front().view->region()->position()));
336 StripSilenceDialog::update_progress_gui (float p)
338 _progress_bar.set_fraction (p);