Initialize uninitialized variable
[ardour.git] / libs / ardour / st_stretch.cc
1 /*
2  * Copyright (C) 2008-2009 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <algorithm>
22 #include <cmath>
23
24 #include "pbd/error.h"
25
26 #include "ardour/types.h"
27 #include "ardour/stretch.h"
28 #include "ardour/audiofilesource.h"
29 #include "ardour/session.h"
30 #include "ardour/audioregion.h"
31
32 #include "pbd/i18n.h"
33
34 using namespace std;
35 using namespace ARDOUR;
36 using namespace PBD;
37 using namespace soundtouch;
38
39 STStretch::STStretch (Session& s, TimeFXRequest& req)
40         : Filter (s)
41         , tsr (req)
42 {
43         float percentage;
44
45         /* the soundtouch code wants a *tempo* change percentage, which is
46            of opposite sign to the length change.
47         */
48
49         percentage = -tsr.time_fraction;
50
51         st.setSampleRate (s.sample_rate());
52         st.setChannels (1);
53         st.setTempoChange (percentage);
54         st.setPitchSemiTones (0);
55         st.setRateChange (0);
56
57         st.setSetting(SETTING_USE_QUICKSEEK, tsr.quick_seek);
58         st.setSetting(SETTING_USE_AA_FILTER, tsr.antialias);
59
60 }
61
62 STStretch::~STStretch ()
63 {
64 }
65
66 int
67 STStretch::run (boost::shared_ptr<Region> a_region, Progress* progress)
68 {
69         SourceList nsrcs;
70         samplecnt_t total_samples;
71         samplecnt_t done;
72         int ret = -1;
73         const samplecnt_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         progress->set_progress (0);
81         tsr.done = false;
82
83         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(a_region);
84
85         total_samples = region->length() * region->n_channels();
86         done = 0;
87
88         /* the name doesn't need to be super-precise, but allow for 2 fractional
89            digits just to disambiguate close but not identical stretches.
90         */
91
92         snprintf (suffix, sizeof (suffix), "@%d", (int) floor (tsr.time_fraction * 100.0f));
93
94         /* create new sources */
95
96         if (make_new_sources (region, nsrcs, suffix)) {
97                 goto out;
98         }
99
100         gain_buffer = new gain_t[bufsize];
101         buffer = new Sample[bufsize];
102
103         // soundtouch throws runtime_error on error
104
105         try {
106                 for (uint32_t i = 0; i < nsrcs.size(); ++i) {
107
108                         boost::shared_ptr<AudioSource> asrc
109                                 = boost::dynamic_pointer_cast<AudioSource>(nsrcs[i]);
110
111                         samplepos_t pos = 0;
112                         samplecnt_t this_read = 0;
113
114                         st.clear();
115
116                         while (!tsr.cancel && pos < region->length()) {
117                                 samplecnt_t this_time;
118
119                                 this_time = min (bufsize, region->length() - pos);
120
121                                 /* read from the master (original) sources for the region,
122                                    not the ones currently in use, in case it's already been
123                                    subject to timefx.
124                                 */
125
126                                 if ((this_read = region->master_read_at (buffer, buffer, gain_buffer, pos + region->position(), this_time)) != this_time) {
127                                         error << string_compose (_("tempoize: error reading data from %1"), asrc->name()) << endmsg;
128                                         goto out;
129                                 }
130
131                                 pos += this_read;
132                                 done += this_read;
133
134                                 progress->set_progress ((float) done / total_samples);
135
136                                 st.putSamples (buffer, this_read);
137
138                                 while ((this_read = st.receiveSamples (buffer, bufsize)) > 0 && !tsr.cancel) {
139                                         if (asrc->write (buffer, this_read) != this_read) {
140                                                 error << string_compose (_("error writing tempo-adjusted data to %1"), asrc->name()) << endmsg;
141                                                 goto out;
142                                         }
143                                 }
144                         }
145
146                         if (!tsr.cancel) {
147                                 st.flush ();
148                         }
149
150                         while (!tsr.cancel && (this_read = st.receiveSamples (buffer, bufsize)) > 0) {
151                                 if (asrc->write (buffer, this_read) != this_read) {
152                                         error << string_compose (_("error writing tempo-adjusted data to %1"), asrc->name()) << endmsg;
153                                         goto out;
154                                 }
155                         }
156                 }
157
158         } catch (runtime_error& err) {
159                 error << _("timefx code failure. please notify ardour-developers.") << endmsg;
160                 error << err.what() << endmsg;
161                 goto out;
162         }
163
164         new_name = region->name();
165         at = new_name.find ('@');
166
167         // remove any existing stretch indicator
168
169         if (at != string::npos && at > 2) {
170                 new_name = new_name.substr (0, at - 1);
171         }
172
173         new_name += suffix;
174
175         if (!tsr.cancel) {
176                 ret = finish (region, nsrcs, new_name);
177         }
178
179         /* now reset ancestral data for each new region */
180
181         for (vector<boost::shared_ptr<Region> >::iterator x = results.begin(); x != results.end(); ++x) {
182                 samplepos_t astart = (*x)->ancestral_start();
183                 samplepos_t alength = (*x)->ancestral_length();
184                 samplepos_t start;
185                 samplecnt_t length;
186
187                 // note: tsr.fraction is a percentage of original length. 100 = no change,
188                 // 50 is half as long, 200 is twice as long, etc.
189
190                 float stretch = (*x)->stretch() * (tsr.time_fraction/100.0);
191
192                 start = (samplepos_t) floor (astart + ((astart - (*x)->start()) / stretch));
193                 length = (samplecnt_t) floor (alength / stretch);
194
195                 (*x)->set_ancestral_data (start, length, stretch, (*x)->shift());
196         }
197
198   out:
199
200         delete [] gain_buffer;
201         delete [] buffer;
202
203         if (ret || tsr.cancel) {
204                 for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
205                         (*si)->mark_for_remove ();
206                 }
207         }
208
209         return ret;
210 }