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>
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.
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.
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.
28 #include <gtkmm/stock.h>
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"
36 #include "gtkmm2ext/utils.h"
38 #include "audio_clock.h"
40 #include "audio_time_axis.h"
41 #include "audio_region_view.h"
42 #include "region_selection.h"
43 #include "time_fx_dialog.h"
47 #include <rubberband/RubberBandStretcher.h>
48 using namespace RubberBand;
54 using namespace ARDOUR;
57 using namespace Gtkmm2ext;
59 TimeFXDialog::TimeFXDialog (Editor& e, bool pitch, samplecnt_t oldlen, samplecnt_t new_length, samplepos_t position)
60 : ArdourDialog (X_("time fx dialog"))
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)
77 , ignore_adjustment_change (false)
78 , ignore_clock_change (false)
82 set_skip_taskbar_hint (true);
83 set_resizable (false);
84 set_name (N_("TimeFXDialog"));
87 set_title (_("Pitch Shift Audio"));
89 set_title (_("Time Stretch Audio"));
92 cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
94 VBox* vbox = manage (new VBox);
97 get_vbox()->set_spacing (4);
99 vbox->set_spacing (18);
100 vbox->set_border_width (5);
102 upper_button_box.set_spacing (6);
104 l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false ));
105 l->set_use_markup ();
107 upper_button_box.pack_start (*l, false, false);
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);
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 ();
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 ();
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 ();
133 table->attach (preserve_formants_button, 1, 3, 3, 4, Gtk::FILL, Gtk::EXPAND, 0, 0);
135 add_button (S_("Time|Shift"), Gtk::RESPONSE_ACCEPT);
137 upper_button_box.pack_start (*table, false, true);
139 Table* table = manage (new Table (4, 2, false));
142 table->set_row_spacings (6);
143 table->set_col_spacings (12);
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);
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);
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);
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));
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);
172 table->attach (stretch_opts_label, 0, 1, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
174 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
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);
180 table->attach (precise_button, 0, 2, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
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));
187 quick_button.set_name (N_("TimeFXButton"));
188 table->attach (quick_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
191 antialias_button.set_name (N_("TimeFXButton"));
192 table->attach (antialias_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
196 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
198 upper_button_box.pack_start (*table, false, true);
201 set_default_response (Gtk::RESPONSE_ACCEPT);
203 VBox* progress_box = manage (new VBox);
204 progress_box->set_spacing (6);
206 l = manage (new Label (_("<b>Progress</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
207 l->set_use_markup ();
209 progress_box->pack_start (*l, false, false);
210 progress_box->pack_start (progress_bar, false, true);
213 vbox->pack_start (upper_button_box, false, true);
214 vbox->pack_start (*progress_box, false, true);
216 get_vbox()->pack_start (*vbox, false, false);
218 show_all_children ();
222 TimeFXDialog::start_updates ()
224 update_connection = Timers::rapid_connect (sigc::mem_fun (*this, &TimeFXDialog::timer_update));
228 TimeFXDialog::update_progress_gui (float p)
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.
239 TimeFXDialog::timer_update ()
241 progress_bar.set_fraction (progress);
243 if (request.done || request.cancel) {
244 update_connection.disconnect ();
249 TimeFXDialog::cancel_in_progress ()
251 request.cancel = true;
252 first_cancel.disconnect();
256 TimeFXDialog::delete_in_progress (GdkEventAny*)
258 request.cancel = true;
259 first_delete.disconnect();
264 TimeFXDialog::get_time_fraction () const
270 return duration_adjustment.get_value() / 100.0;
274 TimeFXDialog::get_pitch_fraction () const
280 float cents = pitch_octave_adjustment.get_value() * 1200.0;
282 cents += pitch_semitone_adjustment.get_value() * 100.0;
283 cents += pitch_cent_adjustment.get_value();
289 // one octave == 1200 cents
290 // adding one octave doubles the frequency
291 // ratio is 2^^octaves
293 return pow(2, cents/1200);
297 TimeFXDialog::duration_adjustment_changed ()
299 if (ignore_adjustment_change) {
303 PBD::Unwinder<bool> uw (ignore_clock_change, true);
305 duration_clock->set ((samplecnt_t) (original_length * (duration_adjustment.get_value()/ 100.0)));
309 TimeFXDialog::duration_clock_changed ()
311 if (ignore_clock_change) {
315 PBD::Unwinder<bool> uw (ignore_adjustment_change, true);
317 duration_adjustment.set_value (100.0 * (duration_clock->current_duration() / (double) original_length));