Re-work TimeFX cancel/abort
[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 #include <ctime>
24 #include <string>
25 #include <set>
26
27 #include "pbd/error.h"
28 #include "pbd/pthread_utils.h"
29 #include "pbd/memento_command.h"
30 #include "pbd/stateful_diff_command.h"
31
32 #include "ardour/audioregion.h"
33 #include "ardour/midi_stretch.h"
34 #include "ardour/pitch.h"
35 #include "ardour/region.h"
36 #include "ardour/region_factory.h"
37 #include "ardour/session.h"
38 #include "ardour/stretch.h"
39
40 #include <gtkmm2ext/utils.h>
41
42 #include "audio_region_view.h"
43 #include "audio_time_axis.h"
44 #include "editor.h"
45 #include "editor_regions.h"
46 #include "region_selection.h"
47 #include "time_fx_dialog.h"
48
49 #ifdef USE_RUBBERBAND
50 #include <rubberband/RubberBandStretcher.h>
51 using namespace RubberBand;
52 #endif
53
54 #include "pbd/i18n.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Gtk;
60 using namespace Gtkmm2ext;
61
62 /** @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
63 int
64 Editor::time_stretch (RegionSelection& regions, float fraction)
65 {
66         RegionList audio;
67         RegionList midi;
68
69         begin_reversible_command (_("stretch/shrink"));
70
71         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
72                 if  ((*i)->region()->data_type() == DataType::AUDIO) {
73                         audio.push_back ((*i)->region());
74                 } else if  ((*i)->region()->data_type() == DataType::MIDI) {
75                         midi.push_back ((*i)->region());
76                 }
77         }
78
79         int aret = time_fx (audio, fraction, false);
80         if (aret < 0) {
81                 abort_reversible_command ();
82                 return aret;
83         }
84
85         set<boost::shared_ptr<Playlist> > midi_playlists_affected;
86
87         for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
88                 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
89
90                 if (playlist) {
91                         playlist->clear_changes ();
92                 }
93
94         }
95
96         ARDOUR::TimeFXRequest request;
97         request.time_fraction = fraction;
98
99         for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
100                 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
101
102                 if (!playlist) {
103                         continue;
104                 }
105
106                 MidiStretch stretch (*_session, request);
107                 stretch.run (*i);
108
109                 playlist->replace_region (regions.front()->region(), stretch.results[0],
110                                           regions.front()->region()->position());
111                 midi_playlists_affected.insert (playlist);
112         }
113
114         for (set<boost::shared_ptr<Playlist> >::iterator p = midi_playlists_affected.begin(); p != midi_playlists_affected.end(); ++p) {
115                 PBD::StatefulDiffCommand* cmd = new StatefulDiffCommand (*p);
116                 _session->add_command (cmd);
117                 if (!cmd->empty ()) {
118                         ++aret;
119                 }
120         }
121
122         if (aret > 0) {
123                 commit_reversible_command ();
124         } else {
125                 abort_reversible_command ();
126         }
127
128         return 0;
129 }
130
131 int
132 Editor::pitch_shift (RegionSelection& regions, float fraction)
133 {
134         RegionList rl;
135
136         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
137                 rl.push_back ((*i)->region());
138         }
139
140         begin_reversible_command (_("pitch shift"));
141
142         int ret = time_fx (rl, fraction, true);
143
144         if (ret > 0) {
145                 commit_reversible_command ();
146         } else {
147                 abort_reversible_command ();
148         }
149
150         return ret < 0 ? -1 : 0;
151 }
152
153 /** @param val Percentage to time stretch by; ignored if pitch-shifting.
154  *  @param pitching true to pitch shift, false to time stretch.
155  *  @return -1 in case of error, otherwise number of regions processed */
156 int
157 Editor::time_fx (RegionList& regions, float val, bool pitching)
158 {
159         delete current_timefx;
160
161         if (regions.empty()) {
162                 current_timefx = 0;
163                 return 0;
164         }
165
166         const samplecnt_t oldlen = (samplecnt_t) (regions.front()->length());
167         const samplecnt_t newlen = (samplecnt_t) (regions.front()->length() * val);
168         const samplecnt_t pos = regions.front()->position ();
169
170         current_timefx = new TimeFXDialog (*this, pitching, oldlen, newlen, pos);
171         current_timefx->regions = regions;
172
173         switch (current_timefx->run ()) {
174         case RESPONSE_ACCEPT:
175                 break;
176         default:
177                 current_timefx->hide ();
178                 return -1;
179         }
180
181         current_timefx->status = 0;
182         current_timefx->request.time_fraction = current_timefx->get_time_fraction ();
183         current_timefx->request.pitch_fraction = current_timefx->get_pitch_fraction ();
184
185         if (current_timefx->request.time_fraction == 1.0 &&
186             current_timefx->request.pitch_fraction == 1.0) {
187                 /* nothing to do */
188                 current_timefx->hide ();
189                 return 0;
190         }
191
192 #ifdef USE_RUBBERBAND
193         /* parse options */
194
195         RubberBandStretcher::Options options = 0;
196
197         bool realtime = false;
198         bool precise = false;
199         bool peaklock = true;
200         bool longwin = false;
201         bool shortwin = false;
202         bool preserve_formants = false;
203         string txt;
204
205         enum {
206                 NoTransients,
207                 BandLimitedTransients,
208                 Transients
209         } transients = Transients;
210
211         precise = current_timefx->precise_button.get_active();
212         preserve_formants = current_timefx->preserve_formants_button.get_active();
213
214         txt = current_timefx->stretch_opts_selector.get_active_text ();
215
216         for (int i = 0; i <= 6; i++) {
217                 if (txt == rb_opt_strings[i]) {
218                         rb_current_opt = i;
219                         break;
220                 }
221         }
222
223         int rb_mode = rb_current_opt;
224
225         if (pitching /*&& rb_current_opt == 6*/) {
226                 /* The timefx dialog does not show the "stretch_opts_selector"
227                  * when pitch-shifting.  So the most recently used option from
228                  * "Time Stretch" would be used (if any). That may even be
229                  * "resample without preserving pitch", which would be invalid.
230                  *
231                  * TODO: also show stretch_opts_selector when pitching (except the option
232                  * to not preserve pitch) and use separate  rb_current_opt when pitching.
233                  *
234                  * Actually overhaul this the dialog and processing opts below and use rubberband's
235                  * "Crispness" levels:
236                  *   -c 0   equivalent to --no-transients --no-lamination --window-long
237                  *   -c 1   equivalent to --detector-soft --no-lamination --window-long (for piano)
238                  *   -c 2   equivalent to --no-transients --no-lamination
239                  *   -c 3   equivalent to --no-transients
240                  *   -c 4   equivalent to --bl-transients
241                  *   -c 5   default processing options
242                  *   -c 6   equivalent to --no-lamination --window-short (may be good for drums)
243                  */
244                 rb_mode = 4;
245         }
246
247         switch (rb_mode) {
248                 case 0:
249                         transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
250                         break;
251                 case 1:
252                         transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
253                         break;
254                 case 2:
255                         transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
256                         break;
257                 case 3:
258                         transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
259                         break;
260                 case 5:
261                         transients = Transients; peaklock = false; longwin = false; shortwin = true;
262                         break;
263                 case 6:
264                         transients = NoTransients;
265                         precise = true;
266                         preserve_formants = false;
267                         current_timefx->request.pitch_fraction = 1.0 / current_timefx->request.time_fraction;
268                         shortwin = true;
269                         // peaklock = false;
270                         break;
271                 default:
272                         /* default/4 */
273                         transients = Transients; peaklock = true; longwin = false; shortwin = false;
274                         break;
275         };
276
277         if (realtime)          options |= RubberBandStretcher::OptionProcessRealTime;
278         if (precise)           options |= RubberBandStretcher::OptionStretchPrecise;
279         if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
280         if (!peaklock)         options |= RubberBandStretcher::OptionPhaseIndependent;
281         if (longwin)           options |= RubberBandStretcher::OptionWindowLong;
282         if (shortwin)          options |= RubberBandStretcher::OptionWindowShort;
283
284         if (pitching)          options |= RubberBandStretcher::OptionPitchHighQuality;
285
286         switch (transients) {
287         case NoTransients:
288                 options |= RubberBandStretcher::OptionTransientsSmooth;
289                 break;
290         case BandLimitedTransients:
291                 options |= RubberBandStretcher::OptionTransientsMixed;
292                 break;
293         case Transients:
294                 options |= RubberBandStretcher::OptionTransientsCrisp;
295                 break;
296         }
297
298         current_timefx->request.opts = (int) options;
299 #else
300         current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
301         current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
302 #endif
303         current_timefx->request.done = false;
304         current_timefx->request.cancel = false;
305
306         /* re-connect the cancel button and delete events */
307
308         current_timefx->first_cancel.disconnect();
309         current_timefx->first_delete.disconnect();
310
311         current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
312                 (sigc::mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
313         current_timefx->first_delete = current_timefx->signal_delete_event().connect
314                 (sigc::mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
315
316
317         if (pthread_create_and_store ("timefx", &current_timefx->request.thread, timefx_thread, current_timefx)) {
318                 current_timefx->hide ();
319                 error << _("timefx cannot be started - thread creation error") << endmsg;
320                 return -1;
321         }
322
323         current_timefx->start_updates ();
324
325         while (!current_timefx->request.done && !current_timefx->request.cancel) {
326                 gtk_main_iteration ();
327         }
328
329         pthread_join (current_timefx->request.thread, 0);
330
331         current_timefx->hide ();
332
333         if (current_timefx->status < 0) {
334                 /* processing was cancelled, some regions may have
335                  * been created and removed via RegionFactory::map_remove()
336                  * The region-list does not update itself when a region is removed.
337                  */
338                 _regions->redisplay ();
339         }
340         return current_timefx->status;
341 }
342
343 void
344 Editor::do_timefx ()
345 {
346         typedef std::map<boost::shared_ptr<Region>, boost::shared_ptr<Region> > ResultMap;
347         ResultMap results;
348
349         uint32_t const N = current_timefx->regions.size ();
350
351         for (RegionList::const_iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
352                 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
353                 if (playlist) {
354                         playlist->clear_changes ();
355                 }
356         }
357
358         for (RegionList::const_iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
359
360                 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (*i);
361                 boost::shared_ptr<Playlist> playlist;
362
363                 if (!region || (playlist = region->playlist()) == 0) {
364                         continue;
365                 }
366
367                 if (current_timefx->request.cancel) {
368                         break;
369                 }
370
371                 Filter* fx;
372
373                 if (current_timefx->pitching) {
374                         fx = new Pitch (*_session, current_timefx->request);
375                 } else {
376 #ifdef USE_RUBBERBAND
377                         fx = new RBStretch (*_session, current_timefx->request);
378 #else
379                         fx = new STStretch (*_session, current_timefx->request);
380 #endif
381                 }
382
383                 current_timefx->descend (1.0 / N);
384
385                 if (fx->run (region, current_timefx)) {
386                         current_timefx->request.cancel = true;
387                         delete fx;
388                         break;
389                 }
390
391                 if (!fx->results.empty()) {
392                         results[region] = fx->results.front();
393                 }
394
395                 current_timefx->ascend ();
396                 delete fx;
397         }
398
399         pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
400         if (current_timefx->request.cancel) {
401                 current_timefx->status = -1;
402                 for (ResultMap::const_iterator i = results.begin(); i != results.end(); ++i) {
403                         boost::weak_ptr<Region> w = i->second;
404                         RegionFactory::map_remove (w);
405                 }
406         } else {
407                 current_timefx->status = 0;
408                 for (ResultMap::const_iterator i = results.begin(); i != results.end(); ++i) {
409                         boost::shared_ptr<Region> region = i->first;
410                         boost::shared_ptr<Region> new_region = i->second;
411                         boost::shared_ptr<Playlist> playlist = region->playlist();
412                         playlist->replace_region (region, new_region, region->position());
413
414                         PBD::StatefulDiffCommand* cmd = new StatefulDiffCommand (playlist);
415                         _session->add_command (cmd);
416                         if (!cmd->empty ()) {
417                                 ++current_timefx->status;
418                         }
419                 }
420         }
421         pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
422
423         current_timefx->request.done = true;
424 }
425
426 void*
427 Editor::timefx_thread (void *arg)
428 {
429         SessionEvent::create_per_thread_pool ("timefx events", 64);
430
431         TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
432
433         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
434
435         tsd->editor.do_timefx ();
436
437         /* GACK! HACK! sleep for a bit so that our request buffer for the GUI
438            event loop doesn't die before any changes we made are processed
439            by the GUI ...
440         */
441         Glib::usleep(G_USEC_PER_SEC / 5);
442         return 0;
443 }