Skip to content

Commit

Permalink
Feature/add multiple out support to kr (#164)
Browse files Browse the repository at this point in the history
* Tag release to correct commit

* release: delete pre-existing

* almost working class def and no difference to the wrapper

* removed the print outs and added size check of the inputs - still not sure how I get that shape of output when it should be 3 of in.asArray.size

* input shape now working

* correct input count division (was the wrong way round)

* computes the real size of the input to decline the output size

* and finally a working class. We're all set!

* troubleshooting prints all over

* new proper class for proper algo

* remove the debugging pritns

* clang-format-ed

* pre-clang-format

* clang-format-ed with the right style sheet

---------

Co-authored-by: Owen Green <[email protected]>
  • Loading branch information
tremblap and weefuzzy authored Sep 4, 2023
1 parent 0daeef1 commit 7fbaf8f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 69 deletions.
92 changes: 44 additions & 48 deletions include/FluidSCWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ template <typename C>
class FluidSCWrapper : public impl::FluidSCWrapperBase<C>
{
using FloatControlsIter = impl::FloatControlsIter;
//I would like to template these to something more scaleable, but baby steps
friend class impl::RealTime<C,FluidSCWrapper>;
friend class impl::NonRealTime<C,FluidSCWrapper>;

// I would like to template these to something more scaleable, but baby steps
friend class impl::RealTime<C, FluidSCWrapper>;
friend class impl::NonRealTime<C, FluidSCWrapper>;

static void doVersion(Unit*, sc_msg_iter*)
{
Expand All @@ -38,12 +38,14 @@ class FluidSCWrapper : public impl::FluidSCWrapperBase<C>
bool mInit{false};

public:

template <size_t N, typename T>
using ArgumentSetter = typename ClientParams<FluidSCWrapper>::template Setter<sc_msg_iter, N, T>;
using ArgumentSetter =
typename ClientParams<FluidSCWrapper>::template Setter<sc_msg_iter, N, T>;

template <size_t N, typename T>
using ControlSetter = typename ClientParams<FluidSCWrapper>::template Setter<FloatControlsIter, N, T>;
using ControlSetter =
typename ClientParams<FluidSCWrapper>::template Setter<FloatControlsIter,
N, T>;

using Client = C;
using ParamSetType = typename C::ParamSetType;
Expand All @@ -66,73 +68,67 @@ class FluidSCWrapper : public impl::FluidSCWrapperBase<C>
getInterfaceTable(ft);
impl::FluidSCWrapperBase<Client>::setup(ft, name);
ft->fDefineUnitCmd(name, "version", doVersion);

std::string commandName("/");
commandName += getName();
commandName += "/version";
ft->fDefinePlugInCmd(commandName.c_str(),
[](World*, void*, sc_msg_iter*, void*){ doVersion(nullptr,nullptr); },
nullptr);

ft->fDefinePlugInCmd(
commandName.c_str(),
[](World*, void*, sc_msg_iter*, void*) { doVersion(nullptr, nullptr); },
nullptr);
}

static auto& setParams(Unit* x, ParamSetType& p, FloatControlsIter& inputs,
Allocator& alloc, bool constrain = false,
bool initialized = true)
{
bool verbose = x->mWorld->mVerbosity > 0;

using Reportage = decltype(static_cast<FluidSCWrapper*>(x)->mReportage);

Reportage* reportage = initialized ? &(static_cast<FluidSCWrapper*>(x)->mReportage) : new Reportage();

Reportage* reportage = initialized
? &(static_cast<FluidSCWrapper*>(x)->mReportage)
: new Reportage();

p.template setParameterValuesRT<ControlSetter>(
verbose ? reportage : nullptr, x, inputs, p, alloc);
if (constrain) p.constrainParameterValuesRT(verbose ? reportage : nullptr);
if(verbose)
if (verbose)
{
for(auto& r:*reportage)
for (auto& r : *reportage)
{
if(!r.ok()) printResult(x->mParent->mNode.mWorld, r);
if (!r.ok()) printResult(x->mParent->mNode.mWorld, r);
}
}
if(!initialized) delete reportage;
if (!initialized) delete reportage;
return p;
}

// static void printResult(SharedState<C>& x, Result& r)
// {
// if (!x.get() || !x->mNodeAlive) return;
// FluidSCWrapper::printResult(x->mNode->mWorld, r);
// }

static void printResult(World* w,Result& r)
static void printResult(World* w, Result& r)
{

switch (r.status())
{
case Result::Status::kWarning:
{
if (!w || w->mVerbosity > 0)
std::cout << "WARNING: " << getName() << " - " << r.message().c_str() << '\n';
break;
}
case Result::Status::kError:
{
std::cout << "ERROR: " << getName() << " - " << r.message().c_str() << '\n';
break;
}
case Result::Status::kCancelled:
{
std::cout << getName() << ": Task cancelled\n" << '\n';
break;
}
default:
{
}
}
case Result::Status::kWarning: {
if (!w || w->mVerbosity > 0)
std::cout << "WARNING: " << getName() << " - " << r.message().c_str()
<< '\n';
break;
}
case Result::Status::kError: {
std::cout << "ERROR: " << getName() << " - " << r.message().c_str()
<< '\n';
break;
}
case Result::Status::kCancelled: {
std::cout << getName() << ": Task cancelled\n" << '\n';
break;
}
default: {
}
}
}

private:
std::array<Result, Client::getParameterDescriptors().size()> mReportage;
};
Expand Down
58 changes: 37 additions & 21 deletions include/wrapper/RealTimeBase.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <Eigen/Core>
#include <data/FluidMemory.hpp>
#include <SC_PlugIn.hpp>
#include <Eigen/Core>

