From 7d54c7a3831bde7a99a3b7824d2304da7c5715b0 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 6 Oct 2008 02:22:14 +0000 Subject: LV2 and polyphony fixes from kfoltman. Fix crash on changing polyphony when control->audio connections are present. Increasing polyphony audibly adds new voices now and controls can be individually twiddled, but changing polyphony nukes individual voice values (issue #223). git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@1623 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/ConnectionImpl.cpp | 102 ++++++++++++++++++++++++++++++------------ src/engine/ConnectionImpl.hpp | 14 +++++- src/engine/InputPort.cpp | 33 ++++++++------ src/engine/InputPort.hpp | 4 +- src/engine/LADSPANode.cpp | 13 ++++++ src/engine/LV2Node.cpp | 21 +++++++-- src/engine/NodeBase.cpp | 2 +- src/engine/PortImpl.hpp | 3 ++ 8 files changed, 142 insertions(+), 50 deletions(-) (limited to 'src/engine') diff --git a/src/engine/ConnectionImpl.cpp b/src/engine/ConnectionImpl.cpp index d226b4dd..2e8e1d25 100644 --- a/src/engine/ConnectionImpl.cpp +++ b/src/engine/ConnectionImpl.cpp @@ -24,8 +24,8 @@ #include "AudioBuffer.hpp" #include "ProcessContext.hpp" -#include -using namespace std; +/*#include +using namespace std;*/ namespace Ingen { @@ -36,15 +36,11 @@ namespace Ingen { * user (InputPort). */ ConnectionImpl::ConnectionImpl(PortImpl* src_port, PortImpl* dst_port) - : _src_port(src_port) + : _mode(DIRECT) + , _src_port(src_port) , _dst_port(dst_port) , _local_buffer(NULL) , _buffer_size(dst_port->buffer_size()) - /*, _must_mix( (src_port->poly() != dst_port->poly()) - || (src_port->buffer(0)->size() < dst_port->buffer(0)->size()) )*/ - , _must_mix( (src_port->polyphonic() && (! dst_port->polyphonic())) - || (src_port->poly() != dst_port->poly() ) - || (src_port->buffer(0)->size() < dst_port->buffer(0)->size()) ) , _pending_disconnection(false) { assert(src_port); @@ -58,11 +54,9 @@ ConnectionImpl::ConnectionImpl(PortImpl* src_port, PortImpl* dst_port) /*assert((src_port->parent_node()->poly() == dst_port->parent_node()->poly()) || (src_port->parent_node()->poly() == 1 || dst_port->parent_node()->poly() == 1));*/ - if (type() == DataType::EVENT) - _must_mix = false; // FIXME: kludge - - if (_must_mix) - _local_buffer = Buffer::create(dst_port->type(), dst_port->buffer(0)->size()); + set_mode(); + if (need_buffer()) + _local_buffer = Buffer::create(dst_port->type(), _buffer_size); /* FIXME: 1->1 connections with a destination with fixed buffers copies unecessarily */ //cerr << src_port->path() << " -> " << dst_port->path() << " must mix: " << _must_mix << endl; @@ -75,10 +69,23 @@ ConnectionImpl::~ConnectionImpl() } +void +ConnectionImpl::set_mode() +{ + if (must_mix()) + _mode = MIX; + else if (must_extend()) + _mode = EXTEND; + + if (type() == DataType::EVENT) + _mode = DIRECT; // FIXME: kludge +} + + void ConnectionImpl::set_buffer_size(size_t size) { - if (_must_mix) { + if (_mode == MIX || _mode == EXTEND) { assert(_local_buffer); delete _local_buffer; @@ -89,22 +96,34 @@ ConnectionImpl::set_buffer_size(size_t size) } +bool +ConnectionImpl::must_mix() const +{ + bool mix = ( /*(_src_port->poly() != _dst_port->poly()) + ||*/ (_src_port->polyphonic() && !_dst_port->polyphonic()) + || (_src_port->parent()->polyphonic() && !_dst_port->parent()->polyphonic()) ); + + return mix; +} + + +bool +ConnectionImpl::must_extend() const +{ + return (_src_port->buffer_size() != _dst_port->buffer_size()); +} + + void ConnectionImpl::prepare_poly(uint32_t poly) { _src_port->prepare_poly(poly); - if (type() == DataType::CONTROL || type() == DataType::AUDIO) - _must_mix = (poly > 1) && ( - (_src_port->poly() != _dst_port->poly()) - || (_src_port->polyphonic() && !_dst_port->polyphonic()) - || (_src_port->parent()->polyphonic() && !_dst_port->parent()->polyphonic()) ); - - /*cerr << src_port()->path() << " * " << src_port()->poly() + /*cerr << "CONNECTION PREPARE: " << src_port()->path() << " * " << src_port()->poly() << " -> " << dst_port()->path() << " * " << dst_port()->poly() - << "\t\tmust mix: " << _must_mix << " at poly " << poly << endl;*/ + << "\t\tmust mix: " << must_mix() << " at poly " << poly << endl;*/ - if (_must_mix && ! _local_buffer) + if (need_buffer() && !_local_buffer) _local_buffer = Buffer::create(_dst_port->type(), _dst_port->buffer(0)->size()); } @@ -113,7 +132,15 @@ void ConnectionImpl::apply_poly(Raul::Maid& maid, uint32_t poly) { _src_port->apply_poly(maid, poly); - if (poly == 1 && _local_buffer && !_must_mix) { + set_mode(); + + /*cerr << "CONNECTION APPLY: " << src_port()->path() << " * " << src_port()->poly() + << " -> " << dst_port()->path() << " * " << dst_port()->poly() + << "\t\tmust mix: " << must_mix() << ", extend: " << must_extend() + << ", poly " << poly << endl;*/ + + // Recycle buffer if it's no longer needed + if (_local_buffer && !need_buffer()) { maid.push(_local_buffer); _local_buffer = NULL; } @@ -135,9 +162,10 @@ ConnectionImpl::process(ProcessContext& context) /*cerr << src_port()->path() << " * " << src_port()->poly() << " -> " << dst_port()->path() << " * " << dst_port()->poly() - << "\t\tmust mix: " << _must_mix << endl;*/ + << "\t\tmode: " << (int)_mode << endl;*/ - if (_must_mix && (type() == DataType::CONTROL || type() == DataType::AUDIO)) { + if (_mode == MIX) { + assert(type() == DataType::AUDIO || type() == DataType::CONTROL); const AudioBuffer* const src_buffer = (AudioBuffer*)src_port()->buffer(0); AudioBuffer* mix_buf = (AudioBuffer*)_local_buffer; @@ -171,11 +199,27 @@ ConnectionImpl::process(ProcessContext& context) // Scale the buffer down. if (src_port()->poly() > 1) mix_buf->scale(1.0f/(float)src_port()->poly(), 0, _buffer_size-1); + + } else if (_mode == EXTEND) { + assert(type() == DataType::AUDIO || type() == DataType::CONTROL); + assert(src_port()->poly() == dst_port()->poly()); // otherwise we should be mixing - } else if (_must_mix && type() == DataType::EVENT) { + const size_t poly = src_port()->poly(); + + const size_t copy_size = std::min(src_port()->buffer(0)->size(), + dst_port()->buffer(0)->size()); + + for (size_t i = 0; i < poly; ++i) { + AudioBuffer* src_buf = (AudioBuffer*)src_port()->buffer(0); + AudioBuffer* dst_buf = (AudioBuffer*)dst_port()->buffer(0); - std::cerr << "WARNING: No event mixing." << std::endl; - + // Copy src to start of dst + dst_buf->copy(src_buf, 0, copy_size-1); + + // Write last value of src buffer to remainder of dst buffer, if necessary + if (copy_size < dst_buf->size()) + dst_buf->set_block(src_buf->value_at(copy_size - 1), copy_size, dst_buf->size() - 1); + } } } diff --git a/src/engine/ConnectionImpl.hpp b/src/engine/ConnectionImpl.hpp index b3da1b54..b3d7cf4b 100644 --- a/src/engine/ConnectionImpl.hpp +++ b/src/engine/ConnectionImpl.hpp @@ -66,19 +66,29 @@ public: * in a mono->poly connection). */ inline Buffer* buffer(size_t voice) const; + + inline size_t buffer_size() const { return _buffer_size; } void set_buffer_size(size_t size); void prepare_poly(uint32_t poly); void apply_poly(Raul::Maid& maid, uint32_t poly); + bool must_mix() const; + bool must_extend() const; + + inline bool need_buffer() const { return must_mix(); } + inline bool can_direct() const { return _mode == DIRECT; } + DataType type() const { return _src_port->type(); } protected: + enum { DIRECT, MIX, EXTEND } _mode; + void set_mode(); + PortImpl* const _src_port; PortImpl* const _dst_port; Buffer* _local_buffer; size_t _buffer_size; - bool _must_mix; bool _pending_disconnection; }; @@ -86,7 +96,7 @@ protected: inline Buffer* ConnectionImpl::buffer(size_t voice) const { - if (_must_mix) { + if (_mode == MIX) { return _local_buffer; } else if ( ! _src_port->polyphonic()) { return _src_port->buffer(0); diff --git a/src/engine/InputPort.cpp b/src/engine/InputPort.cpp index 9f62bcb0..3257144c 100644 --- a/src/engine/InputPort.cpp +++ b/src/engine/InputPort.cpp @@ -42,6 +42,13 @@ InputPort::InputPort(NodeImpl* parent, : PortImpl(parent, name, index, poly, type, value, buffer_size) { } + + +bool +InputPort::can_direct() const +{ + return _connections.size() == 1 && _connections.front()->can_direct(); +} void @@ -64,10 +71,9 @@ InputPort::prepare_poly(uint32_t poly) for (Connections::iterator c = _connections.begin(); c != _connections.end(); ++c) ((ConnectionImpl*)c->get())->prepare_poly(poly); - connect_buffers(); return true; } - + bool InputPort::apply_poly(Raul::Maid& maid, uint32_t poly) @@ -81,13 +87,14 @@ InputPort::apply_poly(Raul::Maid& maid, uint32_t poly) PortImpl::apply_poly(maid, poly); assert(this->poly() == poly); - if (_connections.size() == 1) { + if (can_direct()) { ConnectionImpl* c = _connections.begin()->get(); - for (uint32_t i=0; i < _poly; ++i) + for (uint32_t i=_poly; i < poly; ++i) _buffers->at(i)->join(c->buffer(i)); } - connect_buffers(); + for (uint32_t i=0; i < _poly; ++i) + PortImpl::parent_node()->set_port_buffer(i, _index, buffer(i)); return true; } @@ -106,7 +113,7 @@ InputPort::add_connection(Connections::Node* const c) bool modify_buffers = !_fixed_buffers; if (modify_buffers) { - if (_connections.size() == 1) { + if (can_direct()) { // Use buffer directly to avoid copying for (uint32_t i=0; i < _poly; ++i) { _buffers->at(i)->join(c->elem()->buffer(i)); @@ -157,7 +164,7 @@ InputPort::remove_connection(const OutputPort* src_port) } else if (modify_buffers && _connections.size() == 1) { // Share a buffer for (uint32_t i=0; i < _poly; ++i) { - _buffers->at(i)->join((*_connections.begin())->buffer(i)); + _buffers->at(i)->join(_connections.front()->buffer(i)); } } } @@ -197,10 +204,9 @@ InputPort::pre_process(ProcessContext& context) if ( ! _fixed_buffers) { // If only one connection, try to use buffer directly (zero copy) - if (_connections.size() == 1) { + if (can_direct()) { for (uint32_t i=0; i < _poly; ++i) { - //cerr << path() << " joining to " << (*_connections.begin())->buffer(i) << endl; - _buffers->at(i)->join((*_connections.begin())->buffer(i)); + _buffers->at(i)->join(_connections.front()->buffer(i)); } do_mixdown = false; } @@ -225,7 +231,7 @@ InputPort::pre_process(ProcessContext& context) if (!do_mixdown) { /*#ifndef NDEBUG for (uint32_t i=0; i < _poly; ++i) - assert(buffer(i) == (*_connections.begin())->buffer(i)); + assert(buffer(i) == _connections.front()->buffer(i)); #endif*/ return; } @@ -234,7 +240,7 @@ InputPort::pre_process(ProcessContext& context) for (uint32_t voice=0; voice < _poly; ++voice) { // Copy first connection buffer(voice)->copy( - (*_connections.begin())->buffer(voice), 0, _buffer_size-1); + _connections.front()->buffer(voice), 0, _buffer_size-1); // Accumulate the rest if (_connections.size() > 1) { @@ -254,8 +260,7 @@ InputPort::pre_process(ProcessContext& context) cerr << "WARNING: MIDI mixing not implemented, only first connection used." << endl; // Copy first connection - _buffers->at(0)->copy( - (*_connections.begin())->buffer(0), 0, _buffer_size-1); + _buffers->at(0)->copy(_connections.front()->buffer(0), 0, _buffer_size-1); } } diff --git a/src/engine/InputPort.hpp b/src/engine/InputPort.hpp index 6eca20f4..abc63a91 100644 --- a/src/engine/InputPort.hpp +++ b/src/engine/InputPort.hpp @@ -74,10 +74,12 @@ public: bool is_input() const { return true; } bool is_output() const { return false; } - + virtual void set_buffer_size(size_t size); private: + bool can_direct() const; + Connections _connections; }; diff --git a/src/engine/LADSPANode.cpp b/src/engine/LADSPANode.cpp index 22f5bbf3..6af45985 100644 --- a/src/engine/LADSPANode.cpp +++ b/src/engine/LADSPANode.cpp @@ -74,6 +74,19 @@ LADSPANode::prepare_poly(uint32_t poly) cerr << "Failed to instantiate plugin!" << endl; return false; } + + // Initialize the values of new ports + for (unsigned long j=0; j < num_ports(); ++j) { + PortImpl* const port = _ports->at(j); + Buffer *buffer = port->prepared_buffer(i); + + // FIXME: Preserve individual voice values + if (port->type() == DataType::CONTROL) { + ((AudioBuffer*)buffer)->set_value(port->value().get_float(), 0, 0); + } else if (port->type() == DataType::AUDIO) { + ((AudioBuffer*)buffer)->set_value(0.0f, 0, 0); + } + } if (_activated && _descriptor->activate) _descriptor->activate(_prepared_instances->at(i)); diff --git a/src/engine/LV2Node.cpp b/src/engine/LV2Node.cpp index d8b4c29c..7f8f95e8 100644 --- a/src/engine/LV2Node.cpp +++ b/src/engine/LV2Node.cpp @@ -74,17 +74,31 @@ LV2Node::prepare_poly(uint32_t poly) return true; } + SharedPtr info = _lv2_plugin->lv2_info(); _prepared_instances = new Raul::Array(poly, *_instances); for (uint32_t i = _polyphony; i < _prepared_instances->size(); ++i) { // FIXME: features array (in NodeFactory) must be passed! _prepared_instances->at(i) = slv2_plugin_instantiate( - _lv2_plugin->slv2_plugin(), _srate, NULL); + _lv2_plugin->slv2_plugin(), _srate, info->lv2_features()); if (_prepared_instances->at(i) == NULL) { cerr << "Failed to instantiate plugin!" << endl; return false; } + // Initialize the values of new ports + for (unsigned long j=0; j < num_ports(); ++j) { + PortImpl* const port = _ports->at(j); + Buffer *buffer = port->prepared_buffer(i); + + // FIXME: Preserve individual voice values + if (port->type() == DataType::CONTROL) { + ((AudioBuffer*)buffer)->set_value(port->value().get_float(), 0, 0); + } else if (port->type() == DataType::AUDIO) { + ((AudioBuffer*)buffer)->set_value(0.0f, 0, 0); + } + } + if (_activated) slv2_instance_activate(_prepared_instances->at(i)); } @@ -166,6 +180,9 @@ LV2Node::instantiate() float* def_values = new float[num_ports]; slv2_plugin_get_port_ranges_float(plug, 0, 0, def_values); + SLV2Value pred = slv2_value_new_uri(info->lv2_world(), + "http://lv2plug.in/ns/dev/contexts#context"); + for (uint32_t j=0; j < num_ports; ++j) { SLV2Port id = slv2_plugin_get_port_by_index(plug, j); @@ -215,8 +232,6 @@ LV2Node::instantiate() if (direction == INPUT && data_type == DataType::CONTROL) ((AudioBuffer*)port->buffer(0))->set_value(def, 0, 0); - SLV2Value pred = slv2_value_new_uri(info->lv2_world(), - "http://lv2plug.in/ns/dev/contexts#context"); SLV2Values contexts = slv2_port_get_value(plug, id, pred); for (uint32_t i = 0; i < slv2_values_size(contexts); ++i) { SLV2Value c = slv2_values_get_at(contexts, i); diff --git a/src/engine/NodeBase.cpp b/src/engine/NodeBase.cpp index bb4f0e5c..559b51f4 100644 --- a/src/engine/NodeBase.cpp +++ b/src/engine/NodeBase.cpp @@ -129,7 +129,7 @@ NodeBase::apply_poly(Raul::Maid& maid, uint32_t poly) for (uint32_t i=0; i < num_ports(); ++i) for (uint32_t j=0; j < _polyphony; ++j) - set_port_buffer(j, i, _ports->at(i)->buffer(j)); + set_port_buffer(j, i, _ports->at(i)->prepared_buffer(j)); return true; } diff --git a/src/engine/PortImpl.hpp b/src/engine/PortImpl.hpp index a2eaf2c9..338c7482 100644 --- a/src/engine/PortImpl.hpp +++ b/src/engine/PortImpl.hpp @@ -81,6 +81,9 @@ public: return buf; } } + inline Buffer* prepared_buffer(uint32_t voice) const { + return _prepared_buffers->at(voice); + } /** Called once per process cycle */ virtual void pre_process(ProcessContext& context) = 0; -- cgit v1.2.1