move all destructive functionality into SndFileSource as a mode, and drop Destructive...
[ardour.git] / libs / ardour / audiofilesource.cc
index 0663f5f9b1047c6d11096c259cc616a018a13fa5..1fbde870a0a4c65ca50faae76764257f09741961 100644 (file)
@@ -38,6 +38,7 @@
 #include <ardour/sndfilesource.h>
 #include <ardour/destructive_filesource.h>
 #include <ardour/session.h>
+#include <ardour/source_factory.h>
 
 // if these headers come before sigc++ is included
 // the parser throws ObjC++ errors. (nil is a keyword)
@@ -56,17 +57,16 @@ string AudioFileSource::peak_dir = "";
 string AudioFileSource::search_path;
 
 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
-bool               AudioFileSource::header_position_negative;
-uint64_t           AudioFileSource::header_position_offset;
+uint64_t           AudioFileSource::header_position_offset = 0;
 
-char   AudioFileSource::bwf_country_code[3] = "US";
-char   AudioFileSource::bwf_organization_code[4] = "LAS";
+/* XXX maybe this too */
 char   AudioFileSource::bwf_serial_number[13] = "000000000000";
 
-AudioFileSource::AudioFileSource (string idstr, Flag flags)
-       : AudioSource (idstr), _flags (flags)
+AudioFileSource::AudioFileSource (Session& s, string idstr, Flag flags)
+       : AudioSource (s, idstr), _flags (flags)
 {
        /* constructor used for existing external to session files. file must exist already */
+       _is_embedded = AudioFileSource::determine_embeddedness (idstr);
 
        if (init (idstr, true)) {
                throw failed_constructor ();
@@ -74,18 +74,19 @@ AudioFileSource::AudioFileSource (string idstr, Flag flags)
 
 }
 
-AudioFileSource::AudioFileSource (std::string path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
-       : AudioSource (path), _flags (flags)
+AudioFileSource::AudioFileSource (Session& s, std::string path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
+       : AudioSource (s, path), _flags (flags)
 {
        /* constructor used for new internal-to-session files. file cannot exist */
+       _is_embedded = false;
 
        if (init (path, false)) {
                throw failed_constructor ();
        }
 }
 
-AudioFileSource::AudioFileSource (const XMLNode& node)
-       : AudioSource (node), _flags (Flag (Writable|CanRename))
+AudioFileSource::AudioFileSource (Session& s, const XMLNode& node)
+       : AudioSource (s, node), _flags (Flag (Writable|CanRename))
 {
        /* constructor used for existing internal-to-session files. file must exist */
 
@@ -106,11 +107,16 @@ AudioFileSource::~AudioFileSource ()
        }
 }
 
+bool
+AudioFileSource::determine_embeddedness (std::string path)
+{
+       return (path.find("/") == 0);
+}
+
 bool
 AudioFileSource::removable () const
 {
-       return (_flags & Removable) && ((_flags & RemoveAtDestroy) || 
-                                     ((_flags & RemovableIfEmpty) && is_empty (_path)));
+       return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
 }
 
 int
@@ -119,8 +125,11 @@ AudioFileSource::init (string pathstr, bool must_exist)
        bool is_new = false;
 
        _length = 0;
+       timeline_position = 0;
        next_peak_clear_should_notify = false;
-       
+       _peaks_built = false;
+       file_is_new = false;
+
        if (!find (pathstr, must_exist, is_new)) {
                return -1;
        }
@@ -136,7 +145,7 @@ AudioFileSource::init (string pathstr, bool must_exist)
 string
 AudioFileSource::peak_path (string audio_path)
 {
-       return Session::peak_path_from_audio_path (audio_path);
+       return _session.peak_path_from_audio_path (audio_path);
 }
 
 string
@@ -165,95 +174,6 @@ AudioFileSource::old_peak_path (string audio_path)
        return res;
 }
 
-#ifdef HAVE_COREAUDIO
-
-AudioFileSource*
-AudioFileSource::create (const XMLNode& node)
-{
-       AudioFileSource* es = 0;
-
-       if (node.property (X_("destructive")) != 0) {
-               
-               es = new DestructiveFileSource (node);
-       
-       } else {
-               
-               try {
-                       es = new CoreAudioSource (node);
-               } 
-               
-               
-               catch (failed_constructor& err) {
-                       es = new SndFileSource (node);
-               }
-       }
-       
-       return es;
-}
-
-#else
-
-AudioFileSource*
-AudioFileSource::create (const XMLNode& node)
-{
-       if (node.property (X_("destructive")) != 0) {
-               
-               return new DestructiveFileSource (node);
-               
-       } else {
-               
-               return new SndFileSource (node);
-       }
-}
-
-#endif // HAVE_COREAUDIO
-
-#ifdef HAVE_COREAUDIO
-AudioFileSource*
-AudioFileSource::create (const string& idstr, Flag flags)
-{
-       AudioFileSource* es = 0;
-
-       try {
-               es = new CoreAudioSource (idstr, flags);
-       }
-
-       catch (failed_constructor& err) {
-               es = new SndFileSource (idstr, flags);
-       }
-
-       return es;
-}
-
-#else
-
-AudioFileSource*
-AudioFileSource::create (const string& idstr, Flag flags)
-{
-       return new SndFileSource (idstr, flags);
-}
-
-#endif // HAVE_COREAUDIO
-
-#ifdef HAVE_COREAUDIO
-std::string 
-CFStringRefToStdString(CFStringRef stringRef)
-{
-       CFIndex size = 
-               CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) , 
-               kCFStringEncodingASCII);
-           char *buf = new char[size];
-       
-       std::string result;
-
-       if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingASCII)) {
-           result = buf;
-       }
-       delete [] buf;
-       return result;
-}
-#endif // HAVE_COREAUDIO
-
 bool
 AudioFileSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
 {
@@ -365,6 +285,17 @@ AudioFileSource::set_state (const XMLNode& node)
 
        }
 
