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