ParameterPort

master
zetaPRIME 2022-03-23 18:44:04 -04:00
parent a28d6e48b6
commit e63c93e146
3 changed files with 47 additions and 0 deletions

View File

@ -81,6 +81,7 @@ void Port::cleanConnections() {
std::shared_ptr<Port> Port::makePort(DataType dt) {
if (dt == Audio) return std::make_shared<AudioPort>();
if (dt == Command) return std::make_shared<CommandPort>();
if (dt == Parameter) return std::make_shared<ParameterPort>();
// fallback
return std::make_shared<Port>();
}

View File

@ -87,3 +87,33 @@ void CommandPort::push(std::vector<uint8_t> v) {
data = static_cast<uint8_t*>(audioEngine->tickAlloc(dataSize));
memcpy(data, v.data(), dataSize);
}
void ParameterPort::pull() {
auto t = audioEngine->curTickId();
if (tickUpdatedOn == t) return;
lock.lock();
if (tickUpdatedOn == t) { lock.unlock(); return; } // someone else got here before us
buf = nullptr;
if (type == Input) {
if (isConnected()) {
if (auto p = std::static_pointer_cast<ParameterPort>(connections[0].lock()); p) {
p->pull();
buf = p->buf;
size = p->size;
}
}
} else if (auto pt = std::static_pointer_cast<ParameterPort>(passthroughTo.lock()); pt && pt->dataType() == Parameter) {
pt->pull();
buf = pt->buf;
size = pt->size;
}
if (!buf) { // no buffer pulled from input or passthrough; create a new one
size = audioEngine->curTickSize();
buf = static_cast<double*>(audioEngine->tickAlloc(size * sizeof(double)));
std::fill_n(buf, size, std::numeric_limits<double>::quiet_NaN());
}
tickUpdatedOn = t;
lock.unlock();
}

View File

@ -57,4 +57,20 @@ namespace Xybrid::Data {
/// Push a data buffer
void push(std::vector<uint8_t>);
};
class ParameterPort : public Port {
public:
double* buf;
size_t size;
ParameterPort() = default;
~ParameterPort() override = default;
double& operator[](size_t at) { return buf[at]; }
double& at(size_t at) { return buf[at]; }
Port::DataType dataType() const override { return Port::Parameter; }
void pull() override;
};
}