2 Copyright (C) 2000 Paul Davis
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.
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.
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.
30 #include <samplerate.h>
34 #include <boost/scoped_array.hpp>
35 #include <boost/shared_array.hpp>
37 #include <pbd/basename.h>
38 #include <pbd/convert.h>
40 #include <ardour/ardour.h>
41 #include <ardour/session.h>
42 #include <ardour/audio_diskstream.h>
43 #include <ardour/sndfilesource.h>
44 #include <ardour/sndfile_helpers.h>
45 #include <ardour/audioregion.h>
46 #include <ardour/region_factory.h>
47 #include <ardour/source_factory.h>
48 #include <ardour/resampled_source.h>
52 using namespace ARDOUR;
55 static std::auto_ptr<ImportableSource>
56 open_importable_source (const string& path, nframes_t samplerate, ARDOUR::SrcQuality quality)
58 std::auto_ptr<ImportableSource> source(new ImportableSource(path));
60 if (source->samplerate() == samplerate) {
64 return std::auto_ptr<ImportableSource>(new ResampledImportableSource(path, samplerate, quality));
68 get_non_existent_filename (const std::string& basename, uint channel, uint channels)
71 bool goodfile = false;
72 string base(basename);
77 snprintf (buf, sizeof(buf), "%s-L.wav", base.c_str());
79 snprintf (buf, sizeof(buf), "%s-R.wav", base.c_str());
81 } else if (channels > 1) {
82 snprintf (buf, sizeof(buf), "%s-c%d.wav", base.c_str(), channel+1);
84 snprintf (buf, sizeof(buf), "%s.wav", base.c_str());
87 if (Glib::file_test (buf, Glib::FILE_TEST_EXISTS)) {
89 /* if the file already exists, we must come up with
90 * a new name for it. for now we just keep appending
101 } while ( !goodfile);
106 static vector<string>
107 get_paths_for_new_sources (const string& import_file_path, const string& session_dir, uint channels)
109 vector<string> new_paths;
110 const string basename = basename_nosuffix (import_file_path);
112 for (uint n = 0; n < channels; ++n) {
114 std::string filepath;
116 filepath = session_dir;
118 filepath += get_non_existent_filename (basename, n, channels);
120 new_paths.push_back (filepath);
127 create_mono_sources_for_writing (const vector<string>& new_paths, Session& sess,
128 uint samplerate, vector<boost::shared_ptr<AudioFileSource> >& newfiles)
130 for (vector<string>::const_iterator i = new_paths.begin();
131 i != new_paths.end(); ++i)
133 boost::shared_ptr<Source> source;
137 source = SourceFactory::createWritable (
141 false, // destructive
145 catch (const failed_constructor& err)
147 error << string_compose (_("Unable to create file %1 during import"), *i) << endmsg;
151 newfiles.push_back(boost::dynamic_pointer_cast<AudioFileSource>(source));
157 compose_status_message (const string& path,
158 uint file_samplerate,
159 uint session_samplerate,
163 if (file_samplerate != session_samplerate) {
164 return string_compose (_("converting %1\n(resample from %2KHz to %3KHz)\n(%4 of %5)"),
165 Glib::path_get_basename (path),
166 file_samplerate/1000.0f,
167 session_samplerate/1000.0f,
168 current_file, total_files);
171 return string_compose (_("converting %1\n(%2 of %3)"),
172 Glib::path_get_basename (path),
173 current_file, total_files);
177 write_audio_data_to_new_files (ImportableSource* source, Session::import_status& status,
178 vector<boost::shared_ptr<AudioFileSource> >& newfiles)
180 const nframes_t nframes = ResampledImportableSource::blocksize;
181 uint channels = source->channels();
183 boost::scoped_array<float> data(new float[nframes * channels]);
184 vector<boost::shared_array<Sample> > channel_data;
186 for (uint n = 0; n < channels; ++n) {
187 channel_data.push_back(boost::shared_array<Sample>(new Sample[nframes]));
191 status.progress = 0.0f;
193 while (!status.cancel) {
195 nframes_t nread, nfread;
199 if ((nread = source->read (data.get(), nframes)) == 0) {
202 nfread = nread / channels;
206 for (chn = 0; chn < channels; ++chn) {
209 for (x = chn, n = 0; n < nfread; x += channels, ++n) {
210 channel_data[chn][n] = (Sample) data[x];
216 for (chn = 0; chn < channels; ++chn) {
217 newfiles[chn]->write (channel_data[chn].get(), nfread);
221 status.progress = read_count / (source->ratio () * source->length() * channels);
226 remove_file_source (boost::shared_ptr<AudioFileSource> file_source)
228 ::unlink (file_source->path().c_str());
232 Session::import_audiofiles (import_status& status)
235 typedef vector<boost::shared_ptr<AudioFileSource> > AudioSources;
236 AudioSources all_new_sources;
238 status.sources.clear ();
240 for (vector<Glib::ustring>::iterator p = status.paths.begin();
241 p != status.paths.end() && !status.cancel;
244 std::auto_ptr<ImportableSource> source;
248 source = open_importable_source (*p, frame_rate(), status.quality);
250 catch (const failed_constructor& err)
252 error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg;
253 status.done = status.cancel = true;
257 vector<string> new_paths = get_paths_for_new_sources (*p,
258 get_best_session_directory_for_new_source (),
261 AudioSources newfiles;
263 status.cancel = !create_mono_sources_for_writing (new_paths, *this, frame_rate(), newfiles);
265 // copy on cancel/failure so that any files that were created will be removed below
266 std::copy (newfiles.begin(), newfiles.end(), std::back_inserter(all_new_sources));
268 if (status.cancel) break;
270 for (AudioSources::iterator i = newfiles.begin(); i != newfiles.end(); ++i) {
271 (*i)->prepare_for_peakfile_writes ();
274 status.doing_what = compose_status_message (*p, source->samplerate(),
275 frame_rate(), cnt, status.paths.size());
277 write_audio_data_to_new_files (source.get(), status, newfiles);
280 if (!status.cancel) {
284 now = localtime (&xnow);
285 status.freeze = true;
287 /* flush the final length(s) to the header(s) */
289 for (AudioSources::iterator x = all_new_sources.begin();
290 x != all_new_sources.end(); ++x)
292 (*x)->update_header(0, *now, xnow);
293 (*x)->done_with_peakfile_writes ();
296 /* save state so that we don't lose these new Sources */
300 std::copy (all_new_sources.begin(), all_new_sources.end(),
301 std::back_inserter(status.sources));
303 // this can throw...but it seems very unlikely
304 std::for_each (all_new_sources.begin(), all_new_sources.end(), remove_file_source);