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/session_directory.h>
43 #include <ardour/audio_diskstream.h>
44 #include <ardour/sndfilesource.h>
45 #include <ardour/sndfile_helpers.h>
46 #include <ardour/audioregion.h>
47 #include <ardour/region_factory.h>
48 #include <ardour/source_factory.h>
49 #include <ardour/resampled_source.h>
53 using namespace ARDOUR;
57 get_non_existent_filename (const std::string& basename, uint channel, uint channels)
60 bool goodfile = false;
61 string base(basename);
66 snprintf (buf, sizeof(buf), "%s-L.wav", base.c_str());
68 snprintf (buf, sizeof(buf), "%s-R.wav", base.c_str());
70 } else if (channels > 1) {
71 snprintf (buf, sizeof(buf), "%s-c%d.wav", base.c_str(), channel+1);
73 snprintf (buf, sizeof(buf), "%s.wav", base.c_str());
76 if (sys::exists (buf)) {
78 /* if the file already exists, we must come up with
79 * a new name for it. for now we just keep appending
96 write_audio_data_to_new_files (ImportableSource* source, Session::import_status& status,
97 vector<boost::shared_ptr<AudioFileSource> >& newfiles)
99 const nframes_t nframes = ResampledImportableSource::blocksize;
100 uint channels = source->channels();
102 boost::scoped_array<float> data(new float[nframes * channels]);
103 vector<boost::shared_array<Sample> > channel_data;
105 for (uint n = 0; n < channels; ++n) {
106 channel_data.push_back(boost::shared_array<Sample>(new Sample[nframes]));
110 status.progress = 0.0f;
112 while (!status.cancel) {
114 nframes_t nread, nfread;
118 if ((nread = source->read (data.get(), nframes)) == 0) {
121 nfread = nread / channels;
125 for (chn = 0; chn < channels; ++chn) {
128 for (x = chn, n = 0; n < nfread; x += channels, ++n) {
129 channel_data[chn][n] = (Sample) data[x];
135 for (chn = 0; chn < channels; ++chn) {
136 newfiles[chn]->write (channel_data[chn].get(), nfread);
140 status.progress = read_count / (source->ratio () * source->length() * channels);
145 Session::import_audiofile (import_status& status)
150 vector<string> new_paths;
154 status.sources.clear ();
156 for (vector<Glib::ustring>::iterator p = status.paths.begin(); p != status.paths.end(); ++p, ++cnt) {
158 boost::shared_ptr<SNDFILE> in (sf_open (p->c_str(), SFM_READ, &info), sf_close);
161 error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg;
167 std::auto_ptr<ImportableSource> importable;
169 if ((nframes_t) info.samplerate != frame_rate()) {
170 importable.reset(new ResampledImportableSource (in.get(), &info, frame_rate(), status.quality));
172 importable.reset(new ImportableSource (in.get(), &info));
175 vector<boost::shared_ptr<AudioFileSource> > newfiles;
177 for (int n = 0; n < info.channels; ++n) {
178 newfiles.push_back (boost::shared_ptr<AudioFileSource>());
181 SessionDirectory sdir(get_best_session_directory_for_new_source ());
183 basepath = PBD::basename_nosuffix ((*p));
185 for (int n = 0; n < info.channels; ++n) {
187 std::string filename = get_non_existent_filename (basepath, n, info.channels);
189 sys::path filepath = sdir.sound_path() / filename;
192 newfiles[n] = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *this, filepath.to_string().c_str(), false, frame_rate()));
195 catch (failed_constructor& err) {
196 error << string_compose(_("Session::import_audiofile: cannot open new file source for channel %1"), n+1) << endmsg;
200 new_paths.push_back (filepath.to_string());
201 newfiles[n]->prepare_for_peakfile_writes ();
204 if ((nframes_t) info.samplerate != frame_rate()) {
205 status.doing_what = string_compose (_("converting %1\n(resample from %2KHz to %3KHz)\n(%4 of %5)"),
207 info.samplerate/1000.0f,
208 frame_rate()/1000.0f,
209 cnt, status.paths.size());
212 status.doing_what = string_compose (_("converting %1\n(%2 of %3)"),
214 cnt, status.paths.size());
218 write_audio_data_to_new_files (importable.get(), status, newfiles);
224 for (int n = 0; n < info.channels; ++n) {
225 status.sources.push_back (newfiles[n]);
233 status.freeze = true;
237 now = localtime (&xnow);
239 /* flush the final length(s) to the header(s) */
241 for (SourceList::iterator x = status.sources.begin(); x != status.sources.end() && !status.cancel; ++x) {
242 boost::dynamic_pointer_cast<AudioFileSource>(*x)->update_header(0, *now, xnow);
243 boost::dynamic_pointer_cast<AudioSource>(*x)->done_with_peakfile_writes ();
246 /* save state so that we don't lose these new Sources */
248 if (!status.cancel) {
258 status.sources.clear ();
260 for (vector<string>::iterator i = new_paths.begin(); i != new_paths.end(); ++i) {