2 Copyright (C) 2000 Paul Davis
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.
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.
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.
27 #include "pbd/error.h"
28 #include "pbd/pthread_utils.h"
29 #include "pbd/memento_command.h"
30 #include "pbd/stateful_diff_command.h"
32 #include "ardour/audioregion.h"
33 #include "ardour/midi_stretch.h"
34 #include "ardour/pitch.h"
35 #include "ardour/region.h"
36 #include "ardour/session.h"
37 #include "ardour/stretch.h"
39 #include <gtkmm2ext/utils.h>
41 #include "audio_region_view.h"
42 #include "audio_time_axis.h"
44 #include "region_selection.h"
45 #include "time_fx_dialog.h"
48 #include <rubberband/RubberBandStretcher.h>
49 using namespace RubberBand;
55 using namespace ARDOUR;
58 using namespace Gtkmm2ext;
60 /** @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
62 Editor::time_stretch (RegionSelection& regions, float fraction)
68 begin_reversible_command (_("stretch/shrink"));
70 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
71 if ((*i)->region()->data_type() == DataType::AUDIO) {
72 audio.push_back ((*i)->region());
73 } else if ((*i)->region()->data_type() == DataType::MIDI) {
74 midi.push_back ((*i)->region());
78 if ((aret = time_fx (audio, fraction, false)) != 0) {
79 commit_reversible_command ();
83 set<boost::shared_ptr<Playlist> > midi_playlists_affected;
85 for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
86 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
89 playlist->clear_changes ();
94 ARDOUR::TimeFXRequest request;
95 request.time_fraction = fraction;
97 for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
98 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
104 MidiStretch stretch (*_session, request);
107 playlist->replace_region (regions.front()->region(), stretch.results[0],
108 regions.front()->region()->position());
109 midi_playlists_affected.insert (playlist);
112 for (set<boost::shared_ptr<Playlist> >::iterator p = midi_playlists_affected.begin(); p != midi_playlists_affected.end(); ++p) {
113 _session->add_command (new StatefulDiffCommand (*p));
116 commit_reversible_command ();
122 Editor::pitch_shift (RegionSelection& regions, float fraction)
126 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
127 rl.push_back ((*i)->region());
130 begin_reversible_command (_("pitch shift"));
132 int ret = time_fx (rl, fraction, true);
135 commit_reversible_command ();
137 abort_reversible_command ();
143 /** @param val Percentage to time stretch by; ignored if pitch-shifting.
144 * @param pitching true to pitch shift, false to time stretch.
145 * @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
147 Editor::time_fx (RegionList& regions, float val, bool pitching)
149 if (regions.empty()) {
153 const framecnt_t oldlen = (framecnt_t) (regions.front()->length());
154 const framecnt_t newlen = (framecnt_t) (regions.front()->length() * val);
155 const framecnt_t pos = regions.front()->position ();
157 delete current_timefx;
158 current_timefx = new TimeFXDialog (*this, pitching, oldlen, newlen, pos);
159 current_timefx->regions = regions;
161 switch (current_timefx->run ()) {
162 case RESPONSE_ACCEPT:
165 current_timefx->hide ();
169 current_timefx->status = 0;
170 current_timefx->request.time_fraction = current_timefx->get_time_fraction ();
171 current_timefx->request.pitch_fraction = current_timefx->get_pitch_fraction ();
173 if (current_timefx->request.time_fraction == 1.0 &&
174 current_timefx->request.pitch_fraction == 1.0) {
176 current_timefx->hide ();
180 #ifdef USE_RUBBERBAND
183 RubberBandStretcher::Options options = 0;
185 bool realtime = false;
186 bool precise = false;
187 bool peaklock = true;
188 bool longwin = false;
189 bool shortwin = false;
190 bool preserve_formants = false;
195 BandLimitedTransients,
197 } transients = Transients;
199 precise = current_timefx->precise_button.get_active();
200 preserve_formants = current_timefx->preserve_formants_button.get_active();
202 txt = current_timefx->stretch_opts_selector.get_active_text ();
204 for (int i = 0; i <= 6; i++) {
205 if (txt == rb_opt_strings[i]) {
211 switch (rb_current_opt) {
213 transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
216 transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
219 transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
222 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
225 transients = Transients; peaklock = false; longwin = false; shortwin = true;
228 transients = NoTransients;
230 preserve_formants = false;
231 current_timefx->request.pitch_fraction = 1/val;
237 transients = Transients; peaklock = true; longwin = false; shortwin = false;
241 if (realtime) options |= RubberBandStretcher::OptionProcessRealTime;
242 if (precise) options |= RubberBandStretcher::OptionStretchPrecise;
243 if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
244 if (!peaklock) options |= RubberBandStretcher::OptionPhaseIndependent;
245 if (longwin) options |= RubberBandStretcher::OptionWindowLong;
246 if (shortwin) options |= RubberBandStretcher::OptionWindowShort;
248 switch (transients) {
250 options |= RubberBandStretcher::OptionTransientsSmooth;
252 case BandLimitedTransients:
253 options |= RubberBandStretcher::OptionTransientsMixed;
256 options |= RubberBandStretcher::OptionTransientsCrisp;
260 current_timefx->request.opts = (int) options;
262 current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
263 current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
265 current_timefx->request.done = false;
266 current_timefx->request.cancel = false;
268 /* re-connect the cancel button and delete events */
270 current_timefx->first_cancel.disconnect();
271 current_timefx->first_delete.disconnect();
273 current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
274 (sigc::mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
275 current_timefx->first_delete = current_timefx->signal_delete_event().connect
276 (sigc::mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
278 current_timefx->start_updates ();
280 if (pthread_create_and_store ("timefx", ¤t_timefx->request.thread, timefx_thread, current_timefx)) {
281 current_timefx->hide ();
282 error << _("timefx cannot be started - thread creation error") << endmsg;
286 pthread_detach (current_timefx->request.thread);
288 while (!current_timefx->request.done && !current_timefx->request.cancel) {
289 gtk_main_iteration ();
292 pthread_join (current_timefx->request.thread, 0);
294 current_timefx->hide ();
295 return current_timefx->status;
301 boost::shared_ptr<Playlist> playlist;
302 boost::shared_ptr<Region> new_region;
303 set<boost::shared_ptr<Playlist> > playlists_affected;
305 uint32_t const N = current_timefx->regions.size ();
307 for (RegionList::iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
308 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
311 playlist->clear_changes ();
315 for (RegionList::iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
317 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (*i);
319 if (!region || (playlist = region->playlist()) == 0) {
323 if (current_timefx->request.cancel) {
324 /* we were cancelled */
325 /* XXX what to do about playlists already affected ? */
326 current_timefx->status = 1;
332 if (current_timefx->pitching) {
333 fx = new Pitch (*_session, current_timefx->request);
335 #ifdef USE_RUBBERBAND
336 fx = new RBStretch (*_session, current_timefx->request);
338 fx = new STStretch (*_session, current_timefx->request);
342 current_timefx->descend (1.0 / N);
344 if (fx->run (region, current_timefx)) {
345 current_timefx->status = -1;
346 current_timefx->request.done = true;
351 if (!fx->results.empty()) {
352 new_region = fx->results.front();
354 playlist->replace_region (region, new_region, region->position());
355 playlists_affected.insert (playlist);
358 current_timefx->ascend ();
362 for (set<boost::shared_ptr<Playlist> >::iterator p = playlists_affected.begin(); p != playlists_affected.end(); ++p) {
363 _session->add_command (new StatefulDiffCommand (*p));
366 current_timefx->status = 0;
367 current_timefx->request.done = true;
371 Editor::timefx_thread (void *arg)
373 SessionEvent::create_per_thread_pool ("timefx events", 64);
375 TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
377 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
379 tsd->editor.do_timefx ();
381 /* GACK! HACK! sleep for a bit so that our request buffer for the GUI
382 event loop doesn't die before any changes we made are processed
386 #ifdef PLATFORM_WINDOWS
387 Glib::usleep(2 * G_USEC_PER_SEC);
389 struct timespec t = { 2, 0 };