From d1cc7e5a50e2144d7aea01bc5eceed6657513c1b Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 29 Nov 2013 22:26:33 -0500 Subject: fix up a bunch of confusion regarding the size/capacity/allocation of audio & midi buffers --- libs/ardour/audio_buffer.cc | 26 +++++++++++++++++--------- libs/ardour/audio_port.cc | 9 +++------ libs/ardour/buffer_set.cc | 4 ++-- libs/ardour/midi_buffer.cc | 21 +++++++++++++-------- libs/ardour/thread_buffers.cc | 10 ++++++---- 5 files changed, 41 insertions(+), 29 deletions(-) diff --git a/libs/ardour/audio_buffer.cc b/libs/ardour/audio_buffer.cc index 1fd0337dd1..a36ad81c2a 100644 --- a/libs/ardour/audio_buffer.cc +++ b/libs/ardour/audio_buffer.cc @@ -28,15 +28,15 @@ using namespace PBD; using namespace ARDOUR; AudioBuffer::AudioBuffer(size_t capacity) - : Buffer(DataType::AUDIO, capacity) + : Buffer (DataType::AUDIO) , _owns_data (false) , _data (0) { - if (_capacity > 0) { + if (capacity) { _owns_data = true; // prevent resize() from gagging - resize (_capacity); + resize (capacity); _silent = false; // force silence on the intial buffer state - silence (_capacity); + clear (); } } @@ -50,21 +50,29 @@ void AudioBuffer::resize (size_t size) { if (!_owns_data) { + /* XXX how the hell is this enforced? */ + _capacity = size; return; } - if (size < _capacity) { - _size = size; + if (_data && size < _capacity) { + /* buffer is already large enough */ + + if (size < _size) { + /* truncate */ + _size = size; + } + return; } free (_data); + cache_aligned_malloc ((void**) &_data, sizeof (Sample) * size); + _capacity = size; - _size = size; + _size = 0; _silent = false; - - cache_aligned_malloc ((void**) &_data, sizeof (Sample) * _capacity); } bool diff --git a/libs/ardour/audio_port.cc b/libs/ardour/audio_port.cc index 6a86360b69..2fecbf9392 100644 --- a/libs/ardour/audio_port.cc +++ b/libs/ardour/audio_port.cc @@ -56,14 +56,11 @@ AudioPort::cycle_start (pframes_t nframes) } void -AudioPort::cycle_end (pframes_t) +AudioPort::cycle_end (pframes_t nframes) { if (sends_output() && !_buffer->written()) { - /* we can't use nframes here because the current buffer capacity may - be shorter than the full buffer size if we split the cycle. - */ - if (_buffer->capacity () > 0) { - _buffer->silence (_buffer->capacity()); + if (_buffer->capacity() >= nframes) { + _buffer->silence (nframes); } } } diff --git a/libs/ardour/buffer_set.cc b/libs/ardour/buffer_set.cc index e67aee6be7..227d7a1e95 100644 --- a/libs/ardour/buffer_set.cc +++ b/libs/ardour/buffer_set.cc @@ -167,7 +167,7 @@ BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capac // If there's not enough or they're too small, just nuke the whole thing and // rebuild it (so I'm lazy..) if (bufs.size() < num_buffers - || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) { + || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) { // Nuke it for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) { @@ -179,7 +179,7 @@ BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capac for (size_t i = 0; i < num_buffers; ++i) { bufs.push_back(Buffer::create(type, buffer_capacity)); } - + _available.set(type, num_buffers); _count.set (type, num_buffers); } diff --git a/libs/ardour/midi_buffer.cc b/libs/ardour/midi_buffer.cc index d75b861ea1..1a6cb7fa26 100644 --- a/libs/ardour/midi_buffer.cc +++ b/libs/ardour/midi_buffer.cc @@ -33,12 +33,12 @@ using namespace PBD; // FIXME: mirroring for MIDI buffers? MidiBuffer::MidiBuffer(size_t capacity) - : Buffer(DataType::MIDI, capacity) - , _data(0) + : Buffer (DataType::MIDI) + , _data (0) { if (capacity) { - resize(_capacity); - silence(_capacity); + resize (capacity); + silence (capacity); } } @@ -50,17 +50,22 @@ MidiBuffer::~MidiBuffer() void MidiBuffer::resize(size_t size) { - assert(size > 0); + if (_data && size < _capacity) { + + if (_size < size) { + /* truncate */ + _size = size; + } - if (size < _capacity) { return; } - free(_data); + free (_data); + + cache_aligned_malloc ((void**) &_data, size); _size = 0; _capacity = size; - cache_aligned_malloc ((void**) &_data, _capacity); assert(_data); } diff --git a/libs/ardour/thread_buffers.cc b/libs/ardour/thread_buffers.cc index fd3160bb15..e469187ce9 100644 --- a/libs/ardour/thread_buffers.cc +++ b/libs/ardour/thread_buffers.cc @@ -60,7 +60,7 @@ ThreadBuffers::ensure_buffers (ChanCount howmany) for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { size_t count = std::max (scratch_buffers->available().get(*t), howmany.get(*t)); - size_t size = _engine->raw_buffer_size (*t); + size_t size = _engine->raw_buffer_size (*t) / sizeof (Sample); scratch_buffers->ensure_buffers (*t, count, size); mix_buffers->ensure_buffers (*t, count, size); @@ -68,12 +68,14 @@ ThreadBuffers::ensure_buffers (ChanCount howmany) route_buffers->ensure_buffers (*t, count, size); } + size_t audio_buffer_size = _engine->raw_buffer_size (DataType::AUDIO) / sizeof (Sample); + delete [] gain_automation_buffer; - gain_automation_buffer = new gain_t[_engine->raw_buffer_size (DataType::AUDIO)]; + gain_automation_buffer = new gain_t[audio_buffer_size]; delete [] send_gain_automation_buffer; - send_gain_automation_buffer = new gain_t[_engine->raw_buffer_size (DataType::AUDIO)]; + send_gain_automation_buffer = new gain_t[audio_buffer_size]; - allocate_pan_automation_buffers (_engine->raw_buffer_size (DataType::AUDIO), howmany.n_audio(), false); + allocate_pan_automation_buffers (audio_buffer_size, howmany.n_audio(), false); } void -- cgit v1.2.3