Fix warning.
[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 "gui_thread.h"
33 #include "strip_silence_dialog.h"
34 #include "canvas_impl.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 Glib::StaticMutex StripSilenceDialog::run_lock;
47 Glib::Cond*       StripSilenceDialog::thread_waiting = 0;
48 Glib::Cond*       StripSilenceDialog::thread_run = 0;
49 bool              StripSilenceDialog::thread_should_exit = false;
50 InterThreadInfo   StripSilenceDialog::itt;
51 StripSilenceDialog* StripSilenceDialog::current = 0;
52
53 /** Construct Strip silence dialog box */
54 StripSilenceDialog::StripSilenceDialog (Session* s, list<boost::shared_ptr<ARDOUR::AudioRegion> > const & regions)
55         : ArdourDialog (_("Strip Silence"))
56         , _minimum_length (X_("silence duration"), true, "SilenceDurationClock", true, false, true, false)
57         , _fade_length (X_("silence duration"), true, "SilenceDurationClock", true, false, true, false)
58         , _wave_width (640)
59         , _wave_height (64)
60         , restart_queued (false)
61         , _peaks_ready_connection (0)
62 {
63         set_session (s);
64
65         if (thread_waiting == 0) {
66                 thread_waiting = new Glib::Cond;
67                 thread_run = new Glib::Cond;
68         }
69
70         Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
71         
72         Gtk::Table* table = Gtk::manage (new Gtk::Table (3, 3));
73         table->set_spacings (6);
74
75         int n = 0;
76
77         table->attach (*Gtk::manage (new Gtk::Label (_("Threshold"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
78         table->attach (_threshold, 1, 2, n, n + 1, Gtk::FILL);
79         table->attach (*Gtk::manage (new Gtk::Label (_("dbFS"))), 2, 3, n, n + 1, Gtk::FILL);
80         ++n;
81         
82         _threshold.set_digits (1);
83         _threshold.set_increments (1, 10);
84         _threshold.set_range (-120, 0);
85         _threshold.set_value (-60);
86
87         table->attach (*Gtk::manage (new Gtk::Label (_("Minimum length"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
88         table->attach (_minimum_length, 1, 2, n, n + 1, Gtk::FILL);
89         ++n;
90         
91         _minimum_length.set_session (s);
92         _minimum_length.set_mode (AudioClock::Frames);
93         _minimum_length.set (1000, true);
94
95         table->attach (*Gtk::manage (new Gtk::Label (_("Fade length"), 1, 0.5)), 0, 1, n, n + 1, Gtk::FILL);
96         table->attach (_fade_length, 1, 2, n, n + 1, Gtk::FILL);
97         ++n;
98
99         _fade_length.set_session (s);
100         _fade_length.set_mode (AudioClock::Frames);
101         _fade_length.set (64, true);
102
103         hbox->pack_start (*table);
104
105         table = Gtk::manage (new Gtk::Table (3, 2));
106         table->set_spacings (6);
107         
108         n = 0;
109         
110         table->attach (*Gtk::manage (new Gtk::Label (_("Silent segments:"), 1, 0.5)), 3, 4, n, n + 1, Gtk::FILL);
111         table->attach (_segment_count_label, 5, 6, n, n + 1, Gtk::FILL);
112         _segment_count_label.set_alignment (0, 0.5);
113         ++n;
114
115         table->attach (*Gtk::manage (new Gtk::Label (_("Shortest silence:"), 1, 0.5)), 3, 4, n, n + 1, Gtk::FILL);
116         table->attach (_shortest_silence_label, 5, 6, n, n + 1, Gtk::FILL);
117         _shortest_silence_label.set_alignment (0, 0.5);
118         ++n;
119
120         table->attach (*Gtk::manage (new Gtk::Label (_("Shortest audible:"), 1, 0.5)), 3, 4, n, n + 1, Gtk::FILL);
121         table->attach (_shortest_audible_label, 5, 6, n, n + 1, Gtk::FILL);
122         _shortest_audible_label.set_alignment (0, 0.5);
123         ++n;
124         
125         hbox->pack_start (*table);
126
127         /* dummy label for padding */
128         hbox->pack_start (*Gtk::manage (new Gtk::Label ("")), true, true);
129
130         get_vbox()->pack_start (*hbox, false, false);
131
132         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
133         add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
134
135         _canvas = new CanvasAA ();
136         _canvas->signal_size_allocate().connect (sigc::mem_fun (*this, &StripSilenceDialog::canvas_allocation));
137         _canvas->set_size_request (_wave_width, _wave_height * regions.size());
138
139         for (list<boost::shared_ptr<ARDOUR::AudioRegion> >::const_iterator i = regions.begin(); i != regions.end(); ++i) {
140                 Wave* w = new Wave (_canvas->root(), *i);
141                 _waves.push_back (w);
142         }
143
144         get_vbox()->pack_start (*_canvas, true, true);
145
146         show_all ();
147
148         _threshold.get_adjustment()->signal_value_changed().connect (sigc::mem_fun (*this, &StripSilenceDialog::threshold_changed));
149         _minimum_length.ValueChanged.connect (sigc::mem_fun (*this, &StripSilenceDialog::maybe_start_silence_detection));
150
151         create_waves ();
152         update_silence_rects ();
153         update_threshold_line ();
154
155         maybe_start_silence_detection ();
156 }
157
158
159 StripSilenceDialog::~StripSilenceDialog ()
160 {
161         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
162                 delete *i;
163         }
164
165         _waves.clear ();
166         
167         delete _peaks_ready_connection;
168         delete _canvas;
169 }
170
171 void
172 StripSilenceDialog::create_waves ()
173 {
174         int n = 0;
175
176         delete _peaks_ready_connection;
177         _peaks_ready_connection = 0;
178
179         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
180                 if ((*i)->region->audio_source(0)->peaks_ready (boost::bind (&StripSilenceDialog::peaks_ready, this), &_peaks_ready_connection, gui_context())) {
181                         (*i)->view = new WaveView (*(_canvas->root()));
182                         (*i)->view->property_data_src() = static_cast<gpointer>((*i)->region.get());
183                         (*i)->view->property_cache() = WaveView::create_cache ();
184                         (*i)->view->property_cache_updater() = true;
185                         (*i)->view->property_channel() = 0;
186                         (*i)->view->property_length_function() = (void *) region_length_from_c;
187                         (*i)->view->property_sourcefile_length_function() = (void *) sourcefile_length_from_c;
188                         (*i)->view->property_peak_function() = (void *) region_read_peaks_from_c;
189                         (*i)->view->property_x() = 0;
190                         (*i)->view->property_y() = n * _wave_height;
191                         (*i)->view->property_height() = _wave_height;
192                         (*i)->view->property_samples_per_unit() = (*i)->samples_per_unit;
193                         (*i)->view->property_region_start() = (*i)->region->start();
194                         (*i)->view->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
195                         (*i)->view->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
196                         (*i)->view->property_logscaled() = true;
197                         (*i)->view->property_rectified() = true;
198                 }
199
200                 ++n;
201         }
202 }
203
204 void
205 StripSilenceDialog::peaks_ready ()
206 {
207         delete _peaks_ready_connection;
208         _peaks_ready_connection = 0;
209         create_waves ();
210 }
211
212 void
213 StripSilenceDialog::canvas_allocation (Gtk::Allocation& alloc)
214 {
215         int n = 0;
216
217         _canvas->set_scroll_region (0.0, 0.0, alloc.get_width(), alloc.get_height());
218         _wave_width = alloc.get_width ();
219         _wave_height = alloc.get_height ();
220
221         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i, ++n) {
222                 (*i)->samples_per_unit = ((double) (*i)->region->length() / _wave_width);
223
224                 if ((*i)->view) {
225                         (*i)->view->property_y() = n * _wave_height;
226                         (*i)->view->property_samples_per_unit() = (*i)->samples_per_unit;
227                         (*i)->view->property_height() = _wave_height;
228                 }
229         }
230
231         resize_silence_rects ();
232         update_threshold_line ();
233 }
234
235 void
236 StripSilenceDialog::resize_silence_rects ()
237 {
238         int n = 0;
239
240         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
241
242                 list<pair<frameoffset_t, framecnt_t> >::const_iterator j;
243                 list<SimpleRect*>::iterator r;
244
245                 for (j = (*i)->silence.begin(), r = (*i)->silence_rects.begin(); 
246                      j != (*i)->silence.end() && r != (*i)->silence_rects.end(); ++j, ++r) {
247                         (*r)->property_x1() = j->first / (*i)->samples_per_unit;
248                         (*r)->property_x2() = j->second / (*i)->samples_per_unit;
249                         (*r)->property_y1() = n * _wave_height;
250                         (*r)->property_y2() = (n + 1) * _wave_height;
251                         (*r)->property_outline_pixels() = 0;
252                         (*r)->property_fill_color_rgba() = RGBA_TO_UINT (128, 128, 128, 128);
253                 }
254
255                 ++n;
256         }
257 }
258
259 void
260 StripSilenceDialog::update_threshold_line ()
261 {
262         int n = 0;
263         
264         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
265                 (*i)->threshold_line->property_x1() = 0;
266                 (*i)->threshold_line->property_x2() = _wave_width;
267                 
268                 double const y = alt_log_meter (_threshold.get_value());
269
270                 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
271                 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
272         }
273
274         ++n;
275 }
276
277 void
278 StripSilenceDialog::update_silence_rects ()
279 {
280         int n = 0;
281         uint32_t max_segments = 0;
282         uint32_t sc;
283
284         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
285                 for (list<SimpleRect*>::iterator j = (*i)->silence_rects.begin(); j != (*i)->silence_rects.end(); ++j) {
286                         delete *j;
287                 }
288
289                 (*i)->silence_rects.clear ();                
290                 sc = 0;
291
292                 for (list<pair<frameoffset_t, framecnt_t> >::const_iterator j = (*i)->silence.begin(); j != (*i)->silence.end(); ++j) {
293
294                         SimpleRect* r = new SimpleRect (*(_canvas->root()));
295                         r->property_x1() = j->first / (*i)->samples_per_unit;
296                         r->property_x2() = j->second / (*i)->samples_per_unit;
297                         r->property_y1() = n * _wave_height;
298                         r->property_y2() = (n + 1) * _wave_height;
299                         r->property_outline_pixels() = 0;
300                         r->property_fill_color_rgba() = RGBA_TO_UINT (128, 128, 128, 128);
301                         (*i)->silence_rects.push_back (r);
302                         sc++;
303                 }
304
305                 max_segments = max (max_segments, sc);
306                 ++n;
307         }
308
309         if (min_audible > 0) {
310                 float ms, ma;
311                 char const * aunits;
312                 char const * sunits;
313
314                 ma = (float) min_audible/_session->frame_rate();
315                 ms = (float) min_silence/_session->frame_rate();
316
317                 if (min_audible < _session->frame_rate()) {
318                         aunits = _("ms");
319                         ma *= 1000.0;
320                 } else {
321                         aunits = _("s");
322                 }
323
324                 if (min_silence < _session->frame_rate()) {
325                         sunits = _("ms");
326                         ms *= 1000.0;
327                 } else {
328                         sunits = _("s");
329                 }
330
331                 _segment_count_label.set_text (string_compose ("%1", max_segments));
332                 if (max_segments > 0) {
333                         _shortest_silence_label.set_text (string_compose ("%1 %2", ms, sunits));
334                         _shortest_audible_label.set_text (string_compose ("%1 %2", ma, aunits));
335                 } else {
336                         _shortest_silence_label.set_text ("");
337                         _shortest_audible_label.set_text ("");
338                 }
339         } else {
340                 _segment_count_label.set_text (_("Full silence"));
341                 _shortest_silence_label.set_text ("");
342                 _shortest_audible_label.set_text ("");
343         }
344 }
345
346 bool
347 StripSilenceDialog::_detection_done (void* arg)
348 {
349         StripSilenceDialog* ssd = (StripSilenceDialog*) arg;
350         return ssd->detection_done ();
351 }
352
353 bool
354 StripSilenceDialog::detection_done ()
355 {
356         get_window()->set_cursor (Gdk::Cursor (Gdk::LEFT_PTR));
357         update_silence_rects ();
358         return false;
359 }
360
361 void*
362 StripSilenceDialog::_detection_thread_work (void* arg)
363 {
364         StripSilenceDialog* ssd = (StripSilenceDialog*) arg;
365         return ssd->detection_thread_work ();
366 }
367
368 void*
369 StripSilenceDialog::detection_thread_work ()
370 {
371         ARDOUR_UI::instance()->register_thread ("gui", pthread_self(), "silence", 32);
372         
373         while (1) {
374
375                 run_lock.lock ();
376                 thread_waiting->signal ();
377                 thread_run->wait (run_lock);
378
379                 if (thread_should_exit) {
380                         thread_waiting->signal ();
381                         run_lock.unlock ();
382                         break;
383                 }
384
385                 if (current) {
386                         StripSilenceDialog* ssd = current;
387                         run_lock.unlock ();
388                         
389                         for (list<Wave*>::iterator i = ssd->_waves.begin(); i != ssd->_waves.end(); ++i) {
390                                 (*i)->silence = (*i)->region->find_silence (dB_to_coefficient (ssd->threshold ()), ssd->minimum_length (), ssd->itt);
391                                 ssd->update_stats ((*i)->silence);
392                         }
393                         
394                         if (!ssd->itt.cancel) {
395                                 g_idle_add ((gboolean (*)(void*)) StripSilenceDialog::_detection_done, ssd);
396                         }
397                 } else {
398                         run_lock.unlock ();
399                 }
400
401         }
402         
403         return 0;
404 }
405
406 void
407 StripSilenceDialog::threshold_changed ()
408 {
409         update_threshold_line ();
410         maybe_start_silence_detection ();
411 }
412
413 void
414 StripSilenceDialog::maybe_start_silence_detection ()
415 {
416         if (!restart_queued) {
417                 restart_queued = true;
418                 Glib::signal_idle().connect (sigc::mem_fun (*this, &StripSilenceDialog::start_silence_detection));
419         }
420 }
421
422 bool
423 StripSilenceDialog::start_silence_detection ()
424 {
425         Glib::Mutex::Lock lm (run_lock);
426         restart_queued = false;
427
428         if (!itt.thread) {
429
430                 itt.done = false;
431                 itt.cancel = false;
432                 itt.progress = 0.0;
433                 current = this;
434
435                 pthread_create (&itt.thread, 0, StripSilenceDialog::_detection_thread_work, this);
436                 /* wait for it to get started */
437                 thread_waiting->wait (run_lock);
438
439         } else {
440                 
441                 /* stop whatever the thread is doing */
442
443                 itt.cancel = 1;
444                 current = 0;
445
446                 while (!itt.done) {
447                         thread_run->signal ();
448                         thread_waiting->wait (run_lock);
449                 }
450         }
451
452
453         itt.cancel = false;
454         itt.done = false;
455         itt.progress = 0.0;
456         current = this;
457         
458         /* and start it up (again) */
459         
460         thread_run->signal ();
461
462         /* change cursor */
463
464         get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
465
466         /* don't call again until needed */
467         
468         return false;
469 }
470
471 void
472 StripSilenceDialog::stop_thread ()
473 {
474         Glib::Mutex::Lock lm (run_lock);
475
476         itt.cancel = true;
477         thread_should_exit = true;
478         thread_run->signal (); 
479         thread_waiting->wait (run_lock);
480         itt.thread = 0;
481 }
482
483 void
484 StripSilenceDialog::update_stats (const SilenceResult& res)
485 {
486         if (res.empty()) {
487                 return;
488         }
489
490         max_silence = 0;
491         min_silence = max_frames;
492         max_audible = 0;
493         min_audible = max_frames;
494         
495         SilenceResult::const_iterator cur;
496
497         cur = res.begin();
498
499         framepos_t start = 0;
500         framepos_t end;
501         bool in_silence;
502
503         if (cur->first == 0) {
504                 /* initial segment, starting at zero, is silent */
505                 end = cur->second;
506                 in_silence = true;
507         } else {
508                 /* initial segment, starting at zero, is audible */
509                 end = cur->first;
510                 in_silence = false;
511         }
512
513         while (cur != res.end()) {
514
515                 framecnt_t interval_duration;
516
517                 interval_duration = end - start;
518
519                 if (in_silence) {
520
521                         max_silence = max (max_silence, interval_duration);
522                         min_silence = min (min_silence, interval_duration);
523                 } else {
524
525                         max_audible = max (max_audible, interval_duration);
526                         min_audible = min (min_audible, interval_duration);
527                 }
528
529                 start = end;
530                 ++cur;
531                 end = cur->first;
532                 in_silence = !in_silence;
533         }
534 }
535
536 nframes_t
537 StripSilenceDialog::minimum_length () const
538 {
539         return _minimum_length.current_duration (_waves.front()->region->position());
540 }
541
542 nframes_t
543 StripSilenceDialog::fade_length () const
544 {
545         return _minimum_length.current_duration (_waves.front()->region->position());
546 }
547                 
548 StripSilenceDialog::Wave::Wave (Group* g, boost::shared_ptr<AudioRegion> r)
549         : region (r), view (0), samples_per_unit (1)
550 {
551         threshold_line = new ArdourCanvas::SimpleLine (*g);
552         threshold_line->property_color_rgba() = RGBA_TO_UINT (0, 0, 0, 128);
553 }
554
555 StripSilenceDialog::Wave::~Wave ()
556 {
557         delete view;
558         delete threshold_line;
559         for (list<SimpleRect*>::iterator i = silence_rects.begin(); i != silence_rects.end(); ++i) {
560                 delete *i;
561         }
562 }