Skip to content

Commit

Permalink
PoC: Enhance/choices param (#108)
Browse files Browse the repository at this point in the history
* Add ChoiceT parameter type

* Add `select` control to `BufStatsClient` to road test `ChoicesParam`

* Choices params: Don't use a set because it breaks ordering

* SpectralShape: Add select param
  • Loading branch information
weefuzzy authored May 3, 2022
1 parent a0627dc commit 74c3d05
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
43 changes: 42 additions & 1 deletion include/clients/common/ParameterTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ under the European Union’s Horizon 2020 research and innovation programme
#include "Result.hpp"
#include "../../data/FluidIndex.hpp"
#include <memory>
#include <bitset>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include <set>

namespace fluid {
namespace client {
Expand Down Expand Up @@ -126,6 +127,37 @@ struct EnumT : ParamTypeBase
};


struct ChoicesT: ParamTypeBase
{
using type = std::bitset<16>;


template <index... N>
constexpr ChoicesT(const char* name, const char* displayName,
const char (&... string)[N])
: ParamTypeBase(name, displayName), strings{string...}, fixedSize(1),
numOptions(sizeof...(N)), defaultValue((1 << numOptions) - 1)
{
static_assert(sizeof...(N) > 0, "Fluid Param: No choice strings supplied!");
static_assert(sizeof...(N) <= 16,
"Fluid Param: : Maximum 16 things in an choice param");
}
const char* strings[16]; // unilateral descision klaxon: if you have more than
// 16 things in an Enum, you need to rethink
const index fixedSize;
const index numOptions;
const type defaultValue;

index lookup(std::string name)
{
static std::vector<std::string> lookupTable(strings, strings + numOptions);

auto pos = std::find(lookupTable.begin(), lookupTable.end(), name);
return pos != lookupTable.end() ? std::distance(lookupTable.begin(), pos)
: -1;
}
};

// can I avoid making this constexpr and thus using std::string? Let's see;
struct StringT : ParamTypeBase
{
Expand Down Expand Up @@ -492,6 +524,15 @@ EnumParam(const char* name, const char* displayName,
std::make_tuple(EnumT::EnumConstraint()), IsFixed{}};
}

template <typename IsFixed = Fixed<false>, size_t... N>
constexpr ParamSpec<ChoicesT, IsFixed>
ChoicesParam(const char* name, const char* displayName, const char (&... strings)[N])
{
return {ChoicesT(name, displayName, strings...), std::make_tuple(),
Fixed<false>{}};
}


template <typename IsFixed = Fixed<false>, size_t N, typename... Constraints>
constexpr ParamSpec<FloatArrayT, IsFixed, Constraints...>
FloatArrayParam(const char* name, const char* displayName,
Expand Down
23 changes: 20 additions & 3 deletions include/clients/nrt/BufStatsClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum BufferStatsParamIndex {
kStartChan,
kNumChans,
kStats,
kSelect,
kNumDerivatives,
kLow,
kMiddle,
Expand All @@ -43,6 +44,7 @@ constexpr auto BufStatsParams = defineParameters(
LongParam("startChan", "Start Channel", 0, Min(0)),
LongParam("numChans", "Number of Channels", -1),
BufferParam("stats", "Stats Buffer"),
ChoicesParam("select","Selection of Statistics","mean","std","skew","kurtosis","low","mid","high"),
LongParam("numDerivs", "Number of Derivatives", 0, Min(0), Max(2)),
FloatParam("low", "Low Percentile", 0, Min(0), Max(100),
UpperLimit<kMiddle>()),
Expand Down Expand Up @@ -124,7 +126,9 @@ class BufferStatsClient : public FluidBaseClient,
if (numFrames <= get<kNumDerivatives>())
return {Result::Status::kError, "Not enough frames"};

index outputSize = processor.numStats() * (get<kNumDerivatives>() + 1);
index outputSize = get<kSelect>().count() * (get<kNumDerivatives>() + 1);
index processorOutputSize = processor.numStats() * (get<kNumDerivatives>() + 1);

Result resizeResult =
dest.resize(outputSize, numChannels, source.sampleRate());

Expand Down Expand Up @@ -159,14 +163,27 @@ class BufferStatsClient : public FluidBaseClient,
}
}
FluidTensor<double, 2> tmp(numChannels, numFrames);
FluidTensor<double, 2> result(numChannels, outputSize);
FluidTensor<double, 2> result(numChannels, processorOutputSize);
for (int i = 0; i < numChannels; i++)
{
tmp.row(i) <<=
source.samps(get<kOffset>(), numFrames, get<kStartChan>() + i);
}
processor.process(tmp, result, get<kOutliersCutoff>(), weights);
for (int i = 0; i < numChannels; i++) { dest.samps(i) <<= result.row(i); }

auto selection = get<kSelect>();

for (index i = 0; i < numChannels; ++i)
{
auto outputChannel = dest.samps(i);
auto resultChannel = result.row(i);
for(index j = 0, k = 0; j < processorOutputSize; ++j)
{
if(selection[j % 7])
outputChannel(k++) = resultChannel(j);
}
}

return processingResult;
}
};
Expand Down
26 changes: 19 additions & 7 deletions include/clients/rt/SpectralShapeClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace spectralshape {
using algorithm::SpectralShape;

enum SpectralShapeParamIndex {
kSelect,
kMinFreq,
kMaxFreq,
kRollOffPercent,
Expand All @@ -37,6 +38,7 @@ enum SpectralShapeParamIndex {
};

constexpr auto SpectralShapeParams = defineParameters(
ChoicesParam("select","Selection of Features","centroid","spread","skew","kurtosis","rolloff","flatness","crest"),
FloatParam("minFreq", "Low Frequency Bound", 0, Min(0)),
FloatParam("maxFreq", "High Frequency Bound", -1, Min(-1)),
FloatParam("rolloffPercent", "Rolloff Percent", 95, Min(0), Max(100)),
Expand Down Expand Up @@ -70,12 +72,13 @@ class SpectralShapeClient : public FluidBaseClient,
}

SpectralShapeClient(ParamSetViewType& p)
: mParams(p), mSTFTBufferedProcess(get<kMaxFFTSize>(), 1, 0)
: mParams(p), mSTFTBufferedProcess(get<kMaxFFTSize>(), 1, 0),
mMaxOutputSize{asSigned(get<kSelect>().count())}
{
audioChannelsIn(1);
controlChannelsOut({1,7});
controlChannelsOut({1,mMaxOutputSize});
setInputLabels({"audio input"});
setOutputLabels({"centroid, spread, skewness, kurtosis, rolloff, flatness, crest factor"});
setOutputLabels({"spectral features"});
mDescriptors = FluidTensor<double, 1>(7);
}

Expand All @@ -101,10 +104,17 @@ class SpectralShapeClient : public FluidBaseClient,
get<kMaxFreq>(), get<kRollOffPercent>(), get<kFreqUnits>() == 1,
get<kAmpMeasure>() == 1);
});

// for (int i = 0; i < 7; ++i)
// output[asUnsigned(i)](0) = static_cast<T>(mDescriptors(i));
output[0] <<= mDescriptors;

auto selection = get<kSelect>();
index numSelected = asSigned(selection.count());
index numOuts = std::min<index>(mMaxOutputSize,numSelected);
for(index i = 0, j = 0 ; i < 7 && j < numOuts; ++i)
{
if(selection[asUnsigned(i)]) output[0](j++) = static_cast<T>(mDescriptors(i));
}
if(mMaxOutputSize > numSelected)
for(index i = (mMaxOutputSize - numSelected); i < mMaxOutputSize; ++i)
output[0](i) = 0;
}

index latency() { return get<kFFT>().winSize(); }
Expand All @@ -123,6 +133,8 @@ class SpectralShapeClient : public FluidBaseClient,
SpectralShape mAlgorithm;
FluidTensor<double, 1> mMagnitude;
FluidTensor<double, 1> mDescriptors;

index mMaxOutputSize;
};
} // namespace spectralshape

Expand Down

0 comments on commit 74c3d05

Please sign in to comment.