Fix corruption of follow playhead state on quit (#4048).
[ardour2.git] / gtk2_ardour / strip_silence_dialog.cc
blob6a591c08b9eb2afb382e515ed3d675d17451ae39
1 /*
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.
20 #include <iostream>
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"
37 #include "waveview.h"
38 #include "simplerect.h"
39 #include "rgb_macros.h"
40 #include "i18n.h"
41 #include "logmeter.h"
43 using namespace ARDOUR;
44 using namespace std;
45 using namespace ArdourCanvas;
47 /** Construct Strip silence dialog box */
48 StripSilenceDialog::StripSilenceDialog (Session* s, list<RegionView*> const & v)
49 : ArdourDialog (_("Strip Silence"))
50 , ProgressReporter ()
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)
54 , _destroying (false)
56 set_session (s);
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);
67 int n = 0;
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);
72 ++n;
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);
81 ++n;
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);
89 ++n;
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 get_vbox()->pack_start (*hbox, false, false);
99 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
100 add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
102 get_vbox()->pack_start (_progress_bar, true, true, 12);
104 show_all ();
106 _threshold.get_adjustment()->signal_value_changed().connect (sigc::mem_fun (*this, &StripSilenceDialog::threshold_changed));
107 _minimum_length.ValueChanged.connect (sigc::mem_fun (*this, &StripSilenceDialog::restart_thread));
109 update_silence_rects ();
110 update_threshold_line ();
112 /* Create a thread which runs while the dialogue is open to compute the silence regions */
113 Completed.connect (_completed_connection, MISSING_INVALIDATOR, ui_bind (&StripSilenceDialog::update, this), gui_context ());
114 _thread_should_finish = false;
115 pthread_create (&_thread, 0, StripSilenceDialog::_detection_thread_work, this);
119 StripSilenceDialog::~StripSilenceDialog ()
121 _destroying = true;
123 /* Terminate our thread */
125 _lock.lock ();
126 _interthread_info.cancel = true;
127 _thread_should_finish = true;
128 _lock.unlock ();
130 _run_cond.signal ();
131 pthread_join (_thread, 0);
133 delete _peaks_ready_connection;
136 void
137 StripSilenceDialog::silences (AudioIntervalMap& m)
139 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
140 pair<boost::shared_ptr<Region>,AudioIntervalResult> newpair (v->view->region(), v->intervals);
141 m.insert (newpair);
145 void
146 StripSilenceDialog::drop_rects ()
148 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
149 v->view->drop_silent_frames ();
153 void
154 StripSilenceDialog::update_threshold_line ()
156 #if 0
157 int n = 0;
159 /* Don't need to lock here as we're not reading the _waves silence details */
161 for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
162 (*i)->threshold_line->property_x1() = 0;
163 (*i)->threshold_line->property_x2() = _wave_width;
165 double const y = alt_log_meter (_threshold.get_value());
167 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
168 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
171 ++n;
172 #endif
175 void
176 StripSilenceDialog::update ()
178 update_threshold_line ();
179 update_silence_rects ();
182 void
183 StripSilenceDialog::update_silence_rects ()
185 /* Lock so that we don't contend with the detection thread for access to the silence regions */
186 Glib::Mutex::Lock lm (_lock);
187 double const y = _threshold.get_value();
189 for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
190 v->view->set_silent_frames (v->intervals, y);
194 void *
195 StripSilenceDialog::_detection_thread_work (void* arg)
197 StripSilenceDialog* d = reinterpret_cast<StripSilenceDialog*> (arg);
198 return d->detection_thread_work ();
201 /** Body of our silence detection thread */
202 void *
203 StripSilenceDialog::detection_thread_work ()
205 ARDOUR_UI::instance()->register_thread ("gui", pthread_self(), "silence", 32);
207 /* Hold this lock when we are doing work */
208 _lock.lock ();
210 while (1) {
211 for (list<ViewInterval>::iterator i = views.begin(); i != views.end(); ++i) {
212 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> ((*i).view->region());
214 if (ar) {
215 i->intervals = ar->find_silence (dB_to_coefficient (threshold ()), minimum_length (), _interthread_info);
218 if (_interthread_info.cancel) {
219 break;
223 if (!_interthread_info.cancel) {
224 Completed (); /* EMIT SIGNAL */
227 /* Our work is done; sleep until there is more to do.
228 * The lock is released while we are waiting.
230 _run_cond.wait (_lock);
232 if (_thread_should_finish) {
233 _lock.unlock ();
234 return 0;
238 return 0;
241 void
242 StripSilenceDialog::restart_thread ()
244 if (_destroying) {
245 /* I don't know how this happens, but it seems to be possible for this
246 method to be called after our destructor has finished executing.
247 If this happens, bad things follow; _lock cannot be locked and
248 Ardour hangs. So if we are destroying, just bail early.
250 return;
253 /* Cancel any current run */
254 _interthread_info.cancel = true;
256 /* Block until the thread waits() */
257 _lock.lock ();
258 /* Reset the flag */
259 _interthread_info.cancel = false;
260 _lock.unlock ();
262 /* And re-awake the thread */
263 _run_cond.signal ();
266 void
267 StripSilenceDialog::threshold_changed ()
269 update_threshold_line ();
270 restart_thread ();
273 framecnt_t
274 StripSilenceDialog::minimum_length () const
276 return _minimum_length.current_duration (views.front().view->region()->position());
279 framecnt_t
280 StripSilenceDialog::fade_length () const
282 return _fade_length.current_duration (views.front().view->region()->position());
285 void
286 StripSilenceDialog::update_progress_gui (float p)
288 _progress_bar.set_fraction (p);