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/pitch.h>
48 #include <rubberband/RubberBandStretcher.h>
49 using namespace RubberBand;
55 using namespace ARDOUR;
59 using namespace Gtkmm2ext;
61 Editor::TimeFXDialog::TimeFXDialog (Editor& e, bool pitch)
62 : ArdourDialog (X_("time fx dialog")),
65 pitch_octave_adjustment (0.0, -4.0, 4.0, 1, 2.0),
66 pitch_semitone_adjustment (0.0, -12.0, 12.0, 1.0, 4.0),
67 pitch_cent_adjustment (0.0, -499.0, 500.0, 5.0, 15.0),
68 pitch_octave_spinner (pitch_octave_adjustment),
69 pitch_semitone_spinner (pitch_semitone_adjustment),
70 pitch_cent_spinner (pitch_cent_adjustment),
71 quick_button (_("Quick but Ugly")),
72 antialias_button (_("Skip Anti-aliasing")),
73 stretch_opts_label (_("Contents:")),
74 precise_button (_("Strict Linear"))
77 set_position (Gtk::WIN_POS_MOUSE);
78 set_name (N_("TimeFXDialog"));
80 WindowTitle title(Glib::get_application_name());
82 title += _("Pitch Shift");
84 title += _("Time Stretch");
86 set_title(title.get_string());
88 cancel_button = add_button (_("Cancel"), Gtk::RESPONSE_CANCEL);
90 get_vbox()->set_spacing (5);
91 get_vbox()->set_border_width (12);
95 upper_button_box.set_spacing (5);
96 upper_button_box.set_border_width (5);
100 l = manage (new Label (_("Octaves")));
101 upper_button_box.pack_start (*l, false, false);
102 upper_button_box.pack_start (pitch_octave_spinner, false, false);
104 l = manage (new Label (_("Semitones (12TET)")));
105 upper_button_box.pack_start (*l, false, false);
106 upper_button_box.pack_start (pitch_semitone_spinner, false, false);
108 l = manage (new Label (_("Cents")));
109 upper_button_box.pack_start (*l, false, false);
110 upper_button_box.pack_start (pitch_cent_spinner, false, false);
112 pitch_cent_spinner.set_digits (1);
114 add_button (_("Shift"), Gtk::RESPONSE_ACCEPT);
116 get_vbox()->pack_start (upper_button_box, false, false);
120 #ifdef USE_RUBBERBAND
121 opts_box.set_spacing (5);
122 opts_box.set_border_width (5);
123 vector<string> strings;
125 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
127 stretch_opts_selector.set_active_text (editor.rb_opt_strings[4]);
129 opts_box.pack_start (precise_button, false, false);
130 opts_box.pack_start (stretch_opts_label, false, false);
131 opts_box.pack_start (stretch_opts_selector, false, false);
133 get_vbox()->pack_start (opts_box, false, false);
136 upper_button_box.set_homogeneous (true);
137 upper_button_box.set_spacing (5);
138 upper_button_box.set_border_width (5);
140 upper_button_box.pack_start (quick_button, true, true);
141 upper_button_box.pack_start (antialias_button, true, true);
143 quick_button.set_name (N_("TimeFXButton"));
144 antialias_button.set_name (N_("TimeFXButton"));
146 get_vbox()->pack_start (upper_button_box, false, false);
149 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
152 get_vbox()->pack_start (progress_bar);
154 progress_bar.set_name (N_("TimeFXProgress"));
156 show_all_children ();
160 Editor::TimeFXDialog::update_progress ()
162 progress_bar.set_fraction (request.progress);
163 return !request.done;
167 Editor::TimeFXDialog::cancel_in_progress ()
170 request.cancel = true;
171 first_cancel.disconnect();
175 Editor::TimeFXDialog::delete_in_progress (GdkEventAny* ev)
178 request.cancel = true;
179 first_delete.disconnect();
184 Editor::time_stretch (RegionSelection& regions, float fraction)
186 return time_fx (regions, fraction, false);
190 Editor::pitch_shift (RegionSelection& regions, float fraction)
192 return time_fx (regions, fraction, true);
196 Editor::time_fx (RegionSelection& regions, float val, bool pitching)
198 if (current_timefx != 0) {
199 delete current_timefx;
202 current_timefx = new TimeFXDialog (*this, pitching);
204 current_timefx->progress_bar.set_fraction (0.0f);
206 switch (current_timefx->run ()) {
207 case RESPONSE_ACCEPT:
210 current_timefx->hide ();
214 current_timefx->status = 0;
215 current_timefx->regions = regions;
219 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
220 float pitch_fraction;
221 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
222 cents += current_timefx->pitch_cent_adjustment.get_value();
225 // user didn't change anything
226 current_timefx->hide ();
230 // one octave == 1200 cents
231 // adding one octave doubles the frequency
232 // ratio is 2^^octaves
234 pitch_fraction = pow(2, cents/1200);
236 current_timefx->request.time_fraction = 1.0;
237 current_timefx->request.pitch_fraction = pitch_fraction;
241 current_timefx->request.time_fraction = val;
242 current_timefx->request.pitch_fraction = 1.0;
246 #ifdef USE_RUBBERBAND
249 RubberBandStretcher::Options options = 0;
251 bool realtime = false;
252 bool precise = false;
253 bool peaklock = true;
254 bool longwin = false;
255 bool shortwin = false;
260 BandLimitedTransients,
262 } transients = Transients;
264 precise = current_timefx->precise_button.get_active();
266 txt = current_timefx->stretch_opts_selector.get_active_text ();
268 if (txt == rb_opt_strings[0]) {
269 transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
270 } else if (txt == rb_opt_strings[1]) {
271 transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
272 } else if (txt == rb_opt_strings[2]) {
273 transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
274 } else if (txt == rb_opt_strings[3]) {
275 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
276 } else if (txt == rb_opt_strings[5]) {
277 transients = Transients; peaklock = false; longwin = false; shortwin = true;
281 transients = Transients; peaklock = true; longwin = false; shortwin = false;
285 if (realtime) options |= RubberBandStretcher::OptionProcessRealTime;
286 if (precise) options |= RubberBandStretcher::OptionStretchPrecise;
287 if (!peaklock) options |= RubberBandStretcher::OptionPhaseIndependent;
288 if (longwin) options |= RubberBandStretcher::OptionWindowLong;
289 if (shortwin) options |= RubberBandStretcher::OptionWindowShort;
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.progress = 0.0f;
309 current_timefx->request.done = false;
310 current_timefx->request.cancel = false;
312 /* re-connect the cancel button and delete events */
314 current_timefx->first_cancel.disconnect();
315 current_timefx->first_delete.disconnect();
317 current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
318 (mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
319 current_timefx->first_delete = current_timefx->signal_delete_event().connect
320 (mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
322 if (pthread_create_and_store ("timefx", ¤t_timefx->request.thread, 0, timefx_thread, current_timefx)) {
323 current_timefx->hide ();
324 error << _("timefx cannot be started - thread creation error") << endmsg;
328 pthread_detach (current_timefx->request.thread);
330 sigc::connection c = Glib::signal_timeout().connect (mem_fun (current_timefx, &TimeFXDialog::update_progress), 100);
332 while (!current_timefx->request.done && !current_timefx->request.cancel) {
333 gtk_main_iteration ();
338 current_timefx->hide ();
339 return current_timefx->status;
343 Editor::do_timefx (TimeFXDialog& dialog)
346 boost::shared_ptr<Playlist> playlist;
347 boost::shared_ptr<Region> new_region;
348 bool in_command = false;
350 for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
351 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
357 boost::shared_ptr<AudioRegion> region (arv->audio_region());
358 TimeAxisView* tv = &(arv->get_time_axis_view());
359 RouteTimeAxisView* rtv;
360 RegionSelection::iterator tmp;
365 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
370 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
375 if ((playlist = t->diskstream()->playlist()) == 0) {
380 if (dialog.request.cancel) {
381 /* we were cancelled */
388 if (dialog.pitching) {
389 fx = new Pitch (*session, dialog.request);
391 fx = new Stretch (*session, dialog.request);
394 if (fx->run (region)) {
396 dialog.request.done = true;
401 if (!fx->results.empty()) {
402 new_region = fx->results.front();
405 begin_reversible_command (dialog.pitching ? _("pitch shift") : _("time stretch"));
409 XMLNode &before = playlist->get_state();
410 playlist->replace_region (region, new_region, region->position());
411 XMLNode &after = playlist->get_state();
412 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
420 commit_reversible_command ();
424 dialog.request.done = true;
428 Editor::timefx_thread (void *arg)
430 PBD::ThreadCreated (pthread_self(), X_("TimeFX"));
432 TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
434 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
436 tsd->editor.do_timefx (*tsd);