2 Copyright (C) 2000-2009 Paul Davis
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.
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.
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.
27 #include "pbd/error.h"
28 #include "pbd/pthread_utils.h"
29 #include "pbd/memento_command.h"
30 #include "pbd/unwind.h"
31 #include "pbd/stacktrace.h"
33 #include <gtkmm2ext/utils.h>
35 #include "audio_clock.h"
37 #include "audio_time_axis.h"
38 #include "audio_region_view.h"
39 #include "region_selection.h"
40 #include "time_fx_dialog.h"
44 #include <rubberband/RubberBandStretcher.h>
45 using namespace RubberBand;
51 using namespace ARDOUR;
54 using namespace Gtkmm2ext;
56 TimeFXDialog::TimeFXDialog (Editor& e, bool pitch, framecnt_t oldlen, framecnt_t new_length, framepos_t position)
57 : ArdourDialog (X_("time fx dialog"))
60 , quick_button (_("Quick but Ugly"))
61 , antialias_button (_("Skip Anti-aliasing"))
62 , stretch_opts_label (_("Contents"))
63 , precise_button (_("Minimize time distortion"))
64 , preserve_formants_button(_("Preserve Formants"))
65 , original_length (oldlen)
66 , pitch_octave_adjustment (0.0, -4.0, 4.0, 1, 2.0)
67 , pitch_semitone_adjustment (0.0, -12.0, 12.0, 1.0, 4.0)
68 , pitch_cent_adjustment (0.0, -499.0, 500.0, 5.0, 15.0)
69 , pitch_octave_spinner (pitch_octave_adjustment)
70 , pitch_semitone_spinner (pitch_semitone_adjustment)
71 , pitch_cent_spinner (pitch_cent_adjustment)
72 , duration_adjustment (100.0, -1000.0, 1000.0, 1.0, 10.0)
74 , ignore_adjustment_change (false)
75 , ignore_clock_change (false)
79 set_skip_taskbar_hint (true);
80 set_resizable (false);
81 set_name (N_("TimeFXDialog"));
84 set_title (_("Pitch Shift Audio"));
86 set_title (_("Time Stretch Audio"));
89 cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
91 VBox* vbox = manage (new VBox);
94 get_vbox()->set_spacing (4);
96 vbox->set_spacing (18);
97 vbox->set_border_width (5);
99 upper_button_box.set_spacing (6);
101 l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false ));
102 l->set_use_markup ();
104 upper_button_box.pack_start (*l, false, false);
107 Table* table = manage (new Table (4, 3, false));
108 table->set_row_spacings (6);
109 table->set_col_spacing (1, 6);
110 l = manage (new Label ("", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false )); //Common gnome way for padding
111 l->set_padding (8, 0);
112 table->attach (*l, 0, 1, 0, 4, Gtk::FILL, Gtk::FILL, 0, 0);
114 l = manage (new Label (_("Octaves:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
115 table->attach (*l, 1, 2, 0, 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
116 table->attach (pitch_octave_spinner, 2, 3, 0, 1, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
117 pitch_octave_spinner.set_activates_default ();
119 l = manage (new Label (_("Semitones:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
120 table->attach (*l, 1, 2, 1, 2, Gtk::FILL, Gtk::EXPAND, 0, 0);
121 table->attach (pitch_semitone_spinner, 2, 3, 1, 2, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
122 pitch_semitone_spinner.set_activates_default ();
124 l = manage (new Label (_("Cents:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
125 pitch_cent_spinner.set_digits (1);
126 table->attach (*l, 1, 2, 2, 3, Gtk::FILL, Gtk::EXPAND, 0, 0);
127 table->attach (pitch_cent_spinner, 2, 3, 2, 3, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
128 pitch_cent_spinner.set_activates_default ();
130 table->attach (preserve_formants_button, 1, 3, 3, 4, Gtk::FILL, Gtk::EXPAND, 0, 0);
132 add_button (S_("Time|Shift"), Gtk::RESPONSE_ACCEPT);
134 upper_button_box.pack_start (*table, false, true);
136 Table* table = manage (new Table (4, 2, false));
139 table->set_row_spacings (6);
140 table->set_col_spacings (12);
142 #ifdef USE_RUBBERBAND
143 vector<string> strings;
144 duration_clock = manage (new AudioClock (X_("stretch"), true, X_("stretch"), true, false, true, false, true));
145 duration_clock->set_session (e.session());
146 duration_clock->set (new_length, true);
147 duration_clock->set_mode (AudioClock::BBT);
148 duration_clock->set_bbt_reference (position);
150 Gtk::Alignment* clock_align = manage (new Gtk::Alignment);
151 clock_align->add (*duration_clock);
152 clock_align->set (0.0, 0.5, 0.0, 1.0);
154 l = manage (new Gtk::Label (_("Duration")));
155 table->attach (*l, 0, 1, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
156 table->attach (*clock_align, 1, 2, row, row+1, Gtk::AttachOptions (Gtk::EXPAND|Gtk::FILL), Gtk::FILL, 0, 0);
159 const double fract = ((double) new_length) / original_length;
160 /* note the *100.0 to convert fract into a percentage */
161 duration_adjustment.set_value (fract*100.0);
162 Gtk::SpinButton* spinner = manage (new Gtk::SpinButton (duration_adjustment, 1.0, 3));
164 l = manage (new Gtk::Label (_("Percent")));
165 table->attach (*l, 0, 1, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
166 table->attach (*spinner, 1, 2, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
169 table->attach (stretch_opts_label, 0, 1, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
171 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
173 stretch_opts_selector.set_active_text (editor.rb_opt_strings[editor.rb_current_opt]);
174 table->attach (stretch_opts_selector, 1, 2, row, row+1, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
177 table->attach (precise_button, 0, 2, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
180 duration_clock->ValueChanged.connect (sigc::mem_fun (*this, &TimeFXDialog::duration_clock_changed));
181 duration_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &TimeFXDialog::duration_adjustment_changed));
184 quick_button.set_name (N_("TimeFXButton"));
185 table->attach (quick_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
188 antialias_button.set_name (N_("TimeFXButton"));
189 table->attach (antialias_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
193 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
195 upper_button_box.pack_start (*table, false, true);
198 set_default_response (Gtk::RESPONSE_ACCEPT);
200 VBox* progress_box = manage (new VBox);
201 progress_box->set_spacing (6);
203 l = manage (new Label (_("<b>Progress</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
204 l->set_use_markup ();
206 progress_box->pack_start (*l, false, false);
207 progress_box->pack_start (progress_bar, false, true);
210 vbox->pack_start (upper_button_box, false, true);
211 vbox->pack_start (*progress_box, false, true);
213 get_vbox()->pack_start (*vbox, false, false);
215 show_all_children ();
219 TimeFXDialog::start_updates ()
221 update_connection = Timers::rapid_connect (sigc::mem_fun (*this, &TimeFXDialog::timer_update));
225 TimeFXDialog::update_progress_gui (float p)
227 /* time/pitch FX are applied in a dedicated thread, so we cannot just
228 update the GUI when notified about progress. That is deferred to a
229 timer-driven callback which will ensure that the visual progress
230 indicator is updated.
236 TimeFXDialog::timer_update ()
238 progress_bar.set_fraction (progress);
240 if (request.done || request.cancel) {
241 update_connection.disconnect ();
246 TimeFXDialog::cancel_in_progress ()
249 request.cancel = true;
250 first_cancel.disconnect();
254 TimeFXDialog::delete_in_progress (GdkEventAny*)
257 request.cancel = true;
258 first_delete.disconnect();
263 TimeFXDialog::get_time_fraction () const
269 return duration_adjustment.get_value() / 100.0;
273 TimeFXDialog::get_pitch_fraction () const
279 float cents = pitch_octave_adjustment.get_value() * 1200.0;
281 cents += pitch_semitone_adjustment.get_value() * 100.0;
282 cents += pitch_cent_adjustment.get_value();
288 // one octave == 1200 cents
289 // adding one octave doubles the frequency
290 // ratio is 2^^octaves
292 return pow(2, cents/1200);
296 TimeFXDialog::duration_adjustment_changed ()
298 if (ignore_adjustment_change) {
302 PBD::Unwinder<bool> uw (ignore_clock_change, true);
304 duration_clock->set ((framecnt_t) (original_length * (duration_adjustment.get_value()/ 100.0)));
308 TimeFXDialog::duration_clock_changed ()
310 if (ignore_clock_change) {
314 PBD::Unwinder<bool> uw (ignore_adjustment_change, true);
316 duration_adjustment.set_value (100.0 * (duration_clock->current_duration() / (double) original_length));