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