70cac67d0370bb450c68accba87cefb723537fb4
[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 "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/diskstream.h>
40
41 #include "i18n.h"
42
43 using namespace ARDOUR;
44 using namespace SigC;
45 using namespace Gtk;
46
47 Editor::TimeStretchDialog::TimeStretchDialog (Editor& e)
48         : ArdourDialog ("time stretch dialog"),
49           editor (e),
50           quick_button (_("Quick but Ugly")),
51           antialias_button (_("Skip Anti-aliasing")),
52           cancel_button (_("Cancel")),
53           action_button (_("Stretch/Shrink it"))
54 {
55         set_modal (true);
56         set_position (GTK_WIN_POS_MOUSE);
57         set_title (_("ardour: timestretch"));
58         set_name (N_("TimeStretchDialog"));
59
60         set_hide_on_stop (false);
61
62         add (packer);
63
64         packer.set_spacing (5);
65         packer.set_border_width (5);
66         packer.pack_start (upper_button_box);
67         packer.pack_start (progress_bar);
68         packer.pack_start (lower_button_box);
69         
70         upper_button_box.set_homogeneous (true);
71         upper_button_box.set_spacing (5);
72         upper_button_box.set_border_width (5);
73         upper_button_box.pack_start (quick_button, true, true);
74         upper_button_box.pack_start (antialias_button, true, true);
75
76         lower_button_box.set_homogeneous (true);
77         lower_button_box.set_spacing (5);
78         lower_button_box.set_border_width (5);
79         lower_button_box.pack_start (action_button, true, true);
80         lower_button_box.pack_start (cancel_button, true, true);
81
82         action_button.set_name (N_("TimeStretchButton"));
83         cancel_button.set_name (N_("TimeStretchButton"));
84         quick_button.set_name (N_("TimeStretchButton"));
85         antialias_button.set_name (N_("TimeStretchButton"));
86         progress_bar.set_name (N_("TimeStretchProgress"));
87
88         action_button.clicked.connect (bind (slot (*this, &ArdourDialog::stop), 1));
89 }
90
91 gint
92 Editor::TimeStretchDialog::update_progress ()
93 {
94         progress_bar.set_percentage (request.progress);
95         return request.running;
96 }
97
98 void
99 Editor::TimeStretchDialog::cancel_timestretch_in_progress ()
100 {
101         status = -2;
102         request.running = false;
103 }
104
105 gint
106 Editor::TimeStretchDialog::delete_timestretch_in_progress (GdkEventAny* ev)
107 {
108         status = -2;
109         request.running = false;
110         return TRUE;
111 }
112
113 int
114 Editor::run_timestretch (AudioRegionSelection& regions, float fraction)
115 {
116         pthread_t thread;
117
118         if (current_timestretch == 0) {
119                 current_timestretch = new TimeStretchDialog (*this);
120         }
121
122         current_timestretch->progress_bar.set_percentage (0.0f);
123         current_timestretch->first_cancel = current_timestretch->cancel_button.clicked.connect (bind (slot (*current_timestretch, &ArdourDialog::stop), -1));
124         current_timestretch->first_delete = current_timestretch->delete_event.connect (slot (*current_timestretch, &ArdourDialog::wm_close_event));
125
126         current_timestretch->run ();
127
128         if (current_timestretch->run_status() != 1) {
129                 current_timestretch->close ();
130                 return 1; /* no error, but we did nothing */
131         }
132
133         current_timestretch->status = 0;
134         current_timestretch->regions = regions;
135         current_timestretch->request.fraction = fraction;
136         current_timestretch->request.quick_seek = current_timestretch->quick_button.get_active();
137         current_timestretch->request.antialias = !current_timestretch->antialias_button.get_active();
138         current_timestretch->request.progress = 0.0f;
139         current_timestretch->request.running = true;
140         
141         /* re-connect the cancel button and delete events */
142         
143         current_timestretch->first_cancel.disconnect();
144         current_timestretch->first_delete.disconnect();
145         
146         current_timestretch->cancel_button.clicked.connect (slot (current_timestretch, &TimeStretchDialog::cancel_timestretch_in_progress));
147         current_timestretch->delete_event.connect (slot (current_timestretch, &TimeStretchDialog::delete_timestretch_in_progress));
148
149         if (pthread_create_and_store ("timestretch", &thread, 0, timestretch_thread, current_timestretch)) {
150                 current_timestretch->close ();
151                 error << _("timestretch cannot be started - thread creation error") << endmsg;
152                 return -1;
153         }
154
155         pthread_detach (thread);
156
157         SigC::Connection c = Main::timeout.connect (slot (current_timestretch, &TimeStretchDialog::update_progress), 100);
158
159         while (current_timestretch->request.running) {
160                 gtk_main_iteration ();
161         }
162
163         c.disconnect ();
164
165         current_timestretch->close ();
166         return current_timestretch->status;
167 }
168
169 void
170 Editor::do_timestretch (TimeStretchDialog& dialog)
171 {
172         AudioTrack* at;
173         Playlist* playlist;
174         AudioRegion* new_region;
175
176         for (AudioRegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
177
178                 AudioRegion& aregion ((*i)->region);
179                 TimeAxisView* tv = &(*i)->get_time_axis_view();
180                 AudioTimeAxisView* atv;
181                 AudioRegionSelection::iterator tmp;
182                 
183                 tmp = i;
184                 ++tmp;
185
186                 if ((atv = dynamic_cast<AudioTimeAxisView*> (tv)) == 0) {
187                         i = tmp;
188                         continue;
189                 }
190
191                 if ((at = dynamic_cast<AudioTrack*> (&atv->route())) == 0) {
192                         i = tmp;
193                         continue;
194                 }
195         
196                 if ((playlist = at->disk_stream().playlist()) == 0) {
197                         i = tmp;
198                         continue;
199                 }
200
201                 dialog.request.region = &aregion;
202
203                 if (!dialog.request.running) {
204                         /* we were cancelled */
205                         dialog.status = 1;
206                         return;
207                 }
208
209                 if ((new_region = session->tempoize_region (dialog.request)) == 0) {
210                         dialog.status = -1;
211                         dialog.request.running = false;
212                         return;
213                 }
214
215                 session->add_undo (playlist->get_memento());
216                 playlist->replace_region (aregion, *new_region, aregion.position());
217                 session->add_redo_no_execute (playlist->get_memento());
218
219                 i = tmp;
220         }
221
222         dialog.status = 0;
223         dialog.request.running = false;
224 }
225
226 void*
227 Editor::timestretch_thread (void *arg)
228 {
229         PBD::ThreadCreated (pthread_self(), X_("TimeFX"));
230
231         TimeStretchDialog* tsd = static_cast<TimeStretchDialog*>(arg);
232
233         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
234
235         tsd->editor.do_timestretch (*tsd);
236
237         return 0;
238 }
239