Split TimeFXDialog from Editor.
[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
24 #include <string>
25
26 #include "pbd/error.h"
27 #include "pbd/pthread_utils.h"
28 #include "pbd/memento_command.h"
29
30 #include <gtkmm2ext/window_title.h>
31 #include <gtkmm2ext/utils.h>
32
33 #include "audio_region_view.h"
34 #include "audio_time_axis.h"
35 #include "editor.h"
36 #include "region_selection.h"
37 #include "time_fx_dialog.h"
38
39 #include "ardour/session.h"
40 #include "ardour/region.h"
41 #include "ardour/audioplaylist.h"
42 #include "ardour/audio_track.h"
43 #include "ardour/audioregion.h"
44 #include "ardour/audio_diskstream.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 sigc;
60 using namespace Gtk;
61 using namespace Gtkmm2ext;
62
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()->diskstream()->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                 XMLNode &before = playlist->get_state();
86                 playlist->replace_region (regions.front()->region(), stretch.results[0],
87                                 regions.front()->region()->position());
88                 XMLNode &after = playlist->get_state();
89                 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
90                 commit_reversible_command ();
91         }
92
93         return 0;
94 }
95
96 int
97 Editor::pitch_shift (RegionSelection& regions, float fraction)
98 {
99         return time_fx (regions, fraction, true);
100 }
101
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         if (txt == rb_opt_strings[0]) {
176                 transients = NoTransients; peaklock = false; longwin = true; shortwin = false; 
177         } else if (txt == rb_opt_strings[1]) {
178                 transients = NoTransients; peaklock = false; longwin = false; shortwin = false; 
179         } else if (txt == rb_opt_strings[2]) {
180                 transients = NoTransients; peaklock = true; longwin = false; shortwin = false; 
181         } else if (txt == rb_opt_strings[3]) {
182                 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false; 
183         } else if (txt == rb_opt_strings[5]) {
184                 transients = Transients; peaklock = false; longwin = false; shortwin = true; 
185         } else {
186                 /* default/4 */
187
188                 transients = Transients; peaklock = true; longwin = false; shortwin = false; 
189         }
190
191         if (realtime)          options |= RubberBandStretcher::OptionProcessRealTime;
192         if (precise)           options |= RubberBandStretcher::OptionStretchPrecise;
193         if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
194         if (!peaklock)         options |= RubberBandStretcher::OptionPhaseIndependent;
195         if (longwin)           options |= RubberBandStretcher::OptionWindowLong;
196         if (shortwin)          options |= RubberBandStretcher::OptionWindowShort;
197                 
198         switch (transients) {
199         case NoTransients:
200                 options |= RubberBandStretcher::OptionTransientsSmooth;
201                 break;
202         case BandLimitedTransients:
203                 options |= RubberBandStretcher::OptionTransientsMixed;
204                 break;
205         case Transients:
206                 options |= RubberBandStretcher::OptionTransientsCrisp;
207                 break;
208         }
209
210         current_timefx->request.opts = (int) options;
211 #else
212         current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
213         current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
214 #endif
215         current_timefx->request.progress = 0.0f;
216         current_timefx->request.done = false;
217         current_timefx->request.cancel = false;
218         
219         /* re-connect the cancel button and delete events */
220         
221         current_timefx->first_cancel.disconnect();
222         current_timefx->first_delete.disconnect();
223         
224         current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect 
225                 (mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
226         current_timefx->first_delete = current_timefx->signal_delete_event().connect 
227                 (mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
228
229         if (pthread_create_and_store ("timefx", &current_timefx->request.thread, 0, timefx_thread, current_timefx)) {
230                 current_timefx->hide ();
231                 error << _("timefx cannot be started - thread creation error") << endmsg;
232                 return -1;
233         }
234
235         pthread_detach (current_timefx->request.thread);
236
237         sigc::connection c = Glib::signal_timeout().connect (mem_fun (current_timefx, &TimeFXDialog::update_progress), 100);
238
239         while (!current_timefx->request.done && !current_timefx->request.cancel) {
240                 gtk_main_iteration ();
241         }
242
243         c.disconnect ();
244         
245         current_timefx->hide ();
246         return current_timefx->status;
247 }
248
249 void
250 Editor::do_timefx (TimeFXDialog& dialog)
251 {
252         Track*    t;
253         boost::shared_ptr<Playlist> playlist;
254         boost::shared_ptr<Region>   new_region;
255         bool in_command = false;
256         
257         for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
258                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
259
260                 if (!arv) {
261                         continue;
262                 }
263
264                 boost::shared_ptr<AudioRegion> region (arv->audio_region());
265                 TimeAxisView* tv = &(arv->get_time_axis_view());
266                 RouteTimeAxisView* rtv;
267                 RegionSelection::iterator tmp;
268                 
269                 tmp = i;
270                 ++tmp;
271
272                 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
273                         i = tmp;
274                         continue;
275                 }
276
277                 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
278                         i = tmp;
279                         continue;
280                 }
281         
282                 if ((playlist = t->diskstream()->playlist()) == 0) {
283                         i = tmp;
284                         continue;
285                 }
286
287                 if (dialog.request.cancel) {
288                         /* we were cancelled */
289                         dialog.status = 1;
290                         return;
291                 }
292
293                 Filter* fx;
294
295                 if (dialog.pitching) {
296                         fx = new Pitch (*session, dialog.request);
297                 } else {
298 #ifdef USE_RUBBERBAND
299                         fx = new RBStretch (*session, dialog.request);
300 #else
301                         fx = new STStretch (*session, dialog.request);
302 #endif
303                 }
304
305                 if (fx->run (region)) {
306                         dialog.status = -1;
307                         dialog.request.done = true;
308                         delete fx;
309                         return;
310                 }
311
312                 if (!fx->results.empty()) {
313                         new_region = fx->results.front();
314
315                         if (!in_command) {
316                                 begin_reversible_command (dialog.pitching ? _("pitch shift") : _("time stretch"));
317                                 in_command = true;
318                         }
319
320                         XMLNode &before = playlist->get_state();
321                         playlist->replace_region (region, new_region, region->position());
322                         XMLNode &after = playlist->get_state();
323                         session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
324                 }
325
326                 i = tmp;
327                 delete fx;
328         }
329
330         if (in_command) {
331                 commit_reversible_command ();
332         }
333
334         dialog.status = 0;
335         dialog.request.done = true;
336 }
337
338 void*
339 Editor::timefx_thread (void *arg)
340 {
341         PBD::notify_gui_about_thread_creation (pthread_self(), X_("TimeFX"));
342
343         TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
344
345         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
346
347         tsd->editor.do_timefx (*tsd);
348
349         return 0;
350 }
351