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.
25 #include <pbd/error.h>
26 #include <pbd/pthread_utils.h>
27 #include <pbd/memento_command.h>
29 #include <gtkmm2ext/window_title.h>
32 #include "audio_time_axis.h"
33 #include "audio_region_view.h"
34 #include "region_selection.h"
36 #include <ardour/session.h>
37 #include <ardour/region.h>
38 #include <ardour/audioplaylist.h>
39 #include <ardour/audio_track.h>
40 #include <ardour/audioregion.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/stretch.h>
43 #include <ardour/pitch.h>
47 using namespace ARDOUR;
51 using namespace Gtkmm2ext;
53 Editor::TimeFXDialog::TimeFXDialog (Editor& e, bool pitch)
54 : ArdourDialog (X_("time fx dialog")),
57 pitch_octave_adjustment (0.0, 0.0, 4.0, 1, 2.0),
58 pitch_semitone_adjustment (0.0, 0.0, 12.0, 1.0, 4.0),
59 pitch_cent_adjustment (0.0, 0.0, 150.0, 5.0, 15.0),
60 pitch_octave_spinner (pitch_octave_adjustment),
61 pitch_semitone_spinner (pitch_semitone_adjustment),
62 pitch_cent_spinner (pitch_cent_adjustment),
63 quick_button (_("Quick but Ugly")),
64 antialias_button (_("Skip Anti-aliasing"))
67 set_position (Gtk::WIN_POS_MOUSE);
68 set_name (N_("TimeFXDialog"));
70 WindowTitle title(Glib::get_application_name());
72 title += _("Pitch Shift");
74 title += _("Time Stretch");
76 set_title(title.get_string());
78 cancel_button = add_button (_("Cancel"), Gtk::RESPONSE_CANCEL);
80 get_vbox()->set_spacing (5);
81 get_vbox()->set_border_width (12);
82 get_vbox()->pack_start (upper_button_box, false, false);
83 get_vbox()->pack_start (progress_bar);
87 upper_button_box.set_spacing (5);
88 upper_button_box.set_border_width (5);
92 l = manage (new Label (_("Octaves")));
93 upper_button_box.pack_start (*l, false, false);
94 upper_button_box.pack_start (pitch_octave_spinner, false, false);
96 l = manage (new Label (_("Semitones (12TET)")));
97 upper_button_box.pack_start (*l, false, false);
98 upper_button_box.pack_start (pitch_semitone_spinner, false, false);
100 l = manage (new Label (_("Cents")));
101 upper_button_box.pack_start (*l, false, false);
102 upper_button_box.pack_start (pitch_cent_spinner, false, false);
104 pitch_cent_spinner.set_digits (1);
106 add_button (_("Shift"), Gtk::RESPONSE_ACCEPT);
110 upper_button_box.set_homogeneous (true);
111 upper_button_box.set_spacing (5);
112 upper_button_box.set_border_width (5);
113 upper_button_box.pack_start (quick_button, true, true);
114 upper_button_box.pack_start (antialias_button, true, true);
116 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
119 quick_button.set_name (N_("TimeFXButton"));
120 antialias_button.set_name (N_("TimeFXButton"));
121 progress_bar.set_name (N_("TimeFXProgress"));
123 show_all_children ();
127 Editor::TimeFXDialog::update_progress ()
129 progress_bar.set_fraction (request.progress);
130 return !request.done;
134 Editor::TimeFXDialog::cancel_in_progress ()
137 request.cancel = true;
138 first_cancel.disconnect();
142 Editor::TimeFXDialog::delete_in_progress (GdkEventAny* ev)
145 request.cancel = true;
146 first_delete.disconnect();
151 Editor::time_stretch (RegionSelection& regions, float fraction)
153 return time_fx (regions, fraction, false);
157 Editor::pitch_shift (RegionSelection& regions, float fraction)
159 return time_fx (regions, fraction, true);
163 Editor::time_fx (RegionSelection& regions, float val, bool pitching)
165 if (current_timefx != 0) {
166 delete current_timefx;
169 current_timefx = new TimeFXDialog (*this, pitching);
171 current_timefx->progress_bar.set_fraction (0.0f);
173 switch (current_timefx->run ()) {
174 case RESPONSE_ACCEPT:
177 current_timefx->hide ();
181 current_timefx->status = 0;
182 current_timefx->regions = regions;
186 float cents = current_timefx->pitch_octave_adjustment.get_value() * 1200.0;
187 cents += current_timefx->pitch_semitone_adjustment.get_value() * 100.0;
188 cents += current_timefx->pitch_cent_adjustment.get_value();
191 // user didn't change anything
192 current_timefx->hide ();
196 // we now have the pitch shift in cents. divide by 1200 to get octaves
197 // then multiply by 2.0 because 1 octave == doubling the frequency
202 // add 1.0 to convert to RB scale
206 current_timefx->request.time_fraction = 1.0;
207 current_timefx->request.pitch_fraction = cents;
211 current_timefx->request.time_fraction = val;
212 current_timefx->request.pitch_fraction = 1.0;
216 current_timefx->request.quick_seek = current_timefx->quick_button.get_active();
217 current_timefx->request.antialias = !current_timefx->antialias_button.get_active();
218 current_timefx->request.progress = 0.0f;
219 current_timefx->request.done = false;
220 current_timefx->request.cancel = false;
222 /* re-connect the cancel button and delete events */
224 current_timefx->first_cancel.disconnect();
225 current_timefx->first_delete.disconnect();
227 current_timefx->first_cancel = current_timefx->cancel_button->signal_clicked().connect
228 (mem_fun (current_timefx, &TimeFXDialog::cancel_in_progress));
229 current_timefx->first_delete = current_timefx->signal_delete_event().connect
230 (mem_fun (current_timefx, &TimeFXDialog::delete_in_progress));
232 if (pthread_create_and_store ("timefx", ¤t_timefx->request.thread, 0, timefx_thread, current_timefx)) {
233 current_timefx->hide ();
234 error << _("timefx cannot be started - thread creation error") << endmsg;
238 pthread_detach (current_timefx->request.thread);
240 sigc::connection c = Glib::signal_timeout().connect (mem_fun (current_timefx, &TimeFXDialog::update_progress), 100);
242 while (!current_timefx->request.done) {
243 gtk_main_iteration ();
248 current_timefx->hide ();
249 return current_timefx->status;
253 Editor::do_timefx (TimeFXDialog& dialog)
256 boost::shared_ptr<Playlist> playlist;
257 boost::shared_ptr<Region> new_region;
258 bool in_command = false;
260 for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
261 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
267 boost::shared_ptr<AudioRegion> region (arv->audio_region());
268 TimeAxisView* tv = &(arv->get_time_axis_view());
269 RouteTimeAxisView* rtv;
270 RegionSelection::iterator tmp;
275 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
280 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
285 if ((playlist = t->diskstream()->playlist()) == 0) {
290 if (dialog.request.cancel) {
291 /* we were cancelled */
298 if (dialog.pitching) {
299 fx = new Pitch (*session, dialog.request);
301 fx = new Stretch (*session, dialog.request);
304 if (fx->run (region)) {
306 dialog.request.done = true;
311 if (!fx->results.empty()) {
312 new_region = fx->results.front();
315 begin_reversible_command (dialog.pitching ? _("pitch shift") : _("time stretch"));
319 XMLNode &before = playlist->get_state();
320 playlist->replace_region (region, new_region, region->position());
321 XMLNode &after = playlist->get_state();
322 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
330 commit_reversible_command ();
334 dialog.request.done = true;
338 Editor::timefx_thread (void *arg)
340 PBD::ThreadCreated (pthread_self(), X_("TimeFX"));
342 TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
344 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
346 tsd->editor.do_timefx (*tsd);