Add AutomationControl::parameter() for terseness.
[ardour.git] / libs / ardour / import.cc
index 643feb87ef3834254be18278bfe25fb021c2aaba..bc16cde1568a1de2af7e7274ca978f9f51baeb04 100644 (file)
@@ -34,7 +34,9 @@
 #include <pbd/basename.h>
 
 #include <ardour/ardour.h>
+#include <ardour/types.h>
 #include <ardour/session.h>
+#include <ardour/session_directory.h>
 #include <ardour/audio_diskstream.h>
 #include <ardour/sndfilesource.h>
 #include <ardour/sndfile_helpers.h>
@@ -117,7 +119,6 @@ Session::import_audiofile (import_status& status)
        float *data = 0;
        Sample **channel_data = 0;
        long nfiles = 0;
-       long n;
        string basepath;
        string sounds_dir;
        nframes_t so_far;
@@ -132,6 +133,8 @@ Session::import_audiofile (import_status& status)
 
        if ((in = sf_open (status.paths.front().c_str(), SFM_READ, &info)) == 0) {
                error << string_compose(_("Import: cannot open input sound file \"%1\""), status.paths.front()) << endmsg;
+               status.done = 1;
+               status.cancel = 1;
                return -1;
        }
 
@@ -141,14 +144,16 @@ Session::import_audiofile (import_status& status)
                importable = new ImportableSource (in, &info);
        }
 
-       for (n = 0; n < info.channels; ++n) {
+       for (int n = 0; n < info.channels; ++n) {
                newfiles.push_back (boost::shared_ptr<AudioFileSource>());
        }
 
-       sounds_dir = discover_best_sound_dir ();
+       SessionDirectory sdir(get_best_session_directory_for_new_source ());
+       sounds_dir = sdir.sound_path().to_string();
+
        basepath = PBD::basename_nosuffix (status.paths.front());
 
-       for (n = 0; n < info.channels; ++n) {
+       for (int n = 0; n < info.channels; ++n) {
 
                bool goodfile = false;
 
@@ -160,12 +165,12 @@ Session::import_audiofile (import_status& status)
                                        snprintf (buf, sizeof(buf), "%s/%s-R.wav", sounds_dir.c_str(), basepath.c_str());
                                }
                        } else if (info.channels > 1) {
-                               snprintf (buf, sizeof(buf), "%s/%s-c%lu.wav", sounds_dir.c_str(), basepath.c_str(), n+1);
+                               snprintf (buf, sizeof(buf), "%s/%s-c%d.wav", sounds_dir.c_str(), basepath.c_str(), n+1);
                        } else {
                                snprintf (buf, sizeof(buf), "%s/%s.wav", sounds_dir.c_str(), basepath.c_str());
                        }
 
-                       if (::access (buf, F_OK) == 0) {
+                       if (Glib::file_test (buf, Glib::FILE_TEST_EXISTS)) {
 
                                /* if the file already exists, we must come up with
                                 *  a new name for it.  for now we just keep appending
@@ -182,7 +187,8 @@ Session::import_audiofile (import_status& status)
                } while ( !goodfile);
 
                try { 
-                       newfiles[n] = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (*this, buf, false, frame_rate()));
+                       newfiles[n] = boost::dynamic_pointer_cast<AudioFileSource> (
+                               SourceFactory::createWritable (DataType::AUDIO, *this, buf, false, frame_rate()));
                }
 
                catch (failed_constructor& err) {
@@ -197,7 +203,7 @@ Session::import_audiofile (import_status& status)
        data = new float[nframes * info.channels];
        channel_data = new Sample * [ info.channels ];
        
-       for (n = 0; n < info.channels; ++n) {
+       for (int n = 0; n < info.channels; ++n) {
                channel_data[n] = new Sample[nframes];
        }
 
@@ -208,19 +214,21 @@ Session::import_audiofile (import_status& status)
 
        while (!status.cancel) {
 
-               nframes_t nread;
+               nframes_t nread, nfread;
                long x;
                long chn;
                
                if ((nread = importable->read (data, nframes)) == 0) {
                        break;
                }
+               nfread = nread / info.channels;
 
                /* de-interleave */
                                
                for (chn = 0; chn < info.channels; ++chn) {
-                       
-                       for (x = chn, n = 0; n < nframes; x += info.channels, ++n) {
+
+                       nframes_t n;
+                       for (x = chn, n = 0; n < nfread; x += info.channels, ++n) {
                                channel_data[chn][n] = (Sample) data[x];
                        }
                }
@@ -228,7 +236,7 @@ Session::import_audiofile (import_status& status)
                /* flush to disk */
 
                for (chn = 0; chn < info.channels; ++chn) {
-                       newfiles[chn]->write (channel_data[chn], nread / info.channels);
+                       newfiles[chn]->write (channel_data[chn], nfread);
                }
 
                so_far += nread;
@@ -254,7 +262,7 @@ Session::import_audiofile (import_status& status)
        if (status.multichan) {
                /* all sources are used in a single multichannel region */
 
-               for (n = 0; n < nfiles && !status.cancel; ++n) {
+               for (int n = 0; n < nfiles && !status.cancel; ++n) {
                        /* flush the final length to the header */
                        newfiles[n]->update_header(0, *now, xnow);
                        sources.push_back(newfiles[n]);
@@ -271,7 +279,7 @@ Session::import_audiofile (import_status& status)
                status.new_regions.push_back (r);
 
        } else {
-               for (n = 0; n < nfiles && !status.cancel; ++n) {
+               for (int n = 0; n < nfiles && !status.cancel; ++n) {
 
                        /* flush the final length to the header */
 
@@ -306,14 +314,13 @@ Session::import_audiofile (import_status& status)
        }
        
        if (channel_data) {
-               for (n = 0; n < info.channels; ++n) {
+               for (int n = 0; n < info.channels; ++n) {
                        delete [] channel_data[n];
                }
                delete [] channel_data;
        }
 
        if (status.cancel) {
-
                status.new_regions.clear ();
 
                for (vector<string>::iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
@@ -362,7 +369,7 @@ ResampledImportableSource::read (Sample* output, nframes_t nframes)
 
        if ((err = src_process (src_state, &src_data))) {
                error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
-               return false ;
+               return 0 ;
        } 
        
        /* Terminate if at end */
@@ -374,6 +381,6 @@ ResampledImportableSource::read (Sample* output, nframes_t nframes)
        src_data.data_in += src_data.input_frames_used * sf_info->channels ;
        src_data.input_frames -= src_data.input_frames_used ;
 
-       return nframes;
+       return src_data.output_frames_gen * sf_info->channels;
 }