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