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