+       if ((prop = node.property (X_("name"))) != 0) {
+               _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
+       } else {
+               _is_embedded = false;
+       }
+
+       if ((prop = node.property (X_("destructive"))) != 0) {
+               /* old style, from the period when we had DestructiveFileSource */
+               _flags = Flag (_flags | Destructive);
+       }
+
        return 0;
 }
 
@@ -374,7 +305,8 @@ AudioFileSource::mark_for_remove ()
        if (!writable()) {
                return;
        }
-       _flags = Flag (_flags | RemoveAtDestroy);
+
+       _flags = Flag (_flags | Removable | RemoveAtDestroy);
 }
 
 void
@@ -405,6 +337,11 @@ AudioFileSource::mark_take (string id)
 int
 AudioFileSource::move_to_trash (const string trash_dir_name)
 {
+       if (is_embedded()) {
+               cerr << "tried to move an embedded region to trash" << endl;
+               return -1;
+       }
+
        string newpath;
 
        if (!writable()) {
@@ -552,7 +489,11 @@ AudioFileSource::find (string pathstr, bool must_exist, bool& isnew)
                /* external files and/or very very old style sessions include full paths */
                
                _path = pathstr;
-               _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
+               if (is_embedded()) {
+                       _name = pathstr;
+               } else {
+                       _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
+               }
                
                if (access (_path.c_str(), R_OK) != 0) {
 
@@ -592,25 +533,14 @@ AudioFileSource::set_search_path (string p)
 }
 
 void
-AudioFileSource::set_header_position_offset (jack_nframes_t offset, bool negative)
+AudioFileSource::set_header_position_offset (nframes_t offset)
 {
        header_position_offset = offset;
-       header_position_negative = negative;
-
        HeaderPositionOffsetChanged ();
 }
 
-void 
-AudioFileSource::handle_header_position_change ()
-{
-       if (writable()) {
-               set_header_timeline_position ();
-               flush_header ();
-       }
-}
-
 void
-AudioFileSource::set_timeline_position (jack_nframes_t pos)
+AudioFileSource::set_timeline_position (nframes_t pos)
 {
        timeline_position = pos;
 }
@@ -618,8 +548,14 @@ AudioFileSource::set_timeline_position (jack_nframes_t pos)
 void
 AudioFileSource::set_allow_remove_if_empty (bool yn)
 {
-       if (writable()) {
-               allow_remove_if_empty = yn;
+       if (!writable()) {
+               return;
+       }
+
+       if (yn) {
+               _flags = Flag (_flags | RemovableIfEmpty);
+       } else {
+               _flags = Flag (_flags & ~RemovableIfEmpty);
        }
 }
 
@@ -635,6 +571,12 @@ AudioFileSource::set_name (string newname, bool destructive)
                return -1;
        }
 
+       // Test whether newpath exists, if yes notify the user but continue. 
+       if (access(newpath.c_str(),F_OK) == 0) {
+               error << _("Programming error! Ardour tried to rename a file over another file! It's safe to continue working, but please report this to the developers.") << endmsg;
+               return -1;
+       }
+
        if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
                error << string_compose (_("cannot rename audio file for %1 to %2"), _name, newpath) << endmsg;
                return -1;
@@ -647,16 +589,24 @@ AudioFileSource::set_name (string newname, bool destructive)
 }
 
 bool
-AudioFileSource::is_empty (string path)
+AudioFileSource::is_empty (Session& s, string path)
 {
        bool ret = false;
-       AudioFileSource* afs = create (path, NoPeakFile);
+       boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (s, path, NoPeakFile, false));
 
        if (afs) {
                ret = (afs->length() == 0);
-               delete afs;
        }
 
        return ret;
 }
 
+int
+AudioFileSource::setup_peakfile ()
+{
+       if (!(_flags & NoPeakFile)) {
+               return initialize_peakfile (file_is_new, _path);
+       } else {
+               return 0;
+       }
+}