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>
49 #include <ardour/sndfileimportable.h>
50 #include <ardour/analyser.h>
53 #include <ardour/caimportable.h>
58 using namespace ARDOUR;
62 static boost::shared_ptr<ImportableSource>
63 open_importable_source (const string& path, nframes_t samplerate, ARDOUR::SrcQuality quality)
67 /* see if we can use CoreAudio to handle the IO */
70 boost::shared_ptr<CAImportableSource> source(new CAImportableSource(path));
72 if (source->samplerate() == samplerate) {
76 /* rewrap as a resampled source */
78 return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
83 /* fall back to SndFile */
88 boost::shared_ptr<SndFileImportableSource> source(new SndFileImportableSource(path));
90 if (source->samplerate() == samplerate) {
94 /* rewrap as a resampled source */
96 return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
103 #ifdef HAVE_COREAUDIO
109 get_non_existent_filename (const bool allow_replacing, const std::string destdir, const std::string& basename, uint channel, uint channels)
111 char buf[PATH_MAX+1];
112 bool goodfile = false;
113 string base(basename);
118 snprintf (buf, sizeof(buf), "%s-L.wav", base.c_str());
120 snprintf (buf, sizeof(buf), "%s-R.wav", base.c_str());
122 } else if (channels > 1) {
123 snprintf (buf, sizeof(buf), "%s-c%d.wav", base.c_str(), channel+1);
125 snprintf (buf, sizeof(buf), "%s.wav", base.c_str());
129 string tempname = destdir + "/" + buf;
130 if (!allow_replacing && Glib::file_test (tempname, Glib::FILE_TEST_EXISTS)) {
132 /* if the file already exists, we must come up with
133 * a new name for it. for now we just keep appending
144 } while ( !goodfile);
149 static vector<string>
150 get_paths_for_new_sources (const bool allow_replacing, const string& import_file_path, const string& session_dir, uint channels)
152 vector<string> new_paths;
153 const string basename = basename_nosuffix (import_file_path);
155 for (uint n = 0; n < channels; ++n) {
157 std::string filepath;
159 filepath = session_dir;
161 filepath += get_non_existent_filename (allow_replacing, session_dir, basename, n, channels);
163 new_paths.push_back (filepath);
170 map_existing_mono_sources (const vector<string>& new_paths, Session& sess,
171 uint samplerate, vector<boost::shared_ptr<AudioFileSource> >& newfiles, Session *session)
173 for (vector<string>::const_iterator i = new_paths.begin();
174 i != new_paths.end(); ++i)
176 boost::shared_ptr<Source> source = session->source_by_path_and_channel(*i, 0);
179 error << string_compose(_("Could not find a source for %1 even though we are updating this file!"), (*i)) << endl;
183 newfiles.push_back(boost::dynamic_pointer_cast<AudioFileSource>(source));
189 create_mono_sources_for_writing (const vector<string>& new_paths, Session& sess,
190 uint samplerate, vector<boost::shared_ptr<AudioFileSource> >& newfiles)
192 for (vector<string>::const_iterator i = new_paths.begin();
193 i != new_paths.end(); ++i)
195 boost::shared_ptr<Source> source;
199 source = SourceFactory::createWritable (
202 false, // destructive
206 catch (const failed_constructor& err)
208 error << string_compose (_("Unable to create file %1 during import"), *i) << endmsg;
212 newfiles.push_back(boost::dynamic_pointer_cast<AudioFileSource>(source));
218 compose_status_message (const string& path,
219 uint file_samplerate,
220 uint session_samplerate,
224 if (file_samplerate != session_samplerate) {
225 return string_compose (_("converting %1\n(resample from %2KHz to %3KHz)\n(%4 of %5)"),
226 Glib::path_get_basename (path),
227 file_samplerate/1000.0f,
228 session_samplerate/1000.0f,
229 current_file, total_files);
232 return string_compose (_("converting %1\n(%2 of %3)"),
233 Glib::path_get_basename (path),
234 current_file, total_files);
238 write_audio_data_to_new_files (ImportableSource* source, Session::import_status& status,
239 vector<boost::shared_ptr<AudioFileSource> >& newfiles)
241 const nframes_t nframes = ResampledImportableSource::blocksize;
242 uint channels = source->channels();
244 boost::scoped_array<float> data(new float[nframes * channels]);
245 vector<boost::shared_array<Sample> > channel_data;
247 for (uint n = 0; n < channels; ++n) {
248 channel_data.push_back(boost::shared_array<Sample>(new Sample[nframes]));
252 status.progress = 0.0f;
254 while (!status.cancel) {
256 nframes_t nread, nfread;
260 if ((nread = source->read (data.get(), nframes)) == 0) {
263 nfread = nread / channels;
267 for (chn = 0; chn < channels; ++chn) {
270 for (x = chn, n = 0; n < nfread; x += channels, ++n) {
271 channel_data[chn][n] = (Sample) data[x];
277 for (chn = 0; chn < channels; ++chn) {
278 newfiles[chn]->write (channel_data[chn].get(), nfread);
282 status.progress = read_count / (source->ratio () * source->length() * channels);
287 remove_file_source (boost::shared_ptr<AudioFileSource> file_source)
289 ::unlink (file_source->path().c_str());
292 // This function is still unable to cleanly update an existing source, even though
293 // it is possible to set the import_status flag accordingly. The functinality
294 // is disabled at the GUI until the Source implementations are able to provide
295 // the necessary API.
297 Session::import_audiofiles (import_status& status)
300 typedef vector<boost::shared_ptr<AudioFileSource> > AudioSources;
301 AudioSources all_new_sources;
303 status.sources.clear ();
305 for (vector<Glib::ustring>::iterator p = status.paths.begin();
306 p != status.paths.end() && !status.cancel;
309 boost::shared_ptr<ImportableSource> source;
313 source = open_importable_source (*p, frame_rate(), status.quality);
316 catch (const failed_constructor& err)
318 error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg;
319 status.done = status.cancel = true;
323 vector<string> new_paths = get_paths_for_new_sources (status.replace_existing_source,
325 discover_best_sound_dir (),
328 AudioSources newfiles;
330 if (status.replace_existing_source) {
331 fatal << "THIS IS NOT IMPLEMENTED YET, IT SHOULD NEVER GET CALLED!!! DYING!" << endl;
332 status.cancel = !map_existing_mono_sources (new_paths, *this, frame_rate(), newfiles, this);
334 status.cancel = !create_mono_sources_for_writing (new_paths, *this, frame_rate(), newfiles);
337 // copy on cancel/failure so that any files that were created will be removed below
338 std::copy (newfiles.begin(), newfiles.end(), std::back_inserter(all_new_sources));
340 if (status.cancel) break;
342 for (AudioSources::iterator i = newfiles.begin(); i != newfiles.end(); ++i) {
343 (*i)->prepare_for_peakfile_writes ();
346 status.doing_what = compose_status_message (*p, source->samplerate(),
347 frame_rate(), cnt, status.paths.size());
349 write_audio_data_to_new_files (source.get(), status, newfiles);
352 if (!status.cancel) {
356 now = localtime (&xnow);
357 status.freeze = true;
359 /* flush the final length(s) to the header(s) */
361 for (AudioSources::iterator x = all_new_sources.begin(); x != all_new_sources.end(); ++x)
363 (*x)->update_header(0, *now, xnow);
364 (*x)->done_with_peakfile_writes ();
366 /* now that there is data there, requeue the file for analysis */
368 if (Config->get_auto_analyse_audio()) {
369 Analyser::queue_source_for_analysis (boost::static_pointer_cast<Source>(*x), false);
373 /* save state so that we don't lose these new Sources */
377 std::copy (all_new_sources.begin(), all_new_sources.end(),
378 std::back_inserter(status.sources));
380 // this can throw...but it seems very unlikely
381 std::for_each (all_new_sources.begin(), all_new_sources.end(), remove_file_source);