namespace fluid {
namespace client {
Expand Down Expand Up @@ -82,17 +82,18 @@ struct RealTimeBase
std::forward<Result&>(countScan));
return countScan;
}


void init(SCUnit& unit, Client& client, FloatControlsIter& controls, Allocator& alloc)

void init(SCUnit& unit, Client& client, FloatControlsIter& controls,
Allocator& alloc)
{
assert(!(client.audioChannelsOut() > 0 &&
client.controlChannelsOut().count > 0) &&
"Client can't have both audio and control outputs");
client.sampleRate(unit.fullSampleRate());
mInputConnections.reserve(asUnsigned(client.audioChannelsIn()));
mOutputConnections.reserve(asUnsigned(client.audioChannelsOut()));
mContext = FluidContext(unit.fullBufferSize(), alloc);
mContext = FluidContext(unit.fullBufferSize(), alloc);
Result r;
if (!(r = expectedSize(controls)).ok())
{
Expand All @@ -113,16 +114,22 @@ struct RealTimeBase
}
else if (client.controlChannelsIn())
{
mControlInputBuffer.resize(unit.mSpecialIndex + 1);
mAudioInputs.emplace_back(mControlInputBuffer);
mControlInputBuffer.resize(client.controlChannelsIn(),
(unit.mSpecialIndex + 1) /
client.controlChannelsIn());
for (index i = 0; i < client.controlChannelsIn(); ++i)
{
mAudioInputs.emplace_back(mControlInputBuffer.row(i));
}
mInputMapper = &RealTimeBase::mapControlInputs;
}
else mInputMapper = &RealTimeBase::mapNoOp;
else { mInputMapper = &RealTimeBase::mapNoOp; }

index outputSize = client.controlChannelsOut().size > 0
? std::max(client.audioChannelsOut(),
client.maxControlChannelsOut())
: unit.mSpecialIndex + 1;
: (unit.mSpecialIndex + 1);

mOutputs.reserve(asUnsigned(outputSize));

if (client.audioChannelsOut())
Expand All @@ -132,7 +139,7 @@ struct RealTimeBase
mOutputConnections.emplace_back(true);
mOutputs.emplace_back(nullptr, 0, 0);
}

mOutMapperPre = &RealTimeBase::mapAudioOutputs;
mOutMapperPost = &RealTimeBase::mapNoOp;
}
Expand Down Expand Up @@ -177,19 +184,28 @@ struct RealTimeBase
}
}

void mapControlInputs(SCUnit& unit, Client&)
void mapControlInputs(SCUnit& unit, Client& client)
{
for (index i = 0; i < unit.mSpecialIndex + 1; ++i)
assert((unit.mSpecialIndex + 1) % client.controlChannelsIn() == 0 &&
"Control channels can't be mapped");
index itemsPerChannel =
(unit.mSpecialIndex + 1) / client.controlChannelsIn();
for (index i = 0, offset = 0; i < client.controlChannelsIn();
++i, offset += itemsPerChannel)
{
assert(i <= std::numeric_limits<int>::max());
mControlInputBuffer[i] = unit.in0(static_cast<int>(i));
for (index j = 0; j < itemsPerChannel; ++j)
{
assert(j <= std::numeric_limits<int>::max());
mControlInputBuffer(i, j) = unit.in0(static_cast<int>(offset + j));
}
}
}

void mapControlOutputs(SCUnit& unit, Client&)
{
index numOuts = std::min<index>(mControlOutputBuffer.size(),unit.mNumOutputs);

index numOuts =
std::min<index>(mControlOutputBuffer.size(), unit.mNumOutputs);

for (index i = 0; i < numOuts; ++i)
{
assert(i <= std::numeric_limits<int>::max());
Expand All @@ -205,9 +221,9 @@ struct RealTimeBase
IsModel_t<Client>::value ? !mPrevTrig && unit.in0(0) > 0 : false;

mPrevTrig = trig;
#ifdef EIGEN_RUNTIME_NO_MALLOC
#ifdef EIGEN_RUNTIME_NO_MALLOC
Eigen::internal::set_is_malloc_allowed(false);
#endif
#endif
if (updateParams)
{
Wrapper::setParams(&unit, params, controls, alloc);
Expand All @@ -218,17 +234,17 @@ struct RealTimeBase
(this->*mOutMapperPre)(unit, client);
client.process(mAudioInputs, mOutputs, mContext);
(this->*mOutMapperPost)(unit, client);
#ifdef EIGEN_RUNTIME_NO_MALLOC
Eigen::internal::set_is_malloc_allowed(true); //not really
#endif
#ifdef EIGEN_RUNTIME_NO_MALLOC
Eigen::internal::set_is_malloc_allowed(true); // not really
#endif
}

private:
std::vector<bool> mInputConnections;
std::vector<bool> mOutputConnections;
std::vector<HostVector> mAudioInputs;
std::vector<HostVector> mOutputs;
FluidTensor<float, 1> mControlInputBuffer;
FluidTensor<float, 2> mControlInputBuffer;
FluidTensor<float, 1> mControlOutputBuffer;
bool mPrevTrig;
IOMapFn mInputMapper;
Expand Down

0 comments on commit 7fbaf8f

Please sign in to comment.