more MTC debugging
[ardour.git] / libs / ardour / smf_source.cc
index 679f244dbe80dfe90552c91e35c3e6b23a10b1cf..f48fc301f71cd7d77de994c964814a8b91e29d06 100644 (file)
@@ -39,6 +39,7 @@
 #include "ardour/event_type_map.h"
 #include "ardour/midi_model.h"
 #include "ardour/midi_ring_buffer.h"
+#include "ardour/midi_state_tracker.h"
 #include "ardour/session.h"
 #include "ardour/smf_source.h"
 
@@ -48,22 +49,24 @@ using namespace ARDOUR;
 using namespace Glib;
 
 /** Constructor used for new internal-to-session files.  File cannot exist. */
-SMFSource::SMFSource (Session& s, const ustring& path, bool embedded, Source::Flag flags)
+SMFSource::SMFSource (Session& s, const ustring& path, Source::Flag flags)
        : Source(s, DataType::MIDI, path, flags)
        , MidiSource(s, path)
-       , FileSource(s, DataType::MIDI, path, embedded, flags)
+       , FileSource(s, DataType::MIDI, path, flags)
        , Evoral::SMF()
        , _last_ev_time_beats(0.0)
        , _last_ev_time_frames(0)
        , _smf_last_read_end (0)
 {
-       if (init(_name, false)) {
+       if (init(_path, false)) {
                throw failed_constructor ();
        }
 
        if (create(path)) {
                throw failed_constructor ();
        }
+
+       load_model(true, true); // FIXME
 }
 
 /** Constructor used for existing internal-to-session files. */
@@ -75,17 +78,19 @@ SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
        , _last_ev_time_frames(0)
        , _smf_last_read_end (0)
 {
-       if (set_state(node)) {
+       if (set_state(node, Stateful::loading_state_version)) {
                throw failed_constructor ();
        }
 
-       if (init(_name, true)) {
+       if (init(_path, true)) {
                throw failed_constructor ();
        }
 
        if (open(_path)) {
                throw failed_constructor ();
        }
+
+       load_model(true, true); // FIXME
 }
 
 SMFSource::~SMFSource ()
@@ -97,9 +102,10 @@ SMFSource::~SMFSource ()
 
 /** All stamps in audio frames */
 nframes_t
-SMFSource::read_unlocked (MidiRingBuffer<nframes_t>& destination, sframes_t source_start,
-               sframes_t start, nframes_t duration,
-               sframes_t stamp_offset, sframes_t negative_stamp_offset) const
+SMFSource::read_unlocked (Evoral::EventSink<nframes_t>& destination, sframes_t source_start,
+                         sframes_t start, nframes_t duration,
+                         sframes_t stamp_offset, sframes_t negative_stamp_offset,
+                         MidiStateTracker* tracker) const
 {
        int      ret  = 0;
        uint64_t time = 0; // in SMF ticks, 1 tick per _ppqn
@@ -114,7 +120,7 @@ SMFSource::read_unlocked (MidiRingBuffer<nframes_t>& destination, sframes_t sour
 
        size_t scratch_size = 0; // keep track of scratch to minimize reallocs
 
-       BeatsFramesConverter converter(_session, source_start);
+       BeatsFramesConverter converter(_session.tempo_map(), source_start);
 
        const uint64_t start_ticks = (uint64_t)(converter.from(start) * ppqn());
 
@@ -166,11 +172,18 @@ SMFSource::read_unlocked (MidiRingBuffer<nframes_t>& destination, sframes_t sour
 
                if (ev_frame_time < start + duration) {
                        destination.write(ev_frame_time - negative_stamp_offset, ev_type, ev_size, ev_buffer);
+
+                       if (tracker) {
+                               if (ev_buffer[0] & MIDI_CMD_NOTE_ON) {
+                                       tracker->add (ev_buffer[1], ev_buffer[0] & 0xf);
+                               } else if (ev_buffer[0] & MIDI_CMD_NOTE_OFF) {
+                                       tracker->remove (ev_buffer[1], ev_buffer[0] & 0xf);
+                               }
+                       }
                } else {
                        break;
                }
 
-
                _read_data_count += ev_size;
 
                if (ev_size > scratch_size) {
@@ -230,8 +243,8 @@ SMFSource::write_unlocked (MidiRingBuffer<nframes_t>& source, sframes_t position
                ev.set(buf, size, time);
                ev.set_event_type(EventTypeMap::instance().midi_event_type(ev.buffer()[0]));
                if (!(ev.is_channel_event() || ev.is_smf_meta_event() || ev.is_sysex())) {
-                       cerr << "SMFSource: WARNING: caller tried to write non SMF-Event of type "
-                                       << std::hex << int(ev.buffer()[0]) << endl;
+                       /*cerr << "SMFSource: WARNING: caller tried to write non SMF-Event of type "
+                                       << std::hex << int(ev.buffer()[0]) << endl;*/
                        continue;
                }
 
@@ -255,6 +268,7 @@ SMFSource::write_unlocked (MidiRingBuffer<nframes_t>& source, sframes_t position
 void
 SMFSource::append_event_unlocked_beats (const Evoral::Event<double>& ev)
 {
+       assert(_writing);
        if (ev.size() == 0)  {
                return;
        }
@@ -288,6 +302,7 @@ SMFSource::append_event_unlocked_beats (const Evoral::Event<double>& ev)
 void
 SMFSource::append_event_unlocked_frames (const Evoral::Event<nframes_t>& ev, sframes_t position)
 {
+       assert(_writing);
        if (ev.size() == 0)  {
                return;
        }
@@ -301,7 +316,7 @@ SMFSource::append_event_unlocked_frames (const Evoral::Event<nframes_t>& ev, sfr
                return;
        }
 
-       BeatsFramesConverter converter(_session, position);
+       BeatsFramesConverter converter(_session.tempo_map(), position);
 
        _length_beats = max(_length_beats, converter.from(ev.time()));
 
@@ -329,17 +344,17 @@ SMFSource::get_state ()
 }
 
 int
-SMFSource::set_state (const XMLNode& node)
+SMFSource::set_state (const XMLNode& node, int version)
 {
-       if (Source::set_state (node)) {
+       if (Source::set_state (node, version)) {
                return -1;
        }
 
-       if (MidiSource::set_state (node)) {
+       if (MidiSource::set_state (node, version)) {
                return -1;
        }
 
-       if (FileSource::set_state (node)) {
+       if (FileSource::set_state (node, version)) {
                return -1;
        }
 
@@ -349,6 +364,7 @@ SMFSource::set_state (const XMLNode& node)
 void
 SMFSource::mark_streaming_midi_write_started (NoteMode mode, sframes_t start_frame)
 {
+       Glib::Mutex::Lock lm (_lock);
        MidiSource::mark_streaming_midi_write_started (mode, start_frame);
        Evoral::SMF::begin_write ();
        _last_ev_time_beats = 0.0;
@@ -358,6 +374,7 @@ SMFSource::mark_streaming_midi_write_started (NoteMode mode, sframes_t start_fra
 void
 SMFSource::mark_streaming_write_completed ()
 {
+       Glib::Mutex::Lock lm (_lock);
        MidiSource::mark_streaming_write_completed();
 
        if (!writable()) {
@@ -381,9 +398,9 @@ SMFSource::load_model (bool lock, bool force_reload)
                return;
        }
 
-       if (lock) {
-               Glib::Mutex::Lock lm (_lock);
-       }
+       boost::shared_ptr<Glib::Mutex::Lock> lm;
+       if (lock)
+               lm = boost::shared_ptr<Glib::Mutex::Lock>(new Glib::Mutex::Lock(_lock));
 
        if (_model && !force_reload) {
                return;
@@ -437,8 +454,6 @@ SMFSource::load_model (bool lock, bool force_reload)
        free(buf);
 }
 
-#define LINEAR_INTERPOLATION_MODE_WORKS_PROPERLY 0
-
 void
 SMFSource::set_default_controls_interpolation ()
 {
@@ -446,12 +461,7 @@ SMFSource::set_default_controls_interpolation ()
        Evoral::ControlSet::Controls controls = _model->controls();
        for (Evoral::ControlSet::Controls::iterator c = controls.begin(); c != controls.end(); ++c) {
                (*c).second->list()->set_interpolation(
-                       // to be enabled when ControlList::rt_safe_earliest_event_linear_unlocked works properly
-                       #if LINEAR_INTERPOLATION_MODE_WORKS_PROPERLY
                        EventTypeMap::instance().interpolation_of((*c).first));
-                       #else
-                       Evoral::ControlList::Discrete);
-                       #endif
        }
 }