Default response for the Strip Silence dialogue (part of
[ardour.git] / gtk2_ardour / strip_silence_dialog.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <iostream>
21
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"
27
28 #include "ardour/dB.h"
29 #include "ardour_ui.h"
30 #include "ardour/session.h"
31
32 #include "audio_clock.h"
33 #include "gui_thread.h"
34 #include "strip_silence_dialog.h"
35 #include "canvas_impl.h"
36 #include "region_view.h"
37 #include "simpleline.h"
38 #include "waveview.h"
39 #include "simplerect.h"
40 #include "rgb_macros.h"
41 #include "i18n.h"
42 #include "logmeter.h"
43
44 using namespace ARDOUR;
45 using namespace std;
46 using namespace ArdourCanvas;
47
48 /** Construct Strip silence dialog box */
49 StripSilenceDialog::StripSilenceDialog (Session* s, list<RegionView*> const & v)
50         : ArdourDialog (_("Strip Silence"))
51         , ProgressReporter ()
52         , _minimum_length (new AudioClock (X_("silence duration"), true, "SilenceDurationClock", true, false, true, false))
53         , _fade_length (new AudioClock (X_("silence duration"), true, "SilenceDurationClock", true, false, true, false))
54         , _peaks_ready_connection (0)
55         , _destroying (false)
56 {
57         set_session (s);
58
59         for (list<RegionView*>::const_iterator r = v.begin(); r != v.end(); ++r) {
60                 views.push_back (ViewInterval (*r));
61         }
62
63         Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
64
65         Gtk::Table* table = Gtk::manage (new Gtk::Table (3, 3));
66         table->set_spacings (6);
67
68         int n = 0;
69
70         table->attach (*Gtk::manage (new Gtk::Label (_("Threshold"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
71         table->attach (_threshold, 1, 2, n, n + 1, Gtk::FILL);
72         table->attach (*Gtk::manage (new Gtk::Label (_("dbFS"))), 2, 3, n, n + 1, Gtk::FILL);
73         ++n;
74
75         _threshold.set_digits (1);
76         _threshold.set_increments (1, 10);
77         _threshold.set_range (-120, 0);
78         _threshold.set_value (-60);
79         _threshold.set_activates_default ();
80
81         table->attach (*Gtk::manage (new Gtk::Label (_("Minimum length"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
82         table->attach (*_minimum_length, 1, 2, n, n + 1, Gtk::FILL);
83         ++n;
84
85         _minimum_length->set_session (s);
86         _minimum_length->set_mode (AudioClock::Frames);
87         _minimum_length->set (1000, true);
88
89         table->attach (*Gtk::manage (new Gtk::Label (_("Fade length"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
90         table->attach (*_fade_length, 1, 2, n, n + 1, Gtk::FILL);
91         ++n;
92
93         _fade_length->set_session (s);
94         _fade_length->set_mode (AudioClock::Frames);
95         _fade_length->set (64, true);
96
97         hbox->pack_start (*table);
98
99         get_vbox()->pack_start (*hbox, false, false);
100
101         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
102         add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
103         set_default_response (Gtk::RESPONSE_OK);
104
105         get_vbox()->pack_start (_progress_bar, true, true, 12);
106
107         show_all ();
108
109         _threshold.get_adjustment()->signal_value_changed().connect (sigc::mem_fun (*this, &StripSilenceDialog::threshold_changed));
110         _minimum_length->ValueChanged.connect (sigc::mem_fun (*this, &StripSilenceDialog::restart_thread));
111
112         update_silence_rects ();
113         update_threshold_line ();
114
115         /* Create a thread which runs while the dialogue is open to compute the silence regions */
116         Completed.connect (_completed_connection, MISSING_INVALIDATOR, ui_bind (&StripSilenceDialog::update, this), gui_context ());
117         _thread_should_finish = false;
118         pthread_create (&_thread, 0, StripSilenceDialog::_detection_thread_work, this);
119 }
120
121
122 StripSilenceDialog::~StripSilenceDialog ()
123 {
124         _destroying = true;
125
126         /* Terminate our thread */
127
128         _lock.lock ();
129         _interthread_info.cancel = true;
130         _thread_should_finish = true;
131         _lock.unlock ();
132
133         _run_cond.signal ();
134         pthread_join (_thread, 0);
135
136         delete _minimum_length;
137         delete _fade_length;
138
139         delete _peaks_ready_connection;
140 }
141
142 void
143 StripSilenceDialog::silences (AudioIntervalMap& m)
144 {
145         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
146                 pair<boost::shared_ptr<Region>,AudioIntervalResult> newpair (v->view->region(), v->intervals);
147                 m.insert (newpair);
148         }
149 }
150
151 void
152 StripSilenceDialog::drop_rects ()
153 {
154         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
155                 v->view->drop_silent_frames ();
156         }
157 }
158
159 void
160 StripSilenceDialog::update_threshold_line ()
161 {
162 #if 0
163         int n = 0;
164
165         /* Don't need to lock here as we're not reading the _waves silence details */
166
167         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
168                 (*i)->threshold_line->property_x1() = 0;
169                 (*i)->threshold_line->property_x2() = _wave_width;
170
171                 double const y = alt_log_meter (_threshold.get_value());
172
173                 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
174                 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
175         }
176
177         ++n;
178 #endif
179 }
180
181 void
182 StripSilenceDialog::update ()
183 {
184         update_threshold_line ();
185         update_silence_rects ();
186 }
187
188 void
189 StripSilenceDialog::update_silence_rects ()
190 {
191         /* Lock so that we don't contend with the detection thread for access to the silence regions */
192         Glib::Mutex::Lock lm (_lock);
193         double const y = _threshold.get_value();
194
195         for (list<ViewInterval>::iterator v = views.begin(); v != views.end(); ++v) {
196                 v->view->set_silent_frames (v->intervals, y);
197         }
198 }
199
200 void *
201 StripSilenceDialog::_detection_thread_work (void* arg)
202 {
203         StripSilenceDialog* d = reinterpret_cast<StripSilenceDialog*> (arg);
204         return d->detection_thread_work ();
205 }
206
207 /** Body of our silence detection thread */
208 void *
209 StripSilenceDialog::detection_thread_work ()
210 {
211         ARDOUR_UI::instance()->register_thread ("gui", pthread_self(), "silence", 32);
212
213         /* Hold this lock when we are doing work */
214         _lock.lock ();
215
216         while (1) {
217                 for (list<ViewInterval>::iterator i = views.begin(); i != views.end(); ++i) {
218                         boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> ((*i).view->region());
219
220                         if (ar) {
221                                 i->intervals = ar->find_silence (dB_to_coefficient (threshold ()), minimum_length (), _interthread_info);
222                         }
223
224                         if (_interthread_info.cancel) {
225                                 break;
226                         }
227                 }
228
229                 if (!_interthread_info.cancel) {
230                         Completed (); /* EMIT SIGNAL */
231                 }
232
233                 /* Our work is done; sleep until there is more to do.
234                  * The lock is released while we are waiting.
235                  */
236                 _run_cond.wait (_lock);
237
238                 if (_thread_should_finish) {
239                         _lock.unlock ();
240                         return 0;
241                 }
242         }
243
244         return 0;
245 }
246
247 void
248 StripSilenceDialog::restart_thread ()
249 {
250         if (_destroying) {
251                 /* I don't know how this happens, but it seems to be possible for this
252                    method to be called after our destructor has finished executing.
253                    If this happens, bad things follow; _lock cannot be locked and
254                    Ardour hangs.  So if we are destroying, just bail early.
255                 */
256                 return;
257         }
258
259         /* Cancel any current run */
260         _interthread_info.cancel = true;
261
262         /* Block until the thread waits() */
263         _lock.lock ();
264         /* Reset the flag */
265         _interthread_info.cancel = false;
266         _lock.unlock ();
267
268         /* And re-awake the thread */
269         _run_cond.signal ();
270 }
271
272 void
273 StripSilenceDialog::threshold_changed ()
274 {
275         update_threshold_line ();
276         restart_thread ();
277 }
278
279 framecnt_t
280 StripSilenceDialog::minimum_length () const
281 {
282         return _minimum_length->current_duration (views.front().view->region()->position());
283 }
284
285 framecnt_t
286 StripSilenceDialog::fade_length () const
287 {
288         return _fade_length->current_duration (views.front().view->region()->position());
289 }
290
291 void
292 StripSilenceDialog::update_progress_gui (float p)
293 {
294         _progress_bar.set_fraction (p);
295 }