d40299a0a30579ce90a214e5931a903dbca84689
[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
25 #include <string>
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         // FIXME: kludge, implement stretching of selection of both types
67
68         if (regions.front()->region()->data_type() == DataType::AUDIO) {
69                 // Audio, pop up timefx dialog
70                 return time_fx (regions, fraction, false);
71         } else {
72                 // MIDI, just stretch
73                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&regions.front()->get_time_axis_view());
74                 if (!rtv)
75                         return -1;
76
77                 boost::shared_ptr<Playlist> playlist
78                         = rtv->track()->playlist();
79
80             ARDOUR::TimeFXRequest request;
81                 request.time_fraction = fraction;
82                 MidiStretch stretch(*_session, request);
83                 begin_reversible_command ("midi stretch");
84                 stretch.run(regions.front()->region());
85                 playlist->clear_changes ();
86                 playlist->replace_region (regions.front()->region(), stretch.results[0],
87                                 regions.front()->region()->position());
88                 _session->add_command (new StatefulDiffCommand (playlist));
89                 commit_reversible_command ();
90         }
91
92         return 0;
93 }
94
95 int
96 Editor::pitch_shift (RegionSelection& regions, float fraction)
97 {
98         return time_fx (regions, fraction, true);
99 }
100
101 /** @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
102 int
103 Editor::time_fx (RegionSelection& regions, float val, bool pitching)
104 {
105         delete current_timefx;
106
107         current_timefx = new TimeFXDialog (*this, pitching);
108
109         current_timefx->progress_bar.set_fraction (0.0f);
110
111         switch (current_timefx->run ()) {
112         case RESPONSE_ACCEPT:
113                 break;
114         default:
115                 current_timefx->hide ();
116                 return 1;
117         }
118
119         current_timefx->status = 0;
120         current_timefx->regions = regions;
121
122         if (pitching) {
123
124                 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
125                 float pitch_fraction;
126                 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
127                 cents += current_timefx->pitch_cent_adjustment.get_value();
128
129                 if (cents == 0.0) {
130                         // user didn't change anything
131                         current_timefx->hide ();
132                         return 0;
133                 }
134
135                 // one octave == 1200 cents
136                 // adding one octave doubles the frequency
137                 // ratio is 2^^octaves
138
139                 pitch_fraction = pow(2, cents/1200);
140
141                 current_timefx->request.time_fraction = 1.0;
142                 current_timefx->request.pitch_fraction = pitch_fraction;
143
144         } else {
145
146                 current_timefx->request.time_fraction = val;
147                 current_timefx->request.pitch_fraction = 1.0;
148
149         }
150
151 #ifdef USE_RUBBERBAND
152         /* parse options */
153
154         RubberBandStretcher::Options options = 0;
155
156         bool realtime = false;
157         bool precise = false;
158         bool peaklock = true;
159         bool longwin = false;
160         bool shortwin = false;
161         bool preserve_formants = false;
162         string txt;
163
164         enum {
165                 NoTransients,
166                 BandLimitedTransients,
167                 Transients
168         } transients = Transients;
169
170         precise = current_timefx->precise_button.get_active();
171         preserve_formants = current_timefx->preserve_formants_button.get_active();
172
173         txt = current_timefx->stretch_opts_selector.get_active_text ();
174
175         for (int i = 0; i <= 6; i++) {
176                 if (txt == rb_opt_strings[i]) {
177                         rb_current_opt = i;
178                         break;
179                 }
180         }
181         
182         switch (rb_current_opt) {
183                 case 0:
184                         transients = NoTransients; peaklock = false; longwin = true; shortwin = false; 
185                         break;
186                 case 1:
187                         transients = NoTransients; peaklock = false; longwin = false; shortwin = false; 
188                         break;
189                 case 2:
190                         transients = NoTransients; peaklock = true; longwin = false; shortwin = false; 
191                         break;
192                 case 3:
193                         transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false; 
194                         break;
195                 case 5:
196                         transients = Transients; peaklock = false; longwin = false; shortwin = true; 
197                         break;
198                 case 6:
199                         transients = NoTransients;
200                         precise = true;
201                         preserve_formants = false;
202                         current_timefx->request.pitch_fraction = 1/val;
203                         shortwin = true;
204                         // peaklock = false;
205                         break;
206                 default:
207                         /* default/4 */ 
208                         transients = Transients; peaklock = true; longwin = false; shortwin = false; 
209                         break;
210         };
211
212         if (realtime)          options |= RubberBandStretcher::OptionProcessRealTime;
213         if (precise)           options |= RubberBandStretcher::OptionStretchPrecise;
214         if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
215         if (!peaklock)         options |= RubberBandStretcher::OptionPhaseIndependent;
216         if (longwin)           options |= RubberBandStretcher::OptionWindowLong;
217         if (shortwin)          options |= RubberBandStretcher::OptionWindowShort;
218
219         switch (transients) {
220         case NoTransients:
221                 options |= RubberBandStretcher::OptionTransientsSmooth;
222                 break;
223         case BandLimitedTransients:
224                 options |= RubberBandStretcher::OptionTransientsMixed;
225                 break;
226         case Transients:
227                 options |= RubberBandStretcher::OptionTransientsCrisp;
228                 break;
229         }
230
231         current_timefx->request.opts = (int) options;
232 #else
233         current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
234         current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
235 #endif
236         current_timefx->request.progress = 0.0f;
237         current_timefx->request.done = false;
238         current_timefx->request.cancel = false;
239
240         /* re-connect the cancel button and delete events */
241
242         current_timefx->first_cancel.disconnect();
243         current_timefx->first_delete.disconnect();
244
245         current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
246                 (sigc::mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
247         current_timefx->first_delete = current_timefx->signal_delete_event().connect
248                 (sigc::mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
249
250         if (pthread_create_and_store ("timefx", &current_timefx->request.thread, timefx_thread, current_timefx)) {
251                 current_timefx->hide ();
252                 error << _("timefx cannot be started - thread creation error") << endmsg;
253                 return -1;
254         }
255
256         pthread_detach (current_timefx->request.thread);
257
258         sigc::connection c = Glib::signal_timeout().connect (sigc::mem_fun (current_timefx, &TimeFXDialog::update_progress), 100);
259
260         while (!current_timefx->request.done && !current_timefx->request.cancel) {
261                 gtk_main_iteration ();
262         }
263
264         c.disconnect ();
265
266         current_timefx->hide ();
267         return current_timefx->status;
268 }
269
270 void
271 Editor::do_timefx (TimeFXDialog& dialog)
272 {
273         Track*    t;
274         boost::shared_ptr<Playlist> playlist;
275         boost::shared_ptr<Region>   new_region;
276         bool in_command = false;
277
278         for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
279                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
280
281                 if (!arv) {
282                         continue;
283                 }
284
285                 boost::shared_ptr<AudioRegion> region (arv->audio_region());
286                 TimeAxisView* tv = &(arv->get_time_axis_view());
287                 RouteTimeAxisView* rtv;
288                 RegionSelection::iterator tmp;
289
290                 tmp = i;
291                 ++tmp;
292
293                 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
294                         i = tmp;
295                         continue;
296                 }
297
298                 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
299                         i = tmp;
300                         continue;
301                 }
302
303                 if ((playlist = t->playlist()) == 0) {
304                         i = tmp;
305                         continue;
306                 }
307
308                 if (dialog.request.cancel) {
309                         /* we were cancelled */
310                         dialog.status = 1;
311                         return;
312                 }
313
314                 Filter* fx;
315
316                 if (dialog.pitching) {
317                         fx = new Pitch (*_session, dialog.request);
318                 } else {
319 #ifdef USE_RUBBERBAND
320                         fx = new RBStretch (*_session, dialog.request);
321 #else
322                         fx = new STStretch (*_session, dialog.request);
323 #endif
324                 }
325
326                 if (fx->run (region)) {
327                         dialog.status = -1;
328                         dialog.request.done = true;
329                         delete fx;
330                         return;
331                 }
332
333                 if (!fx->results.empty()) {
334                         new_region = fx->results.front();
335
336                         if (!in_command) {
337                                 _session->begin_reversible_command (dialog.pitching ? _("pitch shift") : _("time stretch"));
338                                 in_command = true;
339                         }
340
341                         playlist->clear_changes ();
342                         playlist->replace_region (region, new_region, region->position());
343                         _session->add_command (new StatefulDiffCommand (playlist));
344                 }
345
346                 i = tmp;
347                 delete fx;
348         }
349
350         if (in_command) {
351                 _session->commit_reversible_command ();
352         }
353
354         dialog.status = 0;
355         dialog.request.done = true;
356 }
357
358 void*
359 Editor::timefx_thread (void *arg)
360 {
361         SessionEvent::create_per_thread_pool ("timefx events", 64);
362
363         TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
364
365         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
366
367         tsd->editor.do_timefx (*tsd);
368
369         /* GACK! HACK! sleep for a bit so that our request buffer for the GUI
370            event loop doesn't die before any changes we made are processed
371            by the GUI ...
372         */
373
374         struct timespec t = { 2, 0 };
375         nanosleep (&t, 0);
376
377         return 0;
378 }
379