Eliminate circular dependency kludge for control_protocol/smpte.o.
[ardour.git] / libs / ardour / smf_source.cc
index cb5f498bca843b6bf4d5fa3742de78eb9c190d8e..78c3e558113339e97d18fa86c7192fddf97d5acd 100644 (file)
@@ -50,16 +50,18 @@ using namespace Glib;
 using namespace PBD;
 
 /** Constructor used for new internal-to-session files.  File cannot exist. */
-SMFSource::SMFSource (Session& s, const ustring& path, Source::Flag flags)
+SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
        : Source(s, DataType::MIDI, path, flags)
        , MidiSource(s, path)
-       , FileSource(s, DataType::MIDI, path, flags)
+       , FileSource(s, DataType::MIDI, path, string(), flags)
        , 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 ();
        }
@@ -67,8 +69,6 @@ SMFSource::SMFSource (Session& s, const ustring& path, Source::Flag flags)
        if (create(path)) {
                throw failed_constructor ();
        }
-
-       load_model(true, true); // FIXME
 }
 
 /** Constructor used for existing internal-to-session files. */
@@ -92,8 +92,6 @@ SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
        if (open(_path)) {
                throw failed_constructor ();
        }
-
-       load_model(true, true); // FIXME
 }
 
 SMFSource::~SMFSource ()
@@ -104,10 +102,9 @@ SMFSource::~SMFSource ()
 }
 
 /** All stamps in audio frames */
-nframes_t
-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,
+framecnt_t
+SMFSource::read_unlocked (Evoral::EventSink<framepos_t>& destination, framepos_t const source_start,
+                         framepos_t start, framecnt_t duration,
                          MidiStateTracker* tracker) const
 {
        int      ret  = 0;
@@ -171,16 +168,14 @@ SMFSource::read_unlocked (Evoral::EventSink<nframes_t>& destination, sframes_t s
                                                                  ev_delta_t, time, ev_buffer[0], ev_type));
 
                assert(time >= start_ticks);
-               const sframes_t ev_frame_time = converter.to(time / (double)ppqn()) + stamp_offset;
 
