most of the 2.X->3.0 commit (up to rev 4299) except for gtk2_ardour/editor_canvas...
[ardour.git] / gtk2_ardour / editor_timefx.cc
1 /*
2     Copyright (C) 2000 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 #include <iostream>
21 #include <cstdlib>
22 #include <cmath>
23
24 #include <string>
25
26 #include <pbd/error.h>
27 #include <pbd/pthread_utils.h>
28 #include <pbd/memento_command.h>
29
30 #include <gtkmm2ext/window_title.h>
31 #include <gtkmm2ext/utils.h>
32
33 #include "editor.h"
34 #include "audio_time_axis.h"
35 #include "audio_region_view.h"
36 #include "region_selection.h"
37
38 #include <ardour/session.h>
39 #include <ardour/region.h>
40 #include <ardour/audioplaylist.h>
41 #include <ardour/audio_track.h>
42 #include <ardour/audioregion.h>
43 #include <ardour/audio_diskstream.h>
44 #include <ardour/stretch.h>
45 #include <ardour/midi_stretch.h>
46 #include <ardour/pitch.h>
47
48 #ifdef USE_RUBBERBAND
49 #include <rubberband/RubberBandStretcher.h>
50 using namespace RubberBand;
51 #endif
52
53 #include "i18n.h"
54
55 using namespace std;
56 using namespace ARDOUR;
57 using namespace PBD;
58 using namespace sigc;
59 using namespace Gtk;
60 using namespace Gtkmm2ext;
61
62 Editor::TimeFXDialog::TimeFXDialog (Editor& e, bool pitch)
63         : ArdourDialog (X_("time fx dialog")),
64           editor (e),
65           pitching (pitch),
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           quick_button (_("Quick but Ugly")),
73           antialias_button (_("Skip Anti-aliasing")),
74           stretch_opts_label (_("Contents:")),
75           precise_button (_("Strict Linear")),
76           preserve_formants_button(_("Preserve Formants"))
77 {
78         set_modal (true);
79         set_position (Gtk::WIN_POS_MOUSE);
80         set_name (N_("TimeFXDialog"));
81
82         WindowTitle title(Glib::get_application_name());
83         if (pitching) {
84                 title += _("Pitch Shift");
85         } else {
86                 title += _("Time Stretch");
87         }
88         set_title(title.get_string());
89
90         cancel_button = add_button (_("Cancel"), Gtk::RESPONSE_CANCEL);
91
92         get_vbox()->set_spacing (5);
93         get_vbox()->set_border_width (12);
94
95         if (pitching) {
96
97                 upper_button_box.set_spacing (5);
98                 upper_button_box.set_border_width (5);
99                 
100                 Gtk::Label* l;
101
102                 l = manage (new Label (_("Octaves")));
103                 upper_button_box.pack_start (*l, false, false);
104                 upper_button_box.pack_start (pitch_octave_spinner, false, false);
105
106                 l = manage (new Label (_("Semitones (12TET)")));
107                 upper_button_box.pack_start (*l, false, false);
108                 upper_button_box.pack_start (pitch_semitone_spinner, false, false);
109
110                 l = manage (new Label (_("Cents")));
111                 upper_button_box.pack_start (*l, false, false);
112                 upper_button_box.pack_start (pitch_cent_spinner, false, false);
113
114                 pitch_cent_spinner.set_digits (1);
115
116                 upper_button_box.pack_start (preserve_formants_button, false, false);
117
118
119                 add_button (_("Shift"), Gtk::RESPONSE_ACCEPT);
120
121                 get_vbox()->pack_start (upper_button_box, false, false);
122
123         } else {
124
125 #ifdef USE_RUBBERBAND
126                 opts_box.set_spacing (5);
127                 opts_box.set_border_width (5);
128                 vector<string> strings;
129
130                 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
131                 /* set default */
132                 stretch_opts_selector.set_active_text (editor.rb_opt_strings[4]);
133
134                 opts_box.pack_start (precise_button, false, false);
135                 opts_box.pack_start (stretch_opts_label, false, false);
136                 opts_box.pack_start (stretch_opts_selector, false, false);
137
138                 get_vbox()->pack_start (opts_box, false, false);
139
140 #else
141                 upper_button_box.set_homogeneous (true);
142                 upper_button_box.set_spacing (5);
143                 upper_button_box.set_border_width (5);
144
145                 upper_button_box.pack_start (quick_button, true, true);
146                 upper_button_box.pack_start (antialias_button, true, true);
147
148                 quick_button.set_name (N_("TimeFXButton"));
149                 antialias_button.set_name (N_("TimeFXButton"));
150
151                 get_vbox()->pack_start (upper_button_box, false, false);
152
153 #endif  
154                 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
155         }
156
157         get_vbox()->pack_start (progress_bar);
158
159         progress_bar.set_name (N_("TimeFXProgress"));
160
161         show_all_children ();
162 }
163
164 gint
165 Editor::TimeFXDialog::update_progress ()
166 {
167         progress_bar.set_fraction (request.progress);
168         return !request.done;
169 }
170
171 void
172 Editor::TimeFXDialog::cancel_in_progress ()
173 {
174         status = -2;
175         request.cancel = true;
176         first_cancel.disconnect();
177 }
178
179 gint
180 Editor::TimeFXDialog::delete_in_progress (GdkEventAny* ev)
181 {
182         status = -2;
183         request.cancel = true;
184         first_delete.disconnect();
185         return TRUE;
186 }
187
188 int
189 Editor::time_stretch (RegionSelection& regions, float fraction)
190 {
191         // FIXME: kludge, implement stretching of selection of both types
192         
193         if (regions.front()->region()->data_type() == DataType::AUDIO) {
194                 // Audio, pop up timefx dialog
195                 return time_fx (regions, fraction, false);
196         } else {
197                 // MIDI, just stretch
198                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&regions.front()->get_time_axis_view());
199                 if (!rtv)
200                         return -1;
201                 
202                 boost::shared_ptr<Playlist> playlist
203                         = rtv->track()->diskstream()->playlist();
204
205             ARDOUR::TimeFXRequest request;
206                 request.time_fraction = fraction;
207                 MidiStretch stretch(*session, request);
208                 begin_reversible_command ("midi stretch");
209                 stretch.run(regions.front()->region());
210                 XMLNode &before = playlist->get_state();
211                 playlist->replace_region (regions.front()->region(), stretch.results[0],
212                                 regions.front()->region()->position());
213                 XMLNode &after = playlist->get_state();
214                 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
215                 commit_reversible_command ();
216         }
217
218         return 0;
219 }
220
221 int
222 Editor::pitch_shift (RegionSelection& regions, float fraction)
223 {
224         return time_fx (regions, fraction, true);
225 }
226
227 int
228 Editor::time_fx (RegionSelection& regions, float val, bool pitching)
229 {
230         if (current_timefx != 0) {
231                 delete current_timefx;
232         }
233
234         current_timefx = new TimeFXDialog (*this, pitching);
235
236         current_timefx->progress_bar.set_fraction (0.0f);
237
238         switch (current_timefx->run ()) {
239         case RESPONSE_ACCEPT:
240                 break;
241         default:
242                 current_timefx->hide ();
243                 return 1;
244         }
245
246         current_timefx->status = 0;
247         current_timefx->regions = regions;
248
249         if (pitching) {
250
251                 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
252                 float pitch_fraction;
253                 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
254                 cents += current_timefx->pitch_cent_adjustment.get_value();
255
256                 if (cents == 0.0) {
257                         // user didn't change anything
258                         current_timefx->hide ();
259                         return 0;
260                 }
261
262                 // one octave == 1200 cents
263                 // adding one octave doubles the frequency
264                 // ratio is 2^^octaves
265                                 
266                 pitch_fraction = pow(2, cents/1200);
267
268                 current_timefx->request.time_fraction = 1.0;
269                 current_timefx->request.pitch_fraction = pitch_fraction;
270                 
271         } else {
272
273                 current_timefx->request.time_fraction = val;
274                 current_timefx->request.pitch_fraction = 1.0;
275
276         }
277
278 #ifdef USE_RUBBERBAND
279         /* parse options */
280
281         RubberBandStretcher::Options options = 0;
282
283         bool realtime = false;
284         bool precise = false;
285         bool peaklock = true;
286         bool longwin = false;
287         bool shortwin = false;
288         bool preserve_formants = false;
289         string txt;
290
291         enum {
292                 NoTransients,
293                 BandLimitedTransients,
294                 Transients
295         } transients = Transients;
296         
297         precise = current_timefx->precise_button.get_active();
298         preserve_formants = current_timefx->preserve_formants_button.get_active();
299         
300         txt = current_timefx->stretch_opts_selector.get_active_text ();
301
302         if (txt == rb_opt_strings[0]) {
303                 transients = NoTransients; peaklock = false; longwin = true; shortwin = false; 
304         } else if (txt == rb_opt_strings[1]) {
305                 transients = NoTransients; peaklock = false; longwin = false; shortwin = false; 
306         } else if (txt == rb_opt_strings[2]) {
307                 transients = NoTransients; peaklock = true; longwin = false; shortwin = false; 
308         } else if (txt == rb_opt_strings[3]) {
309                 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false; 
310         } else if (txt == rb_opt_strings[5]) {
311                 transients = Transients; peaklock = false; longwin = false; shortwin = true; 
312         } else {
313                 /* default/4 */
314
315                 transients = Transients; peaklock = true; longwin = false; shortwin = false; 
316         };
317
318
319         if (realtime)    options |= RubberBandStretcher::OptionProcessRealTime;
320         if (precise)     options |= RubberBandStretcher::OptionStretchPrecise;
321         if (preserve_formants)  options |= RubberBandStretcher::OptionFormantPreserved;
322
323         if (!peaklock)   options |= RubberBandStretcher::OptionPhaseIndependent;
324         if (longwin)     options |= RubberBandStretcher::OptionWindowLong;
325         if (shortwin)    options |= RubberBandStretcher::OptionWindowShort;
326                 
327                 
328                 
329         switch (transients) {
330         case NoTransients:
331                 options |= RubberBandStretcher::OptionTransientsSmooth;
332                 break;
333         case BandLimitedTransients:
334                 options |= RubberBandStretcher::OptionTransientsMixed;
335                 break;
336         case Transients:
337                 options |= RubberBandStretcher::OptionTransientsCrisp;
338                 break;
339         }
340
341         current_timefx->request.opts = (int) options;
342 #else
343         current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
344         current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
345 #endif
346         current_timefx->request.progress = 0.0f;
347         current_timefx->request.done = false;
348         current_timefx->request.cancel = false;
349         
350         /* re-connect the cancel button and delete events */
351         
352         current_timefx->first_cancel.disconnect();
353         current_timefx->first_delete.disconnect();
354         
355         current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect 
356                 (mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
357         current_timefx->first_delete = current_timefx->signal_delete_event().connect 
358                 (mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
359
360         if (pthread_create_and_store ("timefx", &current_timefx->request.thread, 0, timefx_thread, current_timefx)) {
361                 current_timefx->hide ();
362                 error << _("timefx cannot be started - thread creation error") << endmsg;
363                 return -1;
364         }
365
366         pthread_detach (current_timefx->request.thread);
367
368         sigc::connection c = Glib::signal_timeout().connect (mem_fun (current_timefx, &TimeFXDialog::update_progress), 100);
369
370         while (!current_timefx->request.done && !current_timefx->request.cancel) {
371                 gtk_main_iteration ();
372         }
373
374         c.disconnect ();
375         
376         current_timefx->hide ();
377         return current_timefx->status;
378 }
379
380 void
381 Editor::do_timefx (TimeFXDialog& dialog)
382 {
383         Track*    t;
384         boost::shared_ptr<Playlist> playlist;
385         boost::shared_ptr<Region>   new_region;
386         bool in_command = false;
387         
388         for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
389                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
390
391                 if (!arv) {
392                         continue;
393                 }
394
395                 boost::shared_ptr<AudioRegion> region (arv->audio_region());
396                 TimeAxisView* tv = &(arv->get_time_axis_view());
397                 RouteTimeAxisView* rtv;
398                 RegionSelection::iterator tmp;
399                 
400                 tmp = i;
401                 ++tmp;
402
403                 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
404                         i = tmp;
405                         continue;
406                 }
407
408                 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
409                         i = tmp;
410                         continue;
411                 }
412         
413                 if ((playlist = t->diskstream()->playlist()) == 0) {
414                         i = tmp;
415                         continue;
416                 }
417
418                 if (dialog.request.cancel) {
419                         /* we were cancelled */
420                         dialog.status = 1;
421                         return;
422                 }
423
424                 Filter* fx;
425
426                 if (dialog.pitching) {
427                         fx = new Pitch (*session, dialog.request);
428                 } else {
429                         fx = new Stretch (*session, dialog.request);
430                 }
431
432                 if (fx->run (region)) {
433                         dialog.status = -1;
434                         dialog.request.done = true;
435                         delete fx;
436                         return;
437                 }
438
439                 if (!fx->results.empty()) {
440                         new_region = fx->results.front();
441
442                         if (!in_command) {
443                                 begin_reversible_command (dialog.pitching ? _("pitch shift") : _("time stretch"));
444                                 in_command = true;
445                         }
446
447                         XMLNode &before = playlist->get_state();
448                         playlist->replace_region (region, new_region, region->position());
449                         XMLNode &after = playlist->get_state();
450                         session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
451                 }
452
453                 i = tmp;
454                 delete fx;
455         }
456
457         if (in_command) {
458                 commit_reversible_command ();
459         }
460
461         dialog.status = 0;
462         dialog.request.done = true;
463 }
464
465 void*
466 Editor::timefx_thread (void *arg)
467 {
468         PBD::notify_gui_about_thread_creation (pthread_self(), X_("TimeFX"));
469
470         TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
471
472         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
473
474         tsd->editor.do_timefx (*tsd);
475
476         return 0;
477 }
478