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