2 * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2006 Hans Fugal <hans@fugal.net>
5 * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2008-2012 David Robillard <d@drobilla.net>
7 * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
8 * Copyright (C) 2015 Nick Mainsbridge <mainsbridge@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include "pbd/error.h"
33 #include "pbd/pthread_utils.h"
34 #include "pbd/memento_command.h"
35 #include "pbd/stateful_diff_command.h"
37 #include "ardour/audioregion.h"
38 #include "ardour/midi_stretch.h"
39 #include "ardour/pitch.h"
40 #include "ardour/region.h"
41 #include "ardour/region_factory.h"
42 #include "ardour/session.h"
43 #include "ardour/stretch.h"
45 #include <gtkmm2ext/utils.h>
47 #include "audio_region_view.h"
48 #include "audio_time_axis.h"
50 #include "editor_regions.h"
51 #include "region_selection.h"
52 #include "time_fx_dialog.h"
55 #include <rubberband/RubberBandStretcher.h>
56 using namespace RubberBand;
62 using namespace ARDOUR;
65 using namespace Gtkmm2ext;
67 /** @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
69 Editor::time_stretch (RegionSelection& regions, float fraction)
74 begin_reversible_command (_("stretch/shrink"));
76 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
77 if ((*i)->region()->data_type() == DataType::AUDIO) {
78 audio.push_back ((*i)->region());
79 } else if ((*i)->region()->data_type() == DataType::MIDI) {
80 midi.push_back ((*i)->region());
84 int aret = time_fx (audio, fraction, false);
86 abort_reversible_command ();
90 set<boost::shared_ptr<Playlist> > midi_playlists_affected;
92 for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
93 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
96 playlist->clear_changes ();
101 ARDOUR::TimeFXRequest request;
102 request.time_fraction = fraction;
104 for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
105 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
111 MidiStretch stretch (*_session, request);
114 playlist->replace_region (regions.front()->region(), stretch.results[0],
115 regions.front()->region()->position());
116 midi_playlists_affected.insert (playlist);
119 for (set<boost::shared_ptr<Playlist> >::iterator p = midi_playlists_affected.begin(); p != midi_playlists_affected.end(); ++p) {
120 PBD::StatefulDiffCommand* cmd = new StatefulDiffCommand (*p);
121 _session->add_command (cmd);
122 if (!cmd->empty ()) {
128 commit_reversible_command ();
130 abort_reversible_command ();
137 Editor::pitch_shift (RegionSelection& regions, float fraction)
141 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
142 rl.push_back ((*i)->region());
145 begin_reversible_command (_("pitch shift"));
147 int ret = time_fx (rl, fraction, true);
150 commit_reversible_command ();
152 abort_reversible_command ();
155 return ret < 0 ? -1 : 0;
158 /** @param val Percentage to time stretch by; ignored if pitch-shifting.
159 * @param pitching true to pitch shift, false to time stretch.
160 * @return -1 in case of error, otherwise number of regions processed */
162 Editor::time_fx (RegionList& regions, float val, bool pitching)
164 delete current_timefx;
166 if (regions.empty()) {
171 const samplecnt_t oldlen = (samplecnt_t) (regions.front()->length());
172 const samplecnt_t newlen = (samplecnt_t) (regions.front()->length() * val);
173 const samplecnt_t pos = regions.front()->position ();
175 current_timefx = new TimeFXDialog (*this, pitching, oldlen, newlen, pos);
176 current_timefx->regions = regions;
178 switch (current_timefx->run ()) {
179 case RESPONSE_ACCEPT:
182 current_timefx->hide ();
186 current_timefx->status = 0;
187 current_timefx->request.time_fraction = current_timefx->get_time_fraction ();
188 current_timefx->request.pitch_fraction = current_timefx->get_pitch_fraction ();
190 if (current_timefx->request.time_fraction == 1.0 &&
191 current_timefx->request.pitch_fraction == 1.0) {
193 current_timefx->hide ();
197 #ifdef USE_RUBBERBAND
200 RubberBandStretcher::Options options = 0;
202 bool realtime = false;
203 bool precise = false;
204 bool peaklock = true;
205 bool longwin = false;
206 bool shortwin = false;
207 bool preserve_formants = false;
212 BandLimitedTransients,
214 } transients = Transients;
216 precise = current_timefx->precise_button.get_active();
217 preserve_formants = current_timefx->preserve_formants_button.get_active();
219 txt = current_timefx->stretch_opts_selector.get_active_text ();
221 for (int i = 0; i <= 6; i++) {
222 if (txt == rb_opt_strings[i]) {
228 int rb_mode = rb_current_opt;
230 if (pitching /*&& rb_current_opt == 6*/) {
231 /* The timefx dialog does not show the "stretch_opts_selector"
232 * when pitch-shifting. So the most recently used option from
233 * "Time Stretch" would be used (if any). That may even be
234 * "resample without preserving pitch", which would be invalid.
236 * TODO: also show stretch_opts_selector when pitching (except the option
237 * to not preserve pitch) and use separate rb_current_opt when pitching.
239 * Actually overhaul this the dialog and processing opts below and use rubberband's
240 * "Crispness" levels:
241 * -c 0 equivalent to --no-transients --no-lamination --window-long
242 * -c 1 equivalent to --detector-soft --no-lamination --window-long (for piano)
243 * -c 2 equivalent to --no-transients --no-lamination
244 * -c 3 equivalent to --no-transients
245 * -c 4 equivalent to --bl-transients
246 * -c 5 default processing options
247 * -c 6 equivalent to --no-lamination --window-short (may be good for drums)
254 transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
257 transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
260 transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
263 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
266 transients = Transients; peaklock = false; longwin = false; shortwin = true;
269 transients = NoTransients;
271 preserve_formants = false;
272 current_timefx->request.pitch_fraction = 1.0 / current_timefx->request.time_fraction;
278 transients = Transients; peaklock = true; longwin = false; shortwin = false;
282 if (realtime) options |= RubberBandStretcher::OptionProcessRealTime;
283 if (precise) options |= RubberBandStretcher::OptionStretchPrecise;
284 if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
285 if (!peaklock) options |= RubberBandStretcher::OptionPhaseIndependent;
286 if (longwin) options |= RubberBandStretcher::OptionWindowLong;
287 if (shortwin) options |= RubberBandStretcher::OptionWindowShort;
289 if (pitching) options |= RubberBandStretcher::OptionPitchHighQuality;
291 switch (transients) {
293 options |= RubberBandStretcher::OptionTransientsSmooth;
295 case BandLimitedTransients:
296 options |= RubberBandStretcher::OptionTransientsMixed;
299 options |= RubberBandStretcher::OptionTransientsCrisp;
303 current_timefx->request.opts = (int) options;
305 current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
306 current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
308 current_timefx->request.done = false;
309 current_timefx->request.cancel = false;
311 /* re-connect the cancel button and delete events */
313 current_timefx->first_cancel.disconnect();
314 current_timefx->first_delete.disconnect();
316 current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
317 (sigc::mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
318 current_timefx->first_delete = current_timefx->signal_delete_event().connect
319 (sigc::mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
322 if (pthread_create_and_store ("timefx", ¤t_timefx->request.thread, timefx_thread, current_timefx)) {
323 current_timefx->hide ();
324 error << _("timefx cannot be started - thread creation error") << endmsg;
328 current_timefx->start_updates ();
330 while (!current_timefx->request.done && !current_timefx->request.cancel) {
331 gtk_main_iteration ();
334 pthread_join (current_timefx->request.thread, 0);
336 current_timefx->hide ();
338 if (current_timefx->status < 0) {
339 /* processing was cancelled, some regions may have
340 * been created and removed via RegionFactory::map_remove()
341 * The region-list does not update itself when a region is removed.
343 _regions->redisplay ();
345 return current_timefx->status;
351 typedef std::map<boost::shared_ptr<Region>, boost::shared_ptr<Region> > ResultMap;
354 uint32_t const N = current_timefx->regions.size ();
356 for (RegionList::const_iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
357 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
359 playlist->clear_changes ();
363 for (RegionList::const_iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
365 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (*i);
366 boost::shared_ptr<Playlist> playlist;
368 if (!region || (playlist = region->playlist()) == 0) {
372 if (current_timefx->request.cancel) {
378 if (current_timefx->pitching) {
379 fx = new Pitch (*_session, current_timefx->request);
381 #ifdef USE_RUBBERBAND
382 fx = new RBStretch (*_session, current_timefx->request);
384 fx = new STStretch (*_session, current_timefx->request);
388 current_timefx->descend (1.0 / N);
390 if (fx->run (region, current_timefx)) {
391 current_timefx->request.cancel = true;
396 if (!fx->results.empty()) {
397 results[region] = fx->results.front();
400 current_timefx->ascend ();
404 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
405 if (current_timefx->request.cancel) {
406 current_timefx->status = -1;
407 for (ResultMap::const_iterator i = results.begin(); i != results.end(); ++i) {
408 boost::weak_ptr<Region> w = i->second;
409 RegionFactory::map_remove (w);
412 current_timefx->status = 0;
413 for (ResultMap::const_iterator i = results.begin(); i != results.end(); ++i) {
414 boost::shared_ptr<Region> region = i->first;
415 boost::shared_ptr<Region> new_region = i->second;
416 boost::shared_ptr<Playlist> playlist = region->playlist();
417 playlist->replace_region (region, new_region, region->position());
419 PBD::StatefulDiffCommand* cmd = new StatefulDiffCommand (playlist);
420 _session->add_command (cmd);
421 if (!cmd->empty ()) {
422 ++current_timefx->status;
426 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
428 current_timefx->request.done = true;
432 Editor::timefx_thread (void *arg)
434 SessionEvent::create_per_thread_pool ("timefx events", 64);
436 TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
438 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
440 tsd->editor.do_timefx ();
442 /* GACK! HACK! sleep for a bit so that our request buffer for the GUI
443 event loop doesn't die before any changes we made are processed
446 Glib::usleep(G_USEC_PER_SEC / 5);