slightly improved fixes for MIDI issues
[ardour.git] / libs / ardour / source_factory.cc
index 2cbd7624be982853330e4553633670c1728a73d0..148f737551c760f8193442dd4acb711635e49a50 100644 (file)
@@ -22,8 +22,9 @@
 
 #include <ardour/source_factory.h>
 #include <ardour/sndfilesource.h>
-#include <ardour/destructive_filesource.h>
+#include <ardour/silentfilesource.h>
 #include <ardour/configuration.h>
+#include <ardour/smf_source.h>
 
 #ifdef HAVE_COREAUDIO
 #include <ardour/coreaudiosource.h>
@@ -51,56 +52,77 @@ SourceFactory::setup_peakfile (boost::shared_ptr<Source> s)
        return 0;
 }
 
-#ifdef HAVE_COREAUDIO
+boost::shared_ptr<Source>
+SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
+{
+       boost::shared_ptr<Source> ret (new SilentFileSource (s, node, nframes, sr));
+       SourceCreated (ret);
+       return ret;
+}
+
 boost::shared_ptr<Source>
 SourceFactory::create (Session& s, const XMLNode& node)
 {
-       try {
-               boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
-               if (setup_peakfile (ret)) {
-                       return boost::shared_ptr<Source>();
-               }
-               SourceCreated (ret);
-               return ret;
-       } 
-       
+       /* this is allowed to throw */
+
+       DataType type = DataType::AUDIO;
+       const XMLProperty* prop = node.property("type");
+       if (prop) {
+               type = DataType(prop->value());
+       }
        
-       catch (failed_constructor& err) {
+       if (type == DataType::AUDIO) {
+               
+#ifdef HAVE_COREAUDIO
+               try {
+                       boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
+                       if (setup_peakfile (ret)) {
+                               return boost::shared_ptr<Source>();
+                       }
+                       SourceCreated (ret);
+                       return ret;
+               } 
+
+               catch (failed_constructor& err) {
+               
+                       /* this is allowed to throw */
+                       
+                       boost::shared_ptr<Source> ret (new SndFileSource (s, node));
+                       if (setup_peakfile (ret)) {
+                               return boost::shared_ptr<Source>();
+                       }
+                       SourceCreated (ret);
+                       return ret;
+               }
+#else
                boost::shared_ptr<Source> ret (new SndFileSource (s, node));
+
                if (setup_peakfile (ret)) {
                        return boost::shared_ptr<Source>();
                }
+               
                SourceCreated (ret);
                return ret;
-       }
-
-       return boost::shared_ptr<Source>();
-}
+#endif
 
-#else
+       } else if (type == DataType::MIDI) {
 
-boost::shared_ptr<Source>
-SourceFactory::create (Session& s, const XMLNode& node)
-{
-       boost::shared_ptr<Source> ret (new SndFileSource (s, node));
+               boost::shared_ptr<Source> ret (new SMFSource (s, node));
+               
+               SourceCreated (ret);
+               return ret;
 
-       if (setup_peakfile (ret)) {
-               return boost::shared_ptr<Source>();
        }
-
-       SourceCreated (ret);
-
-       return ret;
+       
+       return boost::shared_ptr<Source> ();
 }
 
-#endif // HAVE_COREAUDIO
-
-#ifdef HAVE_COREAUDIO
 boost::shared_ptr<Source>
-SourceFactory::createReadable (Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce)
+SourceFactory::createReadable (DataType type, Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce)
 {
-       if (!(flags & Destructive)) {
-
+       if (type == DataType::AUDIO) {
+       
+#ifdef HAVE_COREAUDIO
                try {
                        boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, chn, flags));
                        if (setup_peakfile (ret)) {
@@ -111,7 +133,7 @@ SourceFactory::createReadable (Session& s, string path, int chn, AudioFileSource
                        }
                        return ret;
                }
-               
+
                catch (failed_constructor& err) {
                        boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
                        if (setup_peakfile (ret)) {
@@ -122,60 +144,67 @@ SourceFactory::createReadable (Session& s, string path, int chn, AudioFileSource
                        }
                        return ret;
                }
-
-       } else {
-
+#else
                boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
+
                if (setup_peakfile (ret)) {
                        return boost::shared_ptr<Source>();
                }
+
                if (announce) {
                        SourceCreated (ret);
                }
-               return ret;
-       }
 
-       return boost::shared_ptr<Source>();
-}
+               return ret;
+#endif
 
-#else
+       } else if (type == DataType::MIDI) {
 
-boost::shared_ptr<Source>
-SourceFactory::createReadable (Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce)
-{
-       boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
+               // FIXME: flags?
+               boost::shared_ptr<Source> ret (new SMFSource (s, path, SMFSource::Flag(0)));
 
-       if (setup_peakfile (ret)) {
-               return boost::shared_ptr<Source>();
-       }
+               if (announce) {
+                       SourceCreated (ret);
+               }
 
-       if (announce) {
-               SourceCreated (ret);
+               return ret;
        }
 
-       return ret;
+       return boost::shared_ptr<Source>();
 }
 
-#endif // HAVE_COREAUDIO
-
 boost::shared_ptr<Source>
-SourceFactory::createWritable (Session& s, std::string path, bool destructive, nframes_t rate, bool announce)
+SourceFactory::createWritable (DataType type, Session& s, std::string path, bool destructive, nframes_t rate, bool announce)
 {
        /* this might throw failed_constructor(), which is OK */
 
-       boost::shared_ptr<Source> ret (new SndFileSource 
-                                      (s, path, 
-                                       Config->get_native_file_data_format(),
-                                       Config->get_native_file_header_format(),
-                                       rate,
-                                       (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
-                                        SndFileSource::default_writable_flags)));      
+       if (type == DataType::AUDIO) {
+               boost::shared_ptr<Source> ret (new SndFileSource 
+                               (s, path, 
+                                Config->get_native_file_data_format(),
+                                Config->get_native_file_header_format(),
+                                rate,
+                                (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
+                                 SndFileSource::default_writable_flags)));     
+
+               if (setup_peakfile (ret)) {
+                       return boost::shared_ptr<Source>();
+               }
+               if (announce) {
+                       SourceCreated (ret);
+               }
+               return ret;
+
+       } else if (type == DataType::MIDI) {
+
+               boost::shared_ptr<Source> ret (new SMFSource (s, path));
+               
+               if (announce) {
+                       SourceCreated (ret);
+               }
+               return ret;
 
-       if (setup_peakfile (ret)) {
-               return boost::shared_ptr<Source>();
-       }
-       if (announce) {
-               SourceCreated (ret);
        }
-       return ret;
+
+       return boost::shared_ptr<Source> ();
 }