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