first steps in providing more precise control over timestretching
[ardour.git] / gtk2_ardour / time_fx_dialog.cc
1 /*
2     Copyright (C) 2000-2009 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20
21 #include <iostream>
22 #include <cstdlib>
23 #include <cmath>
24
25 #include <string>
26
27 #include "pbd/error.h"
28 #include "pbd/pthread_utils.h"
29 #include "pbd/memento_command.h"
30
31 #include <gtkmm2ext/utils.h>
32
33 #include "audio_clock.h"
34 #include "editor.h"
35 #include "audio_time_axis.h"
36 #include "audio_region_view.h"
37 #include "region_selection.h"
38 #include "time_fx_dialog.h"
39
40 #ifdef USE_RUBBERBAND
41 #include <rubberband/RubberBandStretcher.h>
42 using namespace RubberBand;
43 #endif
44
45 #include "i18n.h"
46
47 using namespace std;
48 using namespace ARDOUR;
49 using namespace PBD;
50 using namespace Gtk;
51 using namespace Gtkmm2ext;
52
53 TimeFXDialog::TimeFXDialog (Editor& e, bool pitch, framecnt_t oldlen, framecnt_t new_length, framepos_t position)
54         : ArdourDialog (X_("time fx dialog"))
55         , editor (e)
56         , pitching (pitch)
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)
70         , duration_clock (0)
71         , duration_chosen (_("Duration"))
72         , choice_group (duration_chosen.get_group())
73         , percent_chosen (choice_group, _("Percent"))
74 {
75         set_modal (true);
76         set_skip_taskbar_hint (true);
77         set_resizable (false);
78         set_name (N_("TimeFXDialog"));
79
80         if (pitching) {
81                 set_title (_("Pitch Shift Audio"));
82         } else {
83                 set_title (_("Time Stretch Audio"));
84         }
85
86         cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
87
88         VBox* vbox = manage (new VBox);
89         Gtk::Label* l;
90
91         get_vbox()->set_spacing (4);
92
93         vbox->set_spacing (18);
94         vbox->set_border_width (5);
95
96         upper_button_box.set_spacing (6);
97
98         l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false ));
99         l->set_use_markup ();
100
101         upper_button_box.pack_start (*l, false, false);
102
103         if (pitching) {
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);
110
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 ();
115
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 ();
120
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 ();
126
127                 table->attach (preserve_formants_button, 1, 3, 3, 4, Gtk::FILL, Gtk::EXPAND, 0, 0);
128
129                 add_button (S_("Time|Shift"), Gtk::RESPONSE_ACCEPT);
130
131                 upper_button_box.pack_start (*table, false, true);
132         } else {
133                 Table* table = manage (new Table (4, 2, false));
134                 int row = 0;
135
136                 table->set_row_spacings (6);
137                 table->set_col_spacings (6);
138
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);
146
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);
150
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);
154                 row++;
155
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));
160
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);
163                 row++;
164
165                 table->attach (stretch_opts_label, 0, 1, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
166
167                 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
168                 /* set default */
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);
171                 row++;
172
173                 table->attach (precise_button, 0, 2, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
174                 row++;
175 #else
176                 quick_button.set_name (N_("TimeFXButton"));
177                 table->attach (quick_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
178                 row++;
179
180                 antialias_button.set_name (N_("TimeFXButton"));
181                 table->attach (antialias_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
182
183 #endif
184
185                 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
186
187                 upper_button_box.pack_start (*table, false, true);
188         }
189
190         set_default_response (Gtk::RESPONSE_ACCEPT);
191
192         VBox* progress_box = manage (new VBox);
193         progress_box->set_spacing (6);
194
195         l = manage (new Label (_("<b>Progress</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
196         l->set_use_markup ();
197
198         progress_box->pack_start (*l, false, false);
199         progress_box->pack_start (progress_bar, false, true);
200
201
202         vbox->pack_start (upper_button_box, false, true);
203         vbox->pack_start (*progress_box, false, true);
204
205         get_vbox()->pack_start (*vbox, false, false);
206
207         show_all_children ();
208 }
209
210 void
211 TimeFXDialog::update_progress_gui (float p)
212 {
213         progress_bar.set_fraction (p);
214 }
215
216 void
217 TimeFXDialog::cancel_in_progress ()
218 {
219         status = -2;
220         request.cancel = true;
221         first_cancel.disconnect();
222 }
223
224 gint
225 TimeFXDialog::delete_in_progress (GdkEventAny*)
226 {
227         status = -2;
228         request.cancel = true;
229         first_delete.disconnect();
230         return TRUE;
231 }
232
233 float
234 TimeFXDialog::get_time_fraction () const
235 {
236         if (pitching) {
237                 return 1.0;
238         }
239
240         if (duration_chosen.get_active()) {
241                 return duration_clock->current_duration () / original_length;
242         }
243
244         return percent_adjustment.get_value() / 100.0;
245 }
246
247 float
248 TimeFXDialog::get_pitch_fraction () const
249 {
250         if (!pitching) {
251                 return 1.0;
252         }
253
254         float cents = pitch_octave_adjustment.get_value() * 1200.0;
255
256         cents += pitch_semitone_adjustment.get_value() * 100.0;
257         cents += pitch_cent_adjustment.get_value();
258
259         if (cents == 0.0) {
260                 return 1.0;
261         }
262
263         // one octave == 1200 cents
264         // adding one octave doubles the frequency
265         // ratio is 2^^octaves
266
267         return pow(2, cents/1200);
268 }