Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / time_fx_dialog.cc
1 /*
2  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2015-2019 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
23 #include <iostream>
24 #include <cstdlib>
25 #include <cmath>
26 #include <string>
27
28 #include <gtkmm/stock.h>
29
30 #include "pbd/error.h"
31 #include "pbd/pthread_utils.h"
32 #include "pbd/memento_command.h"
33 #include "pbd/unwind.h"
34 #include "pbd/stacktrace.h"
35
36 #include "gtkmm2ext/utils.h"
37
38 #include "audio_clock.h"
39 #include "editor.h"
40 #include "audio_time_axis.h"
41 #include "audio_region_view.h"
42 #include "region_selection.h"
43 #include "time_fx_dialog.h"
44 #include "timers.h"
45
46 #ifdef USE_RUBBERBAND
47 #include <rubberband/RubberBandStretcher.h>
48 using namespace RubberBand;
49 #endif
50
51 #include "pbd/i18n.h"
52
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
56 using namespace Gtk;
57 using namespace Gtkmm2ext;
58
59 TimeFXDialog::TimeFXDialog (Editor& e, bool pitch, samplecnt_t oldlen, samplecnt_t new_length, samplepos_t position)
60         : ArdourDialog (X_("time fx dialog"))
61         , editor (e)
62         , pitching (pitch)
63         , quick_button (_("Quick but Ugly"))
64         , antialias_button (_("Skip Anti-aliasing"))
65         , stretch_opts_label (_("Contents"))
66         , precise_button (_("Minimize time distortion"))
67         , preserve_formants_button(_("Preserve Formants"))
68         , original_length (oldlen)
69         , pitch_octave_adjustment (0.0, -4.0, 4.0, 1, 2.0)
70         , pitch_semitone_adjustment (0.0, -12.0, 12.0, 1.0, 4.0)
71         , pitch_cent_adjustment (0.0, -499.0, 500.0, 5.0, 15.0)
72         , pitch_octave_spinner (pitch_octave_adjustment)
73         , pitch_semitone_spinner (pitch_semitone_adjustment)
74         , pitch_cent_spinner (pitch_cent_adjustment)
75         , duration_adjustment (100.0, -1000.0, 1000.0, 1.0, 10.0)
76         , duration_clock (0)
77         , ignore_adjustment_change (false)
78         , ignore_clock_change (false)
79         , progress (0.0f)
80 {
81         set_modal (true);
82         set_skip_taskbar_hint (true);
83         set_resizable (false);
84         set_name (N_("TimeFXDialog"));
85
86         if (pitching) {
87                 set_title (_("Pitch Shift Audio"));
88         } else {
89                 set_title (_("Time Stretch Audio"));
90         }
91
92         cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
93
94         VBox* vbox = manage (new VBox);
95         Gtk::Label* l;
96
97         get_vbox()->set_spacing (4);
98
99         vbox->set_spacing (18);
100         vbox->set_border_width (5);
101
102         upper_button_box.set_spacing (6);
103
104         l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false ));
105         l->set_use_markup ();
106
107         upper_button_box.pack_start (*l, false, false);
108
109         if (pitching) {
110                 Table* table = manage (new Table (4, 3, false));
111                 table->set_row_spacings (6);
112                 table->set_col_spacing  (1, 6);
113                 l = manage (new Label ("", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false )); //Common gnome way for padding
114                 l->set_padding (8, 0);
115                 table->attach (*l, 0, 1, 0, 4, Gtk::FILL, Gtk::FILL, 0, 0);
116
117                 l = manage (new Label (_("Octaves:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
118                 table->attach (*l, 1, 2, 0, 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
119                 table->attach (pitch_octave_spinner, 2, 3, 0, 1, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
120                 pitch_octave_spinner.set_activates_default ();
121
122                 l = manage (new Label (_("Semitones:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
123                 table->attach (*l, 1, 2, 1, 2, Gtk::FILL, Gtk::EXPAND, 0, 0);
124                 table->attach (pitch_semitone_spinner, 2, 3, 1, 2, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
125                 pitch_semitone_spinner.set_activates_default ();
126
127                 l = manage (new Label (_("Cents:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
128                 pitch_cent_spinner.set_digits (1);
129                 table->attach (*l, 1, 2, 2, 3, Gtk::FILL, Gtk::EXPAND, 0, 0);
130                 table->attach (pitch_cent_spinner, 2, 3, 2, 3, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
131                 pitch_cent_spinner.set_activates_default ();
132
133                 table->attach (preserve_formants_button, 1, 3, 3, 4, Gtk::FILL, Gtk::EXPAND, 0, 0);
134
135                 add_button (S_("Time|Shift"), Gtk::RESPONSE_ACCEPT);
136
137                 upper_button_box.pack_start (*table, false, true);
138         } else {
139                 Table* table = manage (new Table (4, 2, false));
140                 int row = 0;
141
142                 table->set_row_spacings (6);
143                 table->set_col_spacings (12);
144
145 #ifdef USE_RUBBERBAND
146                 vector<string> strings;
147                 duration_clock = manage (new AudioClock (X_("stretch"), true, X_("stretch"), true, false, true, false, true));
148                 duration_clock->set_session (e.session());
149                 duration_clock->set (new_length, true);
150                 duration_clock->set_mode (AudioClock::BBT);
151                 duration_clock->set_bbt_reference (position);
152
153                 Gtk::Alignment* clock_align = manage (new Gtk::Alignment);
154                 clock_align->add (*duration_clock);
155                 clock_align->set (0.0, 0.5, 0.0, 1.0);
156
157                 l = manage (new Gtk::Label (_("Duration")));
158                 table->attach (*l, 0, 1, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
159                 table->attach (*clock_align, 1, 2, row, row+1, Gtk::AttachOptions (Gtk::EXPAND|Gtk::FILL), Gtk::FILL, 0, 0);
160                 row++;
161
162                 const double fract = ((double) new_length) / original_length;
163                 /* note the *100.0 to convert fract into a percentage */
164                 duration_adjustment.set_value (fract*100.0);
165                 Gtk::SpinButton* spinner = manage (new Gtk::SpinButton (duration_adjustment, 1.0, 3));
166
167                 l = manage (new Gtk::Label (_("Percent")));
168                 table->attach (*l, 0, 1, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
169                 table->attach (*spinner, 1, 2, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
170                 row++;
171
172                 table->attach (stretch_opts_label, 0, 1, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
173
174                 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
175                 /* set default */
176                 stretch_opts_selector.set_active_text (editor.rb_opt_strings[editor.rb_current_opt]);
177                 table->attach (stretch_opts_selector, 1, 2, row, row+1, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
178                 row++;
179
180                 table->attach (precise_button, 0, 2, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
181                 row++;
182
183                 duration_clock->ValueChanged.connect (sigc::mem_fun (*this, &TimeFXDialog::duration_clock_changed));
184                 duration_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &TimeFXDialog::duration_adjustment_changed));
185
186 #else
187                 quick_button.set_name (N_("TimeFXButton"));
188                 table->attach (quick_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
189                 row++;
190
191                 antialias_button.set_name (N_("TimeFXButton"));
192                 table->attach (antialias_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
193
194 #endif
195
196                 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
197
198                 upper_button_box.pack_start (*table, false, true);
199         }
200
201         set_default_response (Gtk::RESPONSE_ACCEPT);
202
203         VBox* progress_box = manage (new VBox);
204         progress_box->set_spacing (6);
205
206         l = manage (new Label (_("<b>Progress</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
207         l->set_use_markup ();
208
209         progress_box->pack_start (*l, false, false);
210         progress_box->pack_start (progress_bar, false, true);
211
212
213         vbox->pack_start (upper_button_box, false, true);
214         vbox->pack_start (*progress_box, false, true);
215
216         get_vbox()->pack_start (*vbox, false, false);
217
218         show_all_children ();
219 }
220
221 void
222 TimeFXDialog::start_updates ()
223 {
224         update_connection = Timers::rapid_connect (sigc::mem_fun (*this, &TimeFXDialog::timer_update));
225 }
226
227 void
228 TimeFXDialog::update_progress_gui (float p)
229 {
230         /* time/pitch FX are applied in a dedicated thread, so we cannot just
231            update the GUI when notified about progress. That is deferred to a
232            timer-driven callback which will ensure that the visual progress
233            indicator is updated.
234         */
235         progress = p;
236 }
237
238 void
239 TimeFXDialog::timer_update ()
240 {
241         progress_bar.set_fraction (progress);
242
243         if (request.done || request.cancel) {
244                 update_connection.disconnect ();
245         }
246 }
247
248 void
249 TimeFXDialog::cancel_in_progress ()
250 {
251         request.cancel = true;
252         first_cancel.disconnect();
253 }
254
255 gint
256 TimeFXDialog::delete_in_progress (GdkEventAny*)
257 {
258         request.cancel = true;
259         first_delete.disconnect();
260         return TRUE;
261 }
262
263 float
264 TimeFXDialog::get_time_fraction () const
265 {
266         if (pitching) {
267                 return 1.0;
268         }
269
270         return duration_adjustment.get_value() / 100.0;
271 }
272
273 float
274 TimeFXDialog::get_pitch_fraction () const
275 {
276         if (!pitching) {
277                 return 1.0;
278         }
279
280         float cents = pitch_octave_adjustment.get_value() * 1200.0;
281
282         cents += pitch_semitone_adjustment.get_value() * 100.0;
283         cents += pitch_cent_adjustment.get_value();
284
285         if (cents == 0.0) {
286                 return 1.0;
287         }
288
289         // one octave == 1200 cents
290         // adding one octave doubles the frequency
291         // ratio is 2^^octaves
292
293         return pow(2, cents/1200);
294 }
295
296 void
297 TimeFXDialog::duration_adjustment_changed ()
298 {
299         if (ignore_adjustment_change) {
300                 return;
301         }
302
303         PBD::Unwinder<bool> uw (ignore_clock_change, true);
304
305         duration_clock->set ((samplecnt_t) (original_length * (duration_adjustment.get_value()/ 100.0)));
306 }
307
308 void
309 TimeFXDialog::duration_clock_changed ()
310 {
311         if (ignore_clock_change) {
312                 return;
313         }
314
315         PBD::Unwinder<bool> uw (ignore_adjustment_change, true);
316
317         duration_adjustment.set_value (100.0 * (duration_clock->current_duration() / (double) original_length));
318 }