PluginInfo::type added to copy constructor. But why is the copy constructor defined...
[ardour.git] / libs / ardour / st_stretch.cc
1 /*
2     Copyright (C) 2004-2007 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 */
19
20 #include <algorithm>
21 #include <cmath>
22
23 #include <pbd/error.h>
24
25 #include <ardour/types.h>
26 #include <ardour/stretch.h>
27 #include <ardour/audiofilesource.h>
28 #include <ardour/session.h>
29 #include <ardour/audioregion.h>
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35 using namespace PBD;
36 using namespace soundtouch;
37
38 Stretch::Stretch (Session& s, TimeFXRequest& req)
39         : AudioFilter (s)
40         , tsr (req)
41 {
42         float percentage;
43
44         /* the soundtouch code wants a *tempo* change percentage, which is 
45            of opposite sign to the length change.  
46         */
47
48         percentage = -tsr.time_fraction;
49
50         st.setSampleRate (s.frame_rate());
51         st.setChannels (1);
52         st.setTempoChange (percentage);
53         st.setPitchSemiTones (0);
54         st.setRateChange (0);
55         
56         st.setSetting(SETTING_USE_QUICKSEEK, tsr.quick_seek);
57         st.setSetting(SETTING_USE_AA_FILTER, tsr.antialias);
58
59         tsr.progress = 0.0f;
60 }
61
62 Stretch::~Stretch ()
63 {
64 }
65
66 int
67 Stretch::run (boost::shared_ptr<AudioRegion> region)
68 {
69         SourceList nsrcs;
70         nframes_t total_frames;
71         nframes_t done;
72         int ret = -1;
73         const nframes_t bufsize = 16384;
74         gain_t *gain_buffer = 0;
75         Sample *buffer = 0;
76         char suffix[32];
77         string new_name;
78         string::size_type at;
79
80         tsr.progress = 0.0f;
81         tsr.done = false;
82
83         total_frames = region->length() * region->n_channels();
84         done = 0;
85
86         /* the name doesn't need to be super-precise, but allow for 2 fractional
87            digits just to disambiguate close but not identical stretches.
88         */
89         
90         snprintf (suffix, sizeof (suffix), "@%d", (int) floor (tsr.time_fraction * 100.0f));
91
92         /* create new sources */
93         
94         if (make_new_sources (region, nsrcs, suffix)) {
95                 goto out;
96         }
97
98         gain_buffer = new gain_t[bufsize];
99         buffer = new Sample[bufsize];
100
101         // soundtouch throws runtime_error on error
102
103         try {
104                 for (uint32_t i = 0; i < nsrcs.size(); ++i) {
105
106                         nframes_t pos = 0;
107                         nframes_t this_read = 0;
108
109                         st.clear();
110
111                         while (!tsr.cancel && pos < region->length()) {
112                                 nframes_t this_time;
113                         
114                                 this_time = min (bufsize, region->length() - pos);
115
116                                 /* read from the master (original) sources for the region, 
117                                    not the ones currently in use, in case it's already been 
118                                    subject to timefx.  
119                                 */
120
121                                 if ((this_read = region->master_read_at (buffer, buffer, gain_buffer, pos + region->position(), this_time)) != this_time) {
122                                         error << string_compose (_("tempoize: error reading data from %1"), nsrcs[i]->name()) << endmsg;
123                                         goto out;
124                                 }
125                         
126                                 pos += this_read;
127                                 done += this_read;
128
129                                 tsr.progress = (float) done / total_frames;
130                                 
131                                 st.putSamples (buffer, this_read);
132                         
133                                 while ((this_read = st.receiveSamples (buffer, bufsize)) > 0 && !tsr.cancel) {
134                                         if (nsrcs[i]->write (buffer, this_read) != this_read) {
135                                                 error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name()) << endmsg;
136                                                 goto out;
137                                         }
138                                 }
139                         }
140                 
141                         if (!tsr.cancel) {
142                                 st.flush ();
143                         }
144                 
145                         while (!tsr.cancel && (this_read = st.receiveSamples (buffer, bufsize)) > 0) {
146                                 if (nsrcs[i]->write (buffer, this_read) != this_read) {
147                                         error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name()) << endmsg;
148                                         goto out;
149                                 }
150                         }
151                 }
152
153         } catch (runtime_error& err) {
154                 error << _("timefx code failure. please notify ardour-developers.") << endmsg;
155                 error << err.what() << endmsg;
156                 goto out;
157         }
158
159         new_name = region->name();
160         at = new_name.find ('@');
161
162         // remove any existing stretch indicator
163
164         if (at != string::npos && at > 2) {
165                 new_name = new_name.substr (0, at - 1);
166         }
167
168         new_name += suffix;
169
170         ret = finish (region, nsrcs, new_name);
171
172         /* now reset ancestral data for each new region */
173
174         for (vector<boost::shared_ptr<AudioRegion> >::iterator x = results.begin(); x != results.end(); ++x) {
175                 nframes64_t astart = (*x)->ancestral_start();
176                 nframes64_t alength = (*x)->ancestral_length();
177                 nframes_t start;
178                 nframes_t length;
179
180                 // note: tsr.fraction is a percentage of original length. 100 = no change, 
181                 // 50 is half as long, 200 is twice as long, etc.
182
183                 float stretch = (*x)->stretch() * (tsr.time_fraction/100.0);
184
185                 start = (nframes_t) floor (astart + ((astart - (*x)->start()) / stretch));
186                 length = (nframes_t) floor (alength / stretch);
187
188                 (*x)->set_ancestral_data (start, length, stretch, (*x)->shift());
189         }
190
191   out:
192
193         if (gain_buffer) {
194                 delete [] gain_buffer;
195         }
196
197         if (buffer) {
198                 delete [] buffer;
199         }
200
201         if (ret || tsr.cancel) {
202                 for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
203                         (*si)->mark_for_remove ();
204                 }
205         }
206         
207         tsr.done = true;
208
209         return ret;
210 }