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.
26 #include <pbd/error.h>
27 #include <pbd/pthread_utils.h>
28 #include <pbd/memento_command.h>
30 #include <gtkmm2ext/window_title.h>
31 #include <gtkmm2ext/utils.h>
34 #include "audio_time_axis.h"
35 #include "audio_region_view.h"
36 #include "region_selection.h"
38 #include <ardour/session.h>
39 #include <ardour/region.h>
40 #include <ardour/audioplaylist.h>
41 #include <ardour/audio_track.h>
42 #include <ardour/audioregion.h>
43 #include <ardour/audio_diskstream.h>
44 #include <ardour/stretch.h>
45 #include <ardour/midi_stretch.h>
46 #include <ardour/pitch.h>
49 #include <rubberband/RubberBandStretcher.h>
50 using namespace RubberBand;
56 using namespace ARDOUR;
60 using namespace Gtkmm2ext;
62 Editor::TimeFXDialog::TimeFXDialog (Editor& e, bool pitch)
63 : ArdourDialog (X_("time fx dialog")),
66 pitch_octave_adjustment (0.0, -4.0, 4.0, 1, 2.0),
67 pitch_semitone_adjustment (0.0, -12.0, 12.0, 1.0, 4.0),
68 pitch_cent_adjustment (0.0, -499.0, 500.0, 5.0, 15.0),
69 pitch_octave_spinner (pitch_octave_adjustment),
70 pitch_semitone_spinner (pitch_semitone_adjustment),
71 pitch_cent_spinner (pitch_cent_adjustment),
72 quick_button (_("Quick but Ugly")),
73 antialias_button (_("Skip Anti-aliasing")),
74 stretch_opts_label (_("Contents:")),
75 precise_button (_("Strict Linear")),
76 preserve_formants_button(_("Preserve Formants"))
79 set_position (Gtk::WIN_POS_MOUSE);
80 set_name (N_("TimeFXDialog"));
82 WindowTitle title(Glib::get_application_name());
84 title += _("Pitch Shift");
86 title += _("Time Stretch");
88 set_title(title.get_string());
90 cancel_button = add_button (_("Cancel"), Gtk::RESPONSE_CANCEL);
92 get_vbox()->set_spacing (5);
93 get_vbox()->set_border_width (12);
97 upper_button_box.set_spacing (5);
98 upper_button_box.set_border_width (5);
102 l = manage (new Label (_("Octaves")));
103 upper_button_box.pack_start (*l, false, false);
104 upper_button_box.pack_start (pitch_octave_spinner, false, false);
106 l = manage (new Label (_("Semitones (12TET)")));
107 upper_button_box.pack_start (*l, false, false);
108 upper_button_box.pack_start (pitch_semitone_spinner, false, false);
110 l = manage (new Label (_("Cents")));
111 upper_button_box.pack_start (*l, false, false);
112 upper_button_box.pack_start (pitch_cent_spinner, false, false);
114 pitch_cent_spinner.set_digits (1);
116 upper_button_box.pack_start (preserve_formants_button, false, false);
119 add_button (_("Shift"), Gtk::RESPONSE_ACCEPT);
121 get_vbox()->pack_start (upper_button_box, false, false);
125 #ifdef USE_RUBBERBAND
126 opts_box.set_spacing (5);
127 opts_box.set_border_width (5);
128 vector<string> strings;
130 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
132 stretch_opts_selector.set_active_text (editor.rb_opt_strings[4]);
134 opts_box.pack_start (precise_button, false, false);
135 opts_box.pack_start (stretch_opts_label, false, false);
136 opts_box.pack_start (stretch_opts_selector, false, false);
138 get_vbox()->pack_start (opts_box, false, false);
141 upper_button_box.set_homogeneous (true);
142 upper_button_box.set_spacing (5);
143 upper_button_box.set_border_width (5);
145 upper_button_box.pack_start (quick_button, true, true);
146 upper_button_box.pack_start (antialias_button, true, true);
148 quick_button.set_name (N_("TimeFXButton"));
149 antialias_button.set_name (N_("TimeFXButton"));
151 get_vbox()->pack_start (upper_button_box, false, false);
154 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
157 get_vbox()->pack_start (progress_bar);
159 progress_bar.set_name (N_("TimeFXProgress"));
161 show_all_children ();
165 Editor::TimeFXDialog::update_progress ()
167 progress_bar.set_fraction (request.progress);
168 return !request.done;
172 Editor::TimeFXDialog::cancel_in_progress ()
175 request.cancel = true;
176 first_cancel.disconnect();
180 Editor::TimeFXDialog::delete_in_progress (GdkEventAny* ev)
183 request.cancel = true;
184 first_delete.disconnect();
189 Editor::time_stretch (RegionSelection& regions, float fraction)
191 // FIXME: kludge, implement stretching of selection of both types
193 if (regions.front()->region()->data_type() == DataType::AUDIO) {
194 // Audio, pop up timefx dialog
195 return time_fx (regions, fraction, false);
197 // MIDI, just stretch
198 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (®ions.front()->get_time_axis_view());
202 boost::shared_ptr<Playlist> playlist
203 = rtv->track()->diskstream()->playlist();
205 ARDOUR::TimeFXRequest request;
206 request.time_fraction = fraction;
207 MidiStretch stretch(*session, request);
208 begin_reversible_command ("midi stretch");
209 stretch.run(regions.front()->region());
210 XMLNode &before = playlist->get_state();
211 playlist->replace_region (regions.front()->region(), stretch.results[0],
212 regions.front()->region()->position());
213 XMLNode &after = playlist->get_state();
214 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
215 commit_reversible_command ();
222 Editor::pitch_shift (RegionSelection& regions, float fraction)
224 return time_fx (regions, fraction, true);
228 Editor::time_fx (RegionSelection& regions, float val, bool pitching)
230 if (current_timefx != 0) {
231 delete current_timefx;
234 current_timefx = new TimeFXDialog (*this, pitching);
236 current_timefx->progress_bar.set_fraction (0.0f);
238 switch (current_timefx->run ()) {
239 case RESPONSE_ACCEPT:
242 current_timefx->hide ();
246 current_timefx->status = 0;
247 current_timefx->regions = regions;
251 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
252 float pitch_fraction;
253 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
254 cents += current_timefx->pitch_cent_adjustment.get_value();
257 // user didn't change anything
258 current_timefx->hide ();
262 // one octave == 1200 cents
263 // adding one octave doubles the frequency
264 // ratio is 2^^octaves
266 pitch_fraction = pow(2, cents/1200);
268 current_timefx->request.time_fraction = 1.0;
269 current_timefx->request.pitch_fraction = pitch_fraction;
273 current_timefx->request.time_fraction = val;
274 current_timefx->request.pitch_fraction = 1.0;
278 #ifdef USE_RUBBERBAND
281 RubberBandStretcher::Options options = 0;
283 bool realtime = false;
284 bool precise = false;
285 bool peaklock = true;
286 bool longwin = false;
287 bool shortwin = false;
288 bool preserve_formants = false;
293 BandLimitedTransients,
295 } transients = Transients;
297 precise = current_timefx->precise_button.get_active();
298 preserve_formants = current_timefx->preserve_formants_button.get_active();
300 txt = current_timefx->stretch_opts_selector.get_active_text ();
302 if (txt == rb_opt_strings[0]) {
303 transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
304 } else if (txt == rb_opt_strings[1]) {
305 transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
306 } else if (txt == rb_opt_strings[2]) {
307 transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
308 } else if (txt == rb_opt_strings[3]) {
309 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
310 } else if (txt == rb_opt_strings[5]) {
311 transients = Transients; peaklock = false; longwin = false; shortwin = true;
315 transients = Transients; peaklock = true; longwin = false; shortwin = false;
319 if (realtime) options |= RubberBandStretcher::OptionProcessRealTime;
320 if (precise) options |= RubberBandStretcher::OptionStretchPrecise;
321 if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
323 if (!peaklock) options |= RubberBandStretcher::OptionPhaseIndependent;
324 if (longwin) options |= RubberBandStretcher::OptionWindowLong;
325 if (shortwin) options |= RubberBandStretcher::OptionWindowShort;
329 switch (transients) {
331 options |= RubberBandStretcher::OptionTransientsSmooth;
333 case BandLimitedTransients:
334 options |= RubberBandStretcher::OptionTransientsMixed;
337 options |= RubberBandStretcher::OptionTransientsCrisp;
341 current_timefx->request.opts = (int) options;
343 current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
344 current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
346 current_timefx->request.progress = 0.0f;
347 current_timefx->request.done = false;
348 current_timefx->request.cancel = false;
350 /* re-connect the cancel button and delete events */
352 current_timefx->first_cancel.disconnect();
353 current_timefx->first_delete.disconnect();
355 current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
356 (mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
357 current_timefx->first_delete = current_timefx->signal_delete_event().connect
358 (mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
360 if (pthread_create_and_store ("timefx", ¤t_timefx->request.thread, 0, timefx_thread, current_timefx)) {
361 current_timefx->hide ();
362 error << _("timefx cannot be started - thread creation error") << endmsg;
366 pthread_detach (current_timefx->request.thread);
368 sigc::connection c = Glib::signal_timeout().connect (mem_fun (current_timefx, &TimeFXDialog::update_progress), 100);
370 while (!current_timefx->request.done && !current_timefx->request.cancel) {
371 gtk_main_iteration ();
376 current_timefx->hide ();
377 return current_timefx->status;
381 Editor::do_timefx (TimeFXDialog& dialog)
384 boost::shared_ptr<Playlist> playlist;
385 boost::shared_ptr<Region> new_region;
386 bool in_command = false;
388 for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
389 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
395 boost::shared_ptr<AudioRegion> region (arv->audio_region());
396 TimeAxisView* tv = &(arv->get_time_axis_view());
397 RouteTimeAxisView* rtv;
398 RegionSelection::iterator tmp;
403 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
408 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
413 if ((playlist = t->diskstream()->playlist()) == 0) {
418 if (dialog.request.cancel) {
419 /* we were cancelled */
426 if (dialog.pitching) {
427 fx = new Pitch (*session, dialog.request);
429 fx = new Stretch (*session, dialog.request);
432 if (fx->run (region)) {
434 dialog.request.done = true;
439 if (!fx->results.empty()) {
440 new_region = fx->results.front();
443 begin_reversible_command (dialog.pitching ? _("pitch shift") : _("time stretch"));
447 XMLNode &before = playlist->get_state();
448 playlist->replace_region (region, new_region, region->position());
449 XMLNode &after = playlist->get_state();
450 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
458 commit_reversible_command ();
462 dialog.request.done = true;
466 Editor::timefx_thread (void *arg)
468 PBD::notify_gui_about_thread_creation (pthread_self(), X_("TimeFX"));
470 TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
472 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
474 tsd->editor.do_timefx (*tsd);