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/region_factory.h"
37 #include "ardour/session.h"
38 #include "ardour/stretch.h"
40 #include <gtkmm2ext/utils.h>
42 #include "audio_region_view.h"
43 #include "audio_time_axis.h"
45 #include "editor_regions.h"
46 #include "region_selection.h"
47 #include "time_fx_dialog.h"
50 #include <rubberband/RubberBandStretcher.h>
51 using namespace RubberBand;
57 using namespace ARDOUR;
60 using namespace Gtkmm2ext;
62 /** @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
64 Editor::time_stretch (RegionSelection& regions, float fraction)
69 begin_reversible_command (_("stretch/shrink"));
71 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
72 if ((*i)->region()->data_type() == DataType::AUDIO) {
73 audio.push_back ((*i)->region());
74 } else if ((*i)->region()->data_type() == DataType::MIDI) {
75 midi.push_back ((*i)->region());
79 int aret = time_fx (audio, fraction, false);
81 abort_reversible_command ();
85 set<boost::shared_ptr<Playlist> > midi_playlists_affected;
87 for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
88 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
91 playlist->clear_changes ();
96 ARDOUR::TimeFXRequest request;
97 request.time_fraction = fraction;
99 for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
100 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
106 MidiStretch stretch (*_session, request);
109 playlist->replace_region (regions.front()->region(), stretch.results[0],
110 regions.front()->region()->position());
111 midi_playlists_affected.insert (playlist);
114 for (set<boost::shared_ptr<Playlist> >::iterator p = midi_playlists_affected.begin(); p != midi_playlists_affected.end(); ++p) {
115 PBD::StatefulDiffCommand* cmd = new StatefulDiffCommand (*p);
116 _session->add_command (cmd);
117 if (!cmd->empty ()) {
123 commit_reversible_command ();
125 abort_reversible_command ();
132 Editor::pitch_shift (RegionSelection& regions, float fraction)
136 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
137 rl.push_back ((*i)->region());
140 begin_reversible_command (_("pitch shift"));
142 int ret = time_fx (rl, fraction, true);
145 commit_reversible_command ();
147 abort_reversible_command ();
150 return ret < 0 ? -1 : 0;
153 /** @param val Percentage to time stretch by; ignored if pitch-shifting.
154 * @param pitching true to pitch shift, false to time stretch.
155 * @return -1 in case of error, otherwise number of regions processed */
157 Editor::time_fx (RegionList& regions, float val, bool pitching)
159 delete current_timefx;
161 if (regions.empty()) {
166 const samplecnt_t oldlen = (samplecnt_t) (regions.front()->length());
167 const samplecnt_t newlen = (samplecnt_t) (regions.front()->length() * val);
168 const samplecnt_t pos = regions.front()->position ();
170 current_timefx = new TimeFXDialog (*this, pitching, oldlen, newlen, pos);
171 current_timefx->regions = regions;
173 switch (current_timefx->run ()) {
174 case RESPONSE_ACCEPT:
177 current_timefx->hide ();
181 current_timefx->status = 0;
182 current_timefx->request.time_fraction = current_timefx->get_time_fraction ();
183 current_timefx->request.pitch_fraction = current_timefx->get_pitch_fraction ();
185 if (current_timefx->request.time_fraction == 1.0 &&
186 current_timefx->request.pitch_fraction == 1.0) {
188 current_timefx->hide ();
192 #ifdef USE_RUBBERBAND
195 RubberBandStretcher::Options options = 0;
197 bool realtime = false;
198 bool precise = false;
199 bool peaklock = true;
200 bool longwin = false;
201 bool shortwin = false;
202 bool preserve_formants = false;
207 BandLimitedTransients,
209 } transients = Transients;
211 precise = current_timefx->precise_button.get_active();
212 preserve_formants = current_timefx->preserve_formants_button.get_active();
214 txt = current_timefx->stretch_opts_selector.get_active_text ();
216 for (int i = 0; i <= 6; i++) {
217 if (txt == rb_opt_strings[i]) {
223 int rb_mode = rb_current_opt;
225 if (pitching /*&& rb_current_opt == 6*/) {
226 /* The timefx dialog does not show the "stretch_opts_selector"
227 * when pitch-shifting. So the most recently used option from
228 * "Time Stretch" would be used (if any). That may even be
229 * "resample without preserving pitch", which would be invalid.
231 * TODO: also show stretch_opts_selector when pitching (except the option
232 * to not preserve pitch) and use separate rb_current_opt when pitching.
234 * Actually overhaul this the dialog and processing opts below and use rubberband's
235 * "Crispness" levels:
236 * -c 0 equivalent to --no-transients --no-lamination --window-long
237 * -c 1 equivalent to --detector-soft --no-lamination --window-long (for piano)
238 * -c 2 equivalent to --no-transients --no-lamination
239 * -c 3 equivalent to --no-transients
240 * -c 4 equivalent to --bl-transients
241 * -c 5 default processing options
242 * -c 6 equivalent to --no-lamination --window-short (may be good for drums)
249 transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
252 transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
255 transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
258 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
261 transients = Transients; peaklock = false; longwin = false; shortwin = true;
264 transients = NoTransients;
266 preserve_formants = false;
267 current_timefx->request.pitch_fraction = 1.0 / current_timefx->request.time_fraction;
273 transients = Transients; peaklock = true; longwin = false; shortwin = false;
277 if (realtime) options |= RubberBandStretcher::OptionProcessRealTime;
278 if (precise) options |= RubberBandStretcher::OptionStretchPrecise;
279 if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
280 if (!peaklock) options |= RubberBandStretcher::OptionPhaseIndependent;
281 if (longwin) options |= RubberBandStretcher::OptionWindowLong;
282 if (shortwin) options |= RubberBandStretcher::OptionWindowShort;
284 if (pitching) options |= RubberBandStretcher::OptionPitchHighQuality;
286 switch (transients) {
288 options |= RubberBandStretcher::OptionTransientsSmooth;
290 case BandLimitedTransients:
291 options |= RubberBandStretcher::OptionTransientsMixed;
294 options |= RubberBandStretcher::OptionTransientsCrisp;
298 current_timefx->request.opts = (int) options;
300 current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
301 current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
303 current_timefx->request.done = false;
304 current_timefx->request.cancel = false;
306 /* re-connect the cancel button and delete events */
308 current_timefx->first_cancel.disconnect();
309 current_timefx->first_delete.disconnect();
311 current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
312 (sigc::mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
313 current_timefx->first_delete = current_timefx->signal_delete_event().connect
314 (sigc::mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
317 if (pthread_create_and_store ("timefx", ¤t_timefx->request.thread, timefx_thread, current_timefx)) {
318 current_timefx->hide ();
319 error << _("timefx cannot be started - thread creation error") << endmsg;
323 current_timefx->start_updates ();
325 while (!current_timefx->request.done && !current_timefx->request.cancel) {
326 gtk_main_iteration ();
329 pthread_join (current_timefx->request.thread, 0);
331 current_timefx->hide ();
333 if (current_timefx->status < 0) {
334 /* processing was cancelled, some regions may have
335 * been created and removed via RegionFactory::map_remove()
336 * The region-list does not update itself when a region is removed.
338 _regions->redisplay ();
340 return current_timefx->status;
346 typedef std::map<boost::shared_ptr<Region>, boost::shared_ptr<Region> > ResultMap;
349 uint32_t const N = current_timefx->regions.size ();
351 for (RegionList::const_iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
352 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
354 playlist->clear_changes ();
358 for (RegionList::const_iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
360 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (*i);
361 boost::shared_ptr<Playlist> playlist;
363 if (!region || (playlist = region->playlist()) == 0) {
367 if (current_timefx->request.cancel) {
373 if (current_timefx->pitching) {
374 fx = new Pitch (*_session, current_timefx->request);
376 #ifdef USE_RUBBERBAND
377 fx = new RBStretch (*_session, current_timefx->request);
379 fx = new STStretch (*_session, current_timefx->request);
383 current_timefx->descend (1.0 / N);
385 if (fx->run (region, current_timefx)) {
386 current_timefx->request.cancel = true;
391 if (!fx->results.empty()) {
392 results[region] = fx->results.front();
395 current_timefx->ascend ();
399 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
400 if (current_timefx->request.cancel) {
401 current_timefx->status = -1;
402 for (ResultMap::const_iterator i = results.begin(); i != results.end(); ++i) {
403 boost::weak_ptr<Region> w = i->second;
404 RegionFactory::map_remove (w);
407 current_timefx->status = 0;
408 for (ResultMap::const_iterator i = results.begin(); i != results.end(); ++i) {
409 boost::shared_ptr<Region> region = i->first;
410 boost::shared_ptr<Region> new_region = i->second;
411 boost::shared_ptr<Playlist> playlist = region->playlist();
412 playlist->replace_region (region, new_region, region->position());
414 PBD::StatefulDiffCommand* cmd = new StatefulDiffCommand (playlist);
415 _session->add_command (cmd);
416 if (!cmd->empty ()) {
417 ++current_timefx->status;
421 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
423 current_timefx->request.done = true;
427 Editor::timefx_thread (void *arg)
429 SessionEvent::create_per_thread_pool ("timefx events", 64);
431 TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
433 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
435 tsd->editor.do_timefx ();
437 /* GACK! HACK! sleep for a bit so that our request buffer for the GUI
438 event loop doesn't die before any changes we made are processed
441 Glib::usleep(G_USEC_PER_SEC / 5);