when changing a Route name, make that sure that a Route with ControlOuts changes...
[ardour2.git] / gtk2_ardour / editor_timefx.cc
blob517102a173f671233ba62b341cac4e5608591ca8
1 /*
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.
20 #include <iostream>
21 #include <cstdlib>
22 #include <cmath>
24 #include <string>
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>
33 #include "editor.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>
47 #ifdef USE_RUBBERBAND
48 #include <rubberband/RubberBandStretcher.h>
49 using namespace RubberBand;
50 #endif
52 #include "i18n.h"
54 using namespace std;
55 using namespace ARDOUR;
56 using namespace PBD;
57 using namespace sigc;
58 using namespace Gtk;
59 using namespace Gtkmm2ext;
61 Editor::TimeFXDialog::TimeFXDialog (Editor& e, bool pitch)
62 : ArdourDialog (X_("time fx dialog")),
63 editor (e),
64 pitching (pitch),
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")),
75 preserve_formants_button(_("Preserve Formants"))
77 set_modal (true);
78 set_position (Gtk::WIN_POS_MOUSE);
79 set_name (N_("TimeFXDialog"));
81 WindowTitle title(Glib::get_application_name());
82 if (pitching) {
83 title += _("Pitch Shift");
84 } else {
85 title += _("Time Stretch");
87 set_title(title.get_string());
89 cancel_button = add_button (_("Cancel"), Gtk::RESPONSE_CANCEL);
91 get_vbox()->set_spacing (5);
92 get_vbox()->set_border_width (12);
94 if (pitching) {
96 upper_button_box.set_spacing (5);
97 upper_button_box.set_border_width (5);
99 Gtk::Label* l;
101 l = manage (new Label (_("Octaves")));
102 upper_button_box.pack_start (*l, false, false);
103 upper_button_box.pack_start (pitch_octave_spinner, false, false);
105 l = manage (new Label (_("Semitones (12TET)")));
106 upper_button_box.pack_start (*l, false, false);
107 upper_button_box.pack_start (pitch_semitone_spinner, false, false);
109 l = manage (new Label (_("Cents")));
110 upper_button_box.pack_start (*l, false, false);
111 upper_button_box.pack_start (pitch_cent_spinner, false, false);
113 pitch_cent_spinner.set_digits (1);
115 upper_button_box.pack_start (preserve_formants_button, false, false);
118 add_button (_("Shift"), Gtk::RESPONSE_ACCEPT);
120 get_vbox()->pack_start (upper_button_box, false, false);
122 } else {
124 #ifdef USE_RUBBERBAND
125 opts_box.set_spacing (5);
126 opts_box.set_border_width (5);
127 vector<string> strings;
129 set_popdown_strings (stretch_opts_selector, editor.rb_opt_strings);
130 /* set default */
131 stretch_opts_selector.set_active_text (editor.rb_opt_strings[4]);
133 opts_box.pack_start (precise_button, false, false);
134 opts_box.pack_start (stretch_opts_label, false, false);
135 opts_box.pack_start (stretch_opts_selector, false, false);
137 get_vbox()->pack_start (opts_box, false, false);
139 #else
140 upper_button_box.set_homogeneous (true);
141 upper_button_box.set_spacing (5);
142 upper_button_box.set_border_width (5);
144 upper_button_box.pack_start (quick_button, true, true);
145 upper_button_box.pack_start (antialias_button, true, true);
147 quick_button.set_name (N_("TimeFXButton"));
148 antialias_button.set_name (N_("TimeFXButton"));
150 get_vbox()->pack_start (upper_button_box, false, false);
152 #endif
153 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
156 get_vbox()->pack_start (progress_bar);
158 progress_bar.set_name (N_("TimeFXProgress"));
160 show_all_children ();
163 gint
164 Editor::TimeFXDialog::update_progress ()
166 progress_bar.set_fraction (request.progress);
167 return !request.done;
170 void
171 Editor::TimeFXDialog::cancel_in_progress ()
173 status = -2;
174 request.cancel = true;
175 first_cancel.disconnect();
178 gint
179 Editor::TimeFXDialog::delete_in_progress (GdkEventAny* ev)
181 status = -2;
182 request.cancel = true;
183 first_delete.disconnect();
184 return TRUE;
188 Editor::time_stretch (RegionSelection& regions, float fraction)
190 return time_fx (regions, fraction, false);
194 Editor::pitch_shift (RegionSelection& regions, float fraction)
196 return time_fx (regions, fraction, true);
200 Editor::time_fx (RegionSelection& regions, float val, bool pitching)
202 if (current_timefx != 0) {
203 delete current_timefx;
206 current_timefx = new TimeFXDialog (*this, pitching);
208 current_timefx->progress_bar.set_fraction (0.0f);
210 switch (current_timefx->run ()) {
211 case RESPONSE_ACCEPT:
212 break;
213 default:
214 current_timefx->hide ();
215 return 1;
218 current_timefx->status = 0;
219 current_timefx->regions = regions;
221 if (pitching) {
223 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
224 float pitch_fraction;
225 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
226 cents += current_timefx->pitch_cent_adjustment.get_value();
228 if (cents == 0.0) {
229 // user didn't change anything
230 current_timefx->hide ();
231 return 0;
234 // one octave == 1200 cents
235 // adding one octave doubles the frequency
236 // ratio is 2^^octaves
238 pitch_fraction = pow(2, cents/1200);
240 current_timefx->request.time_fraction = 1.0;
241 current_timefx->request.pitch_fraction = pitch_fraction;
243 } else {
245 current_timefx->request.time_fraction = val;
246 current_timefx->request.pitch_fraction = 1.0;
250 #ifdef USE_RUBBERBAND
251 /* parse options */
253 RubberBandStretcher::Options options = 0;
255 bool realtime = false;
256 bool precise = false;
257 bool peaklock = true;
258 bool longwin = false;
259 bool shortwin = false;
260 bool preserve_formants = false;
261 string txt;
263 enum {
264 NoTransients,
265 BandLimitedTransients,
266 Transients
267 } transients = Transients;
269 precise = current_timefx->precise_button.get_active();
270 preserve_formants = current_timefx->preserve_formants_button.get_active();
272 txt = current_timefx->stretch_opts_selector.get_active_text ();
274 if (txt == rb_opt_strings[0]) {
275 transients = NoTransients; peaklock = false; longwin = true; shortwin = false;
276 } else if (txt == rb_opt_strings[1]) {
277 transients = NoTransients; peaklock = false; longwin = false; shortwin = false;
278 } else if (txt == rb_opt_strings[2]) {
279 transients = NoTransients; peaklock = true; longwin = false; shortwin = false;
280 } else if (txt == rb_opt_strings[3]) {
281 transients = BandLimitedTransients; peaklock = true; longwin = false; shortwin = false;
282 } else if (txt == rb_opt_strings[5]) {
283 transients = Transients; peaklock = false; longwin = false; shortwin = true;
284 } else {
285 /* default/4 */
287 transients = Transients; peaklock = true; longwin = false; shortwin = false;
291 if (realtime) options |= RubberBandStretcher::OptionProcessRealTime;
292 if (precise) options |= RubberBandStretcher::OptionStretchPrecise;
293 if (preserve_formants) options |= RubberBandStretcher::OptionFormantPreserved;
295 if (!peaklock) options |= RubberBandStretcher::OptionPhaseIndependent;
296 if (longwin) options |= RubberBandStretcher::OptionWindowLong;
297 if (shortwin) options |= RubberBandStretcher::OptionWindowShort;
301 switch (transients) {
302 case NoTransients:
303 options |= RubberBandStretcher::OptionTransientsSmooth;
304 break;
305 case BandLimitedTransients:
306 options |= RubberBandStretcher::OptionTransientsMixed;
307 break;
308 case Transients:
309 options |= RubberBandStretcher::OptionTransientsCrisp;
310 break;
313 current_timefx->request.opts = (int) options;
314 #else
315 current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
316 current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
317 #endif
318 current_timefx->request.progress = 0.0f;
319 current_timefx->request.done = false;
320 current_timefx->request.cancel = false;
322 /* re-connect the cancel button and delete events */
324 current_timefx->first_cancel.disconnect();
325 current_timefx->first_delete.disconnect();
327 current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
328 (mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
329 current_timefx->first_delete = current_timefx->signal_delete_event().connect
330 (mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
332 if (pthread_create_and_store ("timefx", &current_timefx->request.thread, 0, timefx_thread, current_timefx)) {
333 current_timefx->hide ();
334 error << _("timefx cannot be started - thread creation error") << endmsg;
335 return -1;
338 pthread_detach (current_timefx->request.thread);
340 sigc::connection c = Glib::signal_timeout().connect (mem_fun (current_timefx, &TimeFXDialog::update_progress), 100);
342 while (!current_timefx->request.done && !current_timefx->request.cancel) {
343 gtk_main_iteration ();
346 c.disconnect ();
348 current_timefx->hide ();
349 return current_timefx->status;
352 void
353 Editor::do_timefx (TimeFXDialog& dialog)
355 Track* t;
356 boost::shared_ptr<Playlist> playlist;
357 boost::shared_ptr<Region> new_region;
358 bool in_command = false;
360 for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
361 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
363 if (!arv) {
364 continue;
367 boost::shared_ptr<AudioRegion> region (arv->audio_region());
368 TimeAxisView* tv = &(arv->get_time_axis_view());
369 RouteTimeAxisView* rtv;
370 RegionSelection::iterator tmp;
372 tmp = i;
373 ++tmp;
375 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
376 i = tmp;
377 continue;
380 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
381 i = tmp;
382 continue;
385 if ((playlist = t->diskstream()->playlist()) == 0) {
386 i = tmp;
387 continue;
390 if (dialog.request.cancel) {
391 /* we were cancelled */
392 dialog.status = 1;
393 return;
396 AudioFilter* fx;
398 if (dialog.pitching) {
399 fx = new Pitch (*session, dialog.request);
400 } else {
401 fx = new Stretch (*session, dialog.request);
404 if (fx->run (region)) {
405 dialog.status = -1;
406 dialog.request.done = true;
407 delete fx;
408 return;
411 if (!fx->results.empty()) {
412 new_region = fx->results.front();
414 if (!in_command) {
415 begin_reversible_command (dialog.pitching ? _("pitch shift") : _("time stretch"));
416 in_command = true;
419 XMLNode &before = playlist->get_state();
420 playlist->replace_region (region, new_region, region->position());
421 XMLNode &after = playlist->get_state();
422 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
425 i = tmp;
426 delete fx;
429 if (in_command) {
430 commit_reversible_command ();
433 dialog.status = 0;
434 dialog.request.done = true;
437 void*
438 Editor::timefx_thread (void *arg)
440 PBD::notify_gui_about_thread_creation (pthread_self(), X_("TimeFX"));
442 TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
444 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
446 tsd->editor.do_timefx (*tsd);
448 return 0;