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/convert.h>
39 #include <ardour/ardour.h>
40 #include <ardour/session.h>
41 #include <ardour/session_directory.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 std::auto_ptr<ImportableSource>
56 open_importable_source (const string& path, nframes_t samplerate,
57 ARDOUR::SrcQuality quality)
59 std::auto_ptr<ImportableSource> source(new ImportableSource(path));
61 if (source->samplerate() == samplerate) {
65 return std::auto_ptr<ImportableSource>(
66 new ResampledImportableSource(path, samplerate, quality)
71 get_non_existent_filename (const std::string& basename, uint channel, uint channels)
74 bool goodfile = false;
75 string base(basename);
80 snprintf (buf, sizeof(buf), "%s-L.wav", base.c_str());
82 snprintf (buf, sizeof(buf), "%s-R.wav", base.c_str());
84 } else if (channels > 1) {
85 snprintf (buf, sizeof(buf), "%s-c%d.wav", base.c_str(), channel+1);
87 snprintf (buf, sizeof(buf), "%s.wav", base.c_str());
90 if (sys::exists (buf)) {
92 /* if the file already exists, we must come up with
93 * a new name for it. for now we just keep appending
104 } while ( !goodfile);
110 write_audio_data_to_new_files (ImportableSource* source, Session::import_status& status,
111 vector<boost::shared_ptr<AudioFileSource> >& newfiles)
113 const nframes_t nframes = ResampledImportableSource::blocksize;
114 uint channels = source->channels();
116 boost::scoped_array<float> data(new float[nframes * channels]);
117 vector<boost::shared_array<Sample> > channel_data;
119 for (uint n = 0; n < channels; ++n) {
120 channel_data.push_back(boost::shared_array<Sample>(new Sample[nframes]));
124 status.progress = 0.0f;
126 while (!status.cancel) {
128 nframes_t nread, nfread;
132 if ((nread = source->read (data.get(), nframes)) == 0) {
135 nfread = nread / channels;
139 for (chn = 0; chn < channels; ++chn) {
142 for (x = chn, n = 0; n < nfread; x += channels, ++n) {
143 channel_data[chn][n] = (Sample) data[x];
149 for (chn = 0; chn < channels; ++chn) {
150 newfiles[chn]->write (channel_data[chn].get(), nfread);
154 status.progress = read_count / (source->ratio () * source->length() * channels);
159 Session::import_audiofile (import_status& status)
162 vector<string> new_paths;
165 status.sources.clear ();
167 for (vector<Glib::ustring>::iterator p = status.paths.begin(); p != status.paths.end(); ++p, ++cnt) {
169 std::auto_ptr<ImportableSource> source;
173 source = open_importable_source (*p, frame_rate(), status.quality);
175 catch (const failed_constructor& err)
177 error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg;
178 status.done = status.cancel = true;
182 vector<boost::shared_ptr<AudioFileSource> > newfiles;
184 for (uint n = 0; n < source->channels(); ++n) {
185 newfiles.push_back (boost::shared_ptr<AudioFileSource>());
188 SessionDirectory sdir(get_best_session_directory_for_new_source ());
190 const string basename = sys::basename (string(*p));
192 for (uint n = 0; n < source->channels(); ++n) {
194 std::string filename = get_non_existent_filename (basename, n, source->channels());
196 sys::path filepath = sdir.sound_path() / filename;
199 newfiles[n] = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *this, filepath.to_string().c_str(), false, frame_rate()));
202 catch (failed_constructor& err) {
203 error << string_compose(_("Session::import_audiofile: cannot open new file source for channel %1"), n+1) << endmsg;
207 new_paths.push_back (filepath.to_string());
208 newfiles[n]->prepare_for_peakfile_writes ();
211 if ((nframes_t) source->samplerate() != frame_rate()) {
212 status.doing_what = string_compose (_("converting %1\n(resample from %2KHz to %3KHz)\n(%4 of %5)"),
213 sys::path(*p).leaf(),
214 source->samplerate()/1000.0f,
215 frame_rate()/1000.0f,
216 cnt, status.paths.size());
219 status.doing_what = string_compose (_("converting %1\n(%2 of %3)"),
220 sys::path(*p).leaf(),
221 cnt, status.paths.size());
225 write_audio_data_to_new_files (source.get(), status, newfiles);
231 std::copy (newfiles.begin(), newfiles.end(), std::back_inserter(status.sources));
238 if (!status.cancel) {
242 now = localtime (&xnow);
243 status.freeze = true;
245 /* flush the final length(s) to the header(s) */
247 for (SourceList::iterator x = status.sources.begin(); x != status.sources.end(); ++x) {
248 boost::dynamic_pointer_cast<AudioFileSource>(*x)->update_header(0, *now, xnow);
249 boost::dynamic_pointer_cast<AudioSource>(*x)->done_with_peakfile_writes ();
252 /* save state so that we don't lose these new Sources */
262 status.sources.clear ();
264 for (vector<string>::iterator i = new_paths.begin(); i != new_paths.end(); ++i) {