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"
31 #include <gtkmm2ext/utils.h>
33 #include "audio_clock.h"
35 #include "audio_time_axis.h"
36 #include "audio_region_view.h"
37 #include "region_selection.h"
38 #include "time_fx_dialog.h"
41 #include <rubberband/RubberBandStretcher.h>
42 using namespace RubberBand;
48 using namespace ARDOUR;
51 using namespace Gtkmm2ext;
53 TimeFXDialog::TimeFXDialog (Editor& e, bool pitch, framecnt_t oldlen, framecnt_t new_length, framepos_t position)
54 : ArdourDialog (X_("time fx dialog"))
57 , quick_button (_("Quick but Ugly"))
58 , antialias_button (_("Skip Anti-aliasing"))
59 , stretch_opts_label (_("Contents:"))
60 , precise_button (_("Minimize time distortion"))
61 , preserve_formants_button(_("Preserve Formants"))
62 , original_length (oldlen)
63 , pitch_octave_adjustment (0.0, -4.0, 4.0, 1, 2.0)
64 , pitch_semitone_adjustment (0.0, -12.0, 12.0, 1.0, 4.0)
65 , pitch_cent_adjustment (0.0, -499.0, 500.0, 5.0, 15.0)
66 , pitch_octave_spinner (pitch_octave_adjustment)
67 , pitch_semitone_spinner (pitch_semitone_adjustment)
68 , pitch_cent_spinner (pitch_cent_adjustment)
69 , percent_adjustment (100.0, -1000.0, 1000.0, 1.0, 10.0)
71 , duration_chosen (_("Duration"))
72 , choice_group (duration_chosen.get_group())
73 , percent_chosen (choice_group, _("Percent"))
76 set_skip_taskbar_hint (true);
77 set_resizable (false);
78 set_name (N_("TimeFXDialog"));
81 set_title (_("Pitch Shift Audio"));
83 set_title (_("Time Stretch Audio"));
86 cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
88 VBox* vbox = manage (new VBox);
91 get_vbox()->set_spacing (4);
93 vbox->set_spacing (18);
94 vbox->set_border_width (5);
96 upper_button_box.set_spacing (6);
98 l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false ));
101 upper_button_box.pack_start (*l, false, false);
104 Table* table = manage (new Table (4, 3, false));
105 table->set_row_spacings (6);
106 table->set_col_spacing (1, 6);
107 l = manage (new Label ("", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false )); //Common gnome way for padding
108 l->set_padding (8, 0);
109 table->attach (*l, 0, 1, 0, 4, Gtk::FILL, Gtk::FILL, 0, 0);
111 l = manage (new Label (_("Octaves:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
112 table->attach (*l, 1, 2, 0, 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
113 table->attach (pitch_octave_spinner, 2, 3, 0, 1, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
114 pitch_octave_spinner.set_activates_default ();
116 l = manage (new Label (_("Semitones:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
117 table->attach (*l, 1, 2, 1, 2, Gtk::FILL, Gtk::EXPAND, 0, 0);
118 table->attach (pitch_semitone_spinner, 2, 3, 1, 2, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
119 pitch_semitone_spinner.set_activates_default ();
121 l = manage (new Label (_("Cents:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
122 pitch_cent_spinner.set_digits (1);
123 table->attach (*l, 1, 2, 2, 3, Gtk::FILL, Gtk::EXPAND, 0, 0);
124 table->attach (pitch_cent_spinner, 2, 3, 2, 3, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
125 pitch_cent_spinner.set_activates_default ();
127 table->attach (preserve_formants_button, 1, 3, 3, 4, Gtk::FILL, Gtk::EXPAND, 0, 0);
129 add_button (S_("Time|Shift"), Gtk::RESPONSE_ACCEPT);
131 upper_button_box.pack_start (*table, false, true);
133 Table* table = manage (new Table (4, 2, false));
136 table->set_row_spacings (6);
137 table->set_col_spacings (6);
139 #ifdef USE_RUBBERBAND
140 vector<string> strings;
141 duration_clock = manage (new AudioClock (X_("stretch"), true, "stretch", true, false, true, false, true));
142 duration_clock->set_session (e.session());
143 duration_clock->set (new_length, true);
144 duration_clock->set_mode (AudioClock::BBT);
145 duration_clock->set_bbt_reference (position);
147 Gtk::Alignment* clock_align = manage (new Gtk::Alignment);
148 clock_align->add (*duration_clock);
149 clock_align->set (0.0, 0.5, 0.0, 1.0);
151 Gtk::RadioButtonGroup group;
152 table->attach (duration_chosen, 0, 1, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
153 table->attach (*clock_align, 1, 2, row, row+1, Gtk::AttachOptions (Gtk::EXPAND|Gtk::FILL), Gtk::FILL, 0, 0);
156 const double fract = ((double) new_length) / original_length;
157 /* note the *100.0 to convert fract into a percentage */
158 percent_adjustment.set_value (fract*100.0);
159 Gtk::SpinButton* spinner = manage (new Gtk::SpinButton (percent_adjustment, 1.0, 3));
161 table->attach (percent_chosen, 0, 1, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
162 table->attach (*spinner, 1, 2, row, row+1, Gtk::FILL, Gtk::FILL, 0, 0);
165 table->attach (stretch_opts_label, 0, 1, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
167 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
169 stretch_opts_selector.set_active_text (editor.rb_opt_strings[editor.rb_current_opt]);
170 table->attach (stretch_opts_selector, 1, 2, row, row+1, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
173 table->attach (precise_button, 0, 2, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
176 quick_button.set_name (N_("TimeFXButton"));
177 table->attach (quick_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
180 antialias_button.set_name (N_("TimeFXButton"));
181 table->attach (antialias_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
185 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
187 upper_button_box.pack_start (*table, false, true);
190 set_default_response (Gtk::RESPONSE_ACCEPT);
192 VBox* progress_box = manage (new VBox);
193 progress_box->set_spacing (6);
195 l = manage (new Label (_("<b>Progress</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
196 l->set_use_markup ();
198 progress_box->pack_start (*l, false, false);
199 progress_box->pack_start (progress_bar, false, true);
202 vbox->pack_start (upper_button_box, false, true);
203 vbox->pack_start (*progress_box, false, true);
205 get_vbox()->pack_start (*vbox, false, false);
207 show_all_children ();
211 TimeFXDialog::update_progress_gui (float p)
213 progress_bar.set_fraction (p);
217 TimeFXDialog::cancel_in_progress ()
220 request.cancel = true;
221 first_cancel.disconnect();
225 TimeFXDialog::delete_in_progress (GdkEventAny*)
228 request.cancel = true;
229 first_delete.disconnect();
234 TimeFXDialog::get_time_fraction () const
240 if (duration_chosen.get_active()) {
241 return duration_clock->current_duration () / original_length;
244 return percent_adjustment.get_value() / 100.0;
248 TimeFXDialog::get_pitch_fraction () const
254 float cents = pitch_octave_adjustment.get_value() * 1200.0;
256 cents += pitch_semitone_adjustment.get_value() * 100.0;
257 cents += pitch_cent_adjustment.get_value();
263 // one octave == 1200 cents
264 // adding one octave doubles the frequency
265 // ratio is 2^^octaves
267 return pow(2, cents/1200);