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 #ifdef USE_COREAUDIO_FOR_FILE_IO
54 #include <ardour/caimportable.h>
60 using namespace ARDOUR;
64 static boost::shared_ptr<ImportableSource>
65 open_importable_source (const string& path, nframes_t samplerate, ARDOUR::SrcQuality quality)
68 #ifdef USE_COREAUDIO_FOR_FILE_IO
70 /* see if we can use CoreAudio to handle the IO */
73 CAImportableSource* src = new CAImportableSource(path);
74 boost::shared_ptr<CAImportableSource> source (src);
76 if (source->samplerate() == samplerate) {
80 /* rewrap as a resampled source */
82 return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
87 /* fall back to SndFile */
92 boost::shared_ptr<SndFileImportableSource> source(new SndFileImportableSource(path));
94 if (source->samplerate() == samplerate) {
98 /* rewrap as a resampled source */
100 return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
107 #ifdef HAVE_COREAUDIO
108 #ifdef USE_COREAUDIO_FOR_FILE_IO
115 get_non_existent_filename (const bool allow_replacing, const std::string destdir, const std::string& basename, uint channel, uint channels)
117 char buf[PATH_MAX+1];
118 bool goodfile = false;
119 string base(basename);
124 snprintf (buf, sizeof(buf), "%s-L.wav", base.c_str());
126 snprintf (buf, sizeof(buf), "%s-R.wav", base.c_str());
128 } else if (channels > 1) {
129 snprintf (buf, sizeof(buf), "%s-c%d.wav", base.c_str(), channel+1);
131 snprintf (buf, sizeof(buf), "%s.wav", base.c_str());
135 string tempname = destdir + "/" + buf;
136 if (!allow_replacing && Glib::file_test (tempname, Glib::FILE_TEST_EXISTS)) {
138 /* if the file already exists, we must come up with
139 * a new name for it. for now we just keep appending
150 } while ( !goodfile);
155 static vector<string>
156 get_paths_for_new_sources (const bool allow_replacing, const string& import_file_path, const string& session_dir, uint channels)
158 vector<string> new_paths;
159 const string basename = basename_nosuffix (import_file_path);
161 for (uint n = 0; n < channels; ++n) {
163 std::string filepath;
165 filepath = session_dir;
167 filepath += get_non_existent_filename (allow_replacing, session_dir, basename, n, channels);
169 new_paths.push_back (filepath);
176 map_existing_mono_sources (const vector<string>& new_paths, Session& sess,
177 uint samplerate, vector<boost::shared_ptr<AudioFileSource> >& newfiles, Session *session)
179 for (vector<string>::const_iterator i = new_paths.begin();
180 i != new_paths.end(); ++i)
182 boost::shared_ptr<Source> source = session->source_by_path_and_channel(*i, 0);
185 error << string_compose(_("Could not find a source for %1 even though we are updating this file!"), (*i)) << endl;
189 newfiles.push_back(boost::dynamic_pointer_cast<AudioFileSource>(source));
195 create_mono_sources_for_writing (const vector<string>& new_paths, Session& sess,
196 uint samplerate, vector<boost::shared_ptr<AudioFileSource> >& newfiles)
198 for (vector<string>::const_iterator i = new_paths.begin();
199 i != new_paths.end(); ++i)
201 boost::shared_ptr<Source> source;
205 source = SourceFactory::createWritable (
208 false, // destructive
212 catch (const failed_constructor& err)
214 error << string_compose (_("Unable to create file %1 during import"), *i) << endmsg;
218 newfiles.push_back(boost::dynamic_pointer_cast<AudioFileSource>(source));
224 compose_status_message (const string& path,
225 uint file_samplerate,
226 uint session_samplerate,
230 if (file_samplerate != session_samplerate) {
231 return string_compose (_("converting %1\n(resample from %2KHz to %3KHz)\n(%4 of %5)"),
232 Glib::path_get_basename (path),
233 file_samplerate/1000.0f,
234 session_samplerate/1000.0f,
235 current_file, total_files);
238 return string_compose (_("converting %1\n(%2 of %3)"),
239 Glib::path_get_basename (path),
240 current_file, total_files);
244 write_audio_data_to_new_files (ImportableSource* source, Session::import_status& status,
245 vector<boost::shared_ptr<AudioFileSource> >& newfiles)
247 const nframes_t nframes = ResampledImportableSource::blocksize;
248 uint channels = source->channels();
250 boost::scoped_array<float> data(new float[nframes * channels]);
251 vector<boost::shared_array<Sample> > channel_data;
253 for (uint n = 0; n < channels; ++n) {
254 channel_data.push_back(boost::shared_array<Sample>(new Sample[nframes]));
258 status.progress = 0.0f;
260 while (!status.cancel) {
262 nframes_t nread, nfread;
266 if ((nread = source->read (data.get(), nframes)) == 0) {
269 nfread = nread / channels;
273 for (chn = 0; chn < channels; ++chn) {
276 for (x = chn, n = 0; n < nfread; x += channels, ++n) {
277 channel_data[chn][n] = (Sample) data[x];
283 for (chn = 0; chn < channels; ++chn) {
284 newfiles[chn]->write (channel_data[chn].get(), nfread);
288 status.progress = read_count / (source->ratio () * source->length() * channels);
293 remove_file_source (boost::shared_ptr<AudioFileSource> file_source)
295 ::unlink (file_source->path().c_str());
298 // This function is still unable to cleanly update an existing source, even though
299 // it is possible to set the import_status flag accordingly. The functinality
300 // is disabled at the GUI until the Source implementations are able to provide
301 // the necessary API.
303 Session::import_audiofiles (import_status& status)
306 typedef vector<boost::shared_ptr<AudioFileSource> > AudioSources;
307 AudioSources all_new_sources;
309 status.sources.clear ();
311 for (vector<Glib::ustring>::iterator p = status.paths.begin();
312 p != status.paths.end() && !status.cancel;
315 boost::shared_ptr<ImportableSource> source;
319 source = open_importable_source (*p, frame_rate(), status.quality);
322 catch (const failed_constructor& err)
324 error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg;
325 status.done = status.cancel = true;
329 vector<string> new_paths = get_paths_for_new_sources (status.replace_existing_source,
331 discover_best_sound_dir (),
334 AudioSources newfiles;
336 if (status.replace_existing_source) {
337 fatal << "THIS IS NOT IMPLEMENTED YET, IT SHOULD NEVER GET CALLED!!! DYING!" << endl;
338 status.cancel = !map_existing_mono_sources (new_paths, *this, frame_rate(), newfiles, this);
340 status.cancel = !create_mono_sources_for_writing (new_paths, *this, frame_rate(), newfiles);
343 // copy on cancel/failure so that any files that were created will be removed below
344 std::copy (newfiles.begin(), newfiles.end(), std::back_inserter(all_new_sources));
346 if (status.cancel) break;
348 for (AudioSources::iterator i = newfiles.begin(); i != newfiles.end(); ++i) {
349 (*i)->prepare_for_peakfile_writes ();
352 status.doing_what = compose_status_message (*p, source->samplerate(),
353 frame_rate(), cnt, status.total);
355 write_audio_data_to_new_files (source.get(), status, newfiles);
358 if (!status.cancel) {
362 now = localtime (&xnow);
363 status.freeze = true;
365 /* flush the final length(s) to the header(s) */
367 for (AudioSources::iterator x = all_new_sources.begin(); x != all_new_sources.end(); ++x)
369 (*x)->update_header(0, *now, xnow);
370 (*x)->done_with_peakfile_writes ();
372 /* now that there is data there, requeue the file for analysis */
374 if (Config->get_auto_analyse_audio()) {
375 Analyser::queue_source_for_analysis (boost::static_pointer_cast<Source>(*x), false);
379 /* save state so that we don't lose these new Sources */
383 std::copy (all_new_sources.begin(), all_new_sources.end(), std::back_inserter(status.sources));
385 // this can throw...but it seems very unlikely
386 std::for_each (all_new_sources.begin(), all_new_sources.end(), remove_file_source);