From 0f2f4d8efc7f3e1144c6f51fd17c1ab309056c3e Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 31 May 2007 21:37:20 +0000 Subject: Rewrote MidiRingBuffer to more efficiently pack data (flat pack stamps, sizes, and event data into a single buffer). Eliminate a double-copy on MIDI playback (MidiRingBuffer -> MidiBuffer). Various MIDI diskstream/source/SMF fixes (only write when appropriate, handle transport locates, etc). Fix MIDI rec region size/offset problems. Code cleanups. git-svn-id: svn://localhost/ardour2/trunk@1934 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/ardour/ardour/buffer.h | 12 +- libs/ardour/ardour/midi_diskstream.h | 13 +- libs/ardour/ardour/midi_ring_buffer.h | 322 ++++++++++++++++++++++------------ libs/ardour/ardour/midi_source.h | 5 +- libs/ardour/ardour/smf_source.h | 6 +- libs/ardour/ardour/source.h | 12 +- libs/ardour/ardour/types.h | 2 +- 7 files changed, 229 insertions(+), 143 deletions(-) (limited to 'libs/ardour/ardour') diff --git a/libs/ardour/ardour/buffer.h b/libs/ardour/ardour/buffer.h index b890afbb03..86a7aa9f95 100644 --- a/libs/ardour/ardour/buffer.h +++ b/libs/ardour/ardour/buffer.h @@ -197,18 +197,14 @@ public: void read_from(const Buffer& src, nframes_t nframes, nframes_t offset); - bool push_back(const MidiEvent& event); + bool push_back(const MidiEvent& event); + Byte* reserve(nframes_t time, size_t size); const MidiEvent& operator[](size_t i) const { assert(i < _size); return _events[i]; } MidiEvent& operator[](size_t i) { assert(i < _size); return _events[i]; } static size_t max_event_size() { return MAX_EVENT_SIZE; } - //void set_size(size_t size) { _size = size; } - - //const RawMidi* data() const { return _data; } - //RawMidi* data() { return _data; } - private: // These are undefined (prevent copies) MidiBuffer(const MidiBuffer& copy); @@ -221,8 +217,8 @@ private: * (_size * MAX_EVENT_SIZE) */ - MidiEvent* _events; ///< Event structs that point to offsets in _data - RawMidi* _data; ///< MIDI, straight up. No time stamps. + MidiEvent* _events; ///< Event structs that point to offsets in _data + Byte* _data; ///< MIDI, straight up. No time stamps. }; } // namespace ARDOUR diff --git a/libs/ardour/ardour/midi_diskstream.h b/libs/ardour/ardour/midi_diskstream.h index bc126ce364..cf78d0d05b 100644 --- a/libs/ardour/ardour/midi_diskstream.h +++ b/libs/ardour/ardour/midi_diskstream.h @@ -145,19 +145,12 @@ class MidiDiskstream : public Diskstream void engage_record_enable (); void disengage_record_enable (); - // FIXME: This is basically a single ChannelInfo.. abstractify that concept? - MidiRingBuffer* _playback_buf; - MidiRingBuffer* _capture_buf; - //RawMidi* _current_playback_buffer; - //RawMidi* _current_capture_buffer; - //RawMidi* _playback_wrap_buffer; - //RawMidi* _capture_wrap_buffer; + MidiRingBuffer* _playback_buf; + MidiRingBuffer* _capture_buf; MidiPort* _source_port; boost::shared_ptr _write_source; RingBufferNPT* _capture_transition_buf; - //RingBufferNPT::rw_vector _playback_vector; - //RingBufferNPT::rw_vector _capture_vector; - nframes_t _last_flush_frame; + nframes_t _last_flush_frame; }; }; /* namespace ARDOUR */ diff --git a/libs/ardour/ardour/midi_ring_buffer.h b/libs/ardour/ardour/midi_ring_buffer.h index 71f42d7182..86319ad4b2 100644 --- a/libs/ardour/ardour/midi_ring_buffer.h +++ b/libs/ardour/ardour/midi_ring_buffer.h @@ -21,61 +21,67 @@ #include #include -#include #include namespace ARDOUR { -/** A MIDI RingBuffer - * (necessary because MIDI events are variable sized so a generic RB won't do). + +/** A RingBuffer. + * Read/Write realtime safe. + * Single-reader Single-writer thread safe. + * + * This is Raul::RingBuffer, lifted for MIDIRingBuffer to inherit from as it works + * a bit differently than PBD::Ringbuffer. This could/should be replaced with + * the PBD ringbuffer to decrease code size, but this code is tested and known to + * work, so here it sits for now... * - * ALL publically accessible sizes refer to event COUNTS. What actually goes - * on in here is none of the callers business :) + * Ignore this class, use MidiRingBuffer. */ -class MidiRingBuffer { +template +class MidiRingBufferBase { public: - MidiRingBuffer (size_t size) + + /** @param size Size in bytes. + */ + MidiRingBufferBase(size_t size) : _size(size) - , _max_event_size(MidiBuffer::max_event_size()) - , _ev_buf(new MidiEvent[size]) - , _raw_buf(new RawMidi[size * _max_event_size]) + , _buf(new T[size]) { - reset (); + reset(); assert(read_space() == 0); assert(write_space() == size - 1); } - virtual ~MidiRingBuffer() { - delete[] _ev_buf; - delete[] _raw_buf; + virtual ~MidiRingBufferBase() { + delete[] _buf; } - void reset () { - /* !!! NOT THREAD SAFE !!! */ - g_atomic_int_set (&_write_ptr, 0); - g_atomic_int_set (&_read_ptr, 0); + /** Reset(empty) the ringbuffer. + * NOT thread safe. + */ + void reset() { + g_atomic_int_set(&_write_ptr, 0); + g_atomic_int_set(&_read_ptr, 0); } - size_t write_space () { - size_t w, r; + size_t write_space() const { - w = g_atomic_int_get (&_write_ptr); - r = g_atomic_int_get (&_read_ptr); + const size_t w = g_atomic_int_get(&_write_ptr); + const size_t r = g_atomic_int_get(&_read_ptr); if (w > r) { return ((r - w + _size) % _size) - 1; - } else if (w < r) { + } else if(w < r) { return (r - w) - 1; } else { return _size - 1; } } - size_t read_space () { - size_t w, r; + size_t read_space() const { - w = g_atomic_int_get (&_write_ptr); - r = g_atomic_int_get (&_read_ptr); + const size_t w = g_atomic_int_get(&_write_ptr); + const size_t r = g_atomic_int_get(&_read_ptr); if (w > r) { return w - r; @@ -86,139 +92,229 @@ public: size_t capacity() const { return _size; } - /** Read one event and appends it to @a out. */ - //size_t read(MidiBuffer& out); + size_t peek(size_t size, T* dst); + bool full_peek(size_t size, T* dst); + + size_t read(size_t size, T* dst); + bool full_read(size_t size, T* dst); + + void write(size_t size, const T* src); + +protected: + mutable gint _write_ptr; + mutable gint _read_ptr; + + size_t _size; ///< Size (capacity) in bytes + T* _buf; ///< size, event, size, event... +}; - /** Write one event (@a in) */ - size_t write(const MidiEvent& in); // deep copies in - /** Read events all events up to time @a end into @a out, leaving stamps intact. - * Any events before @a start will be dropped. */ - size_t read(MidiBuffer& out, nframes_t start, nframes_t end); +/** Peek at the ringbuffer (read w/o advancing read pointer). + * + * Note that a full read may not be done if the data wraps around. + * Caller must check return value and call again if necessary, or use the + * full_peek method which does this automatically. + */ +template +size_t +MidiRingBufferBase::peek(size_t size, T* dst) +{ + const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr); - /** Write all events from @a in, applying @a offset to all time stamps */ - size_t write(const MidiBuffer& in, nframes_t offset = 0); + const size_t read_size = (priv_read_ptr + size < _size) + ? size + : _size - priv_read_ptr; + + memcpy(dst, &_buf[priv_read_ptr], read_size); + + return read_size; +} - inline void clear_event(size_t index); -private: +template +bool +MidiRingBufferBase::full_peek(size_t size, T* dst) +{ + if (read_space() < size) + return false; - // _event_ indices - mutable gint _write_ptr; - mutable gint _read_ptr; + const size_t read_size = peek(size, dst); - size_t _size; // size (capacity) in events - size_t _max_event_size; // ratio of raw_buf size to ev_buf size - MidiEvent* _ev_buf; // these point into... - RawMidi* _raw_buf; // this + if (read_size < size) + peek(size - read_size, dst + read_size); -}; + return true; +} -/** Just for sanity checking */ -inline void -MidiRingBuffer::clear_event(size_t index) + +/** Read from the ringbuffer. + * + * Note that a full read may not be done if the data wraps around. + * Caller must check return value and call again if necessary, or use the + * full_read method which does this automatically. + */ +template +size_t +MidiRingBufferBase::read(size_t size, T* dst) { - memset(&_ev_buf[index].buffer, 0, _max_event_size); - _ev_buf[index].time = 0; - _ev_buf[index].size = 0; - _ev_buf[index].buffer = 0; + const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr); + + const size_t read_size = (priv_read_ptr + size < _size) + ? size + : _size - priv_read_ptr; + + memcpy(dst, &_buf[priv_read_ptr], read_size); + + g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size); + return read_size; } -inline size_t -MidiRingBuffer::write (const MidiEvent& ev) + +template +bool +MidiRingBufferBase::full_read(size_t size, T* dst) { - //static nframes_t last_write_time = 0; + if (read_space() < size) + return false; + + const size_t read_size = read(size, dst); - assert(ev.size > 0); + if (read_size < size) + read(size - read_size, dst + read_size); - size_t priv_write_ptr = g_atomic_int_get(&_write_ptr); + return true; +} - if (write_space () == 0) { - return 0; + +template +inline void +MidiRingBufferBase::write(size_t size, const T* src) +{ + const size_t priv_write_ptr = g_atomic_int_get(&_write_ptr); + + if (priv_write_ptr + size <= _size) { + memcpy(&_buf[priv_write_ptr], src, size); + g_atomic_int_set(&_write_ptr, (priv_write_ptr + size) % _size); } else { - //assert(ev.time >= last_write_time); + const size_t this_size = _size - priv_write_ptr; + assert(this_size < size); + assert(priv_write_ptr + this_size <= _size); + memcpy(&_buf[priv_write_ptr], src, this_size); + memcpy(&_buf[0], src+this_size, size - this_size); + g_atomic_int_set(&_write_ptr, size - this_size); + } +} - const size_t raw_index = priv_write_ptr * _max_event_size; - MidiEvent* const write_ev = &_ev_buf[priv_write_ptr]; - *write_ev = ev; +/* ******************************************************************** */ + + +/** A MIDI RingBuffer. + * + * This is timestamps and MIDI packed sequentially into a single buffer, similarly + * to LV2 MIDI. The buffer looks like this: + * + * [timestamp][size][size bytes of raw MIDI][timestamp][size][etc..] + */ +class MidiRingBuffer : public MidiRingBufferBase { +public: + + /** @param size Size in bytes. + */ + MidiRingBuffer(size_t size) + : MidiRingBufferBase(size) + {} + + size_t write(nframes_t time, size_t size, const Byte* buf); + bool read(nframes_t time, size_t* size, Byte* buf); + + size_t read(MidiBuffer& dst, nframes_t start, nframes_t end); +}; - memcpy(&_raw_buf[raw_index], ev.buffer, ev.size); - write_ev->buffer = &_raw_buf[raw_index]; - g_atomic_int_set(&_write_ptr, (priv_write_ptr + 1) % _size); - - //printf("MRB - wrote %xd %d %d with time %u at index %zu (raw index %zu)\n", - // write_ev->buffer[0], write_ev->buffer[1], write_ev->buffer[2], write_ev->time, - // priv_write_ptr, raw_index); - - assert(write_ev->size = ev.size); - //last_write_time = ev.time; - //printf("(W) read space: %zu\n", read_space()); +inline bool +MidiRingBuffer::read(nframes_t time, size_t* size, Byte* buf) +{ + bool success = MidiRingBufferBase::full_read(sizeof(nframes_t), (Byte*)time); + if (success) + success = MidiRingBufferBase::full_read(sizeof(size_t), (Byte*)size); + if (success) + success = MidiRingBufferBase::full_read(*size, buf); + + return success; +} + + +inline size_t +MidiRingBuffer::write(nframes_t time, size_t size, const Byte* buf) +{ + assert(size > 0); - return 1; + if (write_space() < (sizeof(nframes_t) + sizeof(size_t) + size)) { + return 0; + } else { + MidiRingBufferBase::write(sizeof(nframes_t), (Byte*)&time); + MidiRingBufferBase::write(sizeof(size_t), (Byte*)&size); + MidiRingBufferBase::write(size, buf); + return size; } } + inline size_t MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end) { if (read_space() == 0) return 0; - size_t priv_read_ptr = g_atomic_int_get(&_read_ptr); - nframes_t time = _ev_buf[priv_read_ptr].time; - size_t count = 0; - size_t limit = read_space(); + MidiEvent ev; - while (time <= end && limit > 0) { - MidiEvent* const read_ev = &_ev_buf[priv_read_ptr]; - if (time >= start) { - dst.push_back(*read_ev); + size_t count = 0; + + while (read_space() > sizeof(nframes_t) + sizeof(size_t)) { + + full_peek(sizeof(nframes_t), (Byte*)&ev.time); + + if (ev.time > end) + break; + + bool success = MidiRingBufferBase::full_read(sizeof(nframes_t), (Byte*)&ev.time); + if (success) + success = MidiRingBufferBase::full_read(sizeof(size_t), (Byte*)&ev.size); + + if (!success) { + cerr << "MRB: READ ERROR (time/size)" << endl; + continue; + } + + if (ev.time >= start) { + Byte* write_loc = dst.reserve(ev.time, ev.size); + success = MidiRingBufferBase::full_read(ev.size, write_loc); + + if (!success) + cerr << "MRB: READ ERROR (data)" << endl; + //printf("MRB - read %#X %d %d with time %u at index %zu\n", - // read_ev->buffer[0], read_ev->buffer[1], read_ev->buffer[2], read_ev->time, + // ev.buffer[0], ev.buffer[1], ev.buffer[2], ev.time, // priv_read_ptr); + // } else { - printf("MRB - SKIPPING - %#X %d %d with time %u at index %zu\n", - read_ev->buffer[0], read_ev->buffer[1], read_ev->buffer[2], read_ev->time, - priv_read_ptr); + printf("MRB - SKIPPING - %#X %d %d with time %u\n", + ev.buffer[0], ev.buffer[1], ev.buffer[2], ev.time); break; } - clear_event(priv_read_ptr); - ++count; - --limit; - - priv_read_ptr = (priv_read_ptr + 1) % _size; - - assert(read_ev->time <= end); - time = _ev_buf[priv_read_ptr].time; + + assert(ev.time <= end); } - g_atomic_int_set(&_read_ptr, priv_read_ptr); - //printf("(R) read space: %zu\n", read_space()); return count; } -inline size_t -MidiRingBuffer::write(const MidiBuffer& in, nframes_t offset) -{ - size_t num_events = in.size(); - size_t to_write = std::min(write_space(), num_events); - - // FIXME: double copy :/ - for (size_t i=0; i < to_write; ++i) { - MidiEvent ev = in[i]; - ev.time += offset; - write(ev); - } - - return to_write; -} } // namespace ARDOUR diff --git a/libs/ardour/ardour/midi_source.h b/libs/ardour/ardour/midi_source.h index 5504db6ab6..dec9536c74 100644 --- a/libs/ardour/ardour/midi_source.h +++ b/libs/ardour/ardour/midi_source.h @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -62,9 +63,7 @@ class MidiSource : public Source static sigc::signal MidiSourceCreated; // The MIDI equivalent to "peaks" - static int start_view_data_thread (); - static void stop_view_data_thread (); - mutable sigc::signal ViewDataRangeReady; + mutable sigc::signal,nframes_t,nframes_t> ViewDataRangeReady; XMLNode& get_state (); int set_state (const XMLNode&); diff --git a/libs/ardour/ardour/smf_source.h b/libs/ardour/ardour/smf_source.h index d422a8af9e..6ab81a2ac3 100644 --- a/libs/ardour/ardour/smf_source.h +++ b/libs/ardour/ardour/smf_source.h @@ -86,6 +86,8 @@ class SMFSource : public MidiSource { XMLNode& get_state (); int set_state (const XMLNode&); + void seek_to(nframes_t time); + private: int init (string idstr, bool must_exist); @@ -96,9 +98,9 @@ class SMFSource : public MidiSource { bool find (std::string path, bool must_exist, bool& is_new); bool removable() const; bool writable() const { return _flags & Writable; } - + int open(); - + void write_chunk_header(char id[4], uint32_t length); void write_chunk(char id[4], uint32_t length, void* data); size_t write_var_len(uint32_t val); diff --git a/libs/ardour/ardour/source.h b/libs/ardour/ardour/source.h index 807ea089e6..2a6e0c8638 100644 --- a/libs/ardour/ardour/source.h +++ b/libs/ardour/ardour/source.h @@ -76,18 +76,18 @@ class Source : public PBD::StatefulDestructible protected: void update_length (nframes_t pos, nframes_t cnt); - Session& _session; - string _name; - DataType _type; - time_t _timestamp; - nframes_t _length; + Session& _session; + string _name; + DataType _type; + time_t _timestamp; + nframes_t _length; Glib::Mutex playlist_lock; typedef std::map, uint32_t > PlaylistMap; PlaylistMap _playlists; private: - uint32_t _in_use; + uint32_t _in_use; }; } diff --git a/libs/ardour/ardour/types.h b/libs/ardour/ardour/types.h index 766c3d4907..813c75f673 100644 --- a/libs/ardour/ardour/types.h +++ b/libs/ardour/ardour/types.h @@ -60,7 +60,7 @@ namespace ARDOUR { typedef uint32_t nframes_t; typedef jack_midi_event_t MidiEvent; - typedef unsigned char RawMidi; + typedef unsigned char Byte; enum IOChange { NoChange = 0, -- cgit v1.2.3