* fixed bug: MIDI region did not sound if its position was less than its start offset
authorHans Baier <hansfbaier@googlemail.com>
Tue, 8 Apr 2008 23:57:59 +0000 (23:57 +0000)
committerHans Baier <hansfbaier@googlemail.com>
Tue, 8 Apr 2008 23:57:59 +0000 (23:57 +0000)
git-svn-id: svn://localhost/ardour2/branches/3.0@3239 d708f5d6-7413-0410-9779-e7cbd77b26cf

libs/ardour/ardour/midi_model.h
libs/ardour/ardour/midi_source.h
libs/ardour/ardour/smf_source.h
libs/ardour/midi_model.cc
libs/ardour/midi_region.cc
libs/ardour/midi_source.cc
libs/ardour/smf_source.cc

index 08521080779cc65577ff3b30b87c0c13e68ce34d..f99855d238905775a5e413ac26118252572f0fb4 100644 (file)
@@ -72,7 +72,7 @@ public:
        bool writing() const { return _writing; }
        void end_write(bool delete_stuck=false);
 
-       size_t read (MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframes_t stamp_offset) const;
+       size_t read (MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframes_t stamp_offset, nframes_t negative_stamp_offset) const;
 
        /** Resizes vector if necessary (NOT realtime safe) */
        void append(const MIDI::Event& ev);
index d465506819b954590882c317b00b57f55e6de545..997f3f9d6948a456c4d8e6ea09027978563e4dba 100644 (file)
@@ -55,7 +55,7 @@ class MidiSource : public Source
        virtual uint32_t    n_channels () const { return 1; }
        
        // FIXME: integrate this with the Readable::read interface somehow
-       virtual nframes_t midi_read (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const;
+       virtual nframes_t midi_read (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset, nframes_t negative_stamp_offset) const;
        virtual nframes_t midi_write (MidiRingBuffer& src, nframes_t cnt);
 
        virtual void append_event_unlocked(EventTimeUnit unit, const MIDI::Event& ev) = 0;
@@ -98,7 +98,7 @@ class MidiSource : public Source
        virtual int flush_header() = 0;
        virtual int flush_footer() = 0;
        
-       virtual nframes_t read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const = 0;
+       virtual nframes_t read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset, nframes_t negative_stamp_offset) const = 0;
        virtual nframes_t write_unlocked (MidiRingBuffer& dst, nframes_t cnt) = 0;
        
        mutable Glib::Mutex _lock;
index 626aa64defc5f70b70ef4ea345552b931273d46a..7c729b10f302e219f6ec4f12fb522dadcae9cb23 100644 (file)
@@ -102,7 +102,7 @@ class SMFSource : public MidiSource {
 
        int init (string idstr, bool must_exist);
 
-       nframes_t read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cn, nframes_t stamp_offset) const;
+       nframes_t read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cn, nframes_t stamp_offset, nframes_t negative_stamp_offset) const;
        nframes_t write_unlocked (MidiRingBuffer& dst, nframes_t cnt);
 
        bool find (std::string path, bool must_exist, bool& is_new);
index 4aa229b45093b2301a78445bb315227114446ffa..43ac4b0f960ae471deb695be7291bce0b9d1331b 100644 (file)
@@ -292,9 +292,9 @@ MidiModel::MidiModel(Session& s, size_t size)
  * \return number of events written to \a dst
  */
 size_t
