From 8d559e4991a491b612e63d5a4deff0ab48a3d3dd Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 31 Jul 2012 15:12:38 +0000 Subject: Merge AudioBuffer into Buffer and avoid all the casting. git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@4584 a436a847-0d15-0410-975c-d299462d15a1 --- src/server/Buffer.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 5 deletions(-) (limited to 'src/server/Buffer.cpp') diff --git a/src/server/Buffer.cpp b/src/server/Buffer.cpp index e877533a..4de832e9 100644 --- a/src/server/Buffer.cpp +++ b/src/server/Buffer.cpp @@ -17,6 +17,7 @@ #define __STDC_LIMIT_MACROS 1 #include +#include #include #include @@ -56,8 +57,15 @@ Buffer::Buffer(BufferFactory& bufs, LV2_URID type, uint32_t capacity) } memset(_atom, 0, capacity); + _atom->size = capacity - sizeof(LV2_Atom); _atom->type = type; - assert(_atom->type != 1); + + if (type == bufs.uris().atom_Sound) { + // Audio port (Vector of float) + LV2_Atom_Vector* vec = (LV2_Atom_Vector*)_atom; + vec->body.child_size = sizeof(float); + vec->body.child_type = bufs.uris().atom_Float; + } clear(); } @@ -67,6 +75,24 @@ Buffer::~Buffer() free(_atom); } +bool +Buffer::is_audio() const +{ + return _type == _factory.uris().atom_Sound; +} + +bool +Buffer::is_control() const +{ + return _type == _factory.uris().atom_Float; +} + +bool +Buffer::is_sequence() const +{ + return _type == _factory.uris().atom_Sequence; +} + void Buffer::recycle() { @@ -76,17 +102,38 @@ Buffer::recycle() void Buffer::clear() { - _atom->size = 0; + if (is_audio() || is_control()) { + _atom->size = _capacity - sizeof(LV2_Atom); + set_block(0, 0, nframes() - 1); + } else if (is_sequence()) { + _atom->size = sizeof(LV2_Atom_Sequence_Body); + } } void Buffer::copy(Context& context, const Buffer* src) { - // Copy only if src is a POD object that fits - if (src->_atom->type != 0 && sizeof(LV2_Atom) + src->_atom->size <= capacity()) { + if (_type == src->type() && src->_atom->size + sizeof(LV2_Atom) <= _capacity) { memcpy(_atom, src->_atom, sizeof(LV2_Atom) + src->_atom->size); + } else if (src->is_audio() && is_control()) { + samples()[0] = src->samples()[context.offset()]; + } else if (src->is_control() && is_audio()) { + samples()[context.offset()] = src->samples()[0]; + } else { + clear(); + } +} + +void +Buffer::set_block(Sample val, size_t start_offset, size_t end_offset) +{ + assert(end_offset >= start_offset); + assert(end_offset < nframes()); + + Sample* const buf = samples(); + for (size_t i = start_offset; i <= end_offset; ++i) { + buf[i] = val; } - assert(_atom->type != 1); } void @@ -130,6 +177,17 @@ Buffer::port_data(PortType port_type, SampleCount offset) const const_cast(this)->port_data(port_type, offset)); } +float +Buffer::peak(const Context& context) const +{ + float peak = 0.0f; + const Sample* const buf = samples(); + for (FrameTime i = 0; i < context.nframes(); ++i) { + peak = fmaxf(peak, buf[i]); + } + return peak; +} + void Buffer::prepare_write(Context& context) { -- cgit v1.2.1