Allow trim of midi regions to before the start of the source (better, this time)...
[ardour.git] / libs / ardour / midi_region.cc
index ae93a774454da0cd82b2ad068c9bfc8dd09fcead..26d43ba2a95519e7caeafc5acc10e25d32d9b23f 100644 (file)
@@ -47,11 +47,31 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
+namespace ARDOUR {
+       namespace Properties {
+               PBD::PropertyDescriptor<void*> midi_data;
+        }
+}
+
+void
+MidiRegion::make_property_quarks ()
+{
+        Properties::midi_data.property_id = g_quark_from_static_string (X_("midi-data"));
+        DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for midi-data = %1\n", Properties::midi_data.property_id));
+}
+
+void
+MidiRegion::register_properties ()
+{
+        /* none yet, but its only a matter of time */
+}
+
 /* Basic MidiRegion constructor (many channels) */
 MidiRegion::MidiRegion (const SourceList& srcs)
        : Region (srcs)
 {
-       // midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
+        register_properties ();
+
        midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
        model_changed ();
        assert(_name.val().find("/") == string::npos);
@@ -62,8 +82,9 @@ MidiRegion::MidiRegion (const SourceList& srcs)
 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, frameoffset_t offset, bool offset_relative)
        : Region (other, offset, offset_relative)
 {
+        register_properties ();
+
        assert(_name.val().find("/") == string::npos);
-       // midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
        midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
        model_changed ();
 }
@@ -100,7 +121,7 @@ MidiRegion::set_position_internal (framepos_t pos, bool allow_bbt_recompute)
        BeatsFramesConverter old_converter(_session.tempo_map(), _position - _start);
        double length_beats = old_converter.from(_length);
 
-       Region::set_position_internal(pos, allow_bbt_recompute);
+       Region::set_position_internal (pos, allow_bbt_recompute);
 
        BeatsFramesConverter new_converter(_session.tempo_map(), pos - _start);
 
@@ -108,19 +129,19 @@ MidiRegion::set_position_internal (framepos_t pos, bool allow_bbt_recompute)
 }
 
 framecnt_t
-MidiRegion::read_at (Evoral::EventSink<nframes_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
+MidiRegion::read_at (Evoral::EventSink<framepos_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
 {
        return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
 }
 
 framecnt_t
-MidiRegion::master_read_at (MidiRingBuffer<nframes_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode) const
+MidiRegion::master_read_at (MidiRingBuffer<framepos_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode) const
 {
        return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
 }
 
 framecnt_t
-MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<nframes_t>& dst, framepos_t position, framecnt_t dur, uint32_t chan_n, 
+MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<framepos_t>& dst, framepos_t position, framecnt_t dur, uint32_t chan_n, 
                      NoteMode mode, MidiStateTracker* tracker) const
 {
        frameoffset_t internal_offset = 0;
@@ -155,13 +176,14 @@ MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<nframes_t>&
 
        boost::shared_ptr<MidiSource> src = midi_source(chan_n);
        src->set_note_mode(mode);
-
-       /*cerr << "MR read @ " << position << " * " << to_read
-               << " _position = " << _position
-           << " _start = " << _start
-           << " offset = " << output_buffer_position
-           << " intoffset = " << internal_offset
-           << endl;*/
+        
+        /*
+          cerr << "MR read @ " << position << " * " << to_read
+          << " _position = " << _position
+          << " _start = " << _start
+          << " intoffset = " << internal_offset
+          << endl;
+        */
 
        /* This call reads events from a source and writes them to `dst' timed in session frames */
 
@@ -233,29 +255,6 @@ MidiRegion::midi_source (uint32_t n) const
        return boost::dynamic_pointer_cast<MidiSource>(source(n));
 }
 
-
-void
-MidiRegion::switch_source(boost::shared_ptr<Source> src)
-{
-       _source_connection.disconnect ();
-       
-       boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
-       if (!msrc) {
-               return;
-       }
-
-       // MIDI regions have only one source
-        SourceList srcs;
-        srcs.push_back (msrc);
-
-        drop_sources ();
-        use_sources (srcs);
-        
-       set_name (msrc->name());
-
-       msrc->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
-}
-
 void
 MidiRegion::model_changed ()
 {
@@ -281,6 +280,15 @@ MidiRegion::model_changed ()
        midi_source()->AutomationStateChanged.connect_same_thread (
                _model_connection, boost::bind (&MidiRegion::model_automation_state_changed, this, _1)
                );
+
+        model()->ContentsChanged.connect_same_thread (
+                _model_contents_connection, boost::bind (&MidiRegion::model_contents_changed, this));
+}
+
+void
+MidiRegion::model_contents_changed ()
+{
+        send_change (PropertyChange (Properties::midi_data));        
 }
 
 void
@@ -298,9 +306,21 @@ MidiRegion::model_automation_state_changed (Evoral::Parameter const & p)
        }
 
        /* the source will have an iterator into the model, and that iterator will have been set up
-          for a given set of filtered_paramters, so now that we've changed that list we must invalidate
+          for a given set of filtered_parameters, so now that we've changed that list we must invalidate
           the iterator.
        */
        Glib::Mutex::Lock lm (midi_source(0)->mutex());
        midi_source(0)->invalidate ();
 }
+
+/** This is called when a trim drag has resulted in a -ve _start time for this region.
+ *  Fix it up by adding some empty space to the source.
+ */
+void
+MidiRegion::fix_negative_start ()
+{
+        BeatsFramesConverter c (_session.tempo_map(), _position);
+
+       model()->insert_silence_at_start (c.from (-_start));
+       _start = 0;
+}