more work on time/pitch stretch/shifting
[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 #include "pbd/unwind.h"
31 #include "pbd/stacktrace.h"
32
33 #include <gtkmm2ext/utils.h>
34
35 #include "audio_clock.h"
36 #include "editor.h"
37 #include "audio_time_axis.h"
38 #include "audio_region_view.h"
39 #include "region_selection.h"
40 #include "time_fx_dialog.h"
41 #include "timers.h"
42
43 #ifdef USE_RUBBERBAND
44 #include <rubberband/RubberBandStretcher.h>
45 using namespace RubberBand;
46 #endif
47
48 #include "i18n.h"
49
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace PBD;
53 using namespace Gtk;
54 using namespace Gtkmm2ext;
55
56 TimeFXDialog::TimeFXDialog (Editor& e, bool pitch, framecnt_t oldlen, framecnt_t new_length, framepos_t position)
57         : ArdourDialog (X_("time fx dialog"))
58         , editor (e)
59         , pitching (pitch)
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)
73         , duration_clock (0)
74         , ignore_adjustment_change (false)
75         , ignore_clock_change (false)
76         , progress (0.0f)
77 {
78         set_modal (true);
79         set_skip_taskbar_hint (true);
80         set_resizable (false);
81         set_name (N_("TimeFXDialog"));
82
83         if (pitching) {
84                 set_title (_("Pitch Shift Audio"));
85         } else {
86                 set_title (_("Time Stretch Audio"));
87         }
88
89         cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
90
91         VBox* vbox = manage (new VBox);
92         Gtk::Label* l;
93
94         get_vbox()->set_spacing (4);
95
96         vbox->set_spacing (18);
97         vbox->set_border_width (5);
98
99         upper_button_box.set_spacing (6);
100
101         l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false ));
102         l->set_use_markup ();
103
104         upper_button_box.pack_start (*l, false, false);
105
106         if (pitching) {
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);
113
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 ();
118
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 ();
123
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 ();
129
130                 table->attach (preserve_formants_button, 1, 3, 3, 4, Gtk::FILL, Gtk::EXPAND, 0, 0);
131
132                 add_button (S_("Time|Shift"), Gtk::RESPONSE_ACCEPT);
133
134                 upper_button_box.pack_start (*table, false, true);
135         } else {
136                 Table* table = manage (new Table (4, 2, false));
137                 int row = 0;
138
139                 table->set_row_spacings (6);
140                 table->set_col_spacings (12);
141
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);
149
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);
153
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);
157                 row++;
158
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));
163
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);
167                 row++;
168
169                 table->attach (stretch_opts_label, 0, 1, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
170
171                 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
172                 /* set default */
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);
175                 row++;
176
177                 table->attach (precise_button, 0, 2, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
178                 row++;
179
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));
182
183 #else
184                 quick_button.set_name (N_("TimeFXButton"));
185                 table->attach (quick_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
186                 row++;
187
188                 antialias_button.set_name (N_("TimeFXButton"));
189                 table->attach (antialias_button, 1, 3, row, row+1, Gtk::FILL, Gtk::EXPAND, 0, 0);
190
191 #endif
192
193                 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
194
195                 upper_button_box.pack_start (*table, false, true);
196         }
197
198         set_default_response (Gtk::RESPONSE_ACCEPT);
199
200         VBox* progress_box = manage (new VBox);
201         progress_box->set_spacing (6);
202
203         l = manage (new Label (_("<b>Progress</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
204         l->set_use_markup ();
205
206         progress_box->pack_start (*l, false, false);
207         progress_box->pack_start (progress_bar, false, true);
208
209
210         vbox->pack_start (upper_button_box, false, true);
211         vbox->pack_start (*progress_box, false, true);
212
213         get_vbox()->pack_start (*vbox, false, false);
214
215         show_all_children ();
216 }
217
218 void
219 TimeFXDialog::start_updates ()
220 {
221         update_connection = Timers::rapid_connect (sigc::mem_fun (*this, &TimeFXDialog::timer_update));
222 }
223
224 void
225 TimeFXDialog::update_progress_gui (float p)
226 {
227         progress = p;
228 }
229
230 void
231 TimeFXDialog::timer_update ()
232 {
233         progress_bar.set_fraction (progress);
234
235         if (request.done || request.cancel) {
236                 update_connection.disconnect ();
237         }
238 }
239
240 void
241 TimeFXDialog::cancel_in_progress ()
242 {
243         status = -2;
244         request.cancel = true;
245         first_cancel.disconnect();
246 }
247
248 gint
249 TimeFXDialog::delete_in_progress (GdkEventAny*)
250 {
251         status = -2;
252         request.cancel = true;
253         first_delete.disconnect();
254         return TRUE;
255 }
256
257 float
258 TimeFXDialog::get_time_fraction () const
259 {
260         if (pitching) {
261                 return 1.0;
262         }
263
264         return duration_adjustment.get_value() / 100.0;
265 }
266
267 float
268 TimeFXDialog::get_pitch_fraction () const
269 {
270         if (!pitching) {
271                 return 1.0;
272         }
273
274         float cents = pitch_octave_adjustment.get_value() * 1200.0;
275
276         cents += pitch_semitone_adjustment.get_value() * 100.0;
277         cents += pitch_cent_adjustment.get_value();
278
279         if (cents == 0.0) {
280                 return 1.0;
281         }
282
283         // one octave == 1200 cents
284         // adding one octave doubles the frequency
285         // ratio is 2^^octaves
286
287         return pow(2, cents/1200);
288 }
289
290 void
291 TimeFXDialog::duration_adjustment_changed ()
292 {
293         if (ignore_adjustment_change) {
294                 return;
295         }
296
297         PBD::Unwinder<bool> uw (ignore_clock_change, true);
298
299         duration_clock->set ((framecnt_t) (original_length * (duration_adjustment.get_value()/ 100.0)));
300 }
301
302 void
303 TimeFXDialog::duration_clock_changed ()
304 {
305         if (ignore_clock_change) {
306                 return;
307         }
308
309         PBD::Unwinder<bool> uw (ignore_adjustment_change, true);
310
311         duration_adjustment.set_value (100.0 * (duration_clock->current_duration() / (double) original_length));
312 }