Merged with trunk R992.
[ardour.git] / libs / ardour / session_timefx.cc
1 /*
2     Copyright (C) 2003 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 <cerrno>
22 #include <stdexcept>
23
24 #include <pbd/basename.h>
25
26 #include <soundtouch/SoundTouch.h>
27
28 #include <ardour/session.h>
29 #include <ardour/audioregion.h>
30 #include <ardour/sndfilesource.h>
31 #include <ardour/region_factory.h>
32 #include <ardour/source_factory.h>
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39 using namespace soundtouch;
40
41 boost::shared_ptr<AudioRegion>
42 Session::tempoize_region (TimeStretchRequest& tsr)
43 {
44         SourceList sources;
45         SourceList::iterator it;
46         boost::shared_ptr<AudioRegion> r;
47         SoundTouch st;
48         string region_name;
49         string ident = X_("-TIMEFX-");
50         float percentage;
51         nframes_t total_frames;
52         nframes_t done;
53
54         /* the soundtouch code wants a *tempo* change percentage, which is 
55            of opposite sign to the length change.  
56         */
57
58         percentage = -tsr.fraction;
59
60         st.setSampleRate (frame_rate());
61         st.setChannels (1);
62         st.setTempoChange (percentage);
63         st.setPitchSemiTones (0);
64         st.setRateChange (0);
65         
66         st.setSetting(SETTING_USE_QUICKSEEK, tsr.quick_seek);
67         st.setSetting(SETTING_USE_AA_FILTER, tsr.antialias);
68
69         vector<string> names = tsr.region->master_source_names();
70
71         tsr.progress = 0.0f;
72         total_frames = tsr.region->length() * tsr.region->n_channels();
73         done = 0;
74
75         for (uint32_t i = 0; i < tsr.region->n_channels(); ++i) {
76                 string path = path_from_region_name (PBD::basename_nosuffix (names[i]), ident);
77
78                 if (path.length() == 0) {
79                         error << string_compose (_("tempoize: error creating name for new audio file based on %1"), tsr.region->name()) 
80                               << endmsg;
81                         goto out;
82                 }
83
84                 try {
85                         sources.push_back (boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *this, path, false, frame_rate())));
86
87                 } catch (failed_constructor& err) {
88                         error << string_compose (_("tempoize: error creating new audio file %1 (%2)"), path, strerror (errno)) << endmsg;
89                         goto out;
90                 }
91         }
92         
93         try {
94                 const nframes_t bufsize = 16384;
95
96                 for (uint32_t i = 0; i < sources.size(); ++i) {
97                         gain_t gain_buffer[bufsize];
98                         Sample buffer[bufsize];
99                         nframes_t pos = 0;
100                         nframes_t this_read = 0;
101
102                         boost::shared_ptr<AudioSource> asrc = boost::dynamic_pointer_cast<AudioSource>(sources[i]);
103                         if (!asrc) {
104                                 cerr << "FIXME: TimeFX for non-audio" << endl;
105                                 continue;
106                         }
107
108                         st.clear();
109                         while (tsr.running && pos < tsr.region->length()) {
110                                 nframes_t this_time;
111                         
112                                 this_time = min (bufsize, tsr.region->length() - pos);
113
114                                 /* read from the master (original) sources for the region, 
115                                    not the ones currently in use, in case it's already been 
116                                    subject to timefx.  */
117
118                                 if ((this_read = tsr.region->master_read_at (buffer, buffer, gain_buffer, pos + tsr.region->position(), this_time)) != this_time) {
119                                         error << string_compose (_("tempoize: error reading data from %1"), sources[i]->name()) << endmsg;
120                                         goto out;
121                                 }
122                         
123                                 pos += this_read;
124                                 done += this_read;
125
126                                 tsr.progress = (float) done / total_frames;
127                                 
128                                 st.putSamples (buffer, this_read);
129                         
130                                 while ((this_read = st.receiveSamples (buffer, bufsize)) > 0 && tsr.running) {
131                                         if (asrc->write (buffer, this_read) != this_read) {
132                                                 error << string_compose (_("error writing tempo-adjusted data to %1"), sources[i]->name()) << endmsg;
133                                                 goto out;
134                                         }
135                                 }
136                         }
137                 
138                         if (tsr.running) {
139                                 st.flush ();
140                         }
141                 
142                         while (tsr.running && (this_read = st.receiveSamples (buffer, bufsize)) > 0) {
143                                 if (asrc->write (buffer, this_read) != this_read) {
144                                         error << string_compose (_("error writing tempo-adjusted data to %1"), sources[i]->name()) << endmsg;
145                                         goto out;
146                                 }
147                         }
148                 }
149         } catch (runtime_error& err) {
150                 error << _("timefx code failure. please notify ardour-developers.") << endmsg;
151                 error << err.what() << endmsg;
152                 goto out;
153         }
154
155         time_t now;
156         struct tm* xnow;
157         time (&now);
158         xnow = localtime (&now);
159
160         for (it = sources.begin(); it != sources.end(); ++it) {
161                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*it);
162                 if (afs) {
163                         afs->update_header (tsr.region->position(), *xnow, now);
164                 }
165         }
166
167         region_name = tsr.region->name() + X_(".t");
168
169         r = (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, 0, sources.front()->length(), region_name,
170                                                                               0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile))));
171              
172   out:
173
174         if (sources.size()) {
175
176                 /* if we failed to complete for any reason, mark the new file 
177                    for deletion.
178                 */
179
180                 if ((!r || !tsr.running)) {
181                         for (it = sources.begin(); it != sources.end(); ++it) {
182                                 (*it)->mark_for_remove ();
183                         }
184                 }
185
186                 sources.clear ();
187         }
188         
189         /* if the process was cancelled, delete the region */
190
191         if (!tsr.running) {
192                 r.reset ();
193         }
194         
195         return r;
196 }