From 823a1d36c1694a220b3b581741ffc92990e03eeb Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 20 Jul 2007 05:37:24 +0000 Subject: [PATCH] Remove random access interface from MidiBuffer, towards killing fixed/limited event size assumption. git-svn-id: svn://localhost/ardour2/trunk@2159 d708f5d6-7413-0410-9779-e7cbd77b26cf --- gtk2_ardour/midi_streamview.cc | 4 ++-- libs/ardour/ardour/midi_buffer.h | 38 +++++++++++++++++++++++++++++--- libs/ardour/meter.cc | 5 ++--- libs/ardour/midi_diskstream.cc | 5 ++++- libs/ardour/midi_model.cc | 6 ++--- libs/ardour/midi_port.cc | 8 +++---- libs/ardour/smf_source.cc | 4 ++-- 7 files changed, 52 insertions(+), 18 deletions(-) diff --git a/gtk2_ardour/midi_streamview.cc b/gtk2_ardour/midi_streamview.cc index 30700c8e94..d6361905b5 100644 --- a/gtk2_ardour/midi_streamview.cc +++ b/gtk2_ardour/midi_streamview.cc @@ -393,8 +393,8 @@ MidiStreamView::update_rec_regions (boost::shared_ptr data, nframes_ /* draw events */ MidiRegionView* mrv = (MidiRegionView*)iter->second; - for (size_t i = 0; i < data->size(); ++i) { - const MidiEvent& ev = (*data.get())[i]; + for (MidiBuffer::iterator i = data->begin(); i != data->end(); ++i) { + const MidiEvent& ev = *i; mrv->add_event(ev); mrv->extend_active_notes(); } diff --git a/libs/ardour/ardour/midi_buffer.h b/libs/ardour/ardour/midi_buffer.h index 79a8391e72..17feea6ff1 100644 --- a/libs/ardour/ardour/midi_buffer.h +++ b/libs/ardour/ardour/midi_buffer.h @@ -43,12 +43,44 @@ public: bool push_back(const jack_midi_event_t& event); Byte* reserve(double time, size_t size); + bool merge(const MidiBuffer& a, const MidiBuffer& b); + + struct iterator { + iterator(MidiBuffer& b, size_t i) : buffer(b), index(i) {} + + inline MidiEvent& operator*() const { return buffer[index]; } + inline iterator& operator++() { ++index; return *this; } // prefix + inline bool operator!=(const iterator& other) const { return index != other.index; } + + MidiBuffer& buffer; + size_t index; + }; + + struct const_iterator { + const_iterator(const MidiBuffer& b, size_t i) : buffer(b), index(i) {} + + inline const MidiEvent& operator*() const { return buffer[index]; } + inline const_iterator& operator++() { ++index; return *this; } // prefix + inline bool operator!=(const const_iterator& other) const { return index != other.index; } + + const MidiBuffer& buffer; + size_t index; + }; + + iterator begin() { return iterator(*this, 0); } + iterator end() { return iterator(*this, _size); } + + const_iterator begin() const { return const_iterator(*this, 0); } + const_iterator end() const { return const_iterator(*this, _size); } + +private: + + friend class iterator; + friend class const_iterator; + const MidiEvent& operator[](size_t i) const { assert(i < _size); return _events[i]; } MidiEvent& operator[](size_t i) { assert(i < _size); return _events[i]; } - bool merge(const MidiBuffer& a, const MidiBuffer& b); - -private: // FIXME: Jack needs to tell us this static const size_t MAX_EVENT_SIZE = 4; // bytes diff --git a/libs/ardour/meter.cc b/libs/ardour/meter.cc index 4a898eecaf..84dac2ded4 100644 --- a/libs/ardour/meter.cc +++ b/libs/ardour/meter.cc @@ -47,9 +47,8 @@ PeakMeter::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nfr // GUI needs a better MIDI meter, not much information can be // expressed through peaks alone - const unsigned n_events = bufs.get_midi(n).size(); - for (size_t i=0; i < n_events; ++i) { - const MidiEvent& ev = bufs.get_midi(n)[i]; + for (MidiBuffer::iterator i = bufs.get_midi(n).begin(); i != bufs.get_midi(n).end(); ++i) { + const MidiEvent& ev = *i; if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_ON) { const float this_vel = log(ev.buffer[2] / 127.0 * (M_E*M_E-M_E) + M_E) - 1.0; //printf("V %d -> %f\n", (int)((Byte)ev.buffer[2]), this_vel); diff --git a/libs/ardour/midi_diskstream.cc b/libs/ardour/midi_diskstream.cc index 6386714c16..691335de93 100644 --- a/libs/ardour/midi_diskstream.cc +++ b/libs/ardour/midi_diskstream.cc @@ -540,9 +540,12 @@ MidiDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t size_t num_events = _source_port->get_midi_buffer().size(); size_t to_write = std::min(_capture_buf->write_space(), num_events); + MidiBuffer::iterator port_iter = _source_port->get_midi_buffer().begin(); + for (size_t i=0; i < to_write; ++i) { - MidiEvent& ev = _source_port->get_midi_buffer()[i]; + const MidiEvent& ev = *port_iter; _capture_buf->write(ev.time + transport_frame, ev.size, ev.buffer); + ++port_iter; } } else { diff --git a/libs/ardour/midi_model.cc b/libs/ardour/midi_model.cc index 9e8a5295a8..1c6990e84d 100644 --- a/libs/ardour/midi_model.cc +++ b/libs/ardour/midi_model.cc @@ -83,9 +83,9 @@ MidiModel::end_write(bool delete_stuck) */ void MidiModel::append(const MidiBuffer& buf) -{ - for (size_t i=0; i < buf.size(); ++i) { - const MidiEvent& ev = buf[i]; +{ + for (MidiBuffer::const_iterator i = buf.begin(); i != buf.end(); ++i) { + const MidiEvent& ev = *i; assert(_notes.empty() || ev.time >= _notes.back().start); diff --git a/libs/ardour/midi_port.cc b/libs/ardour/midi_port.cc index ba8d1aa841..2dc90a09da 100644 --- a/libs/ardour/midi_port.cc +++ b/libs/ardour/midi_port.cc @@ -93,14 +93,14 @@ MidiPort::cycle_end() void* jack_buffer = jack_port_get_buffer(_port, _nframes_this_cycle); - const nframes_t event_count = _buffer.size(); - + //const nframes_t event_count = _buffer.size(); //if (event_count > 0) // cerr << "MIDIPort writing " << event_count << " events." << endl; jack_midi_clear_buffer(jack_buffer); - for (nframes_t i=0; i < event_count; ++i) { - const MidiEvent& ev = _buffer[i]; + + for (MidiBuffer::iterator i = _buffer.begin(); i != _buffer.end(); ++i) { + const MidiEvent& ev = *i; // event times should be frames, relative to cycle start assert(ev.time >= 0); assert(ev.time < _nframes_this_cycle); diff --git a/libs/ardour/smf_source.cc b/libs/ardour/smf_source.cc index ffa7ad823c..08ec1d87a8 100644 --- a/libs/ardour/smf_source.cc +++ b/libs/ardour/smf_source.cc @@ -380,8 +380,8 @@ SMFSource::write_unlocked (MidiRingBuffer& src, nframes_t cnt) const double frames_per_beat = _session.tempo_map().tempo_at(_timeline_position).frames_per_beat( _session.engine().frame_rate()); - for (size_t i=0; i < buf.size(); ++i) { - MidiEvent& ev = buf[i]; + for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) { + MidiEvent& ev = *i; assert(ev.time >= _timeline_position); ev.time -= _timeline_position; assert(ev.time >= _last_ev_time);