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.
26 #include <gtkmm/stock.h>
28 #include "pbd/error.h"
29 #include "pbd/pthread_utils.h"
30 #include "pbd/memento_command.h"
31 #include "pbd/unwind.h"
32 #include "pbd/stacktrace.h"
34 #include "gtkmm2ext/utils.h"
36 #include "audio_clock.h"
38 #include "audio_time_axis.h"
39 #include "audio_region_view.h"
40 #include "region_selection.h"
41 #include "time_fx_dialog.h"
45 #include <rubberband/RubberBandStretcher.h>
46 using namespace RubberBand;
52 using namespace ARDOUR;
55 using namespace Gtkmm2ext;
57 TimeFXDialog::TimeFXDialog (Editor& e, bool pitch, samplecnt_t oldlen, samplecnt_t new_length, samplepos_t position)
58 : ArdourDialog (X_("time fx dialog"))
61 , quick_button (_("Quick but Ugly"))
62 , antialias_button (_("Skip Anti-aliasing"))
63 , stretch_opts_label (_("Contents"))
64 , precise_button (_("Minimize time distortion"))
65 , preserve_formants_button(_("Preserve Formants"))
66 , original_length (oldlen)
67 , pitch_octave_adjustment (0.0, -4.0, 4.0, 1, 2.0)
68 , pitch_semitone_adjustment (0.0, -12.0, 12.0, 1.0, 4.0)
69 , pitch_cent_adjustment (0.0, -499.0, 500.0, 5.0, 15.0)
70 , pitch_octave_spinner (pitch_octave_adjustment)
71 , pitch_semitone_spinner (pitch_semitone_adjustment)
72 , pitch_cent_spinner (pitch_cent_adjustment)
73 , duration_adjustment (100.0, -1000.0, 1000.0, 1.0, 10.0)
75 , ignore_adjustment_change (false)
76 , ignore_clock_change (false)
80 set_skip_taskbar_hint (true);
81 set_resizable (false);
82 set_name (N_("TimeFXDialog"));
85 set_title (_("Pitch Shift Audio"));
87 set_title (_("Time Stretch Audio"));
90 cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
92 VBox* vbox = manage (new VBox);
95 get_vbox()->set_spacing (4);
97 vbox->set_spacing (18);
98 vbox->set_border_width (5);
100 upper_button_box.set_spacing (6);
102 l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false ));
103 l->set_use_markup ();
105 upper_button_box.pack_start (*l, false, false);
108 Table* table = manage (new Table (4, 3, false));
109 table->set_row_spacings (6);
110 table->set_col_spacing (1, 6);
111 l = manage (new Label ("", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false )); //Common gnome way for padding
112 l->set_padding (8, 0);
113 table->attach (*l, 0, 1, 0, 4, Gtk::FILL, Gtk::FILL, 0, 0);
115 l = manage (new Label (_("Octaves:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
116 table->attach (*l, 1, 2, 0, 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
117 table->attach (pitch_octave_spinner, 2, 3, 0, 1, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
118 pitch_octave_spinner.set_activates_default ();
120 l = manage (new Label (_("Semitones:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
121 table->attach (*l, 1, 2, 1, 2, Gtk::FILL, Gtk::EXPAND, 0, 0);
122 table->attach (pitch_semitone_spinner, 2, 3, 1, 2, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
123 pitch_semitone_spinner.set_activates_default ();
125 l = manage (new Label (_("Cents:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
126 pitch_cent_spinner.set_digits (1);
127 table->attach (*l, 1, 2, 2, 3, Gtk::FILL, Gtk::EXPAND, 0, 0);
128 table->attach (pitch_cent_spinner, 2, 3, 2, 3, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
129 pitch_cent_spinner.set_activates_default ();
131 table->attach (preserve_formants_button, 1, 3, 3, 4, Gtk::FILL, Gtk::EXPAND, 0, 0);
133 add_button (S_("Time|Shift"), Gtk::RESPONSE_ACCEPT);
135 upper_button_box.pack_start (*table, false, true);
137 Table* table = manage (new Table (4, 2, false));
140 table->set_row_spacings (6);
141 table->set_col_spacings (12);
143 #ifdef USE_RUBBERBAND
144 vector<string> strings;
145 duration_clock = manage (new AudioClock (X_("stretch"), true, X_("stretch"), true, false, true, false, true));
146 duration_clock->set_session (e.session());
147 duration_clock->set (new_length, true);
148 duration_clock->set_mode (AudioClock::BBT);
149 duration_clock->set_bbt_reference (position);
151 Gtk::Alignment* clock_align = manage (new Gtk::Alignment);
152 clock_align->add (*duration_clock);
153 clock_align->set (0.0, 0.5, 0.0, 1.0);
155 l = manage (new Gtk::Label (_("Duration")));
156 table->attach (*l, 0, 1, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
157 table->attach (*clock_align, 1, 2, row, row+1, Gtk::AttachOptions (Gtk::EXPAND|Gtk::FILL), Gtk::FILL, 0, 0);
160 const double fract = ((double) new_length) / original_length;
161 /* note the *100.0 to convert fract into a percentage */
162 duration_adjustment.set_value (fract*100.0);
163 Gtk::SpinButton* spinner = manage (new Gtk::SpinButton (duration_adjustment, 1.0, 3));
165 l = manage (new Gtk::Label (_("Percent")));
166 table->attach (*l, 0, 1, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
167 table->attach (*spinner, 1, 2, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
170 table->attach (stretch_opts_label, 0, 1, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
172 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
174 stretch_opts_selector.set_active_text (editor.rb_opt_strings[editor.rb_current_opt]);
175 table->attach (stretch_opts_selector, 1, 2, row, row+1, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
178 table->attach (precise_button, 0, 2, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
181 duration_clock->ValueChanged.connect (sigc::mem_fun (*this, &TimeFXDialog::duration_clock_changed));
182 duration_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &TimeFXDialog::duration_adjustment_changed));
185 quick_button.set_name (N_("TimeFXButton"));
186 table->attach (quick_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
189 antialias_button.set_name (N_("TimeFXButton"));
190 table->attach (antialias_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
194 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
196 upper_button_box.pack_start (*table, false, true);
199 set_default_response (Gtk::RESPONSE_ACCEPT);
201 VBox* progress_box = manage (new VBox);
202 progress_box->set_spacing (6);
204 l = manage (new Label (_("<b>Progress</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
205 l->set_use_markup ();
207 progress_box->pack_start (*l, false, false);
208 progress_box->pack_start (progress_bar, false, true);
211 vbox->pack_start (upper_button_box, false, true);
212 vbox->pack_start (*progress_box, false, true);
214 get_vbox()->pack_start (*vbox, false, false);
216 show_all_children ();
220 TimeFXDialog::start_updates ()
222 update_connection = Timers::rapid_connect (sigc::mem_fun (*this, &TimeFXDialog::timer_update));
226 TimeFXDialog::update_progress_gui (float p)
228 /* time/pitch FX are applied in a dedicated thread, so we cannot just
229 update the GUI when notified about progress. That is deferred to a
230 timer-driven callback which will ensure that the visual progress
231 indicator is updated.
237 TimeFXDialog::timer_update ()
239 progress_bar.set_fraction (progress);
241 if (request.done || request.cancel) {
242 update_connection.disconnect ();
247 TimeFXDialog::cancel_in_progress ()
250 request.cancel = true;
251 first_cancel.disconnect();
255 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));