enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 "pbd/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         /* time/pitch FX are applied in a dedicated thread, so we cannot just
228            update the GUI when notified about progress. That is deferred to a
229            timer-driven callback which will ensure that the visual progress
230            indicator is updated.
231         */
232         progress = p;
233 }
234
235 void
236 TimeFXDialog::timer_update ()
237 {
238         progress_bar.set_fraction (progress);
239
240         if (request.done || request.cancel) {
241                 update_connection.disconnect ();
242         }
243 }
244
245 void
246 TimeFXDialog::cancel_in_progress ()
247 {
248         status = -2;
249         request.cancel = true;
250         first_cancel.disconnect();
251 }
252
253 gint
254 TimeFXDialog::delete_in_progress (GdkEventAny*)
255 {
256         status = -2;
257         request.cancel = true;
258         first_delete.disconnect();
259         return TRUE;
260 }
261
262 float
263 TimeFXDialog::get_time_fraction () const
264 {
265         if (pitching) {
266                 return 1.0;
267         }
268
269         return duration_adjustment.get_value() / 100.0;
270 }
271
272 float
273 TimeFXDialog::get_pitch_fraction () const
274 {
275         if (!pitching) {
276                 return 1.0;
277         }
278
279         float cents = pitch_octave_adjustment.get_value() * 1200.0;
280
281         cents += pitch_semitone_adjustment.get_value() * 100.0;
282         cents += pitch_cent_adjustment.get_value();
283
284         if (cents == 0.0) {
285                 return 1.0;
286         }
287
288         // one octave == 1200 cents
289         // adding one octave doubles the frequency
290         // ratio is 2^^octaves
291
292         return pow(2, cents/1200);
293 }
294
295 void
296 TimeFXDialog::duration_adjustment_changed ()
297 {
298         if (ignore_adjustment_change) {
299                 return;
300         }
301
302         PBD::Unwinder<bool> uw (ignore_clock_change, true);
303
304         duration_clock->set ((framecnt_t) (original_length * (duration_adjustment.get_value()/ 100.0)));
305 }
306
307 void
308 TimeFXDialog::duration_clock_changed ()
309 {
310         if (ignore_clock_change) {
311                 return;
312         }
313
314         PBD::Unwinder<bool> uw (ignore_adjustment_change, true);
315
316         duration_adjustment.set_value (100.0 * (duration_clock->current_duration() / (double) original_length));
317 }