Uploaded start of CoreAudioSource. More sfdb work.
[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         add (packer);
61
62         packer.set_spacing (5);
63         packer.set_border_width (5);
64         packer.pack_start (upper_button_box);
65         packer.pack_start (progress_bar);
66         packer.pack_start (lower_button_box);
67         
68         upper_button_box.set_homogeneous (true);
69         upper_button_box.set_spacing (5);
70         upper_button_box.set_border_width (5);
71         upper_button_box.pack_start (quick_button, true, true);
72         upper_button_box.pack_start (antialias_button, true, true);
73
74         lower_button_box.set_homogeneous (true);
75         lower_button_box.set_spacing (5);
76         lower_button_box.set_border_width (5);
77         lower_button_box.pack_start (action_button, true, true);
78         lower_button_box.pack_start (cancel_button, true, true);
79
80         action_button.set_name (N_("TimeStretchButton"));
81         cancel_button.set_name (N_("TimeStretchButton"));
82         quick_button.set_name (N_("TimeStretchButton"));
83         antialias_button.set_name (N_("TimeStretchButton"));
84         progress_bar.set_name (N_("TimeStretchProgress"));
85
86         // GTK2FIX
87         // action_button.signal_clicked().connect (bind (mem_fun(*this, &ArdourDialog::stop), 1));
88 }
89
90 gint
91 Editor::TimeStretchDialog::update_progress ()
92 {
93         progress_bar.set_fraction (request.progress/100);
94         return request.running;
95 }
96
97 void
98 Editor::TimeStretchDialog::cancel_timestretch_in_progress ()
99 {
100         status = -2;
101         request.running = false;
102 }
103
104 gint
105 Editor::TimeStretchDialog::delete_timestretch_in_progress (GdkEventAny* ev)
106 {
107         status = -2;
108         request.running = false;
109         return TRUE;
110 }
111
112 int
113 Editor::run_timestretch (AudioRegionSelection& regions, float fraction)
114 {
115         pthread_t thread;
116
117         if (current_timestretch == 0) {
118                 current_timestretch = new TimeStretchDialog (*this);
119         }
120
121         current_timestretch->progress_bar.set_fraction (0.0f);
122         // GTK2FIX
123         // current_timestretch->first_cancel = current_timestretch->cancel_button.signal_clicked().connect (bind (mem_fun (*current_timestretch, &ArdourDialog::stop), -1));
124         // current_timestretch->first_delete = current_timestretch->signal_delete_event().connect (mem_fun (*current_timestretch, &ArdourDialog::wm_close_event));
125
126         switch (current_timestretch->run ()) {
127         case RESPONSE_ACCEPT:
128                 break;
129         default:
130                 current_timestretch->hide ();
131                 return 1;
132         }
133
134         current_timestretch->status = 0;
135         current_timestretch->regions = regions;
136         current_timestretch->request.fraction = fraction;
137         current_timestretch->request.quick_seek = current_timestretch->quick_button.get_active();
138         current_timestretch->request.antialias = !current_timestretch->antialias_button.get_active();
139         current_timestretch->request.progress = 0.0f;
140         current_timestretch->request.running = true;
141         
142         /* re-connect the cancel button and delete events */
143         
144         current_timestretch->first_cancel.disconnect();
145         current_timestretch->first_delete.disconnect();
146         
147         current_timestretch->cancel_button.signal_clicked().connect (mem_fun (current_timestretch, &TimeStretchDialog::cancel_timestretch_in_progress));
148         current_timestretch->signal_delete_event().connect (mem_fun (current_timestretch, &TimeStretchDialog::delete_timestretch_in_progress));
149
150         if (pthread_create_and_store ("timestretch", &thread, 0, timestretch_thread, current_timestretch)) {
151                 // GTK2FIX
152           //current_timestretch->close ();
153                 error << _("timestretch cannot be started - thread creation error") << endmsg;
154                 return -1;
155         }
156
157         pthread_detach (thread);
158
159         sigc::connection c = Glib::signal_timeout().connect (mem_fun (current_timestretch, &TimeStretchDialog::update_progress), 100);
160
161         while (current_timestretch->request.running) {
162                 gtk_main_iteration ();
163         }
164
165         c.disconnect ();
166         
167         current_timestretch->hide ();
168         return current_timestretch->status;
169 }
170
171 void
172 Editor::do_timestretch (TimeStretchDialog& dialog)
173 {
174         AudioTrack* at;
175         Playlist* playlist;
176         AudioRegion* new_region;
177
178         for (AudioRegionSelection::iterator i = dialog.regions.begin(); i != dialog.regions.end(); ) {
179
180                 AudioRegion& aregion ((*i)->region);
181                 TimeAxisView* tv = &(*i)->get_time_axis_view();
182                 AudioTimeAxisView* atv;
183                 AudioRegionSelection::iterator tmp;
184                 
185                 tmp = i;
186                 ++tmp;
187
188                 if ((atv = dynamic_cast<AudioTimeAxisView*> (tv)) == 0) {
189                         i = tmp;
190                         continue;
191                 }
192
193                 if ((at = dynamic_cast<AudioTrack*> (&atv->route())) == 0) {
194                         i = tmp;
195                         continue;
196                 }
197         
198                 if ((playlist = at->disk_stream().playlist()) == 0) {
199                         i = tmp;
200                         continue;
201                 }
202
203                 dialog.request.region = &aregion;
204
205                 if (!dialog.request.running) {
206                         /* we were cancelled */
207                         dialog.status = 1;
208                         return;
209                 }
210
211                 if ((new_region = session->tempoize_region (dialog.request)) == 0) {
212                         dialog.status = -1;
213                         dialog.request.running = false;
214                         return;
215                 }
216
217                 session->add_undo (playlist->get_memento());
218                 playlist->replace_region (aregion, *new_region, aregion.position());
219                 session->add_redo_no_execute (playlist->get_memento());
220
221                 i = tmp;
222         }
223
224         dialog.status = 0;
225         dialog.request.running = false;
226 }
227
228 void*
229 Editor::timestretch_thread (void *arg)
230 {
231         PBD::ThreadCreated (pthread_self(), X_("TimeFX"));
232
233         TimeStretchDialog* tsd = static_cast<TimeStretchDialog*>(arg);
234
235         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
236
237         tsd->editor.do_timestretch (*tsd);
238
239         return 0;
240 }
241