Merged with trunk R992.
[ardour.git] / libs / ardour / import.cc
index 3411481a3a7c3d88290cb7d3271f22af2ec9b416..f16a6e7d8c28f3fe990860e9d7377254f2aad7f5 100644 (file)
 #include <sndfile.h>
 #include <samplerate.h>
 
+#include <glibmm.h>
+
 #include <pbd/basename.h>
+
 #include <ardour/ardour.h>
+#include <ardour/types.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 <ardour/region_factory.h>
+#include <ardour/source_factory.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;
+       vector<boost::shared_ptr<AudioFileSource> > newfiles;
+       SourceList sources;
        SF_INFO info;
        float *data = 0;
        Sample **channel_data = 0;
@@ -57,14 +65,15 @@ Session::import_audiofile (import_status& status)
        long n;
        string basepath;
        string sounds_dir;
-       jack_nframes_t so_far;
+       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 << string_compose(_("Import: cannot open input sound file \"%1\""), status.pathname) << endmsg;
                return -1;
@@ -89,11 +98,10 @@ Session::import_audiofile (import_status& status)
                }
        }
 
-       newfiles = new FileSource *[info.channels];
        for (n = 0; n < info.channels; ++n) {
-               newfiles[n] = 0;
+               newfiles.push_back (boost::shared_ptr<AudioFileSource>());
        }
-       
+
        sounds_dir = discover_best_sound_dir ();
        basepath = PBD::basename_nosuffix (status.pathname);
 
@@ -130,9 +138,9 @@ Session::import_audiofile (import_status& status)
 
                } while ( !goodfile);
 
-                       
                try { 
-                       newfiles[n] = new FileSource (buf, frame_rate());
+                       newfiles[n] = boost::dynamic_pointer_cast<AudioFileSource> (
+                               SourceFactory::createWritable (DataType::AUDIO, *this, buf, false, frame_rate()));
                }
 
                catch (failed_constructor& err) {
@@ -147,7 +155,7 @@ Session::import_audiofile (import_status& status)
        
        data = new float[BLOCKSIZE * info.channels];
        channel_data = new Sample * [ info.channels ];
-
+       
        for (n = 0; n < info.channels; ++n) {
                channel_data[n] = new Sample[BLOCKSIZE];
        }
@@ -209,10 +217,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)),
-                                       0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile));
+               boost::shared_ptr<AudioRegion> r (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (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 +233,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())),
-                                               0, AudioRegion::Flag (AudioRegion::DefaultFlags | AudioRegion::WholeFile | AudioRegion::Import));
-
-                       new_regions.push_back (r);
+                       status.new_regions.push_back (boost::dynamic_pointer_cast<AudioRegion> 
+                                                     (RegionFactory::create (boost::static_pointer_cast<Source> (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))));
                }
        }
        
@@ -245,7 +253,7 @@ Session::import_audiofile (import_status& status)
        if (data) {
                delete [] data;
        }
-
+       
        if (channel_data) {
                for (n = 0; n < info.channels; ++n) {
                        delete [] channel_data[n];
@@ -254,19 +262,13 @@ Session::import_audiofile (import_status& status)
        }
 
        if (status.cancel) {
-               for (vector<AudioRegion *>::iterator i = new_regions.begin(); i != new_regions.end(); ++i) {
-                       delete *i;
-               }
+               status.new_regions.clear ();
 
                for (vector<string>::iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
                        unlink ((*i).c_str());
                }
        }
 
-       if (newfiles) {
-               delete [] newfiles;
-       }
-
        if (tmp_convert_file.length()) {
                unlink(tmp_convert_file.c_str());
        }
@@ -279,7 +281,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;
@@ -304,14 +306,18 @@ Session::sample_rate_convert (import_status& status, string infile, string& outf
        sf_count_t  input_count = 0;
 
        SNDFILE* in = sf_open(infile.c_str(), SFM_READ, &sf_info);
+       if (!in) {
+               error << string_compose(_("Import/SRC: could not open input file: %1"), outfile) << endmsg;
+               return false;
+       }
        sf_count_t total_input_frames = sf_info.frames;
        
        outfile = build_tmp_convert_name(infile);
        SNDFILE* out = sf_open(outfile.c_str(), SFM_RDWR, &sf_info);
-       if(!out) {
-               error << string_compose(_("Import: could not open temp file: %1"), outfile) << endmsg;
-               return false;
-       }
+       if (!out) {
+               error << string_compose(_("Import/SRC: could not open output file: %1"), outfile) << endmsg;
+               return false;
+       }
        
        sf_seek (in, 0, SEEK_SET) ;
        sf_seek (out, 0, SEEK_SET) ;
@@ -370,8 +376,6 @@ Session::sample_rate_convert (import_status& status, string infile, string& outf
        sf_close(in);
        sf_close(out);
 
-       status.done = true;
-
        if (status.cancel) {
                return false;
        } else {