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