1 /*
2  * Copyright (C) 2009-2010 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2009-2011 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include <gtkmm/spinbutton.h>
23 #include <glibmm/threads.h>
24 
25 #include <pbd/xml++.h>
26 
27 #include "ardour/types.h"
28 #include "ardour_dialog.h"
29 #include "progress_reporter.h"
30 
31 namespace ARDOUR {
32 	class Session;
33 }
34 
35 class AudioClock;
36 class RegionView;
37 
38 /// Dialog box to set options for the `strip silence' filter
39 class StripSilenceDialog : public ArdourDialog, public ProgressReporter
40 {
41 public:
42 	StripSilenceDialog (ARDOUR::Session*, std::list<RegionView*> const &);
43 	~StripSilenceDialog ();
44 
threshold()45 	double threshold () const {
46 		return _threshold.get_value ();
47 	}
48 
49 	void drop_rects ();
50 
51 	void silences (ARDOUR::AudioIntervalMap&);
52 
53 	ARDOUR::samplecnt_t minimum_length () const;
54 	ARDOUR::samplecnt_t fade_length () const;
55 
on_response(int response_id)56 	void on_response (int response_id) {
57 		Gtk::Dialog::on_response (response_id);
58 	}
59 
60 	XMLNode& get_state ();
61 	void set_state (const XMLNode &);
62 
63 private:
64 	void create_waves ();
65 	void canvas_allocation (Gtk::Allocation &);
66 	void update_silence_rects ();
67 	void resize_silence_rects ();
68 	void update ();
69 	void update_threshold_line ();
70 	void update_stats (ARDOUR::AudioIntervalResult const &);
71 	void threshold_changed ();
72 	void update_progress_gui (float);
73 	void restart_thread ();
74 	void finished(int);
75 
76 	Gtk::SpinButton _threshold;
77 	AudioClock*      _minimum_length;
78 	AudioClock*      _fade_length;
79 	Gtk::ProgressBar _progress_bar;
80 
81 	Gtk::Button* cancel_button;
82 	Gtk::Button* apply_button;
83 
84 	struct ViewInterval {
85 		RegionView* view;
86 		ARDOUR::AudioIntervalResult intervals;
87 
ViewIntervalViewInterval88 		ViewInterval (RegionView* rv) : view (rv) {}
89 	};
90 
91 	std::list<ViewInterval> views;
92 
93 	bool _destroying;
94 
95 	pthread_t _thread; ///< thread to compute silence in the background
96 	static void * _detection_thread_work (void *);
97 	void * detection_thread_work ();
98 	Glib::Threads::Mutex _lock; ///< lock held while the thread is doing work
99 	Glib::Threads::Cond  _run_cond; ///< condition to wake the thread
100 	bool _thread_should_finish; ///< true if the thread should terminate
101 	PBD::Signal0<void> Completed; ///< emitted when a silence detection has completed
102 	PBD::ScopedConnection _completed_connection;
103 	ARDOUR::InterThreadInfo _interthread_info;
104 
105 	sigc::connection progress_idle_connection;
106 	bool idle_update_progress(); ///< GUI-thread progress updates of background silence computation
107 	int analysis_progress_cur;
108 	int analysis_progress_max;
109 
110 	int _threshold_value;
111 	ARDOUR::samplecnt_t _minimum_length_value;
112 	ARDOUR::samplecnt_t _fade_length_value;
113 };
114