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