Remove unnecessary dialog argument to do_timefx; use current_timefx instead.
[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 <gtkmm2ext/utils.h>
33
34 #include "audio_region_view.h"
35 #include "audio_time_axis.h"
36 #include "editor.h"
37 #include "region_selection.h"
38 #include "time_fx_dialog.h"
39
40 #include "ardour/session.h"
41 #include "ardour/region.h"
42 #include "ardour/audioplaylist.h"
43 #include "ardour/audio_track.h"
44 #include "ardour/audioregion.h"
45 #include "ardour/stretch.h"
46 #include "ardour/midi_stretch.h"
47 #include "ardour/pitch.h"
48
49 #ifdef USE_RUBBERBAND
50 #include "rubberband/RubberBandStretcher.h"
51 using namespace RubberBand;
52 #endif
53
54 #include "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         int aret;
69
70         begin_reversible_command (_("stretch/shrink"));
71
72         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
73                 if  ((*i)->region()->data_type() == DataType::AUDIO) {
74                         audio.push_back ((*i)->region());
75                 } else if  ((*i)->region()->data_type() == DataType::MIDI) {
76                         midi.push_back ((*i)->region());
77                 }
78         }
79
80         if ((aret = time_fx (audio, fraction, false)) != 0) {
81                 return aret;
82         }
83
84         set<boost::shared_ptr<Playlist> > midi_playlists_affected;
85
86         for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
87                 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
88
89                 if (playlist) {
90                         playlist->clear_changes ();
91                 }
92
93         }
94
95         ARDOUR::TimeFXRequest request;
96         request.time_fraction = fraction;
97
98         for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
99                 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
100
101                 if (!playlist) {
102                         continue;
103                 }
104
105                 MidiStretch stretch (*_session, request);
106                 stretch.run (*i);
107
108                 playlist->replace_region (regions.front()->region(), stretch.results[0],
109                                           regions.front()->region()->position());
110                 midi_playlists_affected.insert (playlist);
111         }
112
113         for (set<boost::shared_ptr<Playlist> >::iterator p = midi_playlists_affected.begin(); p != midi_playlists_affected.end(); ++p) {
114                 _session->add_command (new StatefulDiffCommand (*p));
115         }
116
117         commit_reversible_command ();
118
119         return 0;
120 }
121
122 int
123 Editor::pitch_shift (RegionSelection& regions, float fraction)
124 {
125         RegionList rl;
126
127         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
128                 rl.push_back ((*i)->region());
129         }
130
131         begin_reversible_command (_("pitch shift"));
132
133         int ret = time_fx (rl, fraction, true);
134
135         if (ret == 0) {
136                 commit_reversible_command ();
137         }
138
139         return ret;
140 }
141
142 /** @param val Percentage to time stretch by; ignored if pitch-shifting.
143  *  @param pitching true to pitch shift, false to time stretch.
144  *  @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
145 int
146 Editor::time_fx (RegionList& regions, float val, bool pitching)
147 {
148         delete current_timefx;
149         current_timefx = new TimeFXDialog (*this, pitching);
150         current_timefx->regions = regions;
151
152         /* See if we have any audio regions on our list */
153         RegionList::iterator i = regions.begin ();
154         while (i != regions.end() && boost::dynamic_pointer_cast<AudioRegion> (*i) == 0) {
155                 ++i;
156         }
157
158         if (i == regions.end ()) {
159                 /* No audio regions; we can just do the timefx without a dialogue */
160                 do_timefx ();
161                 return 0;
162         }
163         
164         switch (current_timefx->run ()) {
165         case RESPONSE_ACCEPT:
166                 break;
167         default:
168                 current_timefx->hide ();
169                 return 1;
170         }
171
172         current_timefx->status = 0;
173
174         if (pitching) {
175
176                 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
177                 float pitch_fraction;
178                 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
179                 cents += current_timefx->pitch_cent_adjustment.get_value();
180
181                 if (cents == 0.0) {
182                         // user didn't change anything
183                         current_timefx->hide ();
184                         return 0;
185                 }
186
187                 // one octave == 1200 cents
188                 // adding one octave doubles the frequency
189                 // ratio is 2^^octaves
190
191                 pitch_fraction = pow(2, cents/1200);
192
193                 current_timefx->request.time_fraction = 1.0;
194                 current_timefx->request.pitch_fraction = pitch_fraction;
195
196         } else {
197
198                 current_timefx->request.time_fraction = val;
199                 current_timefx->request.pitch_fraction = 1.0;
200
201         }
202
203 #ifdef USE_RUBBERBAND
204         /* parse options */
205
206         RubberBandStretcher::Options options = 0;
207
208         bool realtime = false;
209         bool precise = false;
210         bool peaklock = true;
211         bool longwin = false;
212         bool shortwin = false;
213         bool preserve_formants = false;
214         string txt;
215
216         enum {
217                 NoTransients,
218                 BandLimitedTransients,
219                 Transients
220         } transients = Transients;
221
222         precise = current_timefx->precise_button.get_active();
223         preserve_formants = current_timefx->preserve_formants_button.get_active();
224
225         txt = current_timefx->stretch_opts_selector.get_active_text ();
226
227         for (int i = 0; i <= 6; i++) {
228                 if (txt == rb_opt_strings[i]) {
229                         rb_current_opt = i;
230                         break;
231                 }
232         }
233
234         switch (rb_current_opt) {
235                 case 0:
236                         transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
237                         break;
238                 case 1:
239                         transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
240                         break;
241                 case 2:
242                         transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
243                         break;
244                 case 3:
245                         transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
246                         break;
247                 case 5:
248                         transients = Transients; peaklock = false; longwin = false; shortwin = true;
249                         break;
250                 case 6:
251                         transients = NoTransients;
252                         precise = true;
253                         preserve_formants = false;
254                         current_timefx->request.pitch_fraction = 1/val;
255                         shortwin = true;
256                         // peaklock = false;
257                         break;
258                 default:
259                         /* default/4 */
260                         transients = Transients; peaklock = true; longwin = false; shortwin = false;
261                         break;
262         };
263
264         if (realtime)          options |= RubberBandStretcher::OptionProcessRealTime;
265         if (precise)           options |= RubberBandStretcher::OptionStretchPrecise;
266         if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
267         if (!peaklock)         options |= RubberBandStretcher::OptionPhaseIndependent;
268         if (longwin)           options |= RubberBandStretcher::OptionWindowLong;
269         if (shortwin)          options |= RubberBandStretcher::OptionWindowShort;
270
271         switch (transients) {
272         case NoTransients:
273                 options |= RubberBandStretcher::OptionTransientsSmooth;
274                 break;
275         case BandLimitedTransients:
276                 options |= RubberBandStretcher::OptionTransientsMixed;
277                 break;
278         case Transients:
279                 options |= RubberBandStretcher::OptionTransientsCrisp;
280                 break;
281         }
282
283         current_timefx->request.opts = (int) options;
284 #else
285         current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
286         current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
287 #endif
288         current_timefx->request.done = false;
289         current_timefx->request.cancel = false;
290
291         /* re-connect the cancel button and delete events */
292
293         current_timefx->first_cancel.disconnect();
294         current_timefx->first_delete.disconnect();
295
296         current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
297                 (sigc::mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
298         current_timefx->first_delete = current_timefx->signal_delete_event().connect
299                 (sigc::mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
300
301         if (pthread_create_and_store ("timefx", &current_timefx->request.thread, timefx_thread, current_timefx)) {
302                 current_timefx->hide ();
303                 error << _("timefx cannot be started - thread creation error") << endmsg;
304                 return -1;
305         }
306
307         pthread_detach (current_timefx->request.thread);
308
309         while (!current_timefx->request.done && !current_timefx->request.cancel) {
310                 gtk_main_iteration ();
311         }
312
313         current_timefx->hide ();
314         return current_timefx->status;
315 }
316
317 void
318 Editor::do_timefx ()
319 {
320         boost::shared_ptr<Playlist> playlist;
321         boost::shared_ptr<Region>   new_region;
322         set<boost::shared_ptr<Playlist> > playlists_affected;
323
324         uint32_t const N = current_timefx->regions.size ();
325
326         for (RegionList::iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
327                 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
328
329                 if (playlist) {
330                         playlist->clear_changes ();
331                 }
332         }
333
334         for (RegionList::iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
335
336                 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (*i);
337
338                 if (!region || (playlist = region->playlist()) == 0) {
339                         continue;
340                 }
341
342                 if (current_timefx->request.cancel) {
343                         /* we were cancelled */
344                         /* XXX what to do about playlists already affected ? */
345                         current_timefx->status = 1;
346                         return;
347                 }
348
349                 Filter* fx;
350
351                 if (current_timefx->pitching) {
352                         fx = new Pitch (*_session, current_timefx->request);
353                 } else {
354 #ifdef USE_RUBBERBAND
355                         fx = new RBStretch (*_session, current_timefx->request);
356 #else
357                         fx = new STStretch (*_session, current_timefx->request);
358 #endif
359                 }
360
361                 current_timefx->descend (1.0 / N);
362
363                 if (fx->run (region, current_timefx)) {
364                         current_timefx->status = -1;
365                         current_timefx->request.done = true;
366                         delete fx;
367                         return;
368                 }
369
370                 if (!fx->results.empty()) {
371                         new_region = fx->results.front();
372
373                         playlist->replace_region (region, new_region, region->position());
374                         playlists_affected.insert (playlist);
375                 }
376
377                 current_timefx->ascend ();
378                 delete fx;
379         }
380
381         for (set<boost::shared_ptr<Playlist> >::iterator p = playlists_affected.begin(); p != playlists_affected.end(); ++p) {
382                 _session->add_command (new StatefulDiffCommand (*p));
383         }
384
385         current_timefx->status = 0;
386         current_timefx->request.done = true;
387 }
388
389 void*
390 Editor::timefx_thread (void *arg)
391 {
392         SessionEvent::create_per_thread_pool ("timefx events", 64);
393
394         TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
395
396         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
397
398         tsd->editor.do_timefx ();
399
400         /* GACK! HACK! sleep for a bit so that our request buffer for the GUI
401            event loop doesn't die before any changes we made are processed
402            by the GUI ...
403         */
404
405         struct timespec t = { 2, 0 };
406         nanosleep (&t, 0);
407
408         return 0;
409 }
410