fix conflicts and merge with master
[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/audioregion.h"
41 #include "ardour/midi_stretch.h"
42 #include "ardour/pitch.h"
43 #include "ardour/region.h"
44 #include "ardour/session.h"
45 #include "ardour/stretch.h"
46
47 #ifdef USE_RUBBERBAND
48 #include "rubberband/RubberBandStretcher.h"
49 using namespace RubberBand;
50 #endif
51
52 #include "i18n.h"
53
54 using namespace std;
55 using namespace ARDOUR;
56 using namespace PBD;
57 using namespace Gtk;
58 using namespace Gtkmm2ext;
59
60 /** @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
61 int
62 Editor::time_stretch (RegionSelection& regions, float fraction)
63 {
64         RegionList audio;
65         RegionList midi;
66         int aret;
67
68         begin_reversible_command (_("stretch/shrink"));
69
70         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
71                 if  ((*i)->region()->data_type() == DataType::AUDIO) {
72                         audio.push_back ((*i)->region());
73                 } else if  ((*i)->region()->data_type() == DataType::MIDI) {
74                         midi.push_back ((*i)->region());
75                 }
76         }
77
78         if ((aret = time_fx (audio, fraction, false)) != 0) {
79                 return aret;
80         }
81
82         set<boost::shared_ptr<Playlist> > midi_playlists_affected;
83
84         for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
85                 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
86
87                 if (playlist) {
88                         playlist->clear_changes ();
89                 }
90
91         }
92
93         ARDOUR::TimeFXRequest request;
94         request.time_fraction = fraction;
95
96         for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
97                 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
98
99                 if (!playlist) {
100                         continue;
101                 }
102
103                 MidiStretch stretch (*_session, request);
104                 stretch.run (*i);
105
106                 playlist->replace_region (regions.front()->region(), stretch.results[0],
107                                           regions.front()->region()->position());
108                 midi_playlists_affected.insert (playlist);
109         }
110
111         for (set<boost::shared_ptr<Playlist> >::iterator p = midi_playlists_affected.begin(); p != midi_playlists_affected.end(); ++p) {
112                 _session->add_command (new StatefulDiffCommand (*p));
113         }
114
115         commit_reversible_command ();
116
117         return 0;
118 }
119
120 int
121 Editor::pitch_shift (RegionSelection& regions, float fraction)
122 {
123         RegionList rl;
124
125         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
126                 rl.push_back ((*i)->region());
127         }
128
129         begin_reversible_command (_("pitch shift"));
130
131         int ret = time_fx (rl, fraction, true);
132
133         if (ret == 0) {
134                 commit_reversible_command ();
135         }
136
137         return ret;
138 }
139
140 /** @param val Percentage to time stretch by; ignored if pitch-shifting.
141  *  @param pitching true to pitch shift, false to time stretch.
142  *  @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
143 int
144 Editor::time_fx (RegionList& regions, float val, bool pitching)
145 {
146         delete current_timefx;
147         current_timefx = new TimeFXDialog (*this, pitching);
148         current_timefx->regions = regions;
149
150         /* See if we have any audio regions on our list */
151         RegionList::iterator i = regions.begin ();
152         while (i != regions.end() && boost::dynamic_pointer_cast<AudioRegion> (*i) == 0) {
153                 ++i;
154         }
155
156         if (i == regions.end ()) {
157                 /* No audio regions; we can just do the timefx without a dialogue */
158                 do_timefx ();
159                 return 0;
160         }
161         
162         switch (current_timefx->run ()) {
163         case RESPONSE_ACCEPT:
164                 break;
165         default:
166                 current_timefx->hide ();
167                 return 1;
168         }
169
170         current_timefx->status = 0;
171
172         if (pitching) {
173
174                 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
175                 float pitch_fraction;
176                 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
177                 cents += current_timefx->pitch_cent_adjustment.get_value();
178
179                 if (cents == 0.0) {
180                         // user didn't change anything
181                         current_timefx->hide ();
182                         return 0;
183                 }
184
185                 // one octave == 1200 cents
186                 // adding one octave doubles the frequency
187                 // ratio is 2^^octaves
188
189                 pitch_fraction = pow(2, cents/1200);
190
191                 current_timefx->request.time_fraction = 1.0;
192                 current_timefx->request.pitch_fraction = pitch_fraction;
193
194         } else {
195
196                 current_timefx->request.time_fraction = val;
197                 current_timefx->request.pitch_fraction = 1.0;
198
199         }
200
201 #ifdef USE_RUBBERBAND
202         /* parse options */
203
204         RubberBandStretcher::Options options = 0;
205
206         bool realtime = false;
207         bool precise = false;
208         bool peaklock = true;
209         bool longwin = false;
210         bool shortwin = false;
211         bool preserve_formants = false;
212         string txt;
213
214         enum {
215                 NoTransients,
216                 BandLimitedTransients,
217                 Transients
218         } transients = Transients;
219
220         precise = current_timefx->precise_button.get_active();
221         preserve_formants = current_timefx->preserve_formants_button.get_active();
222
223         txt = current_timefx->stretch_opts_selector.get_active_text ();
224
225         for (int i = 0; i <= 6; i++) {
226                 if (txt == rb_opt_strings[i]) {
227                         rb_current_opt = i;
228                         break;
229                 }
230         }
231
232         switch (rb_current_opt) {
233                 case 0:
234                         transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
235                         break;
236                 case 1:
237                         transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
238                         break;
239                 case 2:
240                         transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
241                         break;
242                 case 3:
243                         transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
244                         break;
245                 case 5:
246                         transients = Transients; peaklock = false; longwin = false; shortwin = true;
247                         break;
248                 case 6:
249                         transients = NoTransients;
250                         precise = true;
251                         preserve_formants = false;
252                         current_timefx->request.pitch_fraction = 1/val;
253                         shortwin = true;
254                         // peaklock = false;
255                         break;
256                 default:
257                         /* default/4 */
258                         transients = Transients; peaklock = true; longwin = false; shortwin = false;
259                         break;
260         };
261
262         if (realtime)          options |= RubberBandStretcher::OptionProcessRealTime;
263         if (precise)           options |= RubberBandStretcher::OptionStretchPrecise;
264         if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
265         if (!peaklock)         options |= RubberBandStretcher::OptionPhaseIndependent;
266         if (longwin)           options |= RubberBandStretcher::OptionWindowLong;
267         if (shortwin)          options |= RubberBandStretcher::OptionWindowShort;
268
269         switch (transients) {
270         case NoTransients:
271                 options |= RubberBandStretcher::OptionTransientsSmooth;
272                 break;
273         case BandLimitedTransients:
274                 options |= RubberBandStretcher::OptionTransientsMixed;
275                 break;
276         case Transients:
277                 options |= RubberBandStretcher::OptionTransientsCrisp;
278                 break;
279         }
280
281         current_timefx->request.opts = (int) options;
282 #else
283         current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
284         current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
285 #endif
286         current_timefx->request.done = false;
287         current_timefx->request.cancel = false;
288
289         /* re-connect the cancel button and delete events */
290
291         current_timefx->first_cancel.disconnect();
292         current_timefx->first_delete.disconnect();
293
294         current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
295                 (sigc::mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
296         current_timefx->first_delete = current_timefx->signal_delete_event().connect
297                 (sigc::mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
298
299         if (pthread_create_and_store ("timefx", &current_timefx->request.thread, timefx_thread, current_timefx)) {
300                 current_timefx->hide ();
301                 error << _("timefx cannot be started - thread creation error") << endmsg;
302                 return -1;
303         }
304
305         pthread_detach (current_timefx->request.thread);
306
307         while (!current_timefx->request.done && !current_timefx->request.cancel) {
308                 gtk_main_iteration ();
309         }
310
311         pthread_join (current_timefx->request.thread, 0);
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 #ifdef WIN32
406         Sleep(2000);
407 #else
408         struct timespec t = { 2, 0 };
409         nanosleep (&t, 0);
410 #endif
411         return 0;
412 }
413