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/stock.h>
25 #include "ardour/audioregion.h"
26 #include "ardour/audiosource.h"
28 #include "ardour/dB.h"
29 #include "ardour_ui.h"
30 #include "ardour/session.h"
32 #include "gui_thread.h"
33 #include "strip_silence_dialog.h"
34 #include "canvas_impl.h"
35 #include "region_view.h"
36 #include "simpleline.h"
38 #include "simplerect.h"
39 #include "rgb_macros.h"
43 using namespace ARDOUR;
45 using namespace ArdourCanvas;
47 /** Construct Strip silence dialog box */
48 StripSilenceDialog::StripSilenceDialog (Session* s, list<RegionView*> const & v)
49 : ArdourDialog (_("Strip Silence"))
51 , _minimum_length (X_("silence duration"), true, "SilenceDurationClock", true, false, true, false)
52 , _fade_length (X_("silence duration"), true, "SilenceDurationClock", true, false, true, false)
53 , _peaks_ready_connection (0)
58 for (list<RegionView*>::const_iterator r = v.begin(); r != v.end(); ++r) {
59 views.push_back (ViewInterval (*r));
62 Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
64 Gtk::Table* table = Gtk::manage (new Gtk::Table (3, 3));
65 table->set_spacings (6);
69 table->attach (*Gtk::manage (new Gtk::Label (_("Threshold"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
70 table->attach (_threshold, 1, 2, n, n + 1, Gtk::FILL);
71 table->attach (*Gtk::manage (new Gtk::Label (_("dbFS"))), 2, 3, n, n + 1, Gtk::FILL);
74 _threshold.set_digits (1);
75 _threshold.set_increments (1, 10);
76 _threshold.set_range (-120, 0);
77 _threshold.set_value (-60);
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::Frames);
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::Frames);
93 _fade_length.set (64, true);
95 hbox->pack_start (*table);
97 table = Gtk::manage (new Gtk::Table (3, 2));
98 table->set_spacings (6);
102 table->attach (*Gtk::manage (new Gtk::Label (_("Silent segments:"), 1, 0.5)), 3, 4, n, n + 1, Gtk::FILL);
103 table->attach (_segment_count_label, 5, 6, n, n + 1, Gtk::FILL);
104 _segment_count_label.set_alignment (0, 0.5);
107 table->attach (*Gtk::manage (new Gtk::Label (_("Shortest silence:"), 1, 0.5)), 3, 4, n, n + 1, Gtk::FILL);
108 table->attach (_shortest_silence_label, 5, 6, n, n + 1, Gtk::FILL);
109 _shortest_silence_label.set_alignment (0, 0.5);
112 table->attach (*Gtk::manage (new Gtk::Label (_("Shortest audible:"), 1, 0.5)), 3, 4, n, n + 1, Gtk::FILL);
113 table->attach (_shortest_audible_label, 5, 6, n, n + 1, Gtk::FILL);
114 _shortest_audible_label.set_alignment (0, 0.5);
117 hbox->pack_start (*table);
119 /* dummy label for padding */
120 hbox->pack_start (*Gtk::manage (new Gtk::Label ("")), true, true);
122 get_vbox()->pack_start (*hbox, false, false);
124 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
125 add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
127 get_vbox()->pack_start (_progress_bar, true, true);
131 _threshold.get_adjustment()->signal_value_changed().connect (sigc::mem_fun (*this, &StripSilenceDialog::threshold_changed));
132 _minimum_length.ValueChanged.connect (sigc::mem_fun (*this, &StripSilenceDialog::restart_thread));
134 update_silence_rects ();
135 update_threshold_line ();
137 /* Create a thread which runs while the dialogue is open to compute the silence regions */
138 Completed.connect (_completed_connection, MISSING_INVALIDATOR, ui_bind (&StripSilenceDialog::update, this), gui_context ());
139 _thread_should_finish = false;
140 pthread_create (&_thread, 0, StripSilenceDialog::_detection_thread_work, this);
144 StripSilenceDialog::~StripSilenceDialog ()
148 /* Terminate our thread */
151 _interthread_info.cancel = true;
152 _thread_should_finish = true;
156 pthread_join (_thread, 0);
158 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
159 (*v).view->drop_silent_frames ();
162 delete _peaks_ready_connection;
166 StripSilenceDialog::update_threshold_line ()
171 /* Don't need to lock here as we're not reading the _waves silence details */
173 for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
174 (*i)->threshold_line->property_x1() = 0;
175 (*i)->threshold_line->property_x2() = _wave_width;
177 double const y = alt_log_meter (_threshold.get_value());
179 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
180 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
188 StripSilenceDialog::update ()
191 update_threshold_line ();
192 /* XXX: first one only?! */
193 // update_stats (_waves.front()->silence);
195 update_silence_rects ();
196 cerr << "UPDATE done\n";
200 StripSilenceDialog::update_silence_rects ()
202 uint32_t max_segments = 0;
204 /* Lock so that we don't contend with the detection thread for access to the silence regions */
205 Glib::Mutex::Lock lm (_lock);
207 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
208 (*v).view->set_silent_frames ((*v).intervals);
209 max_segments = max (max_segments, (uint32_t) (*v).intervals.size());
213 cerr << "minaudible in update " << min_audible << " minsilence = " << min_silence << endl;
215 if (min_audible > 0) {
220 ma = (float) min_audible/_session->frame_rate();
221 ms = (float) min_silence/_session->frame_rate();
223 /* ma and ms are now in seconds */
226 aunits = _("minutes");
228 } else if (min_audible < _session->frame_rate()) {
232 aunits = _("seconds");
236 sunits = _("minutes");
238 } else if (min_silence < _session->frame_rate()) {
245 _segment_count_label.set_text (string_compose ("%1", max_segments));
246 if (max_segments > 0) {
247 _shortest_silence_label.set_text (string_compose ("%1 %2", ms, sunits));
248 _shortest_audible_label.set_text (string_compose ("%1 %2", ma, aunits));
250 _shortest_silence_label.set_text ("");
251 _shortest_audible_label.set_text ("");
254 _segment_count_label.set_text (_("Full silence"));
255 _shortest_silence_label.set_text ("");
256 _shortest_audible_label.set_text ("");
262 StripSilenceDialog::_detection_thread_work (void* arg)
264 StripSilenceDialog* d = reinterpret_cast<StripSilenceDialog*> (arg);
265 return d->detection_thread_work ();
268 /** Body of our silence detection thread */
270 StripSilenceDialog::detection_thread_work ()
272 ARDOUR_UI::instance()->register_thread ("gui", pthread_self(), "silence", 32);
274 /* Hold this lock when we are doing work */
278 for (list<ViewInterval>::iterator i = views.begin(); i != views.end(); ++i) {
279 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> ((*i).view->region());
282 (*i).intervals = ar->find_silence (dB_to_coefficient (threshold ()), minimum_length (), _interthread_info);
285 if (_interthread_info.cancel) {
290 if (!_interthread_info.cancel) {
291 Completed (); /* EMIT SIGNAL */
294 /* Our work is done; sleep until there is more to do.
295 * The lock is released while we are waiting.
297 _run_cond.wait (_lock);
299 if (_thread_should_finish) {
309 StripSilenceDialog::restart_thread ()
312 /* I don't know how this happens, but it seems to be possible for this
313 method to be called after our destructor has finished executing.
314 If this happens, bad things follow; _lock cannot be locked and
315 Ardour hangs. So if we are destroying, just bail early.
320 /* Cancel any current run */
321 _interthread_info.cancel = true;
323 /* Block until the thread waits() */
326 _interthread_info.cancel = false;
329 /* And re-awake the thread */
334 StripSilenceDialog::threshold_changed ()
336 update_threshold_line ();
341 StripSilenceDialog::update_stats (const AudioIntervalResult& res)
348 min_silence = max_framepos;
350 min_audible = max_framepos;
352 AudioIntervalResult::const_iterator cur;
353 bool saw_silence = false;
354 bool saw_audible = false;
358 framepos_t start = 0;
362 if (cur->first == 0) {
363 /* initial segment, starting at zero, is silent */
367 /* initial segment, starting at zero, is audible */
372 while (cur != res.end()) {
374 framecnt_t interval_duration;
376 interval_duration = end - start;
380 cerr << "Silent duration: " << interval_duration << endl;
382 max_silence = max (max_silence, interval_duration);
383 min_silence = min (min_silence, interval_duration);
386 cerr << "Audible duration: " << interval_duration << endl;
388 max_audible = max (max_audible, interval_duration);
389 min_audible = min (min_audible, interval_duration);
395 in_silence = !in_silence;
408 cerr << "max aud: " << max_audible << " min aud: " << min_audible << " max sil: " << max_silence << " min sil: " << min_silence << endl;
412 StripSilenceDialog::minimum_length () const
414 return _minimum_length.current_duration (views.front().view->region()->position());
418 StripSilenceDialog::fade_length () const
420 return _minimum_length.current_duration (views.front().view->region()->position());
424 StripSilenceDialog::update_progress_gui (float p)
426 _progress_bar.set_fraction (p);