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