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 <pbd/basename.h>
35 #include <pbd/convert.h>
37 #include <ardour/ardour.h>
38 #include <ardour/session.h>
39 #include <ardour/audio_diskstream.h>
40 #include <ardour/sndfilesource.h>
41 #include <ardour/sndfile_helpers.h>
42 #include <ardour/audioregion.h>
43 #include <ardour/region_factory.h>
44 #include <ardour/source_factory.h>
45 #include <ardour/resampled_source.h>
49 using namespace ARDOUR;
53 Session::import_audiofile (import_status& status)
56 vector<boost::shared_ptr<AudioFileSource> > newfiles;
59 Sample **channel_data = 0;
66 vector<string> new_paths;
68 ImportableSource* importable = 0;
69 const nframes_t nframes = ResampledImportableSource::blocksize;
72 status.sources.clear ();
74 for (vector<Glib::ustring>::iterator p = status.paths.begin(); p != status.paths.end(); ++p, ++cnt) {
76 if ((in = sf_open ((*p).c_str(), SFM_READ, &info)) == 0) {
77 error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg;
83 if ((nframes_t) info.samplerate != frame_rate()) {
84 importable = new ResampledImportableSource (in, &info, frame_rate(), status.quality);
86 importable = new ImportableSource (in, &info);
91 for (int n = 0; n < info.channels; ++n) {
92 newfiles.push_back (boost::shared_ptr<AudioFileSource>());
95 sounds_dir = discover_best_sound_dir ();
96 basepath = PBD::basename_nosuffix ((*p));
98 for (int n = 0; n < info.channels; ++n) {
100 bool goodfile = false;
103 if (info.channels == 2) {
105 snprintf (buf, sizeof(buf), "%s/%s-L.wav", sounds_dir.c_str(), basepath.c_str());
107 snprintf (buf, sizeof(buf), "%s/%s-R.wav", sounds_dir.c_str(), basepath.c_str());
109 } else if (info.channels > 1) {
110 snprintf (buf, sizeof(buf), "%s/%s-c%d.wav", sounds_dir.c_str(), basepath.c_str(), n+1);
112 snprintf (buf, sizeof(buf), "%s/%s.wav", sounds_dir.c_str(), basepath.c_str());
115 if (Glib::file_test (buf, Glib::FILE_TEST_EXISTS)) {
117 /* if the file already exists, we must come up with
118 * a new name for it. for now we just keep appending
129 } while ( !goodfile);
132 newfiles[n] = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (*this, buf, false, frame_rate()));
135 catch (failed_constructor& err) {
136 error << string_compose(_("Session::import_audiofile: cannot open new file source for channel %1"), n+1) << endmsg;
140 new_paths.push_back (buf);
141 newfiles[n]->prepare_for_peakfile_writes ();
149 data = new float[nframes * info.channels];
152 delete [] channel_data;
155 channel_data = new Sample * [ info.channels ];
157 for (int n = 0; n < info.channels; ++n) {
158 channel_data[n] = new Sample[nframes];
163 if ((nframes_t) info.samplerate != frame_rate()) {
164 status.doing_what = string_compose (_("converting %1\n(resample from %2KHz to %3KHz)\n(%4 of %5)"),
166 info.samplerate/1000.0f,
167 frame_rate()/1000.0f,
168 cnt, status.paths.size());
171 status.doing_what = string_compose (_("converting %1\n(%2 of %3)"),
173 cnt, status.paths.size());
177 status.progress = 0.0;
179 while (!status.cancel) {
181 nframes_t nread, nfread;
185 if ((nread = importable->read (data, nframes)) == 0) {
188 nfread = nread / info.channels;
192 for (chn = 0; chn < info.channels; ++chn) {
195 for (x = chn, n = 0; n < nfread; x += info.channels, ++n) {
196 channel_data[chn][n] = (Sample) data[x];
202 for (chn = 0; chn < info.channels; ++chn) {
203 newfiles[chn]->write (channel_data[chn], nfread);
207 status.progress = so_far / (importable->ratio () * info.frames * info.channels);
214 for (int n = 0; n < info.channels; ++n) {
215 status.sources.push_back (newfiles[n]);
223 status.freeze = true;
227 now = localtime (&xnow);
229 /* flush the final length(s) to the header(s) */
231 for (SourceList::iterator x = status.sources.begin(); x != status.sources.end() && !status.cancel; ++x) {
232 boost::dynamic_pointer_cast<AudioFileSource>(*x)->update_header(0, *now, xnow);
233 boost::dynamic_pointer_cast<AudioSource>(*x)->done_with_peakfile_writes ();
236 /* save state so that we don't lose these new Sources */
238 if (!status.cancel) {
251 for (int n = 0; n < info.channels; ++n) {
252 delete [] channel_data[n];
254 delete [] channel_data;
259 status.sources.clear ();
261 for (vector<string>::iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
262 unlink ((*i).c_str());