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 2048U
48 Session::import_audiofile (import_status& status)
51 FileSource **newfiles = 0;
52 ARDOUR::AudioRegion::SourceList sources;
55 Sample **channel_data = 0;
60 jack_nframes_t so_far;
63 vector<AudioRegion *> new_regions;
64 vector<string> new_paths;
66 string tmp_convert_file;
68 if ((in = sf_open (status.pathname.c_str(), SFM_READ, &info)) == 0) {
69 error << string_compose(_("Import: cannot open input sound file \"%1\""), status.pathname) << endmsg;
72 if ((uint32_t) info.samplerate != frame_rate()) {
74 status.doing_what = _("resampling audio");
75 // resample to session frame_rate
76 if (sample_rate_convert(status, status.pathname, tmp_convert_file)) {
77 if ((in = sf_open (tmp_convert_file.c_str(), SFM_READ, &info)) == 0) {
78 error << string_compose(_("Import: cannot open converted sound file \"%1\""), tmp_convert_file) << endmsg;
81 } else if (!status.cancel){
83 error << string_compose(_("Import: error while resampling sound file \"%1\""), status.pathname) << endmsg;
92 newfiles = new FileSource *[info.channels];
93 for (n = 0; n < info.channels; ++n) {
97 sounds_dir = discover_best_sound_dir ();
98 basepath = PBD::basename_nosuffix (status.pathname);
100 for (n = 0; n < info.channels; ++n) {
102 bool goodfile = false;
105 if (info.channels == 2) {
107 snprintf (buf, sizeof(buf), "%s%s-L.wav", sounds_dir.c_str(), basepath.c_str());
109 snprintf (buf, sizeof(buf), "%s%s-R.wav", sounds_dir.c_str(), basepath.c_str());
111 } else if (info.channels > 1) {
112 snprintf (buf, sizeof(buf), "%s%s-c%lu.wav", sounds_dir.c_str(), basepath.c_str(), n+1);
114 snprintf (buf, sizeof(buf), "%s%s.wav", sounds_dir.c_str(), basepath.c_str());
117 if (::access (buf, F_OK) == 0) {
119 /* if the file already exists, we must come up with
120 * a new name for it. for now we just keep appending
131 } while ( !goodfile);
135 newfiles[n] = new FileSource (buf, frame_rate());
138 catch (failed_constructor& err) {
139 error << string_compose(_("Session::import_audiofile: cannot open new file source for channel %1"), n+1) << endmsg;
143 new_paths.push_back (buf);
148 data = new float[BLOCKSIZE * info.channels];
149 channel_data = new Sample * [ info.channels ];
151 for (n = 0; n < info.channels; ++n) {
152 channel_data[n] = new Sample[BLOCKSIZE];
157 status.doing_what = _("converting audio");
158 status.progress = 0.0;
160 while (!status.cancel) {
166 if ((nread = sf_readf_float (in, data, BLOCKSIZE)) == 0) {
172 for (chn = 0; chn < info.channels; ++chn) {
173 for (x = chn, n = 0; n < nread; x += info.channels, ++n) {
174 channel_data[chn][n] = (Sample) data[x];
180 for (chn = 0; chn < info.channels; ++chn) {
181 newfiles[chn]->write (channel_data[chn], nread);
185 status.progress = so_far / (float) (info.frames * info.channels);
188 if (status.multichan) {
189 status.doing_what = _("building region");
191 status.doing_what = _("building regions");
194 status.freeze = true;
198 now = localtime (&xnow);
204 if (status.multichan) {
205 /* all sources are used in a single multichannel region */
206 for (n = 0; n < nfiles && !status.cancel; ++n) {
207 /* flush the final length to the header */
208 newfiles[n]->update_header(0, *now, xnow);
209 sources.push_back(newfiles[n]);
212 AudioRegion *r = new AudioRegion (sources, 0, newfiles[0]->length(), region_name_from_path (PBD::basename(basepath)),
213 0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile));
215 new_regions.push_back (r);
218 for (n = 0; n < nfiles && !status.cancel; ++n) {
220 /* flush the final length to the header */
222 newfiles[n]->update_header(0, *now, xnow);
224 /* The sources had zero-length when created, which means that the Session
225 did not bother to create whole-file AudioRegions for them. Do it now.
228 AudioRegion *r = new AudioRegion (*newfiles[n], 0, newfiles[n]->length(), region_name_from_path (PBD::basename (newfiles[n]->name())),
229 0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile | AudioRegion::Import));
231 new_regions.push_back (r);
235 /* save state so that we don't lose these new Sources */
237 if (!status.cancel) {
250 for (n = 0; n < info.channels; ++n) {
251 delete [] channel_data[n];
253 delete [] channel_data;
257 for (vector<AudioRegion *>::iterator i = new_regions.begin(); i != new_regions.end(); ++i) {
261 for (vector<string>::iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
262 unlink ((*i).c_str());
270 if (tmp_convert_file.length()) {
271 unlink(tmp_convert_file.c_str());
280 Session::build_tmp_convert_name(string infile)
282 string tmp_name(_path + "/." + PBD::basename (infile.c_str()) + "XXXXXX");
283 char* tmp = new char[tmp_name.length() + 1];
284 tmp_name.copy(tmp, string::npos);
285 tmp[tmp_name.length()] = 0;
287 string outfile = tmp;
294 Session::sample_rate_convert (import_status& status, string infile, string& outfile)
296 float input [BLOCKSIZE] ;
297 float output [BLOCKSIZE] ;
300 SRC_STATE* src_state ;
303 sf_count_t output_count = 0 ;
304 sf_count_t input_count = 0;
306 SNDFILE* in = sf_open(infile.c_str(), SFM_READ, &sf_info);
307 sf_count_t total_input_frames = sf_info.frames;
309 outfile = build_tmp_convert_name(infile);
310 SNDFILE* out = sf_open(outfile.c_str(), SFM_RDWR, &sf_info);
312 error << string_compose(_("Import: could not open temp file: %1"), outfile) << endmsg;
316 sf_seek (in, 0, SEEK_SET) ;
317 sf_seek (out, 0, SEEK_SET) ;
319 /* Initialize the sample rate converter. */
320 if ((src_state = src_new (SRC_SINC_BEST_QUALITY, sf_info.channels, &err)) == 0) {
321 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
325 src_data.end_of_input = 0 ; /* Set this later. */
327 /* Start with zero to force load in while loop. */
328 src_data.input_frames = 0 ;
329 src_data.data_in = input ;
331 src_data.src_ratio = (1.0 * frame_rate()) / sf_info.samplerate ;
333 src_data.data_out = output ;
334 src_data.output_frames = BLOCKSIZE / sf_info.channels ;
336 while (!status.cancel) {
337 /* If the input buffer is empty, refill it. */
338 if (src_data.input_frames == 0) {
339 src_data.input_frames = sf_readf_float (in, input, BLOCKSIZE / sf_info.channels) ;
340 src_data.data_in = input ;
342 /* The last read will not be a full buffer, so snd_of_input. */
343 if (src_data.input_frames < (int)BLOCKSIZE / sf_info.channels) {
344 src_data.end_of_input = SF_TRUE ;
348 if ((err = src_process (src_state, &src_data))) {
349 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
353 /* Terminate if at end */
354 if (src_data.end_of_input && src_data.output_frames_gen == 0) {
359 sf_writef_float (out, output, src_data.output_frames_gen) ;
360 output_count += src_data.output_frames_gen ;
361 input_count += src_data.input_frames_used;
363 src_data.data_in += src_data.input_frames_used * sf_info.channels ;
364 src_data.input_frames -= src_data.input_frames_used ;
366 status.progress = (float) input_count / total_input_frames;
369 src_state = src_delete (src_state) ;