slightly improved fixes for MIDI issues
[ardour.git] / libs / ardour / source_factory.cc
index 8432bcbd6fd83e40db8830214c697b763ed45c2e..148f737551c760f8193442dd4acb711635e49a50 100644 (file)
     $Id$
 */
 
+#include <pbd/error.h>
+
 #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>
+#endif
 
 #include "i18n.h"
 
 using namespace ARDOUR;
 using namespace std;
+using namespace PBD;
 
 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
 
-#ifdef HAVE_COREAUDIO
+int
+SourceFactory::setup_peakfile (boost::shared_ptr<Source> s)
+{
+       boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
+       if (as) {
+               if (as->setup_peakfile ()) {
+                       error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
+                       return -1;
+               }
+       }
 
+       return 0;
+}
 
 boost::shared_ptr<Source>
-SourceFactory::create (const XMLNode& node)
+SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
 {
-       if (node.property (X_("destructive")) != 0) {
-               
-               boost::shared_ptr<Source> ret (new DestructiveFileSource (node));
-               SourceCreated (ret);
-               return ret;
-               
-       } else {
+       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)
+{
+       /* this is allowed to throw */
+
+       DataType type = DataType::AUDIO;
+       const XMLProperty* prop = node.property("type");
+       if (prop) {
+               type = DataType(prop->value());
+       }
+       
+       if (type == DataType::AUDIO) {
                
+#ifdef HAVE_COREAUDIO
                try {
-                       boost::shared_ptr<Source> ret (new CoreAudioSource (node));
+                       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) {
-                       boost::shared_ptr<Source> ret (new SndFileSource (node));
+               
+                       /* 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;
                }
-       }
-       
-       return boost::shared_ptr<Source>();
-}
-
 #else
+               boost::shared_ptr<Source> ret (new SndFileSource (s, node));
 
-boost::shared_ptr<Source>
-SourceFactory::create (const XMLNode& node)
-{
-       if (node.property (X_("destructive")) != 0) {
+               if (setup_peakfile (ret)) {
+                       return boost::shared_ptr<Source>();
+               }
                
-               boost::shared_ptr<Source> ret (new DestructiveFileSource (node));
                SourceCreated (ret);
                return ret;
+#endif
+
+       } else if (type == DataType::MIDI) {
+
+               boost::shared_ptr<Source> ret (new SMFSource (s, node));
                
-       } else {
-               
-               boost::shared_ptr<Source> ret (new SndFileSource (node));
                SourceCreated (ret);
                return ret;
+
        }
+       
+       return boost::shared_ptr<Source> ();
 }
 
-#endif // HAVE_COREAUDIO
-
-#ifdef HAVE_COREAUDIO
 boost::shared_ptr<Source>
-SourceFactory::createReadable (string idstr, AudioFileSource::Flag flags, bool announce)
+SourceFactory::createReadable (DataType type, Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce)
 {
-       if (flags & Destructive) {
-               boost::shared_ptr<Source> ret (new DestructiveFileSource (idstr, flags));
-               if (announce) {
-                       SourceCreated (ret);
+       if (type == DataType::AUDIO) {
+       
+#ifdef HAVE_COREAUDIO
+               try {
+                       boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, chn, flags));
+                       if (setup_peakfile (ret)) {
+                               return boost::shared_ptr<Source>();
+                       }
+                       if (announce) {
+                               SourceCreated (ret);
+                       }
+                       return ret;
+               }
+
+               catch (failed_constructor& err) {
+                       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;
+               }
+#else
+               boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
+
+               if (setup_peakfile (ret)) {
+                       return boost::shared_ptr<Source>();
                }
-               return ret;
-       }
 
-       try {
-               boost::shared_ptr<Source> ret (new CoreAudioSource (idstr, flags));
                if (announce) {
                        SourceCreated (ret);
                }
+
                return ret;
-       }
+#endif
+
+       } else if (type == DataType::MIDI) {
+
+               // FIXME: flags?
+               boost::shared_ptr<Source> ret (new SMFSource (s, path, SMFSource::Flag(0)));
 
-       catch (failed_constructor& err) {
-               boost::shared_ptr<Source> ret (new SndFileSource (idstr, flags));
                if (announce) {
                        SourceCreated (ret);
                }
+
                return ret;
        }
 
        return boost::shared_ptr<Source>();
 }
 
-#else
-
 boost::shared_ptr<Source>
-SourceFactory::createReadable (string idstr, AudioFileSource::Flag flags, bool announce)
-{
-       boost::shared_ptr<Source> ret (new SndFileSource (idstr, flags));
-       if (announce) {
-               SourceCreated (ret);
-       }
-       return ret;
-}
-
-#endif // HAVE_COREAUDIO
-
-boost::shared_ptr<Source>
-SourceFactory::createWritable (std::string path, bool destructive, jack_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 */
-       
-       if (destructive) {
-               boost::shared_ptr<Source> ret (new DestructiveFileSource (path,
-                                                                         Config->get_native_file_data_format(),
-                                                                         Config->get_native_file_header_format(),
-                                                                         rate));
+
+       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));
                
-       } else {
-               boost::shared_ptr<Source> ret (new SndFileSource (path, 
-                                                                 Config->get_native_file_data_format(),
-                                                                 Config->get_native_file_header_format(),
-                                                                 rate));
                if (announce) {
                        SourceCreated (ret);
                }
                return ret;
+
        }
+
+       return boost::shared_ptr<Source> ();
 }