-MidiModel::read(MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframes_t stamp_offset) const
+MidiModel::read(MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframes_t stamp_offset, nframes_t negative_stamp_offset) const
 {
-       cerr << this << " MM::read @ " << start << " * " << nframes << " + " << stamp_offset << endl;
+       cerr << this << " MM::read @ " << start << " frames: " << nframes << " -> " << stamp_offset << endl;
        cerr << this << " MM # notes: " << n_notes() << endl;
 
        size_t read_events = 0;
@@ -310,7 +310,7 @@ MidiModel::read(MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframes
 
        while (_read_iter != end() && _read_iter->time() < start + nframes) {
                assert(_read_iter->size() > 0);
-               dst.write(_read_iter->time() + stamp_offset, _read_iter->size(), _read_iter->buffer());
+               dst.write(_read_iter->time() + stamp_offset - negative_stamp_offset, _read_iter->size(), _read_iter->buffer());
                cerr << this << " MM::read event @ " << _read_iter->time()  
                     << " type: " << hex << int(_read_iter->type()) << dec 
                     << " note: " << int(_read_iter->note()) 
index 26eb0e5054b0e9226220df4629a7f6f773423143..c3ce6db327c97ad912fa267403b425ee8d03db5d 100644 (file)
@@ -131,7 +131,8 @@ MidiRegion::master_read_at (MidiRingBuffer& out, nframes_t position, nframes_t d
 nframes_t
 MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst, nframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
 {
-       //cerr << _name << "._read_at(" << position << ") - " << _position << " duration: " << dur << endl;
+       cerr << "reading from region " << _name << " position: " << _position << " start: " << _start << endl;
+       cerr << _name << "._read_at(" << position << ") - " << position << " duration: " << dur << endl;
 
        nframes_t internal_offset = 0;
        nframes_t src_offset      = 0;
@@ -171,6 +172,17 @@ MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst, nframes_t pos
        boost::shared_ptr<MidiSource> src = midi_source(chan_n);
        src->set_note_mode(mode);
 
+       nframes_t output_buffer_position = 0;
+       nframes_t negative_output_buffer_position = 0;
+       if(_position >= _start) {
+               // handle resizing of beginnings of regions correctly
+               output_buffer_position = _position - _start;
+       } else {
+               // when _start is greater than _position, we have to subtract
+               // _start from the note times in the midi source
+               negative_output_buffer_position = _start; 
+       }
+       
        if (src->midi_read (
                        // the destination buffer
                        dst,  
@@ -179,7 +191,9 @@ MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst, nframes_t pos
                        // how many bytes
                        to_read, 
                        // the offset in the output buffer
-                       _position - _start
+                       output_buffer_position,
+                       // what to substract from note times written in the output buffer
+                       negative_output_buffer_position
                ) != to_read) {
                return 0; /* "read nothing" */
        }
index eab87376ebb1c6a615eaceaf0841b8761ddf5f29..aeb898a31be68d170806bbd243c2cd4f5af1790b 100644 (file)
@@ -101,15 +101,15 @@ MidiSource::set_state (const XMLNode& node)
 }
 
 nframes_t
-MidiSource::midi_read (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const
+MidiSource::midi_read (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset, nframes_t negative_stamp_offset) const
 {
        Glib::Mutex::Lock lm (_lock);
        if (_model) {
-               const size_t n_events = _model->read(dst, start, cnt, stamp_offset);
+               const size_t n_events = _model->read(dst, start, cnt, stamp_offset, negative_stamp_offset);
                cout << "Read " << n_events << " events from model." << endl;
                return cnt;
        } else {
-               return read_unlocked (dst, start, cnt, stamp_offset);
+               return read_unlocked (dst, start, cnt, stamp_offset, negative_stamp_offset);
        }
 }
 
index 6df78f3b299257dd8513a8cb5c4258115d81c58c..18d7e87d88e57596ec86458c14f22c26ccf8fe72 100644 (file)
@@ -331,7 +331,7 @@ SMFSource::read_event(uint32_t* delta_t, uint32_t* size, Byte** buf) const
 
 /** All stamps in audio frames */
 nframes_t
-SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const
+SMFSource::read_unlocked (MidiRingBuffer& 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;
 
@@ -377,7 +377,7 @@ SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, n
                                        ((time / (double)_ppqn) * frames_per_beat)) + stamp_offset;
 
                        if (ev_frame_time <= start + cnt)
-                               dst.write(ev_frame_time, ev_size, ev_buffer);
+                               dst.write(ev_frame_time - negative_stamp_offset, ev_size, ev_buffer);
                        else
                                break;
                }