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