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/session.h"
41 #include "ardour/region.h"
42 #include "ardour/audioplaylist.h"
43 #include "ardour/audio_track.h"
44 #include "ardour/audioregion.h"
45 #include "ardour/stretch.h"
46 #include "ardour/midi_stretch.h"
47 #include "ardour/pitch.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)
66 // FIXME: kludge, implement stretching of selection of both types
68 if (regions.front()->region()->data_type() == DataType::AUDIO) {
69 // Audio, pop up timefx dialog
70 return time_fx (regions, fraction, false);
73 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (®ions.front()->get_time_axis_view());
77 boost::shared_ptr<Playlist> playlist = rtv->track()->playlist();
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_changes ();
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 ();
95 Editor::pitch_shift (RegionSelection& regions, float fraction)
97 return time_fx (regions, fraction, true);
100 /** @param val Percentage to time stretch by; ignored if pitch-shifting.
101 * @param pitching true to pitch shift, false to time stretch.
102 * @return -1 in case of error, 1 if operation was cancelled by the user, 0 if everything went ok */
104 Editor::time_fx (RegionSelection& regions, float val, bool pitching)
106 delete current_timefx;
108 current_timefx = new TimeFXDialog (*this, pitching);
110 switch (current_timefx->run ()) {
111 case RESPONSE_ACCEPT:
114 current_timefx->hide ();
118 current_timefx->status = 0;
119 current_timefx->regions = regions;
123 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
124 float pitch_fraction;
125 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
126 cents += current_timefx->pitch_cent_adjustment.get_value();
129 // user didn't change anything
130 current_timefx->hide ();
134 // one octave == 1200 cents
135 // adding one octave doubles the frequency
136 // ratio is 2^^octaves
138 pitch_fraction = pow(2, cents/1200);
140 current_timefx->request.time_fraction = 1.0;
141 current_timefx->request.pitch_fraction = pitch_fraction;
145 current_timefx->request.time_fraction = val;
146 current_timefx->request.pitch_fraction = 1.0;
150 #ifdef USE_RUBBERBAND
153 RubberBandStretcher::Options options = 0;
155 bool realtime = false;
156 bool precise = false;
157 bool peaklock = true;
158 bool longwin = false;
159 bool shortwin = false;
160 bool preserve_formants = false;
165 BandLimitedTransients,
167 } transients = Transients;
169 precise = current_timefx->precise_button.get_active();
170 preserve_formants = current_timefx->preserve_formants_button.get_active();
172 txt = current_timefx->stretch_opts_selector.get_active_text ();
174 for (int i = 0; i <= 6; i++) {
175 if (txt == rb_opt_strings[i]) {
181 switch (rb_current_opt) {
183 transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
186 transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
189 transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
192 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
195 transients = Transients; peaklock = false; longwin = false; shortwin = true;
198 transients = NoTransients;
200 preserve_formants = false;
201 current_timefx->request.pitch_fraction = 1/val;
207 transients = Transients; peaklock = true; longwin = false; shortwin = false;
211 if (realtime) options |= RubberBandStretcher::OptionProcessRealTime;
212 if (precise) options |= RubberBandStretcher::OptionStretchPrecise;
213 if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
214 if (!peaklock) options |= RubberBandStretcher::OptionPhaseIndependent;
215 if (longwin) options |= RubberBandStretcher::OptionWindowLong;
216 if (shortwin) options |= RubberBandStretcher::OptionWindowShort;
218 switch (transients) {
220 options |= RubberBandStretcher::OptionTransientsSmooth;
222 case BandLimitedTransients:
223 options |= RubberBandStretcher::OptionTransientsMixed;
226 options |= RubberBandStretcher::OptionTransientsCrisp;
230 current_timefx->request.opts = (int) options;
232 current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
233 current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
235 current_timefx->request.done = false;
236 current_timefx->request.cancel = false;
238 /* re-connect the cancel button and delete events */
240 current_timefx->first_cancel.disconnect();
241 current_timefx->first_delete.disconnect();
243 current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
244 (sigc::mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
245 current_timefx->first_delete = current_timefx->signal_delete_event().connect
246 (sigc::mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
248 if (pthread_create_and_store ("timefx", ¤t_timefx->request.thread, timefx_thread, current_timefx)) {
249 current_timefx->hide ();
250 error << _("timefx cannot be started - thread creation error") << endmsg;
254 pthread_detach (current_timefx->request.thread);
256 while (!current_timefx->request.done && !current_timefx->request.cancel) {
257 gtk_main_iteration ();
260 current_timefx->hide ();
261 return current_timefx->status;
265 Editor::do_timefx (TimeFXDialog& dialog)
268 boost::shared_ptr<Playlist> playlist;
269 boost::shared_ptr<Region> new_region;
270 bool in_command = false;
272 uint32_t const N = dialog.regions.size ();
274 for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
275 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
281 boost::shared_ptr<AudioRegion> region (arv->audio_region());
282 TimeAxisView* tv = &(arv->get_time_axis_view());
283 RouteTimeAxisView* rtv;
284 RegionSelection::iterator tmp;
289 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
294 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
299 if ((playlist = t->playlist()) == 0) {
304 if (dialog.request.cancel) {
305 /* we were cancelled */
312 if (dialog.pitching) {
313 fx = new Pitch (*_session, dialog.request);
315 #ifdef USE_RUBBERBAND
316 fx = new RBStretch (*_session, dialog.request);
318 fx = new STStretch (*_session, dialog.request);
322 current_timefx->descend (1.0 / N);
324 if (fx->run (region, current_timefx)) {
326 dialog.request.done = true;
331 if (!fx->results.empty()) {
332 new_region = fx->results.front();
335 _session->begin_reversible_command (dialog.pitching ? _("pitch shift") : _("time stretch"));
339 playlist->clear_changes ();
340 playlist->replace_region (region, new_region, region->position());
341 _session->add_command (new StatefulDiffCommand (playlist));
344 current_timefx->ascend ();
351 _session->commit_reversible_command ();
355 dialog.request.done = true;
359 Editor::timefx_thread (void *arg)
361 SessionEvent::create_per_thread_pool ("timefx events", 64);
363 TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
365 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
367 tsd->editor.do_timefx (*tsd);
369 /* GACK! HACK! sleep for a bit so that our request buffer for the GUI
370 event loop doesn't die before any changes we made are processed
374 struct timespec t = { 2, 0 };