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>
35 #include <pbd/basename.h>
37 #include <ardour/ardour.h>
38 #include <ardour/session.h>
39 #include <ardour/diskstream.h>
40 #include <ardour/filesource.h>
41 #include <ardour/sndfile_helpers.h>
42 #include <ardour/audioregion.h>
46 using namespace ARDOUR;
48 #define BLOCKSIZE 4096U
51 Session::import_audiofile (import_status& status)
54 FileSource **newfiles = 0;
55 ARDOUR::AudioRegion::SourceList sources;
58 Sample **channel_data = 0;
64 jack_nframes_t so_far;
67 vector<string> new_paths;
69 string tmp_convert_file;
71 status.new_regions.clear ();
73 if ((in = sf_open (status.pathname.c_str(), SFM_READ, &info)) == 0) {
74 error << string_compose(_("Import: cannot open input sound file \"%1\""), status.pathname) << endmsg;
77 if ((uint32_t) info.samplerate != frame_rate()) {
79 status.doing_what = _("resampling audio");
80 // resample to session frame_rate
81 if (sample_rate_convert(status, status.pathname, tmp_convert_file)) {
82 if ((in = sf_open (tmp_convert_file.c_str(), SFM_READ, &info)) == 0) {
83 error << string_compose(_("Import: cannot open converted sound file \"%1\""), tmp_convert_file) << endmsg;
86 } else if (!status.cancel){
88 error << string_compose(_("Import: error while resampling sound file \"%1\""), status.pathname) << endmsg;
97 newfiles = new FileSource *[info.channels];
98 for (n = 0; n < info.channels; ++n) {
102 sounds_dir = discover_best_sound_dir ();
103 basepath = PBD::basename_nosuffix (status.pathname);
105 for (n = 0; n < info.channels; ++n) {
107 bool goodfile = false;
110 if (info.channels == 2) {
112 snprintf (buf, sizeof(buf), "%s%s-L.wav", sounds_dir.c_str(), basepath.c_str());
114 snprintf (buf, sizeof(buf), "%s%s-R.wav", sounds_dir.c_str(), basepath.c_str());
116 } else if (info.channels > 1) {
117 snprintf (buf, sizeof(buf), "%s%s-c%lu.wav", sounds_dir.c_str(), basepath.c_str(), n+1);
119 snprintf (buf, sizeof(buf), "%s%s.wav", sounds_dir.c_str(), basepath.c_str());
122 if (::access (buf, F_OK) == 0) {
124 /* if the file already exists, we must come up with
125 * a new name for it. for now we just keep appending
136 } while ( !goodfile);
140 newfiles[n] = new FileSource (buf, frame_rate(), false, Config->get_native_file_data_format());
143 catch (failed_constructor& err) {
144 error << string_compose(_("Session::import_audiofile: cannot open new file source for channel %1"), n+1) << endmsg;
148 new_paths.push_back (buf);
153 data = new float[BLOCKSIZE * info.channels];
154 channel_data = new Sample * [ info.channels ];
155 workbuf = new char[BLOCKSIZE * 4];
157 for (n = 0; n < info.channels; ++n) {
158 channel_data[n] = new Sample[BLOCKSIZE];
163 status.doing_what = _("converting audio");
164 status.progress = 0.0;
166 while (!status.cancel) {
172 if ((nread = sf_readf_float (in, data, BLOCKSIZE)) == 0) {
178 for (chn = 0; chn < info.channels; ++chn) {
179 for (x = chn, n = 0; n < nread; x += info.channels, ++n) {
180 channel_data[chn][n] = (Sample) data[x];
186 for (chn = 0; chn < info.channels; ++chn) {
187 newfiles[chn]->write (channel_data[chn], nread, workbuf);
191 status.progress = so_far / (float) (info.frames * info.channels);
194 if (status.multichan) {
195 status.doing_what = _("building region");
197 status.doing_what = _("building regions");
200 status.freeze = true;
204 now = localtime (&xnow);
210 if (status.multichan) {
211 /* all sources are used in a single multichannel region */
212 for (n = 0; n < nfiles && !status.cancel; ++n) {
213 /* flush the final length to the header */
214 newfiles[n]->update_header(0, *now, xnow);
215 sources.push_back(newfiles[n]);
218 AudioRegion *r = new AudioRegion (sources, 0, newfiles[0]->length(), region_name_from_path (Glib::path_get_basename (basepath)),
219 0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile));
221 status.new_regions.push_back (r);
224 for (n = 0; n < nfiles && !status.cancel; ++n) {
226 /* flush the final length to the header */
228 newfiles[n]->update_header(0, *now, xnow);
230 /* The sources had zero-length when created, which means that the Session
231 did not bother to create whole-file AudioRegions for them. Do it now.
234 AudioRegion *r = new AudioRegion (*newfiles[n], 0, newfiles[n]->length(), region_name_from_path (Glib::path_get_basename (newfiles[n]->name())),
235 0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile | AudioRegion::Import));
237 status.new_regions.push_back (r);
241 /* save state so that we don't lose these new Sources */
243 if (!status.cancel) {
259 for (n = 0; n < info.channels; ++n) {
260 delete [] channel_data[n];
262 delete [] channel_data;
266 for (vector<AudioRegion *>::iterator i = status.new_regions.begin(); i != status.new_regions.end(); ++i) {
270 for (vector<string>::iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
271 unlink ((*i).c_str());
279 if (tmp_convert_file.length()) {
280 unlink(tmp_convert_file.c_str());
289 Session::build_tmp_convert_name(string infile)
291 string tmp_name(_path + "/." + Glib::path_get_basename (infile.c_str()) + "XXXXXX");
292 char* tmp = new char[tmp_name.length() + 1];
293 tmp_name.copy(tmp, string::npos);
294 tmp[tmp_name.length()] = 0;
296 string outfile = tmp;
303 Session::sample_rate_convert (import_status& status, string infile, string& outfile)
305 float input [BLOCKSIZE] ;
306 float output [BLOCKSIZE] ;
309 SRC_STATE* src_state ;
312 sf_count_t output_count = 0 ;
313 sf_count_t input_count = 0;
315 SNDFILE* in = sf_open(infile.c_str(), SFM_READ, &sf_info);
316 sf_count_t total_input_frames = sf_info.frames;
318 outfile = build_tmp_convert_name(infile);
319 SNDFILE* out = sf_open(outfile.c_str(), SFM_RDWR, &sf_info);
321 error << string_compose(_("Import: could not open temp file: %1"), outfile) << endmsg;
325 sf_seek (in, 0, SEEK_SET) ;
326 sf_seek (out, 0, SEEK_SET) ;
328 /* Initialize the sample rate converter. */
329 if ((src_state = src_new (SRC_SINC_BEST_QUALITY, sf_info.channels, &err)) == 0) {
330 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
334 src_data.end_of_input = 0 ; /* Set this later. */
336 /* Start with zero to force load in while loop. */
337 src_data.input_frames = 0 ;
338 src_data.data_in = input ;
340 src_data.src_ratio = (1.0 * frame_rate()) / sf_info.samplerate ;
342 src_data.data_out = output ;
343 src_data.output_frames = BLOCKSIZE / sf_info.channels ;
345 while (!status.cancel) {
346 /* If the input buffer is empty, refill it. */
347 if (src_data.input_frames == 0) {
348 src_data.input_frames = sf_readf_float (in, input, BLOCKSIZE / sf_info.channels) ;
349 src_data.data_in = input ;
351 /* The last read will not be a full buffer, so snd_of_input. */
352 if (src_data.input_frames < (int)BLOCKSIZE / sf_info.channels) {
353 src_data.end_of_input = SF_TRUE ;
357 if ((err = src_process (src_state, &src_data))) {
358 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
362 /* Terminate if at end */
363 if (src_data.end_of_input && src_data.output_frames_gen == 0) {
368 sf_writef_float (out, output, src_data.output_frames_gen) ;
369 output_count += src_data.output_frames_gen ;
370 input_count += src_data.input_frames_used;
372 src_data.data_in += src_data.input_frames_used * sf_info.channels ;
373 src_data.input_frames -= src_data.input_frames_used ;
375 status.progress = (float) input_count / total_input_frames;
378 src_state = src_delete (src_state) ;