Fix warning.
[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 <gtkmm/table.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/stock.h>
23 #include "ardour/audioregion.h"
24 #include "ardour/audiosource.h"
25
26 #include "ardour/dB.h"
27 #include "ardour_ui.h"
28 #include "gui_thread.h"
29 #include "strip_silence_dialog.h"
30 #include "canvas_impl.h"
31 #include "waveview.h"
32 #include "simplerect.h"
33 #include "rgb_macros.h"
34 #include "i18n.h"
35
36 /** Construct Strip silence dialog box */
37 StripSilenceDialog::StripSilenceDialog (std::list<boost::shared_ptr<ARDOUR::AudioRegion> > const & regions)
38         : ArdourDialog (_("Strip Silence")), _wave_width (640), _wave_height (64)
39 {
40         for (std::list<boost::shared_ptr<ARDOUR::AudioRegion> >::const_iterator i = regions.begin(); i != regions.end(); ++i) {
41
42                 Wave w;
43                 w.region = *i;
44                 w.view = 0;
45                 w.samples_per_unit = 1;
46                 _waves.push_back (w);
47
48         }
49
50         Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
51         hbox->set_spacing (16);
52
53         Gtk::Table* table = Gtk::manage (new Gtk::Table (4, 3));
54         table->set_spacings (4);
55
56         Gtk::Label* l = Gtk::manage (new Gtk::Label (_("Threshold:")));
57         l->set_alignment (1, 0.5);
58         table->attach (*l, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
59         _threshold.set_digits (1);
60         _threshold.set_increments (1, 10);
61         _threshold.set_range (-120, 0);
62         _threshold.set_value (-60);
63         table->attach (_threshold, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL);
64         l = Gtk::manage (new Gtk::Label (_("dBFS")));
65         l->set_alignment (0, 0.5);
66         table->attach (*l, 2, 3, 0, 1, Gtk::FILL, Gtk::FILL);
67
68         l = Gtk::manage (new Gtk::Label (_("Minimum length:")));
69         l->set_alignment (1, 0.5);
70         table->attach (*l, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
71         _minimum_length.set_digits (0);
72         _minimum_length.set_increments (1, 10);
73         _minimum_length.set_range (0, 65536);
74         _minimum_length.set_value (256);
75         table->attach (_minimum_length, 1, 2, 1, 2, Gtk::FILL, Gtk::FILL);
76         l = Gtk::manage (new Gtk::Label (_("samples")));
77         table->attach (*l, 2, 3, 1, 2, Gtk::FILL, Gtk::FILL);
78
79         l = Gtk::manage (new Gtk::Label (_("Fade length:")));
80         l->set_alignment (1, 0.5);
81         table->attach (*l, 0, 1, 2, 3, Gtk::FILL, Gtk::FILL);
82         _fade_length.set_digits (0);
83         _fade_length.set_increments (1, 10);
84         _fade_length.set_range (0, 1024);
85         _fade_length.set_value (64);
86         table->attach (_fade_length, 1, 2, 2, 3, Gtk::FILL, Gtk::FILL);
87         l = Gtk::manage (new Gtk::Label (_("samples")));
88         table->attach (*l, 2, 3, 2, 3, Gtk::FILL, Gtk::FILL);
89
90         hbox->pack_start (*table, false, false);
91
92         Gtk::VBox* v = Gtk::manage (new Gtk::VBox);
93         Gtk::Button* b = Gtk::manage (new Gtk::Button (_("Update display")));
94         b->signal_clicked().connect (sigc::mem_fun (*this, &StripSilenceDialog::update_silence_rects));
95         v->pack_start (*b, false, false);
96         hbox->pack_start (*v, false, false);
97
98         get_vbox()->add (*hbox);
99
100         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
101         add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
102
103         _canvas = new ArdourCanvas::CanvasAA ();
104         _canvas->signal_size_allocate().connect (sigc::mem_fun (*this, &StripSilenceDialog::canvas_allocation));
105         _canvas->set_size_request (_wave_width, _wave_height * _waves.size ());
106
107         get_vbox()->pack_start (*_canvas, true, true);
108
109         show_all ();
110
111         create_waves ();
112         update_silence_rects ();
113 }
114
115
116 StripSilenceDialog::~StripSilenceDialog ()
117 {
118         for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
119                 delete i->view;
120                 for (std::list<ArdourCanvas::SimpleRect*>::iterator j = i->silence_rects.begin(); j != i->silence_rects.end(); ++j) {
121                         delete *j;
122                 }
123         }
124
125         delete _canvas;
126 }
127
128 void
129 StripSilenceDialog::create_waves ()
130 {
131         int n = 0;
132
133         for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
134                 if (i->region->audio_source(0)->peaks_ready (boost::bind (&StripSilenceDialog::peaks_ready, this), _peaks_ready_connection, gui_context())) {
135                         i->view = new WaveView (*(_canvas->root()));
136                         i->view->property_data_src() = static_cast<gpointer>(i->region.get());
137                         i->view->property_cache() = WaveView::create_cache ();
138                         i->view->property_cache_updater() = true;
139                         i->view->property_channel() = 0;
140                         i->view->property_length_function() = (void *) region_length_from_c;
141                         i->view->property_sourcefile_length_function() = (void *) sourcefile_length_from_c;
142                         i->view->property_peak_function() = (void *) region_read_peaks_from_c;
143                         i->view->property_x() = 0;
144                         i->view->property_y() = n * _wave_height;
145                         i->view->property_height() = _wave_height;
146                         i->view->property_samples_per_unit() = i->samples_per_unit;
147                         i->view->property_region_start() = i->region->start();
148                         i->view->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
149                         i->view->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
150                 }
151
152                 ++n;
153         }
154 }
155
156 void
157 StripSilenceDialog::peaks_ready ()
158 {
159         _peaks_ready_connection.disconnect ();
160         create_waves ();
161 }
162
163 void
164 StripSilenceDialog::canvas_allocation (Gtk::Allocation& alloc)
165 {
166         _canvas->set_scroll_region (0.0, 0.0, alloc.get_width(), alloc.get_height());
167         _wave_width = alloc.get_width ();
168
169         for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
170                 i->samples_per_unit = ((double) i->region->length() / _wave_width);
171         }
172 }
173
174 void
175 StripSilenceDialog::update_silence_rects ()
176 {
177         int n = 0;
178
179         for (std::list<Wave>::iterator i = _waves.begin(); i != _waves.end(); ++i) {
180                 for (std::list<ArdourCanvas::SimpleRect*>::iterator j = i->silence_rects.begin(); j != i->silence_rects.end(); ++j) {
181                         delete *j;
182                 }
183
184                 i->silence_rects.clear ();
185
186                 std::list<std::pair<nframes_t, nframes_t> > const silence =
187                         i->region->find_silence (dB_to_coefficient (threshold ()), minimum_length ());
188
189                 for (std::list<std::pair<nframes_t, nframes_t> >::const_iterator j = silence.begin(); j != silence.end(); ++j) {
190
191                         ArdourCanvas::SimpleRect* r = new ArdourCanvas::SimpleRect (*(_canvas->root()));
192                         r->property_x1() = j->first / i->samples_per_unit;
193                         r->property_x2() = j->second / i->samples_per_unit;
194                         r->property_y1() = n * _wave_height;
195                         r->property_y2() = (n + 1) * _wave_height;
196                         r->property_outline_pixels() = 0;
197                         r->property_fill_color_rgba() = RGBA_TO_UINT (128, 128, 128, 128);
198                         i->silence_rects.push_back (r);
199
200                 }
201
202                 ++n;
203         }
204 }