Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[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         delete current_timefx;
231
232         current_timefx = new TimeFXDialog (*this, pitching);
233
234         current_timefx->progress_bar.set_fraction (0.0f);
235
236         switch (current_timefx->run ()) {
237         case RESPONSE_ACCEPT:
238                 break;
239         default:
240                 current_timefx->hide ();
241                 return 1;
242         }
243
244         current_timefx->status = 0;
245         current_timefx->regions = regions;
246
247         if (pitching) {
248
249                 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
250                 float pitch_fraction;
251                 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
252                 cents += current_timefx->pitch_cent_adjustment.get_value();
253
254                 if (cents == 0.0) {
255                         // user didn't change anything
256                         current_timefx->hide ();
257                         return 0;
258                 }
259
260                 // one octave == 1200 cents
261                 // adding one octave doubles the frequency
262                 // ratio is 2^^octaves
263                                 
264                 pitch_fraction = pow(2, cents/1200);
265
266                 current_timefx->request.time_fraction = 1.0;
267                 current_timefx->request.pitch_fraction = pitch_fraction;
268                 
269         } else {
270
271                 current_timefx->request.time_fraction = val;
272                 current_timefx->request.pitch_fraction = 1.0;
273
274         }
275
276 #ifdef USE_RUBBERBAND
277         /* parse options */
278
279         RubberBandStretcher::Options options = 0;
280
281         bool realtime = false;
282         bool precise = false;
283         bool peaklock = true;
284         bool longwin = false;
285         bool shortwin = false;
286         bool preserve_formants = false;
287         string txt;
288
289         enum {
290                 NoTransients,
291                 BandLimitedTransients,
292                 Transients
293         } transients = Transients;
294         
295         precise = current_timefx->precise_button.get_active();
296         preserve_formants = current_timefx->preserve_formants_button.get_active();
297         
298         txt = current_timefx->stretch_opts_selector.get_active_text ();
299
300         if (txt == rb_opt_strings[0]) {
301                 transients = NoTransients; peaklock = false; longwin = true; shortwin = false; 
302         } else if (txt == rb_opt_strings[1]) {
303                 transients = NoTransients; peaklock = false; longwin = false; shortwin = false; 
304         } else if (txt == rb_opt_strings[2]) {
305                 transients = NoTransients; peaklock = true; longwin = false; shortwin = false; 
306         } else if (txt == rb_opt_strings[3]) {
307                 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false; 
308         } else if (txt == rb_opt_strings[5]) {
309                 transients = Transients; peaklock = false; longwin = false; shortwin = true; 
310         } else {
311                 /* default/4 */
312
313                 transients = Transients; peaklock = true; longwin = false; shortwin = false; 
314         };
315
316
317         if (realtime)    options |= RubberBandStretcher::OptionProcessRealTime;
318         if (precise)     options |= RubberBandStretcher::OptionStretchPrecise;
319         if (preserve_formants)  options |= RubberBandStretcher::OptionFormantPreserved;
320
321         if (!peaklock)   options |= RubberBandStretcher::OptionPhaseIndependent;
322         if (longwin)     options |= RubberBandStretcher::OptionWindowLong;
323         if (shortwin)    options |= RubberBandStretcher::OptionWindowShort;
324                 
325                 
326                 
327         switch (transients) {
328         case NoTransients:
329                 options |= RubberBandStretcher::OptionTransientsSmooth;
330                 break;
331         case BandLimitedTransients:
332                 options |= RubberBandStretcher::OptionTransientsMixed;
333                 break;
334         case Transients:
335                 options |= RubberBandStretcher::OptionTransientsCrisp;
336                 break;
337         }
338
339         current_timefx->request.opts = (int) options;
340 #else
341         current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
342         current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
343 #endif
344         current_timefx->request.progress = 0.0f;
345         current_timefx->request.done = false;
346         current_timefx->request.cancel = false;
347         
348         /* re-connect the cancel button and delete events */
349         
350         current_timefx->first_cancel.disconnect();
351         current_timefx->first_delete.disconnect();
352         
353         current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect 
354                 (mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
355         current_timefx->first_delete = current_timefx->signal_delete_event().connect 
356                 (mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
357
358         if (pthread_create_and_store ("timefx", &current_timefx->request.thread, 0, timefx_thread, current_timefx)) {
359                 current_timefx->hide ();
360                 error << _("timefx cannot be started - thread creation error") << endmsg;
361                 return -1;
362         }
363
364         pthread_detach (current_timefx->request.thread);
365
366         sigc::connection c = Glib::signal_timeout().connect (mem_fun (current_timefx, &TimeFXDialog::update_progress), 100);
367
368         while (!current_timefx->request.done && !current_timefx->request.cancel) {
369                 gtk_main_iteration ();
370         }
371
372         c.disconnect ();
373         
374         current_timefx->hide ();
375         return current_timefx->status;
376 }
377
378 void
379 Editor::do_timefx (TimeFXDialog& dialog)
380 {
381         Track*    t;
382         boost::shared_ptr<Playlist> playlist;
383         boost::shared_ptr<Region>   new_region;
384         bool in_command = false;
385         
386         for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
387                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
388
389                 if (!arv) {
390                         continue;
391                 }
392
393                 boost::shared_ptr<AudioRegion> region (arv->audio_region());
394                 TimeAxisView* tv = &(arv->get_time_axis_view());
395                 RouteTimeAxisView* rtv;
396                 RegionSelection::iterator tmp;
397                 
398                 tmp = i;
399                 ++tmp;
400
401                 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
402                         i = tmp;
403                         continue;
404                 }
405
406                 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
407                         i = tmp;
408                         continue;
409                 }
410         
411                 if ((playlist = t->diskstream()->playlist()) == 0) {
412                         i = tmp;
413                         continue;
414                 }
415
416                 if (dialog.request.cancel) {
417                         /* we were cancelled */
418                         dialog.status = 1;
419                         return;
420                 }
421
422                 Filter* fx;
423
424                 if (dialog.pitching) {
425                         fx = new Pitch (*session, dialog.request);
426                 } else {
427                         fx = new Stretch (*session, dialog.request);
428                 }
429
430                 if (fx->run (region)) {
431                         dialog.status = -1;
432                         dialog.request.done = true;
433                         delete fx;
434                         return;
435                 }
436
437                 if (!fx->results.empty()) {
438                         new_region = fx->results.front();
439
440                         if (!in_command) {
441                                 begin_reversible_command (dialog.pitching ? _("pitch shift") : _("time stretch"));
442                                 in_command = true;
443                         }
444
445                         XMLNode &before = playlist->get_state();
446                         playlist->replace_region (region, new_region, region->position());
447                         XMLNode &after = playlist->get_state();
448                         session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
449                 }
450
451                 i = tmp;
452                 delete fx;
453         }
454
455         if (in_command) {
456                 commit_reversible_command ();
457         }
458
459         dialog.status = 0;
460         dialog.request.done = true;
461 }
462
463 void*
464 Editor::timefx_thread (void *arg)
465 {
466         PBD::notify_gui_about_thread_creation (pthread_self(), X_("TimeFX"));
467
468         TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
469
470         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
471
472         tsd->editor.do_timefx (*tsd);
473
474         return 0;
475 }
476