Merged with trunk R708
[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     $Id$
19 */
20
21 #include <cstdlib>
22 #include <cmath>
23
24 #include <string>
25
26 #include <pbd/error.h>
27 #include <pbd/pthread_utils.h>
28
29 #include "editor.h"
30 #include "audio_time_axis.h"
31 #include "audio_regionview.h"
32 #include "region_selection.h"
33
34 #include <ardour/session.h>
35 #include <ardour/region.h>
36 #include <ardour/audioplaylist.h>
37 #include <ardour/audio_track.h>
38 #include <ardour/audioregion.h>
39 #include <ardour/audio_diskstream.h>
40
41 #include "i18n.h"
42
43 using namespace ARDOUR;
44 using namespace PBD;
45 using namespace sigc;
46 using namespace Gtk;
47
48 Editor::TimeStretchDialog::TimeStretchDialog (Editor& e)
49         : ArdourDialog ("time stretch dialog"),
50           editor (e),
51           quick_button (_("Quick but Ugly")),
52           antialias_button (_("Skip Anti-aliasing"))
53 {
54         set_modal (true);
55         set_position (Gtk::WIN_POS_MOUSE);
56         set_title (_("ardour: timestretch"));
57         set_name (N_("TimeStretchDialog"));
58
59         get_vbox()->set_spacing (5);
60         get_vbox()->set_border_width (5);
61         get_vbox()->pack_start (upper_button_box);
62         get_vbox()->pack_start (progress_bar);
63
64         upper_button_box.set_homogeneous (true);
65         upper_button_box.set_spacing (5);
66         upper_button_box.set_border_width (5);
67         upper_button_box.pack_start (quick_button, true, true);
68         upper_button_box.pack_start (antialias_button, true, true);
69
70         action_button = add_button (_("Stretch/Shrink it"), Gtk::RESPONSE_ACCEPT);
71         cancel_button = add_button (_("Cancel"), Gtk::RESPONSE_CANCEL);
72
73         quick_button.set_name (N_("TimeStretchButton"));
74         antialias_button.set_name (N_("TimeStretchButton"));
75         progress_bar.set_name (N_("TimeStretchProgress"));
76
77         show_all_children ();
78 }
79
80 gint
81 Editor::TimeStretchDialog::update_progress ()
82 {
83         progress_bar.set_fraction (request.progress);
84         return request.running;
85 }
86
87 void
88 Editor::TimeStretchDialog::cancel_timestretch_in_progress ()
89 {
90         status = -2;
91         request.running = false;
92 }
93
94 gint
95 Editor::TimeStretchDialog::delete_timestretch_in_progress (GdkEventAny* ev)
96 {
97         status = -2;
98         request.running = false;
99         return TRUE;
100 }
101
102 int
103 Editor::run_timestretch (RegionSelection& regions, float fraction)
104 {
105         pthread_t thread;
106
107         if (current_timestretch == 0) {
108                 current_timestretch = new TimeStretchDialog (*this);
109         }
110
111         current_timestretch->progress_bar.set_fraction (0.0f);
112
113         switch (current_timestretch->run ()) {
114         case RESPONSE_ACCEPT:
115                 break;
116         default:
117                 current_timestretch->hide ();
118                 return 1;
119         }
120
121         current_timestretch->status = 0;
122         current_timestretch->regions = regions;
123         current_timestretch->request.fraction = fraction;
124         current_timestretch->request.quick_seek = current_timestretch->quick_button.get_active();
125         current_timestretch->request.antialias = !current_timestretch->antialias_button.get_active();
126         current_timestretch->request.progress = 0.0f;
127         current_timestretch->request.running = true;
128         
129         /* re-connect the cancel button and delete events */
130         
131         current_timestretch->first_cancel.disconnect();
132         current_timestretch->first_delete.disconnect();
133         
134         current_timestretch->cancel_button->signal_clicked().connect (mem_fun (current_timestretch, &TimeStretchDialog::cancel_timestretch_in_progress));
135         current_timestretch->signal_delete_event().connect (mem_fun (current_timestretch, &TimeStretchDialog::delete_timestretch_in_progress));
136
137         if (pthread_create_and_store ("timestretch", &thread, 0, timestretch_thread, current_timestretch)) {
138                 current_timestretch->hide ();
139                 error << _("timestretch cannot be started - thread creation error") << endmsg;
140                 return -1;
141         }
142
143         pthread_detach (thread);
144
145         sigc::connection c = Glib::signal_timeout().connect (mem_fun (current_timestretch, &TimeStretchDialog::update_progress), 100);
146
147         while (current_timestretch->request.running) {
148                 gtk_main_iteration ();
149         }
150
151         c.disconnect ();
152         
153         current_timestretch->hide ();
154         return current_timestretch->status;
155 }
156
157 void
158 Editor::do_timestretch (TimeStretchDialog& dialog)
159 {
160         Track*    t;
161         Playlist* playlist;
162         Region*   new_region;
163
164
165         for (RegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
166                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
167                 if (!arv)
168                         continue;
169
170                 AudioRegion& region (arv->audio_region());
171                 TimeAxisView* tv = &(arv->get_time_axis_view());
172                 RouteTimeAxisView* rtv;
173                 RegionSelection::iterator tmp;
174                 
175                 cerr << "stretch " << region.name() << endl;
176
177                 tmp = i;
178                 ++tmp;
179
180                 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) == 0) {
181                         i = tmp;
182                         continue;
183                 }
184
185                 if ((t = dynamic_cast<Track*> (rtv->route().get())) == 0) {
186                         i = tmp;
187                         continue;
188                 }
189         
190                 if ((playlist = t->diskstream().playlist()) == 0) {
191                         i = tmp;
192                         continue;
193                 }
194
195                 dialog.request.region = &region;
196
197                 if (!dialog.request.running) {
198                         /* we were cancelled */
199                         dialog.status = 1;
200                         return;
201                 }
202
203                 if ((new_region = session->tempoize_region (dialog.request)) == 0) {
204                         dialog.status = -1;
205                         dialog.request.running = false;
206                         return;
207                 }
208
209                 session->add_undo (playlist->get_memento());
210                 playlist->replace_region (region, *new_region, region.position());
211                 session->add_redo_no_execute (playlist->get_memento());
212
213                 i = tmp;
214         }
215
216         dialog.status = 0;
217         dialog.request.running = false;
218 }
219
220 void*
221 Editor::timestretch_thread (void *arg)
222 {
223         PBD::ThreadCreated (pthread_self(), X_("TimeFX"));
224
225         TimeStretchDialog* tsd = static_cast<TimeStretchDialog*>(arg);
226
227         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
228
229         tsd->editor.do_timestretch (*tsd);
230
231         return 0;
232 }
233