Simplify strip silence dialogue threading, hopefully fixing #3560 in the process.
[ardour.git] / gtk2_ardour / strip_silence_dialog.h
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 <gtkmm/spinbutton.h>
21 #include <glibmm/thread.h>
22
23 #include "ardour/types.h"
24 #include "ardour_dialog.h"
25 #include "canvas.h"
26 #include "progress_reporter.h"
27
28 namespace ARDOUR {
29         class AudioRegion;
30         class Session;
31 }
32
33 /// Dialog box to set options for the `strip silence' filter
34 class StripSilenceDialog : public ArdourDialog, public ProgressReporter
35 {
36 public:
37         StripSilenceDialog (ARDOUR::Session*, std::list<boost::shared_ptr<ARDOUR::AudioRegion> > const &);
38         ~StripSilenceDialog ();
39
40         double threshold () const {
41                 return _threshold.get_value ();
42         }
43
44         nframes_t minimum_length () const;
45         nframes_t fade_length () const;
46
47 private:
48         typedef std::list<std::pair<ARDOUR::frameoffset_t,ARDOUR::framecnt_t> > SilenceResult;
49         
50         void create_waves ();
51         void peaks_ready ();
52         void canvas_allocation (Gtk::Allocation &);
53         void update_silence_rects ();
54         void resize_silence_rects ();
55         void update ();
56         void update_threshold_line ();
57         void update_stats (SilenceResult const &);
58         void threshold_changed ();
59         void update_progress_gui (float);
60         void restart_thread ();
61
62         Gtk::SpinButton _threshold;
63         AudioClock      _minimum_length;
64         AudioClock      _fade_length;
65         Gtk::Label      _segment_count_label;
66         Gtk::Label      _shortest_silence_label;
67         Gtk::Label      _shortest_audible_label;
68         Gtk::ProgressBar _progress_bar;
69
70         struct Wave {
71                 boost::shared_ptr<ARDOUR::AudioRegion> region;
72                 ArdourCanvas::WaveView* view;
73                 std::list<ArdourCanvas::SimpleRect*> silence_rects;
74                 ArdourCanvas::SimpleLine* threshold_line;
75                 double samples_per_unit;
76                 SilenceResult silence;
77                 
78                 Wave (ArdourCanvas::Group *, boost::shared_ptr<ARDOUR::AudioRegion>);
79                 ~Wave ();
80         };
81
82         ArdourCanvas::Canvas* _canvas;
83         std::list<Wave*> _waves;
84         int _wave_width;
85         int _wave_height;
86
87         ARDOUR::framecnt_t max_audible;
88         ARDOUR::framecnt_t min_audible;
89         ARDOUR::framecnt_t max_silence;
90         ARDOUR::framecnt_t min_silence;
91
92         PBD::ScopedConnection* _peaks_ready_connection;
93
94         pthread_t _thread; ///< thread to compute silence in the background
95         static void * _detection_thread_work (void *);
96         void * detection_thread_work ();
97         Glib::Mutex _lock; ///< lock held while the thread is doing work
98         Glib::Cond _run_cond; ///< condition to wake the thread
99         bool _thread_should_finish; ///< true if the thread should terminate
100         PBD::Signal0<void> Completed; ///< emitted when a silence detection has completed
101         PBD::ScopedConnection _completed_connection;
102         ARDOUR::InterThreadInfo _interthread_info;
103 };