From 9871e7d7a9297a297e4619b118009bee69dc9d5a Mon Sep 17 00:00:00 2001 From: Joseph <joseph.larralde@gmail.com> Date: Tue, 30 May 2017 11:48:33 +0200 Subject: [PATCH] added JSON i/o abilities to rapidPiPoTools --- .../rapidPiPoTools/rapidPiPoHost.cpp | 637 +++++++----------- .../rapidPiPoTools/rapidPiPoHost.h | 122 +--- .../rapidPiPoTools/rapidPiPoTools.h | 4 +- 3 files changed, 290 insertions(+), 473 deletions(-) diff --git a/src/signalProcessing/rapidPiPoTools/rapidPiPoHost.cpp b/src/signalProcessing/rapidPiPoTools/rapidPiPoHost.cpp index 8ad567a..6663da4 100644 --- a/src/signalProcessing/rapidPiPoTools/rapidPiPoHost.cpp +++ b/src/signalProcessing/rapidPiPoTools/rapidPiPoHost.cpp @@ -1,8 +1,10 @@ #include "rapidPiPoHost.h" #include <iostream> +#include <fstream> -//===================== P I P O = H O S T = U T I L S ========================// +//=========================== H O S T === U T I L S ==========================// +/* static const unsigned int maxWordLen = 256; static bool getPiPoInstanceAndAttrName(const char *attrName, @@ -29,6 +31,7 @@ static bool getPiPoInstanceAndAttrName(const char *attrName, return false; } +//*/ static void fromPiPoStreamAttributes(PiPoStreamAttributes &src, @@ -96,7 +99,7 @@ static void toPiPoStreamAttributes(pipoStreamAttributes &src, ); } -//========================= H O S T = M E T H O D S ==========================// +//========================= H O S T === M E T H O D S ========================// PiPoHost::PiPoHost() : inputStreamAttrs(PIPO_MAX_LABELS), @@ -212,7 +215,8 @@ PiPoHost::setAttr(const std::string &attrName, double value) { PiPo::Attr *attr = this->graph->getAttr(attrName.c_str()); - if (attr != NULL) { + if (attr != NULL) + { int iAttr = attr->getIndex(); return this->graph->setAttr(iAttr, value); } @@ -225,15 +229,19 @@ PiPoHost::setAttr(const std::string &attrName, const std::vector<double> &values { PiPo::Attr *attr = this->graph->getAttr(attrName.c_str()); - if (attr != NULL) { + if (attr != NULL) + { int iAttr = attr->getIndex(); double vals[values.size()]; unsigned int i = 0; - for (auto &value : values) { + + for (auto &value : values) + { vals[i] = value; i++; } - return this->graph->setAttr(iAttr, &vals[0], values.size()); + + return this->graph->setAttr(iAttr, &vals[0], static_cast<unsigned int>(values.size())); } return false; @@ -244,15 +252,19 @@ PiPoHost::setAttr(const std::string &attrName, const std::string &value) // for { PiPo::Attr *attr = this->graph->getAttr(attrName.c_str()); - if (attr != NULL) { - int iAttr = attr->getIndex(); + if (attr != NULL) + { + // int iAttr = attr->getIndex(); PiPo::Type type = attr->getType(); - if (type == PiPo::Type::Enum) { + if (type == PiPo::Type::Enum) + { std::vector<const char *> *list = attr->getEnumList(); - for (int i = 0; i < list->size(); i++) { - if (strcmp(list->at(i), value.c_str()) == 0) { + for (int i = 0; i < list->size(); i++) + { + if (strcmp(list->at(i), value.c_str()) == 0) + { attr->set(0, i); return true; } @@ -263,180 +275,215 @@ PiPoHost::setAttr(const std::string &attrName, const std::string &value) // for return false; } - -int -PiPoHost::propagateInputStreamAttributes() +std::vector<std::string> +PiPoHost::getAttrNames() { - if (this->graph != nullptr) + std::vector<std::string> res; + + for (unsigned int i = 0; i < this->graph->getNumAttrs(); ++i) { - return this->graph->streamAttributes(this->inputStreamAttrs.hasTimeTags, - this->inputStreamAttrs.rate, - this->inputStreamAttrs.offset, - this->inputStreamAttrs.dims[0], - this->inputStreamAttrs.dims[1], - this->inputStreamAttrs.labels, - this->inputStreamAttrs.hasVarSize, - this->inputStreamAttrs.domain, - this->inputStreamAttrs.maxFrames); + res.push_back(this->graph->getAttr(i)->getName()); } - - return 0; -} - - - - -// void -// PiPoHost::streamAttributesChanged(PiPo *pipo, PiPo::Attr *attr) { -// this->propagateInputAttributes(); -// } - -// void -// PiPoHost::signalError(PiPo *pipo, std::string errorMsg) { -// // todo -// } - -// void -// PiPoHost::signalWarning(PiPo *pipo, std::string warningMsg) { -// // todo -// } - -//--------------------- INPUT STREAM ATTRIBUTES SETTERS ----------------------// -/* -void -PiPoHost::setInputHasTimeTags(bool hasTimeTags, bool propagate) { - this->inputStreamAttrs.hasTimeTags = hasTimeTags; + return res; +} - if (propagate) { - this->propagateInputAttributes(); - } +double +PiPoHost::getDoubleAttr(const std::string &attrName) +{ + PiPo::Attr *attr = this->graph->getAttr(attrName.c_str()); + + if (attr != NULL) { + // int iAttr = attr->getIndex(); + PiPo::Type type = attr->getType(); + + if (type == PiPo::Type::Double) { + return attr->getDbl(0); + } + } + + return 0; } -#define MIN_PIPO_SAMPLERATE (1.0 / 31536000000.0) // once a year -#define MAX_PIPO_SAMPLERATE (96000000000.0) +std::vector<double> +PiPoHost::getDoubleArrayAttr(const std::string &attrName) +{ + std::vector<double> res; + PiPo::Attr *attr = this->graph->getAttr(attrName.c_str()); + + if (attr != NULL) { + // int iAttr = attr->getIndex(); + PiPo::Type type = attr->getType(); + + if (type == PiPo::Type::Double) { + for (int i = 0; i < attr->getSize(); ++i) { + res.push_back(attr->getDbl(i)); + } + } + } + + return res; +} -void -PiPoHost::setInputFrameRate(double rate, bool propagate) { - if (rate <= MIN_PIPO_SAMPLERATE) { - this->inputStreamAttrs.rate = MIN_PIPO_SAMPLERATE; - } else if (rate >= MAX_PIPO_SAMPLERATE) { - this->inputStreamAttrs.rate = MAX_PIPO_SAMPLERATE; - } else { - this->inputStreamAttrs.rate = rate; - } +std::string +PiPoHost::getEnumAttr(const std::string &attrName) +{ + PiPo::Attr *attr = this->graph->getAttr(attrName.c_str()); + + if (attr != NULL) { + // int iAttr = attr->getIndex(); + PiPo::Type type = attr->getType(); + + if (type == PiPo::Type::Enum) { + return attr->getStr(0); + } + } - if (propagate) { - this->propagateInputAttributes(); - } + return ""; } -void -PiPoHost::setInputFrameOffset(double offset, bool propagate) { - this->inputStreamAttrs.offset = offset; +//============================= JSON FORMATTING ==============================// - if (propagate) { - this->propagateInputAttributes(); - } +std::string +PiPoHost::getJSON() +{ + Json::Value result = toJSON(); + return result.toStyledString(); } void -PiPoHost::setInputDims(int width, int height, bool propagate) { - this->inputStreamAttrs.dims[0] = width; - this->inputStreamAttrs.dims[1] = height; - - if (propagate) { - this->propagateInputAttributes(); - } +PiPoHost::writeJSON(const std::string &filepath) +{ + Json::Value root = toJSON(); + std::ofstream jsonOut; + jsonOut.open (filepath); + Json::StyledStreamWriter writer; + writer.write(jsonOut, root); + jsonOut.close(); } -void -PiPoHost::setInputLabels(const std::vector<std::string> &labels, bool propagate) { - const char *labs[labels.size()]; - - for (unsigned int i = 0; i < labels.size(); ++i) { - labs[i] = labels[i].c_str(); - } - - this->inputStreamAttrs.labels = &labs[0]; - - if (propagate) { - this->propagateInputAttributes(); - } +bool +PiPoHost::putJSON(const std::string &jsonMessage) +{ + Json::Value parsedFromString; + Json::Reader reader; + bool parsingSuccessful = reader.parse(jsonMessage, parsedFromString); + return (parsingSuccessful && fromJSON(parsedFromString)); } -void -PiPoHost::setInputHasVarSize(bool hasVarSize, bool propagate) { - this->inputStreamAttrs.hasVarSize = hasVarSize; - - if (propagate) { - this->propagateInputAttributes(); - } +bool +PiPoHost::readJSON(const std::string &filepath) +{ + Json::Value root; + std::ifstream file(filepath); + file >> root; + return fromJSON(root); } -void -PiPoHost::setInputDomain(double domain, bool propagate) { - this->inputStreamAttrs.domain = domain; +//======================== PROTECTED HOST METHODS ============================// - if (propagate) { - this->propagateInputAttributes(); - } -} - -void -PiPoHost::setInputMaxFrames(int maxFrames, bool propagate) { - this->inputStreamAttrs.maxFrames = maxFrames; +Json::Value +PiPoHost::toJSON() +{ + Json::Value root; + Json::Value metadata; + Json::Value pipodata; - if (propagate) { - this->propagateInputAttributes(); - } -} + metadata["creator"] = "RAPID-MIX API";// C++"; + metadata["version"] = "v0.1.1"; //TODO: This should be a macro someplace + metadata["family"] = "pipo"; + + root["metadata"] = metadata; -//--------------------- INPUT STREAM ATTRIBUTES GETTERS ----------------------// + pipodata["description"] = this->graphName; + + Json::Value inputStream; + inputStream["hasTimeTags"] = inputStreamAttrs.hasTimeTags; + inputStream["rate"] = inputStreamAttrs.rate; + inputStream["offset"] = inputStreamAttrs.offset; + inputStream["width"] = inputStreamAttrs.dims[0]; + inputStream["height"] = inputStreamAttrs.dims[1]; + inputStream["labels"] = inputStreamAttrs.labels; + inputStream["hasVarSize"] = inputStreamAttrs.hasVarSize; + inputStream["domain"] = inputStreamAttrs.domain; + inputStream["maxFrames"] = inputStreamAttrs.maxFrames; + + Json::Value outputStream; + outputStream["hasTimeTags"] = outputStreamAttrs.hasTimeTags; + outputStream["rate"] = outputStreamAttrs.rate; + outputStream["offset"] = outputStreamAttrs.offset; + outputStream["width"] = outputStreamAttrs.dims[0]; + outputStream["height"] = outputStreamAttrs.dims[1]; + outputStream["labels"] = outputStreamAttrs.labels; + outputStream["hasVarSize"] = outputStreamAttrs.hasVarSize; + outputStream["domain"] = outputStreamAttrs.domain; + outputStream["maxFrames"] = outputStreamAttrs.maxFrames; + + Json::Value streams; + streams["input"] = inputStream; + streams["output"] = outputStream; + + pipodata["streamAttributes"] = streams; + + Json::Value params; + int n = this->graph->getNumAttrs(); + params.resize(static_cast<Json::ArrayIndex>(n)); -bool -PiPoHost::getInputHasTimeTags() { - return this->inputStreamAttrs.hasTimeTags; -} + for (unsigned int i = 0; i < n; ++i) + { + Json::Value param; + PiPo::Attr *a = this->graph->getAttr(i); + param["name"] = a->getName(); + param["value"] = a->getDbl(0); + params[i] = param; + } + + pipodata["parameters"] = params; -double -PiPoHost::getInputFrameRate() { - return this->inputStreamAttrs.rate; -} + root["pipodata"] = pipodata; -double -PiPoHost::getInputFrameOffset() { - return this->inputStreamAttrs.offset; + return root; } -void -PiPoHost::getInputDims(int &width, int &height) { - width = this->inputStreamAttrs.dims[0]; - height = this->inputStreamAttrs.dims[1]; -} +bool +PiPoHost::fromJSON(Json::Value &jv) +{ + if (jv["metadata"]["family"].asString().compare("pipo") == 0 && + jv["pipodata"].size() > 0) { -void -PiPoHost::getInputLabels(std::vector<std::string> &labels) { - //for (unsigned int i = 0; i < ) -} + this->setGraph(jv["pipodata"]["description"].asString()); -bool -PiPoHost::getInputHasVarSize() { - return this->inputStreamAttrs.hasVarSize; + Json::Value inputStream = jv["pipodata"]["streamAttributes"]["input"]; + // setInputStreamAttributes( + // inputStream["hasTimeTags"].getDbl()//, + // //... + // ); + return true; + } + + return false; } -double -PiPoHost::getInputDomain() { - return this->inputStreamAttrs.domain; -} +//========================= PRIVATE HOST METHODS =============================// int -PiPoHost::getInputMaxFrames() { - return this->inputStreamAttrs.maxFrames; +PiPoHost::propagateInputStreamAttributes() +{ + if (this->graph != nullptr) + { + return this->graph->streamAttributes(this->inputStreamAttrs.hasTimeTags, + this->inputStreamAttrs.rate, + this->inputStreamAttrs.offset, + this->inputStreamAttrs.dims[0], + this->inputStreamAttrs.dims[1], + this->inputStreamAttrs.labels, + this->inputStreamAttrs.hasVarSize, + this->inputStreamAttrs.domain, + this->inputStreamAttrs.maxFrames); + } + + return 0; } -//*/ - -//--------------------- OUTPUT STREAM ATTRIBUTES GETTERS ---------------------// void PiPoHost::setOutputAttributes(bool hasTimeTags, double rate, double offset, @@ -473,184 +520,28 @@ PiPoHost::setOutputAttributes(bool hasTimeTags, double rate, double offset, this->outputStreamAttrs.maxFrames = maxFrames; } -/* -bool -PiPoHost::getOutputHasTimeTags() { - return this->outputStreamAttrs.hasTimeTags; -} - -double -PiPoHost::getOutputFrameRate() { - return this->outputStreamAttrs.rate; -} - -double -PiPoHost::getOutputFrameOffset() { - return this->outputStreamAttrs.offset; -} - -void -PiPoHost::getOutputDims(int &width, int &height) { - width = this->outputStreamAttrs.dims[0]; - height = this->outputStreamAttrs.dims[1]; -} - -void -PiPoHost::getOutputLabels(std::vector<std::string> &labels) { - labels.clear(); - - for (unsigned int i = 0; this->outputStreamAttrs.numLabels; ++i) { - - if (this->outputStreamAttrs.labels[i] != NULL) { - labels.push_back(std::string(this->outputStreamAttrs.labels[i])); - } else { - labels.push_back("unnamed"); - } - } -} - -bool -PiPoHost::getOutputHasVarSize() { - return this->outputStreamAttrs.hasVarSize; -} - -double -PiPoHost::getOutputDomain() { - return this->outputStreamAttrs.domain; -} - -int -PiPoHost::getOutputMaxFrames() { - return this->outputStreamAttrs.maxFrames; -} - - -//===================== PIPO HOST CLASS IMPLEMENTATION =======================// - -rapidPiPoHost::rapidPiPoHost(rapidPiPoOwner *rpo) : -owner(rpo), -inputStreamAttrs(PIPO_MAX_LABELS), -outputStreamAttrs(PIPO_MAX_LABELS) { - PiPoCollection::init(); - outputter = new rapidPiPoOutputter(this); - chain = nullptr; -} - -rapidPiPoHost::~rapidPiPoHost() { - if (chain != nullptr) { - delete chain; - } - delete outputter; -} - -rapidPiPoOwner * -rapidPiPoHost::getOwner() { - return owner; -} - -void -rapidPiPoHost::onNewFrame(double time, std::vector<PiPoValue> &frame) { - std::cout << time << std::endl; - owner->onNewFrame(frame); -} // void -// rapidPiPoHost::onNewFrame(const std::function<void(std::vector<PiPoValue>, -// rapidPiPoOwner *rpo)> cb) { -// outputter->setFrameCallback(cb); -// } - -// void -// rapidPiPoHost::onNewFrame(std::function<void(std::vector<PiPoValue>)> cb) { -// outputter->setSimpleFrameCallback(cb); -// } - -std::vector<PiPoValue> -rapidPiPoHost::getLastFrame() { - return outputter->getLastFrame(); -} - -rapidPiPo * -rapidPiPoHost::setPiPoChain(std::string name) { - if (chain != nullptr) { - delete chain; - } - - chain = new rapidPiPo(name); - chain->connect((PiPo *)outputter); - return chain; -} - -void -rapidPiPoHost::clearPiPoChain() { - delete chain; - chain = nullptr; -} - -void -rapidPiPoHost::propagateInputAttributes() { - if (chain != nullptr) { - const char *colNameStr[PIPO_MAX_LABELS]; - - unsigned int numCols = this->inputStreamAttrs.dims[0]; - unsigned int numLabels = this->inputStreamAttrs.numLabels; - - if (numLabels > PIPO_MAX_LABELS) { - numLabels = PIPO_MAX_LABELS; - } - - if (numLabels > numCols) { - numLabels = numCols; - } - - std::vector<std::string> labels(numLabels); - - if (numLabels > 0) { - for (unsigned int i = 0; i < numLabels; i++) { - colNameStr[i] = this->inputStreamAttrs.labels[i]; - } - - for (unsigned int i = numLabels; i < numCols; i++) { - colNameStr[i] = "unnamed"; - } - - for (unsigned int i = 0; i < numCols; ++i) { - labels[i] = std::string(colNameStr[i]); - } - } - - chain->streamAttributes(this->inputStreamAttrs.hasTimeTags, - this->inputStreamAttrs.rate, - this->inputStreamAttrs.offset, - this->inputStreamAttrs.dims[0], - this->inputStreamAttrs.dims[1], - labels, - this->inputStreamAttrs.hasVarSize, - this->inputStreamAttrs.domain, - this->inputStreamAttrs.maxFrames); - } -} - -// void -// rapidPiPoHost::streamAttributesChanged(PiPo *pipo, PiPo::Attr *attr) { +// PiPoHost::streamAttributesChanged(PiPo *pipo, PiPo::Attr *attr) { // this->propagateInputAttributes(); // } // void -// rapidPiPoHost::signalError(PiPo *pipo, std::string errorMsg) { +// PiPoHost::signalError(PiPo *pipo, std::string errorMsg) { // // todo // } // void -// rapidPiPoHost::signalWarning(PiPo *pipo, std::string warningMsg) { +// PiPoHost::signalWarning(PiPo *pipo, std::string warningMsg) { // // todo // } //--------------------- INPUT STREAM ATTRIBUTES SETTERS ----------------------// +/* void -rapidPiPoHost::setInputHasTimeTags(bool hasTimeTags, bool propagate) { +PiPoHost::setInputHasTimeTags(bool hasTimeTags, bool propagate) { this->inputStreamAttrs.hasTimeTags = hasTimeTags; if (propagate) { @@ -662,7 +553,7 @@ rapidPiPoHost::setInputHasTimeTags(bool hasTimeTags, bool propagate) { #define MAX_PIPO_SAMPLERATE (96000000000.0) void -rapidPiPoHost::setInputFrameRate(double rate, bool propagate) { +PiPoHost::setInputFrameRate(double rate, bool propagate) { if (rate <= MIN_PIPO_SAMPLERATE) { this->inputStreamAttrs.rate = MIN_PIPO_SAMPLERATE; } else if (rate >= MAX_PIPO_SAMPLERATE) { @@ -677,7 +568,7 @@ rapidPiPoHost::setInputFrameRate(double rate, bool propagate) { } void -rapidPiPoHost::setInputFrameOffset(double offset, bool propagate) { +PiPoHost::setInputFrameOffset(double offset, bool propagate) { this->inputStreamAttrs.offset = offset; if (propagate) { @@ -686,7 +577,7 @@ rapidPiPoHost::setInputFrameOffset(double offset, bool propagate) { } void -rapidPiPoHost::setInputDims(int width, int height, bool propagate) { +PiPoHost::setInputDims(int width, int height, bool propagate) { this->inputStreamAttrs.dims[0] = width; this->inputStreamAttrs.dims[1] = height; @@ -696,7 +587,7 @@ rapidPiPoHost::setInputDims(int width, int height, bool propagate) { } void -rapidPiPoHost::setInputLabels(const std::vector<std::string> &labels, bool propagate) { +PiPoHost::setInputLabels(const std::vector<std::string> &labels, bool propagate) { const char *labs[labels.size()]; for (unsigned int i = 0; i < labels.size(); ++i) { @@ -711,7 +602,7 @@ rapidPiPoHost::setInputLabels(const std::vector<std::string> &labels, bool propa } void -rapidPiPoHost::setInputHasVarSize(bool hasVarSize, bool propagate) { +PiPoHost::setInputHasVarSize(bool hasVarSize, bool propagate) { this->inputStreamAttrs.hasVarSize = hasVarSize; if (propagate) { @@ -720,7 +611,7 @@ rapidPiPoHost::setInputHasVarSize(bool hasVarSize, bool propagate) { } void -rapidPiPoHost::setInputDomain(double domain, bool propagate) { +PiPoHost::setInputDomain(double domain, bool propagate) { this->inputStreamAttrs.domain = domain; if (propagate) { @@ -729,117 +620,121 @@ rapidPiPoHost::setInputDomain(double domain, bool propagate) { } void -rapidPiPoHost::setInputMaxFrames(int maxFrames, bool propagate) { +PiPoHost::setInputMaxFrames(int maxFrames, bool propagate) { this->inputStreamAttrs.maxFrames = maxFrames; if (propagate) { this->propagateInputAttributes(); } } +//*/ //--------------------- INPUT STREAM ATTRIBUTES GETTERS ----------------------// +/* bool -rapidPiPoHost::getInputHasTimeTags() { +PiPoHost::getInputHasTimeTags() { return this->inputStreamAttrs.hasTimeTags; } double -rapidPiPoHost::getInputFrameRate() { +PiPoHost::getInputFrameRate() { return this->inputStreamAttrs.rate; } double -rapidPiPoHost::getInputFrameOffset() { +PiPoHost::getInputFrameOffset() { return this->inputStreamAttrs.offset; } void -rapidPiPoHost::getInputDims(int &width, int &height) { +PiPoHost::getInputDims(int &width, int &height) { width = this->inputStreamAttrs.dims[0]; height = this->inputStreamAttrs.dims[1]; } void -rapidPiPoHost::getInputLabels(std::vector<std::string> &labels) { +PiPoHost::getInputLabels(std::vector<std::string> &labels) { //for (unsigned int i = 0; i < ) } bool -rapidPiPoHost::getInputHasVarSize() { +PiPoHost::getInputHasVarSize() { return this->inputStreamAttrs.hasVarSize; } double -rapidPiPoHost::getInputDomain() { +PiPoHost::getInputDomain() { return this->inputStreamAttrs.domain; } int -rapidPiPoHost::getInputMaxFrames() { +PiPoHost::getInputMaxFrames() { return this->inputStreamAttrs.maxFrames; } +//*/ //--------------------- OUTPUT STREAM ATTRIBUTES GETTERS ---------------------// -void -rapidPiPoHost::setOutputAttributes(bool hasTimeTags, double rate, double offset, - unsigned int width, unsigned int height, - const char **labels, bool hasVarSize, - double domain, unsigned int maxFrames) { - if (labels != NULL) { - int numLabels = width; +// void +// PiPoHost::setOutputAttributes(bool hasTimeTags, double rate, double offset, +// unsigned int width, unsigned int height, +// const char **labels, bool hasVarSize, +// double domain, unsigned int maxFrames) { +// if (labels != NULL) { +// int numLabels = width; - if (numLabels > PIPO_MAX_LABELS) { - numLabels = PIPO_MAX_LABELS; - } +// if (numLabels > PIPO_MAX_LABELS) { +// numLabels = PIPO_MAX_LABELS; +// } - for (unsigned int i = 0; i < numLabels; i++) { - try { - this->outputStreamAttrs.labels[i] = labels[i]; - } catch(std::exception e) { - this->outputStreamAttrs.labels[i] = "unnamed"; - } - } +// for (unsigned int i = 0; i < numLabels; i++) { +// try { +// this->outputStreamAttrs.labels[i] = labels[i]; +// } catch(std::exception e) { +// this->outputStreamAttrs.labels[i] = "unnamed"; +// } +// } - this->outputStreamAttrs.numLabels = numLabels; - } else { - this->outputStreamAttrs.numLabels = 0; - } +// this->outputStreamAttrs.numLabels = numLabels; +// } else { +// this->outputStreamAttrs.numLabels = 0; +// } - this->outputStreamAttrs.hasTimeTags = hasTimeTags; - this->outputStreamAttrs.rate = rate; - this->outputStreamAttrs.offset = offset; - this->outputStreamAttrs.dims[0] = width; - this->outputStreamAttrs.dims[1] = height; - this->outputStreamAttrs.hasVarSize = hasVarSize; - this->outputStreamAttrs.domain = domain; - this->outputStreamAttrs.maxFrames = maxFrames; -} +// this->outputStreamAttrs.hasTimeTags = hasTimeTags; +// this->outputStreamAttrs.rate = rate; +// this->outputStreamAttrs.offset = offset; +// this->outputStreamAttrs.dims[0] = width; +// this->outputStreamAttrs.dims[1] = height; +// this->outputStreamAttrs.hasVarSize = hasVarSize; +// this->outputStreamAttrs.domain = domain; +// this->outputStreamAttrs.maxFrames = maxFrames; +// } +/* bool -rapidPiPoHost::getOutputHasTimeTags() { +PiPoHost::getOutputHasTimeTags() { return this->outputStreamAttrs.hasTimeTags; } double -rapidPiPoHost::getOutputFrameRate() { +PiPoHost::getOutputFrameRate() { return this->outputStreamAttrs.rate; } double -rapidPiPoHost::getOutputFrameOffset() { +PiPoHost::getOutputFrameOffset() { return this->outputStreamAttrs.offset; } void -rapidPiPoHost::getOutputDims(int &width, int &height) { +PiPoHost::getOutputDims(int &width, int &height) { width = this->outputStreamAttrs.dims[0]; height = this->outputStreamAttrs.dims[1]; } void -rapidPiPoHost::getOutputLabels(std::vector<std::string> &labels) { +PiPoHost::getOutputLabels(std::vector<std::string> &labels) { labels.clear(); for (unsigned int i = 0; this->outputStreamAttrs.numLabels; ++i) { @@ -853,33 +748,17 @@ rapidPiPoHost::getOutputLabels(std::vector<std::string> &labels) { } bool -rapidPiPoHost::getOutputHasVarSize() { +PiPoHost::getOutputHasVarSize() { return this->outputStreamAttrs.hasVarSize; } double -rapidPiPoHost::getOutputDomain() { +PiPoHost::getOutputDomain() { return this->outputStreamAttrs.domain; } int -rapidPiPoHost::getOutputMaxFrames() { +PiPoHost::getOutputMaxFrames() { return this->outputStreamAttrs.maxFrames; } - //*/ - -// void -// rapidPiPoHost::setRapidPiPoParam(rapidPiPoParam *param) { -// char instanceName[maxWordLen]; -// char pipoAttrName[maxWordLen]; - -// if (getPiPoInstanceAndAttrName(param->getName(), instanceName, pipoAttrName)) { -// // todo (or not todo) -// } -// } - -// void -// rapidPiPoHost::getPiPoParams(std::vector<rapidPiPoParam> ¶ms) { - -// } diff --git a/src/signalProcessing/rapidPiPoTools/rapidPiPoHost.h b/src/signalProcessing/rapidPiPoTools/rapidPiPoHost.h index dce7340..b898da3 100644 --- a/src/signalProcessing/rapidPiPoTools/rapidPiPoHost.h +++ b/src/signalProcessing/rapidPiPoTools/rapidPiPoHost.h @@ -5,13 +5,18 @@ #include "PiPoHost.h" #include "PiPoCollection.h" +//#ifdef EXTERNAL_JSONCPP_PATH +#define EXTERNAL_JSONCPP_PATH "json.h" +#include EXTERNAL_JSONCPP_PATH +//#endif /* EXTERNAL_JSONCPP_PATH */ + #define MIN_PIPO_SAMPLERATE (1.0 / 31536000000.0) /* once a year */ #define MAX_PIPO_SAMPLERATE (96000000000.0) #define PIPO_OUT_RING_SIZE 2 struct pipoStreamAttributes { - pipoStreamAttributes() : + pipoStreamAttributes() : // default parameters suited for audio hasTimeTags(false), rate(MIN_PIPO_SAMPLERATE), offset(0), @@ -20,12 +25,12 @@ struct pipoStreamAttributes { labels({ "" }), hasVarSize(false), domain(0), - maxFrames(256) {} + maxFrames(1) {} bool hasTimeTags; double rate; double offset; - unsigned int width; // width, height (by pipo convention) + unsigned int width; unsigned int height; std::vector<std::string> labels; bool hasVarSize; @@ -84,11 +89,21 @@ public: // virtual bool isFloatAttr(const std::string &attrName); // virtual bool isFloatArrayAttr(const std::string &attrName); // virtual bool isStringAttr(const std::string &attrName); + + virtual std::vector<std::string> getAttrNames(); - // virtual bool getBoolAttr(const std::string &attrName); - // virtual int getIntAttr(const std::string &attrName); - // virtual float getFloatAttr(const std::string &attrName); + virtual double getDoubleAttr(const std::string &attrName); + virtual std::vector<double> getDoubleArrayAttr(const std::string &attrName); + virtual std::string getEnumAttr(const std::string &attrName); + /** Get a JSON representation of the model in the form of a styled string */ + virtual std::string getJSON(); + /** Write a JSON model description to specified file path */ + virtual void writeJSON(const std::string &filepath); + /** configure empty model with string. See getJSON() */ + virtual bool putJSON(const std::string &jsonMessage); + /** read a JSON file at file path and build a modelSet from it */ + virtual bool readJSON(const std::string &filepath); // int streamAttributes(bool hasTimeTags, double rate, double offset, @@ -134,6 +149,10 @@ public: // void setPiPoParam(PiPoParam *param); //*/ +protected: + Json::Value toJSON(); + bool fromJSON(Json::Value &jv); + private: int propagateInputStreamAttributes(); void setOutputAttributes(bool hasTimeTags, double rate, double offset, @@ -150,8 +169,8 @@ private: PiPoHost *host; std::atomic<int> writeIndex, readIndex; std::vector<std::vector<PiPoValue>> ringBuffer; - //std::function<void(std::vector<PiPoValue>, PiPoObserver *rpo)> frameCallback; - std::function<void(std::vector<PiPoValue>)> simpleFrameCallback; + // std::function<void(std::vector<PiPoValue>, PiPoObserver *rpo)> frameCallback; + // std::function<void(std::vector<PiPoValue>)> simpleFrameCallback; public: PiPoOut(PiPoHost *host) : @@ -212,9 +231,9 @@ public: // frameCallback = f; //} - void setSimpleFrameCallback(std::function<void(std::vector<PiPoValue>)> f) { - simpleFrameCallback = f; - } + // void setSimpleFrameCallback(std::function<void(std::vector<PiPoValue>)> f) { + // simpleFrameCallback = f; + // } std::vector<PiPoValue> getLastFrame() { std::vector<PiPoValue> f; @@ -227,85 +246,4 @@ public: } }; -//================================ PARAMETER =================================// - -// can we avoid using such a class ? -/* -class rapidPiPoParam { - std::string name; - std::string pipoName; - std::string paramName; - std::vector<std::string> values; - -public: - rapidPiPoParam() : - name(""), pipoName(""), paramName("") {} - - rapidPiPoParam(std::string name, std::string pipoName, std::string paramName, - std::vector<std::string> const &values) { - this->name = name; - this->pipoName = pipoName; - this->paramName = paramName; - this->values = std::vector<std::string>(values.size()); - for (int i = 0; i < values.size(); ++i) { - this->values[i] = values[i]; - } - } - - const char *getName() { - return name.c_str(); - } - - const char *getParamName() { - return paramName.c_str(); - } - - const char *getPiPoName() { - return pipoName.c_str(); - } - - int getNumValues() { - return values.size(); - } - - bool isValueInt(int i) { - // todo - return false; - } - - bool isValueFloat(int i) { - // todo - return false; - } - - bool isValueNum(int i) { - // todo; - return false; - } - - float getValueFloat(int i) { - // todo - return 0.; - } - - int getValueInt(int i) { - // todo - return 0; - } - - const char *getValueString() { - return values[0].c_str(); - //return values[i].c_str(); - } - - std::string getValuesAsString() { - std::string res = values[0]; - for (int i = 1; i < values.size(); ++i) { - res += " " + values[i]; - } - return res; - } -}; -//*/ - #endif /* _RAPID_PIPO_HOST_H_ */ diff --git a/src/signalProcessing/rapidPiPoTools/rapidPiPoTools.h b/src/signalProcessing/rapidPiPoTools/rapidPiPoTools.h index 04265de..d9a2e0e 100644 --- a/src/signalProcessing/rapidPiPoTools/rapidPiPoTools.h +++ b/src/signalProcessing/rapidPiPoTools/rapidPiPoTools.h @@ -3,7 +3,7 @@ #include "rapidPiPoHost.h" -typedef PiPoHost rapidPiPoHost; -typedef pipoStreamAttributes rapidPiPoStreamAttributes; +typedef PiPoHost pipoHost; +typedef pipoStreamAttributes pipoStreamAttributes; # endif /* _RAPID_PIPO_TOOLS_H_ */ -- GitLab