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