Remove unused (and timestamp type nasty) last_event_time() from SMF.
[ardour.git] / libs / ardour / smf_source.cc
index dadd7a8e0a5e2b2a630adacd18661ab6d3e93c11..64bd6874189aace0353fecce440afade69f44cf9 100644 (file)
@@ -33,6 +33,7 @@
 #include <glibmm/miscutils.h>
 
 #include <evoral/SMFReader.hpp>
+#include <evoral/Control.hpp>
 
 #include <ardour/smf_source.h>
 #include <ardour/session.h>
@@ -54,9 +55,10 @@ uint64_t                              SMFSource::header_position_offset;
 
 SMFSource::SMFSource (Session& s, std::string path, Flag flags)
        : MidiSource (s, region_name_from_path(path, false))
-       , SMF ()
+       , Evoral::SMF<double> ()
        , _flags (Flag(flags | Writable)) // FIXME: this needs to be writable for now
        , _allow_remove_if_empty(true)
+       , _last_ev_time(0)
 {
        /* constructor used for new internal-to-session files. file cannot exist */
 
@@ -64,7 +66,7 @@ SMFSource::SMFSource (Session& s, std::string path, Flag flags)
                throw failed_constructor ();
        }
        
-       if (open(path)) {
+       if (create(path)) {
                throw failed_constructor ();
        }
 
@@ -75,6 +77,7 @@ SMFSource::SMFSource (Session& s, const XMLNode& node)
        : MidiSource (s, node)
        , _flags (Flag (Writable|CanRename))
        , _allow_remove_if_empty(true)
+       , _last_ev_time(0)
 {
        /* constructor used for existing internal-to-session files. file must exist */
 
@@ -127,7 +130,7 @@ SMFSource::init (string pathstr, bool must_exist)
 
 /** All stamps in audio frames */
 nframes_t
-SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset, nframes_t negative_stamp_offset) const
+SMFSource::read_unlocked (MidiRingBuffer<double>& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset, nframes_t negative_stamp_offset) const
 {
        //cerr << "SMF read_unlocked " << name() << " read " << start << ", count=" << cnt << ", offset=" << stamp_offset << endl;
 
@@ -145,7 +148,7 @@ SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, n
        size_t scratch_size = 0; // keep track of scratch to minimize reallocs
 
        // FIXME: don't seek to start and search every read (brutal!)
-       SMF::seek_to_start();
+       Evoral::SMF<double>::seek_to_start();
        
        // FIXME: assumes tempo never changes after start
        const double frames_per_beat = _session.tempo_map().tempo_at(_timeline_position).frames_per_beat(
@@ -154,7 +157,7 @@ SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, n
        
        const uint64_t start_ticks = (uint64_t)((start / frames_per_beat) * ppqn());
 
-       while (!SMF::eof()) {
+       while (!Evoral::SMF<double>::eof()) {
                int ret = read_event(&ev_delta_t, &ev_size, &ev_buffer);
                if (ret == -1) { // EOF
                        //cerr << "SMF - EOF\n";
@@ -193,11 +196,11 @@ SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, n
 
 /** All stamps in audio frames */
 nframes_t
-SMFSource::write_unlocked (MidiRingBuffer& src, nframes_t cnt)
+SMFSource::write_unlocked (MidiRingBuffer<double>& src, nframes_t cnt)
 {
        _write_data_count = 0;
                
-       Evoral::EventTime time;
+       double            time;
        Evoral::EventType type;
        uint32_t          size;
 
@@ -207,7 +210,7 @@ SMFSource::write_unlocked (MidiRingBuffer& src, nframes_t cnt)
        if (_model && ! _model->writing())
                _model->start_write();
 
-       Evoral::MIDIEvent ev(0, 0.0, 4, NULL, true);
+       Evoral::MIDIEvent<double> ev(0, 0.0, 4, NULL, true);
 
        while (true) {
                bool ret = src.peek_time(&time);
@@ -247,10 +250,10 @@ SMFSource::write_unlocked (MidiRingBuffer& src, nframes_t cnt)
        }
 
        if (_model) {
-               make_sure_controls_have_the_right_interpolation();
+               set_default_controls_interpolation();
        }
 
-       SMF::flush();
+       Evoral::SMF<double>::flush();
        free(buf);
 
        const nframes_t oldlen = _length;
@@ -263,17 +266,21 @@ SMFSource::write_unlocked (MidiRingBuffer& src, nframes_t cnt)
                
 
 void
-SMFSource::append_event_unlocked(EventTimeUnit unit, const Evoral::Event& ev)
+SMFSource::append_event_unlocked(EventTimeUnit unit, const Evoral::Event<double>& ev)
 {
-       if (ev.size() == 0)
+       if (ev.size() == 0)  {
+               cerr << "SMFSource: Warning: skipping empty event" << endl;
                return;
+       }
 
-       /*printf("SMFSource: %s - append_event_unlocked chan = %u, time = %lf, size = %u, data = ",
-                       name().c_str(), (unsigned)ev.channel(), ev.time(), ev.size()); 
+       /*
+       printf("SMFSource: %s - append_event_unlocked time = %lf, size = %u, data = ",
+                       name().c_str(), ev.time(), ev.size()); 
        for (size_t i=0; i < ev.size(); ++i) {
                printf("%X ", ev.buffer()[i]);
        }
-       printf("\n");*/
+       printf("\n");
+       */
        
        assert(ev.time() >= 0);
        
@@ -296,7 +303,8 @@ SMFSource::append_event_unlocked(EventTimeUnit unit, const Evoral::Event& ev)
                delta_time = (uint32_t)((ev.time() - last_event_time()) * ppqn());
        }
 
-       SMF::append_event_unlocked(delta_time, ev);
+       Evoral::SMF<double>::append_event_delta(delta_time, ev);
+       _last_ev_time = ev.time();
 
        _write_data_count += ev.size();
 }
@@ -351,7 +359,8 @@ void
 SMFSource::mark_streaming_midi_write_started (NoteMode mode, nframes_t start_frame)
 {
        MidiSource::mark_streaming_midi_write_started (mode, start_frame);
-       SMF::begin_write (start_frame);
+       Evoral::SMF<double>::begin_write ();
+       _last_ev_time = 0;
 }
 
 void
@@ -364,7 +373,7 @@ SMFSource::mark_streaming_write_completed ()
        }
        
        _model->set_edited(false);
-       SMF::end_write ();
+       Evoral::SMF<double>::end_write ();
 }
 
 void
@@ -629,10 +638,10 @@ SMFSource::load_model(bool lock, bool force_reload)
        }
 
        _model->start_write();
-       SMF::seek_to_start();
+       Evoral::SMF<double>::seek_to_start();
 
        uint64_t time = 0; /* in SMF ticks */
-       Evoral::Event ev;
+       Evoral::Event<double> ev;
        
        size_t scratch_size = 0; // keep track of scratch and minimize reallocs
        
@@ -652,7 +661,7 @@ SMFSource::load_model(bool lock, bool force_reload)
                
                if (ret > 0) { // didn't skip (meta) event
                        // make ev.time absolute time in frames
-                       ev.time() = time * frames_per_beat / (Evoral::EventTime)ppqn();
+                       ev.time() = time * frames_per_beat / (double)ppqn();
                        ev.set_event_type(EventTypeMap::instance().midi_event_type(buf[0]));
                        _model->append(ev);
                }
@@ -664,7 +673,7 @@ SMFSource::load_model(bool lock, bool force_reload)
                }
        }
 
-       make_sure_controls_have_the_right_interpolation();
+       set_default_controls_interpolation();
        
        _model->end_write(false);
        _model->set_edited(false);
@@ -675,7 +684,7 @@ SMFSource::load_model(bool lock, bool force_reload)
 #define LINEAR_INTERPOLATION_MODE_WORKS_PROPERLY 0
 
 void
-SMFSource::make_sure_controls_have_the_right_interpolation()
+SMFSource::set_default_controls_interpolation()
 {
        // set interpolation style to defaults, can be changed by the GUI later
        Evoral::ControlSet::Controls controls = _model->controls();
@@ -701,6 +710,6 @@ SMFSource::destroy_model()
 void
 SMFSource::flush_midi()
 {
-       SMF::end_write();
+       Evoral::SMF<double>::end_write();
 }