Large nasty commit in the form of a 5000 line patch chock-full of completely
[ardour.git] / libs / ardour / import.cc
index b9ebe5859e28e137e4e83113370cf3855d530f0d..b70a7bbc9c0154e8af0ca4cb9f8c462f78fd8193 100644 (file)
 #include <sndfile.h>
 #include <samplerate.h>
 
+#include <glibmm.h>
+
 #include <pbd/basename.h>
+
 #include <ardour/ardour.h>
 #include <ardour/session.h>
-#include <ardour/diskstream.h>
-#include <ardour/filesource.h>
+#include <ardour/audio_diskstream.h>
+#include <ardour/sndfilesource.h>
 #include <ardour/sndfile_helpers.h>
 #include <ardour/audioregion.h>
 
 #include "i18n.h"
 
 using namespace ARDOUR;
+using namespace PBD;
 
-#define BLOCKSIZE 2048U
+#define BLOCKSIZE 4096U
 
 int
 Session::import_audiofile (import_status& status)
 {
        SNDFILE *in;
-       FileSource **newfiles = 0;
-       ARDOUR::AudioRegion::SourceList sources;
+       AudioFileSource **newfiles = 0;
+       AudioRegion::SourceList sources;
        SF_INFO info;
        float *data = 0;
        Sample **channel_data = 0;
+       char * workbuf = 0;
        long nfiles = 0;
        long n;
        string basepath;
@@ -60,13 +65,14 @@ Session::import_audiofile (import_status& status)
        jack_nframes_t so_far;
        char buf[PATH_MAX+1];
        int ret = -1;
-       vector<AudioRegion *> new_regions;
        vector<string> new_paths;
        struct tm* now;
        string tmp_convert_file;
        
+       status.new_regions.clear ();
+
        if ((in = sf_open (status.pathname.c_str(), SFM_READ, &info)) == 0) {
-               error << compose(_("Import: cannot open input sound file \"%1\""), status.pathname) << endmsg;
+               error << string_compose(_("Import: cannot open input sound file \"%1\""), status.pathname) << endmsg;
                return -1;
        } else {
                if ((uint32_t) info.samplerate != frame_rate()) {
@@ -75,12 +81,12 @@ Session::import_audiofile (import_status& status)
                        // resample to session frame_rate
                        if (sample_rate_convert(status, status.pathname, tmp_convert_file)) {
                                if ((in = sf_open (tmp_convert_file.c_str(), SFM_READ, &info)) == 0) {
-                                       error << compose(_("Import: cannot open converted sound file \"%1\""), tmp_convert_file) << endmsg;
+                                       error << string_compose(_("Import: cannot open converted sound file \"%1\""), tmp_convert_file) << endmsg;
                                        return -1;
                                }
                        } else if (!status.cancel){
                                // error
-                               error << compose(_("Import: error while resampling sound file \"%1\""), status.pathname) << endmsg;
+                               error << string_compose(_("Import: error while resampling sound file \"%1\""), status.pathname) << endmsg;
                                return -1;
                        } else {
                                // canceled
@@ -89,7 +95,7 @@ Session::import_audiofile (import_status& status)
                }
        }
 
-       newfiles = new FileSource *[info.channels];
+       newfiles = new AudioFileSource *[info.channels];
        for (n = 0; n < info.channels; ++n) {
                newfiles[n] = 0;
        }
@@ -132,11 +138,14 @@ Session::import_audiofile (import_status& status)
 
                        
                try { 
-                       newfiles[n] = new FileSource (buf, frame_rate());
+                       newfiles[n] = new SndFileSource (buf, 
+                                                        Config->get_native_file_data_format(),
+                                                        Config->get_native_file_header_format(),
+                                                        frame_rate ());
                }
 
                catch (failed_constructor& err) {
-                       error << compose(_("Session::import_audiofile: cannot open new file source for channel %1"), n+1) << endmsg;
+                       error << string_compose(_("Session::import_audiofile: cannot open new file source for channel %1"), n+1) << endmsg;
                        goto out;
                }
 
@@ -147,7 +156,8 @@ Session::import_audiofile (import_status& status)
        
        data = new float[BLOCKSIZE * info.channels];
        channel_data = new Sample * [ info.channels ];
-
+       workbuf = new char[BLOCKSIZE * 4];
+       
        for (n = 0; n < info.channels; ++n) {
                channel_data[n] = new Sample[BLOCKSIZE];
        }
@@ -178,7 +188,7 @@ Session::import_audiofile (import_status& status)
                /* flush to disk */
 
                for (chn = 0; chn < info.channels; ++chn) {
-                       newfiles[chn]->write (channel_data[chn], nread);
+                       newfiles[chn]->write (channel_data[chn], nread, workbuf);
                }
 
                so_far += nread;
@@ -209,10 +219,10 @@ Session::import_audiofile (import_status& status)
                        sources.push_back(newfiles[n]);
                }
 
-               AudioRegion *r = new AudioRegion (sources, 0, newfiles[0]->length(), region_name_from_path (PBD::basename(basepath)),
+               AudioRegion *r = new AudioRegion (sources, 0, newfiles[0]->length(), region_name_from_path (Glib::path_get_basename (basepath)),
                                        0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile));
                
-               new_regions.push_back (r);
+               status.new_regions.push_back (r);
 
        } else {
                for (n = 0; n < nfiles && !status.cancel; ++n) {
@@ -225,10 +235,10 @@ Session::import_audiofile (import_status& status)
                           did not bother to create whole-file AudioRegions for them. Do it now.
                        */
                
-                       AudioRegion *r = new AudioRegion (*newfiles[n], 0, newfiles[n]->length(), region_name_from_path (PBD::basename (newfiles[n]->name())),
+                       AudioRegion *r = new AudioRegion (*newfiles[n], 0, newfiles[n]->length(), region_name_from_path (Glib::path_get_basename (newfiles[n]->name())),
                                                0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile | AudioRegion::Import));
 
-                       new_regions.push_back (r);
+                       status.new_regions.push_back (r);
                }
        }
        