-#if 0
-               cerr << " frames = " << ev_frame_time
-                    << " w/offset = " << ev_frame_time - negative_stamp_offset
-                    << endl;
-#endif
+               /* Note that we add on the source start time (in session frames) here so that ev_frame_time
+                  is in session frames.
+               */
+               const framepos_t ev_frame_time = converter.to(time / (double)ppqn()) + source_start;
 
                if (ev_frame_time < start + duration) {
-                       destination.write(ev_frame_time - negative_stamp_offset, ev_type, ev_size, ev_buffer);
+                       destination.write (ev_frame_time, ev_type, ev_size, ev_buffer);
 
                        if (tracker) {
                                if (ev_buffer[0] & MIDI_CMD_NOTE_ON) {
@@ -204,13 +199,16 @@ SMFSource::read_unlocked (Evoral::EventSink<nframes_t>& destination, sframes_t s
        return duration;
 }
 
-/** All stamps in audio frames */
-nframes_t
-SMFSource::write_unlocked (MidiRingBuffer<nframes_t>& source, sframes_t position, nframes_t duration)
+/** Write data to this source from a MidiRingBuffer.
+ *  @param source Buffer to read from.
+ *  @param position This source's start position in session frames.
+ */
+framecnt_t
+SMFSource::write_unlocked (MidiRingBuffer<framepos_t>& source, framepos_t position, framecnt_t duration)
 {
        _write_data_count = 0;
 
-       nframes_t         time;
+       framepos_t        time;
        Evoral::EventType type;
        uint32_t          size;
 
@@ -221,7 +219,7 @@ SMFSource::write_unlocked (MidiRingBuffer<nframes_t>& source, sframes_t position
                _model->start_write();
        }
 
-       Evoral::MIDIEvent<nframes_t> ev;
+       Evoral::MIDIEvent<framepos_t> ev;
 
        while (true) {
                bool ret = source.peek_time(&time);
@@ -246,6 +244,7 @@ SMFSource::write_unlocked (MidiRingBuffer<nframes_t>& source, sframes_t position
                        break;
                }
 
+               /* convert from session time to time relative to the source start */
                assert(time >= position);
                time -= position;
 
@@ -265,8 +264,6 @@ SMFSource::write_unlocked (MidiRingBuffer<nframes_t>& source, sframes_t position
        Evoral::SMF::flush();
        free(buf);
 
-       ViewDataRangeReady(position + _last_write_end, duration); /* EMIT SIGNAL */
-
        return duration;
 }
 
@@ -314,9 +311,9 @@ SMFSource::append_event_unlocked_beats (const Evoral::Event<double>& ev)
 
 }
 
-/** Append an event with a timestamp in frames (nframes_t) */
+/** Append an event with a timestamp in frames (framepos_t) */
 void
-SMFSource::append_event_unlocked_frames (const Evoral::Event<nframes_t>& ev, sframes_t position)
+SMFSource::append_event_unlocked_frames (const Evoral::Event<framepos_t>& ev, framepos_t position)
 {
        assert(_writing);
        if (ev.size() == 0)  {
@@ -352,7 +349,7 @@ SMFSource::append_event_unlocked_frames (const Evoral::Event<nframes_t>& ev, sfr
 
        _length_beats = max(_length_beats, ev_time_beats);
 
-       const sframes_t delta_time_frames = ev.time() - _last_ev_time_frames;
+       const framepos_t delta_time_frames = ev.time() - _last_ev_time_frames;
        const double    delta_time_beats  = converter.from(delta_time_frames);
        const uint32_t  delta_time_ticks  = (uint32_t)(lrint(delta_time_beats * (double)ppqn()));
 
@@ -366,7 +363,9 @@ SMFSource::append_event_unlocked_frames (const Evoral::Event<nframes_t>& ev, sfr
 XMLNode&
 SMFSource::get_state ()
 {
-       return MidiSource::get_state();
+        XMLNode& node = MidiSource::get_state();
+        node.add_property (X_("origin"), _origin);
+        return node;
 }
 
 int
@@ -388,7 +387,7 @@ SMFSource::set_state (const XMLNode& node, int version)
 }
 
 void
-SMFSource::mark_streaming_midi_write_started (NoteMode mode, sframes_t start_frame)
+SMFSource::mark_streaming_midi_write_started (NoteMode mode, framepos_t start_frame)
 {
        Glib::Mutex::Lock lm (_lock);
        MidiSource::mark_streaming_midi_write_started (mode, start_frame);
@@ -419,16 +418,15 @@ SMFSource::mark_streaming_write_completed ()
 }
 
 bool
-SMFSource::safe_midi_file_extension (const Glib::ustring& file)
+SMFSource::safe_midi_file_extension (const string& file)
 {
-       return (file.rfind(".mid") != Glib::ustring::npos);
+       return (file.rfind(".mid") != string::npos);
 }
 
 void
 SMFSource::load_model (bool lock, bool force_reload)
 {
        if (_writing) {
-               cout << "early out\n";
                return;
        }
 
@@ -437,16 +435,12 @@ SMFSource::load_model (bool lock, bool force_reload)
                lm = boost::shared_ptr<Glib::Mutex::Lock>(new Glib::Mutex::Lock(_lock));
 
        if (_model && !force_reload) {
-               cout << "earlyish out\n";
                return;
        }
 
-       if (! _model) {
-               _model = boost::shared_ptr<MidiModel>(new MidiModel(this));
-               cerr << _name << " loaded new model " << _model.get() << endl;
+       if (!_model) {
+               _model = boost::shared_ptr<MidiModel> (new MidiModel (shared_from_this ()));
        } else {
-               cerr << _name << " reloading model " << _model.get()
-                       << " (" << _model->n_notes() << " notes)" << endl;
                _model->clear();
        }