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