add latency callback to dummy backend
[ardour.git] / libs / ardour / smf_source.cc
index ad238e27e10e574ffa93f04f7612a4815b4ef607..34eec3b4a6c16acce77c4c9b04a26cd6c21d62e1 100644 (file)
 #include "pbd/stl_delete.h"
 #include "pbd/strsplit.h"
 
+#include <glib/gstdio.h>
 #include <glibmm/miscutils.h>
 #include <glibmm/fileutils.h>
 
 #include "evoral/Control.hpp"
+#include "evoral/SMF.hpp"
 
 #include "ardour/event_type_map.h"
 #include "ardour/midi_model.h"
@@ -48,6 +50,7 @@
 using namespace ARDOUR;
 using namespace Glib;
 using namespace PBD;
+using namespace Evoral;
 
 /** Constructor used for new internal-to-session files.  File cannot exist. */
 SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
@@ -62,9 +65,12 @@ SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
 {
        /* note that origin remains empty */
 
-       if (init(_path, false)) {
+       if (init (_path, false)) {
                throw failed_constructor ();
        }
+        assert (!Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
+       existence_check ();
 
        /* file is not opened until write */
 
@@ -79,6 +85,39 @@ SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
        _open = true;
 }
 
+/** Constructor used for external-to-session files.  File must exist. */
+SMFSource::SMFSource (Session& s, const string& path)
+       : Source(s, DataType::MIDI, path, Source::Flag (0))
+       , MidiSource(s, path, Source::Flag (0))
+       , FileSource(s, DataType::MIDI, path, string(), Source::Flag (0))
+       , Evoral::SMF()
+       , _last_ev_time_beats(0.0)
+       , _last_ev_time_frames(0)
+       , _smf_last_read_end (0)
+       , _smf_last_read_time (0)
+{
+       /* note that origin remains empty */
+
+       if (init (_path, false)) {
+               throw failed_constructor ();
+       }
+        assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
+       existence_check ();
+
+       /* file is not opened until write */
+
+       if (_flags & Writable) {
+               return;
+       }
+
+       if (open (_path)) {
+               throw failed_constructor ();
+       }
+
+       _open = true;
+}
+
 /** Constructor used for existing internal-to-session files. */
 SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
        : Source(s, node)
@@ -93,10 +132,13 @@ SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
                throw failed_constructor ();
        }
 
-       if (init(_path, true)) {
+       if (init (_path, true)) {
                throw failed_constructor ();
        }
 
+        assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
+       existence_check ();
+
        if (open(_path)) {
                throw failed_constructor ();
        }
@@ -107,7 +149,7 @@ SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
 SMFSource::~SMFSource ()
 {
        if (removable()) {
-               unlink (_path.c_str());
+               ::g_unlink (_path.c_str());
        }
 }
 
@@ -457,6 +499,15 @@ SMFSource::mark_midi_streaming_write_completed (Evoral::Sequence<Evoral::Musical
        mark_nonremovable ();
 }
 
+bool
+SMFSource::valid_midi_file (const string& file)
+{
+       if (safe_midi_file_extension (file) ) {
+               return (SMF::test (file) );
+       }
+       return false;
+}
+
 bool
 SMFSource::safe_midi_file_extension (const string& file)
 {