merge from 2.0-ongoing by hand, minus key binding editor
[ardour.git] / libs / ardour / import.cc
1 /*
2     Copyright (C) 2000 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 */
19
20 #include <cstdio>
21 #include <cstdlib>
22 #include <string>
23 #include <climits>
24 #include <cerrno>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <time.h>
28
29 #include <sndfile.h>
30 #include <samplerate.h>
31
32 #include <glibmm.h>
33
34 #include <pbd/basename.h>
35 #include <pbd/convert.h>
36
37 #include <ardour/ardour.h>
38 #include <ardour/session.h>
39 #include <ardour/session_directory.h>
40 #include <ardour/audio_diskstream.h>
41 #include <ardour/sndfilesource.h>
42 #include <ardour/sndfile_helpers.h>
43 #include <ardour/audioregion.h>
44 #include <ardour/region_factory.h>
45 #include <ardour/source_factory.h>
46 #include <ardour/resampled_source.h>
47
48 #include "i18n.h"
49
50 using namespace ARDOUR;
51 using namespace PBD;
52
53 int
54 Session::import_audiofile (import_status& status)
55 {
56         SNDFILE *in;
57         vector<boost::shared_ptr<AudioFileSource> > newfiles;
58         SF_INFO info;
59         float *data = 0;
60         Sample **channel_data = 0;
61         int nfiles = 0;
62         string basepath;
63         string sounds_dir;
64         nframes_t so_far;
65         char buf[PATH_MAX+1];
66         int ret = -1;
67         vector<string> new_paths;
68         struct tm* now;
69         ImportableSource* importable = 0;
70         const nframes_t nframes = ResampledImportableSource::blocksize;
71         uint32_t cnt = 1;
72
73         status.sources.clear ();
74         
75         for (vector<Glib::ustring>::iterator p = status.paths.begin(); p != status.paths.end(); ++p, ++cnt) {
76
77                 if ((in = sf_open ((*p).c_str(), SFM_READ, &info)) == 0) {
78                         error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg;
79                         status.done = 1;
80                         status.cancel = 1;
81                         return -1;
82                 }
83                 
84                 if ((nframes_t) info.samplerate != frame_rate()) {
85                         importable = new ResampledImportableSource (in, &info, frame_rate(), status.quality);
86                 } else {
87                         importable = new ImportableSource (in, &info);
88                 }
89                 
90                 newfiles.clear ();
91
92                 for (int n = 0; n < info.channels; ++n) {
93                         newfiles.push_back (boost::shared_ptr<AudioFileSource>());
94                 }
95                 
96                 SessionDirectory sdir(get_best_session_directory_for_new_source ());
97                 sounds_dir = sdir.sound_path().to_string();
98
99                 basepath = PBD::basename_nosuffix ((*p));
100                 
101                 for (int n = 0; n < info.channels; ++n) {
102                         
103                         bool goodfile = false;
104                         
105                         do {
106                                 if (info.channels == 2) {
107                                         if (n == 0) {
108                                                 snprintf (buf, sizeof(buf), "%s/%s-L.wav", sounds_dir.c_str(), basepath.c_str());
109                                         } else {
110                                                 snprintf (buf, sizeof(buf), "%s/%s-R.wav", sounds_dir.c_str(), basepath.c_str());
111                                         }
112                                 } else if (info.channels > 1) {
113                                         snprintf (buf, sizeof(buf), "%s/%s-c%d.wav", sounds_dir.c_str(), basepath.c_str(), n+1);
114                                 } else {
115                                         snprintf (buf, sizeof(buf), "%s/%s.wav", sounds_dir.c_str(), basepath.c_str());
116                                 }
117
118                                 if (Glib::file_test (buf, Glib::FILE_TEST_EXISTS)) {
119
120                                         /* if the file already exists, we must come up with
121                                          *  a new name for it.  for now we just keep appending
122                                          *  _ to basepath
123                                          */
124                                 
125                                         basepath += "_";
126
127                                 } else {
128
129                                         goodfile = true;
130                                 }
131
132                         } while ( !goodfile);
133
134                         try { 
135                                 newfiles[n] = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *this, buf, false, frame_rate()));
136                         }
137
138                         catch (failed_constructor& err) {
139                                 error << string_compose(_("Session::import_audiofile: cannot open new file source for channel %1"), n+1) << endmsg;
140                                 goto out;
141                         }
142
143                         new_paths.push_back (buf);
144                         newfiles[n]->prepare_for_peakfile_writes ();
145                         nfiles++;
146                 }
147         
148                 data = new float[nframes * info.channels];
149                 channel_data = new Sample * [ info.channels ];
150         
151                 for (int n = 0; n < info.channels; ++n) {
152                         channel_data[n] = new Sample[nframes];
153                 }
154
155                 so_far = 0;
156
157                 if ((nframes_t) info.samplerate != frame_rate()) {
158                         status.doing_what = string_compose (_("converting %1\n(resample from %2KHz to %3KHz)\n(%4 of %5)"),
159                                                             basepath,
160                                                             info.samplerate/1000.0f,
161                                                             frame_rate()/1000.0f,
162                                                             cnt, status.paths.size());
163                                                             
164                 } else {
165                         status.doing_what = string_compose (_("converting %1\n(%2 of %3)"), 
166                                                             basepath,
167                                                             cnt, status.paths.size());
168
169                 }
170
171                 status.progress = 0.0;
172
173                 while (!status.cancel) {
174
175                         nframes_t nread, nfread;
176                         long x;
177                         long chn;
178                 
179                         if ((nread = importable->read (data, nframes)) == 0) {
180                                 break;
181                         }
182                         nfread = nread / info.channels;
183
184                         /* de-interleave */
185                                 
186                         for (chn = 0; chn < info.channels; ++chn) {
187
188                                 nframes_t n;
189                                 for (x = chn, n = 0; n < nfread; x += info.channels, ++n) {
190                                         channel_data[chn][n] = (Sample) data[x];
191                                 }
192                         }
193
194                         /* flush to disk */
195
196                         for (chn = 0; chn < info.channels; ++chn) {
197                                 newfiles[chn]->write (channel_data[chn], nfread);
198                         }
199
200                         so_far += nread;
201                         status.progress = so_far / (importable->ratio () * info.frames * info.channels);
202                 }
203
204                 if (status.cancel) {
205                         goto out;
206                 }
207                 
208                 for (int n = 0; n < info.channels; ++n) {
209                         status.sources.push_back (newfiles[n]);
210                 }
211
212                 if (status.cancel) {
213                         goto out;
214                 }
215         }
216         
217         status.freeze = true;
218
219         time_t xnow;
220         time (&xnow);
221         now = localtime (&xnow);
222
223         /* flush the final length(s) to the header(s) */
224
225         for (SourceList::iterator x = status.sources.begin(); x != status.sources.end() && !status.cancel; ++x) {
226                 boost::dynamic_pointer_cast<AudioFileSource>(*x)->update_header(0, *now, xnow);
227                 boost::dynamic_pointer_cast<AudioSource>(*x)->done_with_peakfile_writes ();
228         }
229
230         /* save state so that we don't lose these new Sources */
231
232         if (!status.cancel) {
233                 save_state (_name);
234         }
235
236         ret = 0;
237
238   out:
239
240         if (data) {
241                 delete [] data;
242         }
243         
244         if (channel_data) {
245                 for (int n = 0; n < info.channels; ++n) {
246                         delete [] channel_data[n];
247                 }
248                 delete [] channel_data;
249         }
250
251         if (status.cancel) {
252
253                 status.sources.clear ();
254
255                 for (vector<string>::iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
256                         unlink ((*i).c_str());
257                 }
258         }
259
260         if (importable) {
261                 delete importable;
262         }
263
264         sf_close (in);  
265         status.done = true;
266
267         return ret;
268 }