2 Copyright (C) 2003 Paul Davis
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.
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.
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.
24 #include <pbd/basename.h>
26 #include <soundtouch/SoundTouch.h>
28 #include <ardour/session.h>
29 #include <ardour/audioregion.h>
30 #include <ardour/filesource.h>
31 #include <ardour/sndfilesource.h>
36 using namespace ARDOUR;
37 using namespace soundtouch;
40 Session::tempoize_region (TimeStretchRequest& tsr)
42 AudioRegion::SourceList sources;
43 AudioRegion::SourceList::iterator it;
47 string ident = X_("-TIMEFX-");
49 jack_nframes_t total_frames;
52 /* the soundtouch code wants a *tempo* change percentage, which is
53 of opposite sign to the length change.
56 percentage = -tsr.fraction;
58 st.setSampleRate (frame_rate());
60 st.setTempoChange (percentage);
61 st.setPitchSemiTones (0);
64 st.setSetting(SETTING_USE_QUICKSEEK, tsr.quick_seek);
65 st.setSetting(SETTING_USE_AA_FILTER, tsr.antialias);
67 vector<string> names = tsr.region->master_source_names();
70 total_frames = tsr.region->length() * tsr.region->n_channels();
73 for (uint32_t i = 0; i < tsr.region->n_channels(); ++i) {
74 string path = path_from_region_name (PBD::basename_nosuffix (names[i]), ident);
76 if (path.length() == 0) {
77 error << string_compose (_("tempoize: error creating name for new audio file based on %1"), tsr.region->name())
83 sources.push_back(new FileSource (path, frame_rate()));
84 } catch (failed_constructor& err) {
85 error << string_compose (_("tempoize: error creating new audio file %1 (%2)"), path, strerror (errno)) << endmsg;
91 const jack_nframes_t bufsize = 16384;
93 for (uint32_t i = 0; i < sources.size(); ++i) {
94 gain_t gain_buffer[bufsize];
95 Sample buffer[bufsize];
96 jack_nframes_t pos = 0;
97 jack_nframes_t this_read = 0;
100 while (tsr.running && pos < tsr.region->length()) {
101 jack_nframes_t this_time;
103 this_time = min (bufsize, tsr.region->length() - pos);
105 /* read from the master (original) sources for the region,
106 not the ones currently in use, in case it's already been
107 subject to timefx. */
109 if ((this_read = tsr.region->master_read_at (buffer, buffer, gain_buffer, pos + tsr.region->position(), this_time)) != this_time) {
110 error << string_compose (_("tempoize: error reading data from %1"), sources[i]->name()) << endmsg;
117 tsr.progress = (float) done / total_frames;
119 st.putSamples (buffer, this_read);
121 while ((this_read = st.receiveSamples (buffer, bufsize)) > 0 && tsr.running) {
122 if (sources[i]->write (buffer, this_read) != this_read) {
123 error << string_compose (_("error writing tempo-adjusted data to %1"), sources[i]->name()) << endmsg;
133 while (tsr.running && (this_read = st.receiveSamples (buffer, bufsize)) > 0) {
134 if (sources[i]->write (buffer, this_read) != this_read) {
135 error << string_compose (_("error writing tempo-adjusted data to %1"), sources[i]->name()) << endmsg;
140 } catch (runtime_error& err) {
141 error << _("timefx code failure. please notify ardour-developers.") << endmsg;
142 error << err.what() << endmsg;
149 xnow = localtime (&now);
151 for (it = sources.begin(); it != sources.end(); ++it) {
152 dynamic_cast<FileSource*>(*it)->update_header (0, *xnow, now);
155 region_name = tsr.region->name() + X_(".t");
157 r = new AudioRegion (sources, 0, sources.front()->length(), region_name,
158 0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile));
163 if (sources.size()) {
165 /* if we failed to complete for any reason, mark the new file
169 if ((r == 0 || !tsr.running)) {
170 for (it = sources.begin(); it != sources.end(); ++it) {
171 (*it)->mark_for_remove ();
177 /* if the process was cancelled, delete the region */