fixes and improvements for strip silence, with lots of thread debugging
[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 "gui_thread.h"
31 #include "strip_silence_dialog.h"
32 #include "canvas_impl.h"
33 #include "waveview.h"
34 #include "simplerect.h"
35 #include "rgb_macros.h"
36 #include "i18n.h"
37
38 using namespace ARDOUR;
39 using namespace std;
40 using namespace ArdourCanvas;
41
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;
48
49 /** Construct Strip silence dialog box */
50 StripSilenceDialog::StripSilenceDialog (std::list<boost::shared_ptr<ARDOUR::AudioRegion> > const & regions)
51         : ArdourDialog (_("Strip Silence"))
52         , _wave_width (640)
53         , _wave_height (64)
54         , restart_queued (false)
55 {
56         if (thread_waiting == 0) {
57                 thread_waiting = new Glib::Cond;
58                 thread_run = new Glib::Cond;
59         }
60         
61         for (std::list<boost::shared_ptr<ARDOUR::AudioRegion> >::const_iterator i = regions.begin(); i != regions.end(); ++i) {
62
63                 Wave w;
64                 w.region = *i;
65                 _waves.push_back (w);
66
67         }
68
69         Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
70         hbox->set_spacing (16);
71
72         Gtk::Table* table = Gtk::manage (new Gtk::Table (4, 3));
73         table->set_spacings (4);
74
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);
86
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);
97
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);
108
109         hbox->pack_start (*table, false, false);
110
111         _segment_count_label.set_text (_("Silent segments: none"));
112         hbox->pack_start (_segment_count_label, false, false);
113
114         get_vbox()->pack_start (*hbox, false, false);
115
116         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
117         add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
118
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 ());
122
123         get_vbox()->pack_start (*_canvas, true, true);
124
125         show_all ();
126
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));
129
130         create_waves ();
131         update_silence_rects ();
132 }
133
134
135 StripSilenceDialog::~StripSilenceDialog ()
136 {
137         for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
138                 delete i->view;
139                 for (std::list<SimpleRect*>::iterator j = i->silence_rects.begin(); j != i->silence_rects.end(); ++j) {
140                         delete *j;
141                 }
142         }
143
144         delete _canvas;
145 }
146
147 void
148 StripSilenceDialog::create_waves ()
149 {
150         int n = 0;
151
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();
169                 }
170
171                 ++n;
172         }
173 }
174
175 void
176 StripSilenceDialog::peaks_ready ()
177 {
178         _peaks_ready_connection.disconnect ();
179         create_waves ();
180 }
181
182 void
183 StripSilenceDialog::canvas_allocation (Gtk::Allocation& alloc)
184 {
185         int n = 0;
186
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 ();
190
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);
193
194                 if (i->view) {
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;
198                 }
199         }
200
201         redraw_silence_rects ();
202 }
203
204 void
205 StripSilenceDialog::redraw_silence_rects ()
206 {
207         int n = 0;
208
209         for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
210
211                 std::list<std::pair<frameoffset_t, framecnt_t> >::const_iterator j;
212                 std::list<SimpleRect*>::iterator r;
213
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);
222                 }
223
224                 ++n;
225         }
226 }
227
228 void
229 StripSilenceDialog::update_silence_rects ()
230 {
231         int n = 0;
232         uint32_t max_segments = 0;
233         uint32_t sc;
234
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) {
237                         delete *j;
238                 }
239
240                 i->silence_rects.clear ();                
241                 sc = 0;
242
243                 for (std::list<std::pair<frameoffset_t, framecnt_t> >::const_iterator j = i->silence.begin(); j != i->silence.end(); ++j) {
244
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);
253                         sc++;
254                 }
255
256                 max_segments = max (max_segments, sc);
257                 ++n;
258         }
259
260         _segment_count_label.set_text (string_compose (_("Silent segments: %1"), max_segments));
261 }
262
263 bool
264 StripSilenceDialog::_detection_done (void* arg)
265 {
266         StripSilenceDialog* ssd = (StripSilenceDialog*) arg;
267         return ssd->detection_done ();
268 }
269
270 bool
271 StripSilenceDialog::detection_done ()
272 {
273         get_window()->set_cursor (Gdk::Cursor (Gdk::LEFT_PTR));
274         update_silence_rects ();
275         return false;
276 }
277
278 void*
279 StripSilenceDialog::_detection_thread_work (void* arg)
280 {
281         StripSilenceDialog* ssd = (StripSilenceDialog*) arg;
282         return ssd->detection_thread_work ();
283 }
284
285 void*
286 StripSilenceDialog::detection_thread_work ()
287 {
288         ARDOUR_UI::instance()->register_thread ("gui", pthread_self(), "silence", 32);
289         
290         cerr << pthread_self() << ": thread exists\n";
291
292         while (1) {
293
294                 run_lock.lock ();
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);
299
300                 cerr << pthread_self() << ": silence thread active\n";
301
302                 if (thread_should_exit) {
303                         thread_waiting->signal ();
304                         run_lock.unlock ();
305                         cerr << pthread_self() << ": silence thread exited\n";
306                         break;
307                 }
308
309                 if (current) {
310                         StripSilenceDialog* ssd = current;
311                         run_lock.unlock ();
312                         
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);
315                         }
316                         
317                         if (!ssd->itt.cancel) {
318                                 g_idle_add ((gboolean (*)(void*)) StripSilenceDialog::_detection_done, ssd);
319                         }
320                 }
321
322                 cerr << pthread_self() << ": silence iteration done\n";
323         }
324
325         return 0;
326 }
327
328 void
329 StripSilenceDialog::maybe_start_silence_detection ()
330 {
331         if (!restart_queued) {
332                 restart_queued = true;
333                 Glib::signal_idle().connect (sigc::mem_fun (*this, &StripSilenceDialog::start_silence_detection));
334         }
335 }
336
337 bool
338 StripSilenceDialog::start_silence_detection ()
339 {
340         Glib::Mutex::Lock lm (run_lock);
341         restart_queued = false;
342
343         if (!itt.thread) {
344
345                 itt.done = false;
346                 itt.cancel = false;
347                 itt.progress = 0.0;
348                 current = this;
349
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";
355
356         } else {
357                 
358                 /* stop whatever the thread is doing */
359
360                 itt.cancel = 1;
361                 current = 0;
362
363                 while (!itt.done) {
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";
369                 }
370         }
371
372
373         itt.cancel = false;
374         itt.done = false;
375         itt.progress = 0.0;
376         current = this;
377         
378         /* and start it up (again) */
379         
380         cerr << "signal thread to run again\n";
381         thread_run->signal ();
382
383         /* change cursor */
384
385         get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
386
387         /* don't call again until needed */
388         
389         return false;
390 }
391
392 void
393 StripSilenceDialog::stop_thread ()
394 {
395         Glib::Mutex::Lock lm (run_lock);
396
397         itt.cancel = true;
398         thread_should_exit = true;
399         thread_run->signal (); 
400         thread_waiting->wait (run_lock);
401 }