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