015a265a9bd608b13afeaf85f4e48666a92e3f64
[ardour.git] / gtk2_ardour / editor_timefx.cc
1 /*
2     Copyright (C) 2000 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cstdlib>
21 #include <cmath>
22
23 #include <string>
24
25 #include <pbd/error.h>
26 #include <pbd/pthread_utils.h>
27 #include <pbd/memento_command.h>
28
29 #include <gtkmm2ext/window_title.h>
30
31 #include "editor.h"
32 #include "audio_time_axis.h"
33 #include "audio_region_view.h"
34 #include "region_selection.h"
35
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>
44
45 #include "i18n.h"
46
47 using namespace ARDOUR;
48 using namespace PBD;
49 using namespace sigc;
50 using namespace Gtk;
51 using namespace Gtkmm2ext;
52
53 Editor::TimeFXDialog::TimeFXDialog (Editor& e, bool pitch)
54         : ArdourDialog (X_("time fx dialog")),
55           editor (e),
56           pitching (pitch),
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"))
65 {
66         set_modal (true);
67         set_position (Gtk::WIN_POS_MOUSE);
68         set_name (N_("TimeFXDialog"));
69
70         WindowTitle title(Glib::get_application_name());
71         if (pitching) {
72                 title += _("Pitch Shift");
73         } else {
74                 title += _("Time Stretch");
75         }
76         set_title(title.get_string());
77
78         cancel_button = add_button (_("Cancel"), Gtk::RESPONSE_CANCEL);
79
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);
84
85         if (pitching) {
86
87                 upper_button_box.set_spacing (5);
88                 upper_button_box.set_border_width (5);
89                 
90                 Gtk::Label* l;
91
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);
95
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);
99
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);
103
104                 pitch_cent_spinner.set_digits (1);
105
106                 add_button (_("Shift"), Gtk::RESPONSE_ACCEPT);
107
108         } else {
109
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);
115         
116                 add_button (_("Stretch/Shrink"), Gtk::RESPONSE_ACCEPT);
117         }
118
119         quick_button.set_name (N_("TimeFXButton"));
120         antialias_button.set_name (N_("TimeFXButton"));
121         progress_bar.set_name (N_("TimeFXProgress"));
122
123         show_all_children ();
124 }
125
126 gint
127 Editor::TimeFXDialog::update_progress ()
128 {
129         progress_bar.set_fraction (request.progress);
130         return !request.done;
131 }
132
133 void
134 Editor::TimeFXDialog::cancel_in_progress ()
135 {
136         status = -2;
137         request.cancel = true;
138         first_cancel.disconnect();
139 }
140
141 gint
142 Editor::TimeFXDialog::delete_in_progress (GdkEventAny* ev)
143 {
144         status = -2;
145         request.cancel = true;
146         first_delete.disconnect();
147         return TRUE;
148 }
149
150 int
151 Editor::time_stretch (RegionSelection& regions, float fraction)
152 {
153         return time_fx (regions, fraction, false);
154 }
155
156 int
157 Editor::pitch_shift (RegionSelection& regions, float fraction)
158 {
159         return time_fx (regions, fraction, true);
160 }
161
162 int
163 Editor::time_fx (RegionSelection& regions, float val, bool pitching)
164 {
165         if (current_timefx != 0) {
166                 delete current_timefx;
167         }
168
169         current_timefx = new TimeFXDialog (*this, pitching);
170
171         current_timefx->progress_bar.set_fraction (0.0f);
172
173         switch (current_timefx->run ()) {
174         case RESPONSE_ACCEPT:
175                 break;
176         default:
177                 current_timefx->hide ();
178                 return 1;
179         }
180
181         current_timefx->status = 0;
182         current_timefx->regions = regions;
183
184         if (pitching) {
185
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();
189
190                 if (cents == 0.0) {
191                         // user didn't change anything
192                         current_timefx->hide ();
193                         return 0;
194                 }
195
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
198                 
199                 cents /= 1200.0;
200                 cents /= 2.0;
201
202                 // add 1.0 to convert to RB scale
203
204                 cents += 1.0;
205
206                 current_timefx->request.time_fraction = 1.0;
207                 current_timefx->request.pitch_fraction = cents;
208
209         } else {
210
211                 current_timefx->request.time_fraction = val;
212                 current_timefx->request.pitch_fraction = 1.0;
213
214         }
215
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;
221         
222         /* re-connect the cancel button and delete events */
223         
224         current_timefx->first_cancel.disconnect();
225         current_timefx->first_delete.disconnect();
226         
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));
231
232         if (pthread_create_and_store ("timefx", &current_timefx->request.thread, 0, timefx_thread, current_timefx)) {
233                 current_timefx->hide ();
234                 error << _("timefx cannot be started - thread creation error") << endmsg;
235                 return -1;
236         }
237
238         pthread_detach (current_timefx->request.thread);
239
240         sigc::connection c = Glib::signal_timeout().connect (mem_fun (current_timefx, &TimeFXDialog::update_progress), 100);
241
242         while (!current_timefx->request.done) {
243                 gtk_main_iteration ();
244         }
245
246         c.disconnect ();
247         
248         current_timefx->hide ();
249         return current_timefx->status;
250 }
251
252 void
253 Editor::do_timefx (TimeFXDialog& dialog)
254 {
255         Track*    t;
256         boost::shared_ptr<Playlist> playlist;
257         boost::shared_ptr<Region>   new_region;
258         bool in_command = false;
259         
260         for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
261                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
262
263                 if (!arv) {
264                         continue;
265                 }
266
267                 boost::shared_ptr<AudioRegion> region (arv->audio_region());
268                 TimeAxisView* tv = &(arv->get_time_axis_view());
269                 RouteTimeAxisView* rtv;
270                 RegionSelection::iterator tmp;
271                 
272                 tmp = i;
273                 ++tmp;
274
275                 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
276                         i = tmp;
277                         continue;
278                 }
279
280                 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
281                         i = tmp;
282                         continue;
283                 }
284         
285                 if ((playlist = t->diskstream()->playlist()) == 0) {
286                         i = tmp;
287                         continue;
288                 }
289
290                 if (dialog.request.cancel) {
291                         /* we were cancelled */
292                         dialog.status = 1;
293                         return;
294                 }
295
296                 Filter* fx;
297
298                 if (dialog.pitching) {
299                         fx = new Pitch (*session, dialog.request);
300                 } else {
301                         fx = new Stretch (*session, dialog.request);
302                 }
303
304                 if (fx->run (region)) {
305                         dialog.status = -1;
306                         dialog.request.done = true;
307                         delete fx;
308                         return;
309                 }
310
311                 if (!fx->results.empty()) {
312                         new_region = fx->results.front();
313
314                         if (!in_command) {
315                                 begin_reversible_command (dialog.pitching ? _("pitch shift") : _("time stretch"));
316                                 in_command = true;
317                         }
318
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));
323                 }
324
325                 i = tmp;
326                 delete fx;
327         }
328
329         if (in_command) {
330                 commit_reversible_command ();
331         }
332
333         dialog.status = 0;
334         dialog.request.done = true;
335 }
336
337 void*
338 Editor::timefx_thread (void *arg)
339 {
340         PBD::ThreadCreated (pthread_self(), X_("TimeFX"));
341
342         TimeFXDialog* tsd = static_cast<TimeFXDialog*>(arg);
343
344         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
345
346         tsd->editor.do_timefx (*tsd);
347
348         return 0;
349 }
350