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