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.
31 #include <samplerate.h>
33 #include <pbd/basename.h>
34 #include <ardour/ardour.h>
35 #include <ardour/session.h>
36 #include <ardour/diskstream.h>
37 #include <ardour/filesource.h>
38 #include <ardour/sndfile_helpers.h>
39 #include <ardour/audioregion.h>
43 using namespace ARDOUR;
45 #define BLOCKSIZE 4096U
48 Session::import_audiofile (import_status& status)
51 FileSource **newfiles = 0;
52 ARDOUR::AudioRegion::SourceList sources;
55 Sample **channel_data = 0;
61 jack_nframes_t so_far;
64 vector<AudioRegion *> new_regions;
65 vector<string> new_paths;
67 string tmp_convert_file;
69 if ((in = sf_open (status.pathname.c_str(), SFM_READ, &info)) == 0) {
70 error << string_compose(_("Import: cannot open input sound file \"%1\""), status.pathname) << endmsg;
73 if ((uint32_t) info.samplerate != frame_rate()) {
75 status.doing_what = _("resampling audio");
76 // resample to session frame_rate
77 if (sample_rate_convert(status, status.pathname, tmp_convert_file)) {
78 if ((in = sf_open (tmp_convert_file.c_str(), SFM_READ, &info)) == 0) {
79 error << string_compose(_("Import: cannot open converted sound file \"%1\""), tmp_convert_file) << endmsg;
82 } else if (!status.cancel){
84 error << string_compose(_("Import: error while resampling sound file \"%1\""), status.pathname) << endmsg;
93 newfiles = new FileSource *[info.channels];
94 for (n = 0; n < info.channels; ++n) {
98 sounds_dir = discover_best_sound_dir ();
99 basepath = PBD::basename_nosuffix (status.pathname);
101 for (n = 0; n < info.channels; ++n) {
103 bool goodfile = false;
106 if (info.channels == 2) {
108 snprintf (buf, sizeof(buf), "%s%s-L.wav", sounds_dir.c_str(), basepath.c_str());
110 snprintf (buf, sizeof(buf), "%s%s-R.wav", sounds_dir.c_str(), basepath.c_str());
112 } else if (info.channels > 1) {
113 snprintf (buf, sizeof(buf), "%s%s-c%lu.wav", sounds_dir.c_str(), basepath.c_str(), n+1);
115 snprintf (buf, sizeof(buf), "%s%s.wav", sounds_dir.c_str(), basepath.c_str());
118 if (::access (buf, F_OK) == 0) {
120 /* if the file already exists, we must come up with
121 * a new name for it. for now we just keep appending
132 } while ( !goodfile);
136 newfiles[n] = new FileSource (buf, frame_rate());
139 catch (failed_constructor& err) {
140 error << string_compose(_("Session::import_audiofile: cannot open new file source for channel %1"), n+1) << endmsg;
144 new_paths.push_back (buf);
149 data = new float[BLOCKSIZE * info.channels];
150 channel_data = new Sample * [ info.channels ];
151 workbuf = new char[BLOCKSIZE * 4];
153 for (n = 0; n < info.channels; ++n) {
154 channel_data[n] = new Sample[BLOCKSIZE];
159 status.doing_what = _("converting audio");
160 status.progress = 0.0;
162 while (!status.cancel) {
168 if ((nread = sf_readf_float (in, data, BLOCKSIZE)) == 0) {
174 for (chn = 0; chn < info.channels; ++chn) {
175 for (x = chn, n = 0; n < nread; x += info.channels, ++n) {
176 channel_data[chn][n] = (Sample) data[x];
182 for (chn = 0; chn < info.channels; ++chn) {
183 newfiles[chn]->write (channel_data[chn], nread, workbuf);
187 status.progress = so_far / (float) (info.frames * info.channels);
190 if (status.multichan) {
191 status.doing_what = _("building region");
193 status.doing_what = _("building regions");
196 status.freeze = true;
200 now = localtime (&xnow);
206 if (status.multichan) {
207 /* all sources are used in a single multichannel region */
208 for (n = 0; n < nfiles && !status.cancel; ++n) {
209 /* flush the final length to the header */
210 newfiles[n]->update_header(0, *now, xnow);
211 sources.push_back(newfiles[n]);
214 AudioRegion *r = new AudioRegion (sources, 0, newfiles[0]->length(), region_name_from_path (PBD::basename(basepath)),
215 0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile));
217 new_regions.push_back (r);
220 for (n = 0; n < nfiles && !status.cancel; ++n) {
222 /* flush the final length to the header */
224 newfiles[n]->update_header(0, *now, xnow);
226 /* The sources had zero-length when created, which means that the Session
227 did not bother to create whole-file AudioRegions for them. Do it now.
230 AudioRegion *r = new AudioRegion (*newfiles[n], 0, newfiles[n]->length(), region_name_from_path (PBD::basename (newfiles[n]->name())),
231 0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile | AudioRegion::Import));
233 new_regions.push_back (r);
237 /* save state so that we don't lose these new Sources */
239 if (!status.cancel) {
255 for (n = 0; n < info.channels; ++n) {
256 delete [] channel_data[n];
258 delete [] channel_data;
262 for (vector<AudioRegion *>::iterator i = new_regions.begin(); i != new_regions.end(); ++i) {
266 for (vector<string>::iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
267 unlink ((*i).c_str());
275 if (tmp_convert_file.length()) {
276 unlink(tmp_convert_file.c_str());
285 Session::build_tmp_convert_name(string infile)
287 string tmp_name(_path + "/." + PBD::basename (infile.c_str()) + "XXXXXX");
288 char* tmp = new char[tmp_name.length() + 1];
289 tmp_name.copy(tmp, string::npos);
290 tmp[tmp_name.length()] = 0;
292 string outfile = tmp;
299 Session::sample_rate_convert (import_status& status, string infile, string& outfile)
301 float input [BLOCKSIZE] ;
302 float output [BLOCKSIZE] ;
305 SRC_STATE* src_state ;
308 sf_count_t output_count = 0 ;
309 sf_count_t input_count = 0;
311 SNDFILE* in = sf_open(infile.c_str(), SFM_READ, &sf_info);
312 sf_count_t total_input_frames = sf_info.frames;
314 outfile = build_tmp_convert_name(infile);
315 SNDFILE* out = sf_open(outfile.c_str(), SFM_RDWR, &sf_info);
317 error << string_compose(_("Import: could not open temp file: %1"), outfile) << endmsg;
321 sf_seek (in, 0, SEEK_SET) ;
322 sf_seek (out, 0, SEEK_SET) ;
324 /* Initialize the sample rate converter. */
325 if ((src_state = src_new (SRC_SINC_BEST_QUALITY, sf_info.channels, &err)) == 0) {
326 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
330 src_data.end_of_input = 0 ; /* Set this later. */
332 /* Start with zero to force load in while loop. */
333 src_data.input_frames = 0 ;
334 src_data.data_in = input ;
336 src_data.src_ratio = (1.0 * frame_rate()) / sf_info.samplerate ;
338 src_data.data_out = output ;
339 src_data.output_frames = BLOCKSIZE / sf_info.channels ;
341 while (!status.cancel) {
342 /* If the input buffer is empty, refill it. */
343 if (src_data.input_frames == 0) {
344 src_data.input_frames = sf_readf_float (in, input, BLOCKSIZE / sf_info.channels) ;
345 src_data.data_in = input ;
347 /* The last read will not be a full buffer, so snd_of_input. */
348 if (src_data.input_frames < (int)BLOCKSIZE / sf_info.channels) {
349 src_data.end_of_input = SF_TRUE ;
353 if ((err = src_process (src_state, &src_data))) {
354 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
358 /* Terminate if at end */
359 if (src_data.end_of_input && src_data.output_frames_gen == 0) {
364 sf_writef_float (out, output, src_data.output_frames_gen) ;
365 output_count += src_data.output_frames_gen ;
366 input_count += src_data.input_frames_used;
368 src_data.data_in += src_data.input_frames_used * sf_info.channels ;
369 src_data.input_frames -= src_data.input_frames_used ;
371 status.progress = (float) input_count / total_input_frames;
374 src_state = src_delete (src_state) ;