@@ -245,7 +255,10 @@ Session::import_audiofile (import_status& status)
        if (data) {
                delete [] data;
        }
-
+       if (workbuf) {
+               delete [] workbuf;
+       }
+       
        if (channel_data) {
                for (n = 0; n < info.channels; ++n) {
                        delete [] channel_data[n];
@@ -254,7 +267,7 @@ Session::import_audiofile (import_status& status)
        }
 
        if (status.cancel) {
-               for (vector<AudioRegion *>::iterator i = new_regions.begin(); i != new_regions.end(); ++i) {
+               for (vector<AudioRegion *>::iterator i = status.new_regions.begin(); i != status.new_regions.end(); ++i) {
                        delete *i;
                }
 
@@ -279,7 +292,7 @@ Session::import_audiofile (import_status& status)
 string
 Session::build_tmp_convert_name(string infile)
 {
-       string tmp_name(_path + "/." + PBD::basename (infile.c_str()) + "XXXXXX");
+       string tmp_name(_path + "/." + Glib::path_get_basename (infile.c_str()) + "XXXXXX");
        char* tmp = new char[tmp_name.length() + 1];
        tmp_name.copy(tmp, string::npos);
        tmp[tmp_name.length()] = 0;
@@ -309,7 +322,7 @@ Session::sample_rate_convert (import_status& status, string infile, string& outf
        outfile = build_tmp_convert_name(infile);
        SNDFILE* out = sf_open(outfile.c_str(), SFM_RDWR, &sf_info);
        if(!out) {
-               error << compose(_("Import: could not open temp file: %1"), outfile) << endmsg;
+               error << string_compose(_("Import: could not open temp file: %1"), outfile) << endmsg;
                return false;
        }
        
@@ -318,7 +331,7 @@ Session::sample_rate_convert (import_status& status, string infile, string& outf
 
        /* Initialize the sample rate converter. */
        if ((src_state = src_new (SRC_SINC_BEST_QUALITY, sf_info.channels, &err)) == 0) {       
-               error << compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
+               error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
                return false ;
        }
 
@@ -346,7 +359,7 @@ Session::sample_rate_convert (import_status& status, string infile, string& outf
                } 
 
                if ((err = src_process (src_state, &src_data))) {
-                       error << compose(_("Import: %1"), src_strerror (err)) << endmsg ;
+                       error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
                        return false ;
                }