Use rectified log waveform in strip silence dialogue. Add threshold graphical indica...
[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                 cout << "thresh " << _threshold.get_value() << " alt log " << alt_log_meter (_threshold.get_value()) << "\n";
271
272                 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
273                 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
274         }
275
276         ++n;
277 }
278
279 void
280 StripSilenceDialog::update_silence_rects ()
281 {
282         int n = 0;
283         uint32_t max_segments = 0;
284         uint32_t sc;
285
286         for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
287                 for (list<SimpleRect*>::iterator j = (*i)->silence_rects.begin(); j != (*i)->silence_rects.end(); ++j) {
288                         delete *j;
289                 }
290
291                 (*i)->silence_rects.clear ();                
292                 sc = 0;
293
294                 for (list<pair<frameoffset_t, framecnt_t> >::const_iterator j = (*i)->silence.begin(); j != (*i)->silence.end(); ++j) {
295
296                         SimpleRect* r = new SimpleRect (*(_canvas->root()));
297                         r->property_x1() = j->first / (*i)->samples_per_unit;
298                         r->property_x2() = j->second / (*i)->samples_per_unit;
299                         r->property_y1() = n * _wave_height;
300                         r->property_y2() = (n + 1) * _wave_height;
301                         r->property_outline_pixels() = 0;
302                         r->property_fill_color_rgba() = RGBA_TO_UINT (128, 128, 128, 128);
303                         (*i)->silence_rects.push_back (r);
304                         sc++;
305                 }
306
307                 max_segments = max (max_segments, sc);
308                 ++n;
309         }
310
311         if (min_audible > 0) {
312                 float ms, ma;
313                 char* aunits;
314                 char* sunits;
315
316                 ma = (float) min_audible/_session->frame_rate();
317                 ms = (float) min_silence/_session->frame_rate();
318
319                 if (min_audible < _session->frame_rate()) {
320                         aunits = _("ms");
321                         ma *= 1000.0;
322                 } else {
323                         aunits = _("s");
324                 }
325
326                 if (min_silence < _session->frame_rate()) {
327                         sunits = _("ms");
328                         ms *= 1000.0;
329                 } else {
330                         sunits = _("s");
331                 }
332
333                 _segment_count_label.set_text (string_compose ("%1", max_segments));
334                 if (max_segments > 0) {
335                         _shortest_silence_label.set_text (string_compose ("%1 %2", ms, sunits));
336                         _shortest_audible_label.set_text (string_compose ("%1 %2", ma, aunits));
337                 } else {
338                         _shortest_silence_label.set_text ("");
339                         _shortest_audible_label.set_text ("");
340                 }
341         } else {
342                 _segment_count_label.set_text (_("Full silence"));
343                 _shortest_silence_label.set_text ("");
344                 _shortest_audible_label.set_text ("");
345         }
346 }
347
348 bool
349 StripSilenceDialog::_detection_done (void* arg)
350 {
351         StripSilenceDialog* ssd = (StripSilenceDialog*) arg;
352         return ssd->detection_done ();
353 }
354
355 bool
356 StripSilenceDialog::detection_done ()
357 {
358         get_window()->set_cursor (Gdk::Cursor (Gdk::LEFT_PTR));
359         update_silence_rects ();
360         return false;
361 }
362
363 void*
364 StripSilenceDialog::_detection_thread_work (void* arg)
365 {
366         StripSilenceDialog* ssd = (StripSilenceDialog*) arg;
367         return ssd->detection_thread_work ();
368 }
369
370 void*
371 StripSilenceDialog::detection_thread_work ()
372 {
373         ARDOUR_UI::instance()->register_thread ("gui", pthread_self(), "silence", 32);
374         
375         while (1) {
376
377                 run_lock.lock ();
378                 thread_waiting->signal ();
379                 thread_run->wait (run_lock);
380
381                 if (thread_should_exit) {
382                         thread_waiting->signal ();
383                         run_lock.unlock ();
384                         break;
385                 }
386
387                 if (current) {
388                         StripSilenceDialog* ssd = current;
389                         run_lock.unlock ();
390                         
391                         for (list<Wave*>::iterator i = ssd->_waves.begin(); i != ssd->_waves.end(); ++i) {
392                                 (*i)->silence = (*i)->region->find_silence (dB_to_coefficient (ssd->threshold ()), ssd->minimum_length (), ssd->itt);
393                                 ssd->update_stats ((*i)->silence);
394                         }
395                         
396                         if (!ssd->itt.cancel) {
397                                 g_idle_add ((gboolean (*)(void*)) StripSilenceDialog::_detection_done, ssd);
398                         }
399                 } else {
400                         run_lock.unlock ();
401                 }
402
403         }
404         
405         return 0;
406 }
407
408 void
409 StripSilenceDialog::threshold_changed ()
410 {
411         update_threshold_line ();
412         maybe_start_silence_detection ();
413 }
414
415 void
416 StripSilenceDialog::maybe_start_silence_detection ()
417 {
418         if (!restart_queued) {
419                 restart_queued = true;
420                 Glib::signal_idle().connect (sigc::mem_fun (*this, &StripSilenceDialog::start_silence_detection));
421         }
422 }
423
424 bool
425 StripSilenceDialog::start_silence_detection ()
426 {
427         Glib::Mutex::Lock lm (run_lock);
428         restart_queued = false;
429
430         if (!itt.thread) {
431
432                 itt.done = false;
433                 itt.cancel = false;
434                 itt.progress = 0.0;
435                 current = this;
436
437                 pthread_create (&itt.thread, 0, StripSilenceDialog::_detection_thread_work, this);
438                 /* wait for it to get started */
439                 thread_waiting->wait (run_lock);
440
441         } else {
442                 
443                 /* stop whatever the thread is doing */
444
445                 itt.cancel = 1;
446                 current = 0;
447
448                 while (!itt.done) {
449                         thread_run->signal ();
450                         thread_waiting->wait (run_lock);
451                 }
452         }
453
454
455         itt.cancel = false;
456         itt.done = false;
457         itt.progress = 0.0;
458         current = this;
459         
460         /* and start it up (again) */
461         
462         thread_run->signal ();
463
464         /* change cursor */
465
466         get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
467
468         /* don't call again until needed */
469         
470         return false;
471 }
472
473 void
474 StripSilenceDialog::stop_thread ()
475 {
476         Glib::Mutex::Lock lm (run_lock);
477
478         itt.cancel = true;
479         thread_should_exit = true;
480         thread_run->signal (); 
481         thread_waiting->wait (run_lock);
482         itt.thread = 0;
483 }
484
485 void
486 StripSilenceDialog::update_stats (const SilenceResult& res)
487 {
488         if (res.empty()) {
489                 return;
490         }
491
492         max_silence = 0;
493         min_silence = max_frames;
494         max_audible = 0;
495         min_audible = max_frames;
496         
497         SilenceResult::const_iterator cur;
498
499         cur = res.begin();
500
501         framepos_t start = 0;
502         framepos_t end;
503         bool in_silence;
504
505         if (cur->first == 0) {
506                 /* initial segment, starting at zero, is silent */
507                 end = cur->second;
508                 in_silence = true;
509         } else {
510                 /* initial segment, starting at zero, is audible */
511                 end = cur->first;
512                 in_silence = false;
513         }
514
515         while (cur != res.end()) {
516
517                 framecnt_t interval_duration;
518
519                 interval_duration = end - start;
520
521                 if (in_silence) {
522
523                         max_silence = max (max_silence, interval_duration);
524                         min_silence = min (min_silence, interval_duration);
525                 } else {
526
527                         max_audible = max (max_audible, interval_duration);
528                         min_audible = min (min_audible, interval_duration);
529                 }
530
531                 start = end;
532                 ++cur;
533                 end = cur->first;
534                 in_silence = !in_silence;
535         }
536 }
537
538 nframes_t
539 StripSilenceDialog::minimum_length () const
540 {
541         return _minimum_length.current_duration (_waves.front()->region->position());
542 }
543
544 nframes_t
545 StripSilenceDialog::fade_length () const
546 {
547         return _minimum_length.current_duration (_waves.front()->region->position());
548 }
549                 
550 StripSilenceDialog::Wave::Wave (Group* g, boost::shared_ptr<AudioRegion> r)
551         : region (r), view (0), samples_per_unit (1)
552 {
553         threshold_line = new ArdourCanvas::SimpleLine (*g);
554         threshold_line->property_color_rgba() = RGBA_TO_UINT (0, 0, 0, 128);
555 }
556
557 StripSilenceDialog::Wave::~Wave ()
558 {
559         delete view;
560         delete threshold_line;
561         for (list<SimpleRect*>::iterator i = silence_rects.begin(); i != silence_rects.end(); ++i) {
562                 delete *i;
563         }
564 }