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.
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 "gui_thread.h"
31 #include "strip_silence_dialog.h"
32 #include "canvas_impl.h"
34 #include "simplerect.h"
35 #include "rgb_macros.h"
38 using namespace ARDOUR;
40 using namespace ArdourCanvas;
42 Glib::StaticMutex StripSilenceDialog::run_lock;
43 Glib::Cond* StripSilenceDialog::thread_waiting = 0;
44 Glib::Cond* StripSilenceDialog::thread_run = 0;
45 bool StripSilenceDialog::thread_should_exit = false;
46 InterThreadInfo StripSilenceDialog::itt;
47 StripSilenceDialog* StripSilenceDialog::current = 0;
49 /** Construct Strip silence dialog box */
50 StripSilenceDialog::StripSilenceDialog (std::list<boost::shared_ptr<ARDOUR::AudioRegion> > const & regions)
51 : ArdourDialog (_("Strip Silence"))
54 , restart_queued (false)
56 if (thread_waiting == 0) {
57 thread_waiting = new Glib::Cond;
58 thread_run = new Glib::Cond;
61 for (std::list<boost::shared_ptr<ARDOUR::AudioRegion> >::const_iterator i = regions.begin(); i != regions.end(); ++i) {
69 Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
70 hbox->set_spacing (16);
72 Gtk::Table* table = Gtk::manage (new Gtk::Table (4, 3));
73 table->set_spacings (4);
75 Gtk::Label* l = Gtk::manage (new Gtk::Label (_("Threshold:")));
76 l->set_alignment (1, 0.5);
77 table->attach (*l, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
78 _threshold.set_digits (1);
79 _threshold.set_increments (1, 10);
80 _threshold.set_range (-120, 0);
81 _threshold.set_value (-60);
82 table->attach (_threshold, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL);
83 l = Gtk::manage (new Gtk::Label (_("dBFS")));
84 l->set_alignment (0, 0.5);
85 table->attach (*l, 2, 3, 0, 1, Gtk::FILL, Gtk::FILL);
87 l = Gtk::manage (new Gtk::Label (_("Minimum length:")));
88 l->set_alignment (1, 0.5);
89 table->attach (*l, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
90 _minimum_length.set_digits (0);
91 _minimum_length.set_increments (1, 10);
92 _minimum_length.set_range (0, 65536);
93 _minimum_length.set_value (256);
94 table->attach (_minimum_length, 1, 2, 1, 2, Gtk::FILL, Gtk::FILL);
95 l = Gtk::manage (new Gtk::Label (_("samples")));
96 table->attach (*l, 2, 3, 1, 2, Gtk::FILL, Gtk::FILL);
98 l = Gtk::manage (new Gtk::Label (_("Fade length:")));
99 l->set_alignment (1, 0.5);
100 table->attach (*l, 0, 1, 2, 3, Gtk::FILL, Gtk::FILL);
101 _fade_length.set_digits (0);
102 _fade_length.set_increments (1, 10);
103 _fade_length.set_range (0, 1024);
104 _fade_length.set_value (64);
105 table->attach (_fade_length, 1, 2, 2, 3, Gtk::FILL, Gtk::FILL);
106 l = Gtk::manage (new Gtk::Label (_("samples")));
107 table->attach (*l, 2, 3, 2, 3, Gtk::FILL, Gtk::FILL);
109 hbox->pack_start (*table, false, false);
111 _segment_count_label.set_text (_("Silent segments: none"));
112 hbox->pack_start (_segment_count_label, false, false);
114 get_vbox()->pack_start (*hbox, false, false);
116 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
117 add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
119 _canvas = new CanvasAA ();
120 _canvas->signal_size_allocate().connect (sigc::mem_fun (*this, &StripSilenceDialog::canvas_allocation));
121 _canvas->set_size_request (_wave_width, _wave_height * _waves.size ());
123 get_vbox()->pack_start (*_canvas, true, true);
127 _threshold.get_adjustment()->signal_value_changed().connect (sigc::mem_fun (*this, &StripSilenceDialog::maybe_start_silence_detection));
128 _minimum_length.get_adjustment()->signal_value_changed().connect (sigc::mem_fun (*this, &StripSilenceDialog::maybe_start_silence_detection));
131 update_silence_rects ();
135 StripSilenceDialog::~StripSilenceDialog ()
137 for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
139 for (std::list<SimpleRect*>::iterator j = i->silence_rects.begin(); j != i->silence_rects.end(); ++j) {
148 StripSilenceDialog::create_waves ()
152 for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
153 if (i->region->audio_source(0)->peaks_ready (boost::bind (&StripSilenceDialog::peaks_ready, this), _peaks_ready_connection, gui_context())) {
154 i->view = new WaveView (*(_canvas->root()));
155 i->view->property_data_src() = static_cast<gpointer>(i->region.get());
156 i->view->property_cache() = WaveView::create_cache ();
157 i->view->property_cache_updater() = true;
158 i->view->property_channel() = 0;
159 i->view->property_length_function() = (void *) region_length_from_c;
160 i->view->property_sourcefile_length_function() = (void *) sourcefile_length_from_c;
161 i->view->property_peak_function() = (void *) region_read_peaks_from_c;
162 i->view->property_x() = 0;
163 i->view->property_y() = n * _wave_height;
164 i->view->property_height() = _wave_height;
165 i->view->property_samples_per_unit() = i->samples_per_unit;
166 i->view->property_region_start() = i->region->start();
167 i->view->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
168 i->view->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
176 StripSilenceDialog::peaks_ready ()
178 _peaks_ready_connection.disconnect ();
183 StripSilenceDialog::canvas_allocation (Gtk::Allocation& alloc)
187 _canvas->set_scroll_region (0.0, 0.0, alloc.get_width(), alloc.get_height());
188 _wave_width = alloc.get_width ();
189 _wave_height = alloc.get_height ();
191 for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i, ++n) {
192 i->samples_per_unit = ((double) i->region->length() / _wave_width);
195 i->view->property_y() = n * _wave_height;
196 i->view->property_samples_per_unit() = i->samples_per_unit;
197 i->view->property_height() = _wave_height;
201 redraw_silence_rects ();
205 StripSilenceDialog::redraw_silence_rects ()
209 for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
211 std::list<std::pair<frameoffset_t, framecnt_t> >::const_iterator j;
212 std::list<SimpleRect*>::iterator r;
214 for (j = i->silence.begin(), r = i->silence_rects.begin();
215 j != i->silence.end() && r != i->silence_rects.end(); ++j, ++r) {
216 (*r)->property_x1() = j->first / i->samples_per_unit;
217 (*r)->property_x2() = j->second / i->samples_per_unit;
218 (*r)->property_y1() = n * _wave_height;
219 (*r)->property_y2() = (n + 1) * _wave_height;
220 (*r)->property_outline_pixels() = 0;
221 (*r)->property_fill_color_rgba() = RGBA_TO_UINT (128, 128, 128, 128);
229 StripSilenceDialog::update_silence_rects ()
232 uint32_t max_segments = 0;
235 for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
236 for (std::list<SimpleRect*>::iterator j = i->silence_rects.begin(); j != i->silence_rects.end(); ++j) {
240 i->silence_rects.clear ();
243 for (std::list<std::pair<frameoffset_t, framecnt_t> >::const_iterator j = i->silence.begin(); j != i->silence.end(); ++j) {
245 SimpleRect* r = new SimpleRect (*(_canvas->root()));
246 r->property_x1() = j->first / i->samples_per_unit;
247 r->property_x2() = j->second / i->samples_per_unit;
248 r->property_y1() = n * _wave_height;
249 r->property_y2() = (n + 1) * _wave_height;
250 r->property_outline_pixels() = 0;
251 r->property_fill_color_rgba() = RGBA_TO_UINT (128, 128, 128, 128);
252 i->silence_rects.push_back (r);
256 max_segments = max (max_segments, sc);
260 _segment_count_label.set_text (string_compose (_("Silent segments: %1"), max_segments));
264 StripSilenceDialog::_detection_done (void* arg)
266 StripSilenceDialog* ssd = (StripSilenceDialog*) arg;
267 return ssd->detection_done ();
271 StripSilenceDialog::detection_done ()
273 get_window()->set_cursor (Gdk::Cursor (Gdk::LEFT_PTR));
274 update_silence_rects ();
279 StripSilenceDialog::_detection_thread_work (void* arg)
281 StripSilenceDialog* ssd = (StripSilenceDialog*) arg;
282 return ssd->detection_thread_work ();
286 StripSilenceDialog::detection_thread_work ()
288 ARDOUR_UI::instance()->register_thread ("gui", pthread_self(), "silence", 32);
290 cerr << pthread_self() << ": thread exists\n";
295 cerr << pthread_self() << ": thread notes that its waiting\n";
296 thread_waiting->signal ();
297 cerr << pthread_self() << ": thread waits to run\n";
298 thread_run->wait (run_lock);
300 cerr << pthread_self() << ": silence thread active\n";
302 if (thread_should_exit) {
303 thread_waiting->signal ();
305 cerr << pthread_self() << ": silence thread exited\n";
310 StripSilenceDialog* ssd = current;
313 for (std::list<Wave>::iterator i = ssd->_waves.begin(); i != ssd->_waves.end(); ++i) {
314 i->silence = i->region->find_silence (dB_to_coefficient (ssd->threshold ()), ssd->minimum_length (), ssd->itt);
317 if (!ssd->itt.cancel) {
318 g_idle_add ((gboolean (*)(void*)) StripSilenceDialog::_detection_done, ssd);
322 cerr << pthread_self() << ": silence iteration done\n";
329 StripSilenceDialog::maybe_start_silence_detection ()
331 if (!restart_queued) {
332 restart_queued = true;
333 Glib::signal_idle().connect (sigc::mem_fun (*this, &StripSilenceDialog::start_silence_detection));
338 StripSilenceDialog::start_silence_detection ()
340 Glib::Mutex::Lock lm (run_lock);
341 restart_queued = false;
350 pthread_create (&itt.thread, 0, StripSilenceDialog::_detection_thread_work, this);
351 /* wait for it to get started */
352 cerr << "Wait for new thread to be ready\n";
353 thread_waiting->wait (run_lock);
354 cerr << "\tits ready\n";
358 /* stop whatever the thread is doing */
364 cerr << "tell existing thread to stop\n";
365 thread_run->signal ();
366 cerr << "wait for existing thread to stop\n";
367 thread_waiting->wait (run_lock);
368 cerr << "its stopped\n";
378 /* and start it up (again) */
380 cerr << "signal thread to run again\n";
381 thread_run->signal ();
385 get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
387 /* don't call again until needed */
393 StripSilenceDialog::stop_thread ()
395 Glib::Mutex::Lock lm (run_lock);
398 thread_should_exit = true;
399 thread_run->signal ();
400 thread_waiting->wait (run_lock);