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 <gtkmm2ext/utils.h>
34 #include "audio_region_view.h"
35 #include "audio_time_axis.h"
37 #include "region_selection.h"
38 #include "time_fx_dialog.h"
40 #include "ardour/audioregion.h"
41 #include "ardour/midi_stretch.h"
42 #include "ardour/pitch.h"
43 #include "ardour/region.h"
44 #include "ardour/session.h"
45 #include "ardour/stretch.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) {
82 set<boost::shared_ptr<Playlist> > midi_playlists_affected;
84 for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
85 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
88 playlist->clear_changes ();
93 ARDOUR::TimeFXRequest request;
94 request.time_fraction = fraction;
96 for (RegionList::iterator i = midi.begin(); i != midi.end(); ++i) {
97 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
103 MidiStretch stretch (*_session, request);
106 playlist->replace_region (regions.front()->region(), stretch.results[0],
107 regions.front()->region()->position());
108 midi_playlists_affected.insert (playlist);
111 for (set<boost::shared_ptr<Playlist> >::iterator p = midi_playlists_affected.begin(); p != midi_playlists_affected.end(); ++p) {
112 _session->add_command (new StatefulDiffCommand (*p));
115 commit_reversible_command ();
121 Editor::pitch_shift (RegionSelection& regions, float fraction)
125 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
126 rl.push_back ((*i)->region());
129 begin_reversible_command (_("pitch shift"));
131 int ret = time_fx (rl, fraction, true);
134 commit_reversible_command ();
140 /** @param val Percentage to time stretch by; ignored if pitch-shifting.
141 * @param pitching true to pitch shift, false to time stretch.
142 * @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
144 Editor::time_fx (RegionList& regions, float val, bool pitching)
146 delete current_timefx;
147 current_timefx = new TimeFXDialog (*this, pitching);
148 current_timefx->regions = regions;
150 /* See if we have any audio regions on our list */
151 RegionList::iterator i = regions.begin ();
152 while (i != regions.end() && boost::dynamic_pointer_cast<AudioRegion> (*i) == 0) {
156 if (i == regions.end ()) {
157 /* No audio regions; we can just do the timefx without a dialogue */
162 switch (current_timefx->run ()) {
163 case RESPONSE_ACCEPT:
166 current_timefx->hide ();
170 current_timefx->status = 0;
174 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
175 float pitch_fraction;
176 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
177 cents += current_timefx->pitch_cent_adjustment.get_value();
180 // user didn't change anything
181 current_timefx->hide ();
185 // one octave == 1200 cents
186 // adding one octave doubles the frequency
187 // ratio is 2^^octaves
189 pitch_fraction = pow(2, cents/1200);
191 current_timefx->request.time_fraction = 1.0;
192 current_timefx->request.pitch_fraction = pitch_fraction;
196 current_timefx->request.time_fraction = val;
197 current_timefx->request.pitch_fraction = 1.0;
201 #ifdef USE_RUBBERBAND
204 RubberBandStretcher::Options options = 0;
206 bool realtime = false;
207 bool precise = false;
208 bool peaklock = true;
209 bool longwin = false;
210 bool shortwin = false;
211 bool preserve_formants = false;
216 BandLimitedTransients,
218 } transients = Transients;
220 precise = current_timefx->precise_button.get_active();
221 preserve_formants = current_timefx->preserve_formants_button.get_active();
223 txt = current_timefx->stretch_opts_selector.get_active_text ();
225 for (int i = 0; i <= 6; i++) {
226 if (txt == rb_opt_strings[i]) {
232 switch (rb_current_opt) {
234 transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
237 transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
240 transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
243 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
246 transients = Transients; peaklock = false; longwin = false; shortwin = true;
249 transients = NoTransients;
251 preserve_formants = false;
252 current_timefx->request.pitch_fraction = 1/val;
258 transients = Transients; peaklock = true; longwin = false; shortwin = false;
262 if (realtime) options |= RubberBandStretcher::OptionProcessRealTime;
263 if (precise) options |= RubberBandStretcher::OptionStretchPrecise;
264 if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
265 if (!peaklock) options |= RubberBandStretcher::OptionPhaseIndependent;
266 if (longwin) options |= RubberBandStretcher::OptionWindowLong;
267 if (shortwin) options |= RubberBandStretcher::OptionWindowShort;
269 switch (transients) {
271 options |= RubberBandStretcher::OptionTransientsSmooth;
273 case BandLimitedTransients:
274 options |= RubberBandStretcher::OptionTransientsMixed;
277 options |= RubberBandStretcher::OptionTransientsCrisp;
281 current_timefx->request.opts = (int) options;
283 current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
284 current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
286 current_timefx->request.done = false;
287 current_timefx->request.cancel = false;
289 /* re-connect the cancel button and delete events */
291 current_timefx->first_cancel.disconnect();
292 current_timefx->first_delete.disconnect();
294 current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
295 (sigc::mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
296 current_timefx->first_delete = current_timefx->signal_delete_event().connect
297 (sigc::mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
299 if (pthread_create_and_store ("timefx", ¤t_timefx->request.thread, timefx_thread, current_timefx)) {
300 current_timefx->hide ();
301 error << _("timefx cannot be started - thread creation error") << endmsg;
305 pthread_detach (current_timefx->request.thread);
307 while (!current_timefx->request.done && !current_timefx->request.cancel) {
308 gtk_main_iteration ();
311 pthread_join (current_timefx->request.thread, 0);
313 current_timefx->hide ();
314 return current_timefx->status;
320 boost::shared_ptr<Playlist> playlist;
321 boost::shared_ptr<Region> new_region;
322 set<boost::shared_ptr<Playlist> > playlists_affected;
324 uint32_t const N = current_timefx->regions.size ();
326 for (RegionList::iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
327 boost::shared_ptr<Playlist> playlist = (*i)->playlist();
330 playlist->clear_changes ();
334 for (RegionList::iterator i = current_timefx->regions.begin(); i != current_timefx->regions.end(); ++i) {
336 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (*i);
338 if (!region || (playlist = region->playlist()) == 0) {
342 if (current_timefx->request.cancel) {
343 /* we were cancelled */
344 /* XXX what to do about playlists already affected ? */
345 current_timefx->status = 1;
351 if (current_timefx->pitching) {
352 fx = new Pitch (*_session, current_timefx->request);
354 #ifdef USE_RUBBERBAND
355 fx = new RBStretch (*_session, current_timefx->request);
357 fx = new STStretch (*_session, current_timefx->request);
361 current_timefx->descend (1.0 / N);
363 if (fx->run (region, current_timefx)) {
364 current_timefx->status = -1;
365 current_timefx->request.done = true;
370 if (!fx->results.empty()) {
371 new_region = fx->results.front();
373 playlist->replace_region (region, new_region, region->position());
374 playlists_affected.insert (playlist);
377 current_timefx->ascend ();
381 for (set<boost::shared_ptr<Playlist> >::iterator p = playlists_affected.begin(); p != playlists_affected.end(); ++p) {
382 _session->add_command (new StatefulDiffCommand (*p));
385 current_timefx->status = 0;
386 current_timefx->request.done = true;
390 Editor::timefx_thread (void *arg)
392 SessionEvent::create_per_thread_pool ("timefx events", 64);
394 TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
396 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
398 tsd->editor.do_timefx ();
400 /* GACK! HACK! sleep for a bit so that our request buffer for the GUI
401 event loop doesn't die before any changes we made are processed
405 #ifdef PLATFORM_WINDOWS
406 Glib::usleep(2 * G_USEC_PER_SEC);
408 struct timespec t = { 2, 0 };