fix math bug with numthreads computation
[ardour2.git] / gtk2_ardour / strip_silence_dialog.cc
blob55429dc3daca1f05ee9664c0af97fcf44acf29e5
1 /*
2 Copyright (C) 2009 Paul Davis
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.
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.
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.
20 #include <iostream>
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"
28 #include "ardour/dB.h"
29 #include "ardour_ui.h"
30 #include "ardour/session.h"
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"
42 using namespace ARDOUR;
43 using namespace std;
44 using namespace ArdourCanvas;
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;
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)
63 set_session (s);
65 if (thread_waiting == 0) {
66 thread_waiting = new Glib::Cond;
67 thread_run = new Glib::Cond;
70 Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
72 Gtk::Table* table = Gtk::manage (new Gtk::Table (3, 3));
73 table->set_spacings (6);
75 int n = 0;
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;
82 _threshold.set_digits (1);
83 _threshold.set_increments (1, 10);
84 _threshold.set_range (-120, 0);
85 _threshold.set_value (-60);
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;
91 _minimum_length.set_session (s);
92 _minimum_length.set_mode (AudioClock::Frames);
93 _minimum_length.set (1000, true);
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;
99 _fade_length.set_session (s);
100 _fade_length.set_mode (AudioClock::Frames);
101 _fade_length.set (64, true);
103 hbox->pack_start (*table);
105 table = Gtk::manage (new Gtk::Table (3, 2));
106 table->set_spacings (6);
108 n = 0;
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;
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;
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;
125 hbox->pack_start (*table);
127 /* dummy label for padding */
128 hbox->pack_start (*Gtk::manage (new Gtk::Label ("")), true, true);
130 get_vbox()->pack_start (*hbox, false, false);
132 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
133 add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
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());
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);
144 get_vbox()->pack_start (*_canvas, true, true);
146 show_all ();
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));
151 create_waves ();
152 update_silence_rects ();
153 update_threshold_line ();
155 maybe_start_silence_detection ();
159 StripSilenceDialog::~StripSilenceDialog ()
161 for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
162 delete *i;
165 _waves.clear ();
167 delete _peaks_ready_connection;
168 delete _canvas;
171 void
172 StripSilenceDialog::create_waves ()
174 int n = 0;
176 delete _peaks_ready_connection;
177 _peaks_ready_connection = 0;
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;
200 ++n;
204 void
205 StripSilenceDialog::peaks_ready ()
207 delete _peaks_ready_connection;
208 _peaks_ready_connection = 0;
209 create_waves ();
212 void
213 StripSilenceDialog::canvas_allocation (Gtk::Allocation& alloc)
215 int n = 0;
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 ();
221 for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i, ++n) {
222 (*i)->samples_per_unit = ((double) (*i)->region->length() / _wave_width);
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;
231 resize_silence_rects ();
232 update_threshold_line ();
235 void
236 StripSilenceDialog::resize_silence_rects ()
238 int n = 0;
240 for (list<Wave*>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
242 list<pair<frameoffset_t, framecnt_t> >::const_iterator j;
243 list<SimpleRect*>::iterator r;
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);
255 ++n;
259 void
260 StripSilenceDialog::update_threshold_line ()
262 int n = 0;
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;
268 double const y = alt_log_meter (_threshold.get_value());
270 (*i)->threshold_line->property_y1() = (n + 1 - y) * _wave_height;
271 (*i)->threshold_line->property_y2() = (n + 1 - y) * _wave_height;
274 ++n;
277 void
278 StripSilenceDialog::update_silence_rects ()
280 int n = 0;
281 uint32_t max_segments = 0;
282 uint32_t sc;
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;
289 (*i)->silence_rects.clear ();
290 sc = 0;
292 for (list<pair<frameoffset_t, framecnt_t> >::const_iterator j = (*i)->silence.begin(); j != (*i)->silence.end(); ++j) {
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++;
305 max_segments = max (max_segments, sc);
306 ++n;
309 if (min_audible > 0) {
310 float ms, ma;
311 char const * aunits;
312 char const * sunits;
314 ma = (float) min_audible/_session->frame_rate();
315 ms = (float) min_silence/_session->frame_rate();
317 if (min_audible < _session->frame_rate()) {
318 aunits = _("ms");
319 ma *= 1000.0;
320 } else {
321 aunits = _("s");
324 if (min_silence < _session->frame_rate()) {
325 sunits = _("ms");
326 ms *= 1000.0;
327 } else {
328 sunits = _("s");
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 ("");
339 } else {
340 _segment_count_label.set_text (_("Full silence"));
341 _shortest_silence_label.set_text ("");
342 _shortest_audible_label.set_text ("");
346 bool
347 StripSilenceDialog::_detection_done (void* arg)
349 StripSilenceDialog* ssd = (StripSilenceDialog*) arg;
350 return ssd->detection_done ();
353 bool
354 StripSilenceDialog::detection_done ()
356 get_window()->set_cursor (Gdk::Cursor (Gdk::LEFT_PTR));
357 update_silence_rects ();
358 return false;
361 void*
362 StripSilenceDialog::_detection_thread_work (void* arg)
364 StripSilenceDialog* ssd = (StripSilenceDialog*) arg;
365 return ssd->detection_thread_work ();
368 void*
369 StripSilenceDialog::detection_thread_work ()
371 ARDOUR_UI::instance()->register_thread ("gui", pthread_self(), "silence", 32);
373 while (1) {
375 run_lock.lock ();
376 thread_waiting->signal ();
377 thread_run->wait (run_lock);
379 if (thread_should_exit) {
380 thread_waiting->signal ();
381 run_lock.unlock ();
382 break;
385 if (current) {
386 StripSilenceDialog* ssd = current;
387 run_lock.unlock ();
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);
394 if (!ssd->itt.cancel) {
395 g_idle_add ((gboolean (*)(void*)) StripSilenceDialog::_detection_done, ssd);
397 } else {
398 run_lock.unlock ();
403 return 0;
406 void
407 StripSilenceDialog::threshold_changed ()
409 update_threshold_line ();
410 maybe_start_silence_detection ();
413 void
414 StripSilenceDialog::maybe_start_silence_detection ()
416 if (!restart_queued) {
417 restart_queued = true;
418 Glib::signal_idle().connect (sigc::mem_fun (*this, &StripSilenceDialog::start_silence_detection));
422 bool
423 StripSilenceDialog::start_silence_detection ()
425 Glib::Mutex::Lock lm (run_lock);
426 restart_queued = false;
428 if (!itt.thread) {
430 itt.done = false;
431 itt.cancel = false;
432 itt.progress = 0.0;
433 current = this;
435 pthread_create (&itt.thread, 0, StripSilenceDialog::_detection_thread_work, this);
436 /* wait for it to get started */
437 thread_waiting->wait (run_lock);
439 } else {
441 /* stop whatever the thread is doing */
443 itt.cancel = 1;
444 current = 0;
446 while (!itt.done) {
447 thread_run->signal ();
448 thread_waiting->wait (run_lock);
453 itt.cancel = false;
454 itt.done = false;
455 itt.progress = 0.0;
456 current = this;
458 /* and start it up (again) */
460 thread_run->signal ();
462 /* change cursor */
464 get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
466 /* don't call again until needed */
468 return false;
471 void
472 StripSilenceDialog::stop_thread ()
474 Glib::Mutex::Lock lm (run_lock);
476 itt.cancel = true;
477 thread_should_exit = true;
478 thread_run->signal ();
479 thread_waiting->wait (run_lock);
480 itt.thread = 0;
483 void
484 StripSilenceDialog::update_stats (const SilenceResult& res)
486 if (res.empty()) {
487 return;
490 max_silence = 0;
491 min_silence = max_frames;
492 max_audible = 0;
493 min_audible = max_frames;
495 SilenceResult::const_iterator cur;
497 cur = res.begin();
499 framepos_t start = 0;
500 framepos_t end;
501 bool in_silence;
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;
513 while (cur != res.end()) {
515 framecnt_t interval_duration;
517 interval_duration = end - start;
519 if (in_silence) {
521 max_silence = max (max_silence, interval_duration);
522 min_silence = min (min_silence, interval_duration);
523 } else {
525 max_audible = max (max_audible, interval_duration);
526 min_audible = min (min_audible, interval_duration);
529 start = end;
530 ++cur;
531 end = cur->first;
532 in_silence = !in_silence;
536 nframes_t
537 StripSilenceDialog::minimum_length () const
539 return _minimum_length.current_duration (_waves.front()->region->position());
542 nframes_t
543 StripSilenceDialog::fade_length () const
545 return _minimum_length.current_duration (_waves.front()->region->position());
548 StripSilenceDialog::Wave::Wave (Group* g, boost::shared_ptr<AudioRegion> r)
549 : region (r), view (0), samples_per_unit (1)
551 threshold_line = new ArdourCanvas::SimpleLine (*g);
552 threshold_line->property_color_rgba() = RGBA_TO_UINT (0, 0, 0, 128);
555 StripSilenceDialog::Wave::~Wave ()
557 delete view;
558 delete threshold_line;
559 for (list<SimpleRect*>::iterator i = silence_rects.begin(); i != silence_rects.end(); ++i) {
560 delete *i;