refactor playlist sources to allow for MIDI and upcoming work on save/restore
[ardour.git] / libs / ardour / source_factory.cc
index ca1dc3b35466859e312280138fafd56c567f934c..4d3f02b1dc68bad81516a0a40b77f624436f23bf 100644 (file)
@@ -28,6 +28,8 @@
 #include "pbd/pthread_utils.h"
 #include "pbd/stacktrace.h"
 
+#include "ardour/audioplaylist.h"
+#include "ardour/audio_playlist_source.h"
 #include "ardour/source_factory.h"
 #include "ardour/sndfilesource.h"
 #include "ardour/silentfilesource.h"
@@ -124,7 +126,9 @@ boost::shared_ptr<Source>
 SourceFactory::createSilent (Session& s, const XMLNode& node, framecnt_t nframes, float sr)
 {
        Source* src = new SilentFileSource (s, node, nframes, sr);
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
        // boost_debug_shared_ptr_mark_interesting (src, "Source");
+#endif
        boost::shared_ptr<Source> ret (src);
        // no analysis data - the file is non-existent
        SourceCreated (ret);
@@ -145,7 +149,9 @@ SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
 
                try {
                        Source* src = new SndFileSource (s, node);
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
                        // boost_debug_shared_ptr_mark_interesting (src, "Source");
+#endif
                        boost::shared_ptr<Source> ret (src);
                        if (setup_peakfile (ret, defer_peaks)) {
                                return boost::shared_ptr<Source>();
@@ -162,7 +168,9 @@ SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
                        /* this is allowed to throw */
 
                        Source *src = new CoreAudioSource (s, node);
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
                        // boost_debug_shared_ptr_mark_interesting (src, "Source");
+#endif
                        boost::shared_ptr<Source> ret (src);
 
                        if (setup_peakfile (ret, defer_peaks)) {
@@ -178,12 +186,14 @@ SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
                }
 
        } else if (type == DataType::MIDI) {
-               Source* src = new SMFSource (s, node);
+               boost::shared_ptr<SMFSource> src (new SMFSource (s, node));
+               src->load_model (true, true);
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
                // boost_debug_shared_ptr_mark_interesting (src, "Source");
-               boost::shared_ptr<Source> ret (src);
-               ret->check_for_analysis_data_on_disk ();
-               SourceCreated (ret);
-               return ret;
+#endif
+               src->check_for_analysis_data_on_disk ();
+               SourceCreated (src);
+               return src;
        }
 
        return boost::shared_ptr<Source>();
@@ -200,7 +210,9 @@ SourceFactory::createReadable (DataType type, Session& s, const string& path,
                        try {
 
                                Source* src = new SndFileSource (s, path, chn, flags);
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
                                // boost_debug_shared_ptr_mark_interesting (src, "Source");
+#endif
                                boost::shared_ptr<Source> ret (src);
                                
                                if (setup_peakfile (ret, defer_peaks)) {
@@ -218,7 +230,9 @@ SourceFactory::createReadable (DataType type, Session& s, const string& path,
 #ifdef USE_COREAUDIO_FOR_FILES
 
                                Source* src = new CoreAudioSource (s, path, chn, flags);
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
                                // boost_debug_shared_ptr_mark_interesting (src, "Source");
+#endif
                                boost::shared_ptr<Source> ret (src);
                                if (setup_peakfile (ret, defer_peaks)) {
                                        return boost::shared_ptr<Source>();
@@ -240,8 +254,11 @@ SourceFactory::createReadable (DataType type, Session& s, const string& path,
 
        } else if (type == DataType::MIDI) {
                
-               Source* src = new SMFSource (s, path, SMFSource::Flag(0));
+               SMFSource* src = new SMFSource (s, path, SMFSource::Flag(0));
+               src->load_model (true, true);
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
                // boost_debug_shared_ptr_mark_interesting (src, "Source");
+#endif
                boost::shared_ptr<Source> ret (src);
 
                if (announce) {
@@ -256,20 +273,22 @@ SourceFactory::createReadable (DataType type, Session& s, const string& path,
 }
 
 boost::shared_ptr<Source>
-SourceFactory::createWritable (DataType type, Session& s, const std::string& path, 
-                              bool destructive, nframes_t rate, bool announce, bool defer_peaks)
+SourceFactory::createWritable (DataType type, Session& s, const std::string& path, const std::string& origin,
+                              bool destructive, framecnt_t rate, bool announce, bool defer_peaks)
 {
        /* this might throw failed_constructor(), which is OK */
 
        if (type == DataType::AUDIO) {
-               Source* src = new SndFileSource (s, path, 
+               Source* src = new SndFileSource (s, path, origin,
                                s.config.get_native_file_data_format(),
                                s.config.get_native_file_header_format(),
                                rate,
                                (destructive
                                        ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
                                 : SndFileSource::default_writable_flags));
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
                // boost_debug_shared_ptr_mark_interesting (src, "Source");
+#endif
                boost::shared_ptr<Source> ret (src);
 
                if (setup_peakfile (ret, defer_peaks)) {
@@ -285,19 +304,65 @@ SourceFactory::createWritable (DataType type, Session& s, const std::string& pat
 
        } else if (type == DataType::MIDI) {
                 // XXX writable flags should belong to MidiSource too
-               Source* src = new SMFSource (s, path, SndFileSource::default_writable_flags); 
+               boost::shared_ptr<SMFSource> src (new SMFSource (s, path, SndFileSource::default_writable_flags));
+               assert (src->writable ());
+               
+               src->load_model (true, true);
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
                // boost_debug_shared_ptr_mark_interesting (src, "Source");
-               boost::shared_ptr<Source> ret (src);
+#endif
 
                // no analysis data - this is a new file
 
                if (announce) {
-                       SourceCreated (ret);
+                       SourceCreated (src);
                }
-               return ret;
+               return src;
 
        }
 
        return boost::shared_ptr<Source> ();
 }
 
+boost::shared_ptr<Source>
+SourceFactory::createFromPlaylist (DataType type, Session& s, boost::shared_ptr<Playlist> p, const std::string& name,
+                                  uint32_t chn, frameoffset_t start, framecnt_t len, bool copy, bool defer_peaks)
+{
+       if (type == DataType::AUDIO) {
+               try {
+
+                       boost::shared_ptr<AudioPlaylist> ap = boost::dynamic_pointer_cast<AudioPlaylist>(p);
+                       
+                       if (ap) {
+                               
+                               if (copy) {
+                                       ap.reset (new AudioPlaylist (ap, start, len, name, true));
+                                       start = 0;
+                               }
+                               
+                               Source* src = new AudioPlaylistSource (s, name, ap, chn, start, len, Source::Flag (0));
+                               boost::shared_ptr<Source> ret (src);
+                               
+                               if (setup_peakfile (ret, defer_peaks)) {
+                                       return boost::shared_ptr<Source>();
+                               }
+                               
+                               ret->check_for_analysis_data_on_disk ();
+                               
+                               /* we never announce these sources */
+                               
+                               return ret;
+                       }
+               }
+
+               catch (failed_constructor& err) {
+                       /* relax - return at function scope */
+               }
+
+       } else if (type == DataType::MIDI) {
+
+       }
+
+       return boost::shared_ptr<Source>();
+}
+