annotate capnproto/VampnProto.h @ 6:d8358afe3f2c

Update build
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 13 May 2016 16:28:26 +0100
parents 6e8607ebad03
children c8451896c40e
rev   line source
c@5 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@5 2
c@5 3 #include "vamp.capnp.h"
c@5 4
c@5 5 #include <capnp/message.h>
c@5 6 #include <capnp/serialize-packed.h>
c@5 7
c@5 8 #include <vamp-hostsdk/Plugin.h>
c@5 9 #include <vamp-hostsdk/PluginLoader.h>
c@5 10 #include <vamp-hostsdk/PluginStaticData.h>
c@5 11
c@5 12 namespace vampipe
c@5 13 {
c@5 14
c@5 15 /**
c@5 16 * Convert the structures laid out in the Vamp SDK classes into Cap'n
c@6 17 * Proto structures (and back again).
c@5 18 *
c@6 19 * At least some of this will be necessary for any implementation
c@6 20 * using Cap'n Proto that uses the C++ Vamp SDK to provide its
c@6 21 * reference structures. An implementation could alternatively use the
c@6 22 * Cap'n Proto structures directly, and interact with Vamp plugins
c@6 23 * using the Vamp C API, without using the C++ Vamp SDK classes at
c@6 24 * all.
c@5 25 */
c@6 26 class VampnProto
c@5 27 {
c@5 28 public:
c@5 29 typedef ::capnp::MallocMessageBuilder MsgBuilder;
c@5 30
c@5 31 template <typename T, typename B>
c@5 32 static void buildBasicDescriptor(B &basic, const T &t) {
c@5 33 basic.setIdentifier(t.identifier);
c@5 34 basic.setName(t.name);
c@5 35 basic.setDescription(t.description);
c@5 36 }
c@5 37
c@5 38 template <typename T, typename B>
c@5 39 static void readBasicDescriptor(T &t, const B &basic) {
c@5 40 t.identifier = basic.getIdentifier();
c@5 41 t.name = basic.getName();
c@5 42 t.description = basic.getDescription();
c@5 43 }
c@5 44
c@5 45 template <typename T, typename M>
c@5 46 static void buildValueExtents(M &m, const T &t) {
c@5 47 m.setMinValue(t.minValue);
c@5 48 m.setMaxValue(t.maxValue);
c@5 49 }
c@5 50
c@5 51 template <typename T, typename M>
c@5 52 static void readValueExtents(T &t, const M &m) {
c@5 53 t.minValue = m.getMinValue();
c@5 54 t.maxValue = m.getMaxValue();
c@5 55 }
c@5 56
c@5 57 static void buildRealTime(RealTime::Builder &b, const Vamp::RealTime &t) {
c@5 58 b.setSec(t.sec);
c@5 59 b.setNsec(t.nsec);
c@5 60 }
c@5 61
c@5 62 static void readRealTime(Vamp::RealTime &t, const RealTime::Reader &r) {
c@5 63 t.sec = r.getSec();
c@5 64 t.nsec = r.getNsec();
c@5 65 }
c@5 66
c@5 67 static SampleType
c@5 68 fromSampleType(Vamp::Plugin::OutputDescriptor::SampleType t) {
c@5 69 switch (t) {
c@5 70 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep:
c@5 71 return SampleType::ONE_SAMPLE_PER_STEP;
c@5 72 case Vamp::Plugin::OutputDescriptor::FixedSampleRate:
c@5 73 return SampleType::FIXED_SAMPLE_RATE;
c@5 74 case Vamp::Plugin::OutputDescriptor::VariableSampleRate:
c@5 75 return SampleType::VARIABLE_SAMPLE_RATE;
c@5 76 }
c@5 77 throw std::logic_error("unexpected Vamp SampleType enum value");
c@5 78 }
c@5 79
c@5 80 static Vamp::Plugin::OutputDescriptor::SampleType
c@5 81 toSampleType(SampleType t) {
c@5 82 switch (t) {
c@5 83 case SampleType::ONE_SAMPLE_PER_STEP:
c@5 84 return Vamp::Plugin::OutputDescriptor::OneSamplePerStep;
c@5 85 case SampleType::FIXED_SAMPLE_RATE:
c@5 86 return Vamp::Plugin::OutputDescriptor::FixedSampleRate;
c@5 87 case SampleType::VARIABLE_SAMPLE_RATE:
c@5 88 return Vamp::Plugin::OutputDescriptor::VariableSampleRate;
c@5 89 }
c@5 90 throw std::logic_error("unexpected Capnp SampleType enum value");
c@5 91 }
c@5 92
c@5 93 static void
c@5 94 buildOutputDescriptor(OutputDescriptor::Builder &b,
c@5 95 const Vamp::Plugin::OutputDescriptor &od) {
c@5 96
c@5 97 auto basic = b.initBasic();
c@5 98 buildBasicDescriptor(basic, od);
c@5 99
c@5 100 b.setUnit(od.unit);
c@5 101
c@5 102 b.setSampleType(fromSampleType(od.sampleType));
c@5 103 b.setSampleRate(od.sampleRate);
c@5 104 b.setHasDuration(od.hasDuration);
c@5 105
c@5 106 b.setHasFixedBinCount(od.hasFixedBinCount);
c@5 107 if (od.hasFixedBinCount) {
c@5 108 b.setBinCount(od.binCount);
c@5 109 if (od.binNames.size() > 0) {
c@5 110 auto binNames = b.initBinNames(od.binNames.size());
c@5 111 for (size_t i = 0; i < od.binNames.size(); ++i) {
c@5 112 binNames.set(i, od.binNames[i]);
c@5 113 }
c@5 114 }
c@5 115 }
c@5 116
c@5 117 b.setHasKnownExtents(od.hasKnownExtents);
c@5 118 if (od.hasKnownExtents) {
c@5 119 buildValueExtents(b, od);
c@5 120 }
c@5 121
c@5 122 b.setIsQuantized(od.isQuantized);
c@5 123 if (od.isQuantized) {
c@5 124 b.setQuantizeStep(od.quantizeStep);
c@5 125 }
c@5 126 }
c@5 127
c@5 128 static void
c@5 129 readOutputDescriptor(Vamp::Plugin::OutputDescriptor &od,
c@5 130 const OutputDescriptor::Reader &r) {
c@5 131
c@5 132 readBasicDescriptor(od, r.getBasic());
c@5 133
c@5 134 od.unit = r.getUnit();
c@5 135
c@5 136 od.sampleType = toSampleType(r.getSampleType());
c@5 137 od.sampleRate = r.getSampleRate();
c@5 138 od.hasDuration = r.getHasDuration();
c@5 139
c@5 140 od.hasFixedBinCount = r.getHasFixedBinCount();
c@5 141 if (od.hasFixedBinCount) {
c@5 142 od.binCount = r.getBinCount();
c@5 143 for (const auto &n: r.getBinNames()) {
c@5 144 od.binNames.push_back(n);
c@5 145 }
c@5 146 }
c@5 147
c@5 148 od.hasKnownExtents = r.getHasKnownExtents();
c@5 149 if (od.hasKnownExtents) {
c@5 150 readValueExtents(od, r);
c@5 151 }
c@5 152
c@5 153 od.isQuantized = r.getIsQuantized();
c@5 154 if (od.isQuantized) {
c@5 155 od.quantizeStep = r.getQuantizeStep();
c@5 156 }
c@5 157 }
c@5 158
c@5 159 static void
c@5 160 buildParameterDescriptor(ParameterDescriptor::Builder &b,
c@5 161 const Vamp::Plugin::ParameterDescriptor &pd) {
c@5 162
c@5 163 auto basic = b.initBasic();
c@5 164 buildBasicDescriptor(basic, pd);
c@5 165
c@5 166 b.setUnit(pd.unit);
c@5 167
c@5 168 buildValueExtents(b, pd);
c@5 169
c@5 170 b.setDefaultValue(pd.defaultValue);
c@5 171
c@5 172 b.setIsQuantized(pd.isQuantized);
c@5 173 if (pd.isQuantized) {
c@5 174 b.setQuantizeStep(pd.quantizeStep);
c@5 175 }
c@5 176
c@5 177 if (pd.valueNames.size() > 0) {
c@5 178 auto valueNames = b.initValueNames(pd.valueNames.size());
c@5 179 for (size_t i = 0; i < pd.valueNames.size(); ++i) {
c@5 180 valueNames.set(i, pd.valueNames[i]);
c@5 181 }
c@5 182 }
c@5 183 }
c@5 184
c@5 185 static void
c@5 186 readParameterDescriptor(Vamp::Plugin::ParameterDescriptor &pd,
c@5 187 const ParameterDescriptor::Reader &r) {
c@5 188
c@5 189 readBasicDescriptor(pd, r.getBasic());
c@5 190
c@5 191 pd.unit = r.getUnit();
c@5 192
c@5 193 readValueExtents(pd, r);
c@5 194
c@5 195 pd.defaultValue = r.getDefaultValue();
c@5 196
c@5 197 pd.isQuantized = r.getIsQuantized();
c@5 198 if (pd.isQuantized) {
c@5 199 pd.quantizeStep = r.getQuantizeStep();
c@5 200 }
c@5 201
c@5 202 for (const auto &n: r.getValueNames()) {
c@5 203 pd.valueNames.push_back(n);
c@5 204 }
c@5 205 }
c@5 206
c@5 207 static void
c@5 208 buildFeature(Feature::Builder &b,
c@5 209 const Vamp::Plugin::Feature &f) {
c@5 210
c@5 211 b.setHasTimestamp(f.hasTimestamp);
c@5 212 if (f.hasTimestamp) {
c@5 213 auto timestamp = b.initTimestamp();
c@5 214 buildRealTime(timestamp, f.timestamp);
c@5 215 }
c@5 216
c@5 217 b.setHasDuration(f.hasDuration);
c@5 218 if (f.hasDuration) {
c@5 219 auto duration = b.initDuration();
c@5 220 buildRealTime(duration, f.duration);
c@5 221 }
c@5 222
c@5 223 b.setLabel(f.label);
c@5 224
c@5 225 if (f.values.size() > 0) {
c@5 226 auto values = b.initValues(f.values.size());
c@5 227 for (size_t i = 0; i < f.values.size(); ++i) {
c@5 228 values.set(i, f.values[i]);
c@5 229 }
c@5 230 }
c@5 231 }
c@5 232
c@5 233 static void
c@5 234 readFeature(Vamp::Plugin::Feature &f,
c@5 235 const Feature::Reader &r) {
c@5 236
c@5 237 f.hasTimestamp = r.getHasTimestamp();
c@5 238 if (f.hasTimestamp) {
c@5 239 readRealTime(f.timestamp, r.getTimestamp());
c@5 240 }
c@5 241
c@5 242 f.hasDuration = r.getHasDuration();
c@5 243 if (f.hasDuration) {
c@5 244 readRealTime(f.duration, r.getDuration());
c@5 245 }
c@5 246
c@5 247 f.label = r.getLabel();
c@5 248
c@5 249 for (auto v: r.getValues()) {
c@5 250 f.values.push_back(v);
c@5 251 }
c@5 252 }
c@5 253
c@5 254 static void
c@5 255 buildFeatureSet(FeatureSet::Builder &b,
c@5 256 const Vamp::Plugin::FeatureSet &fs) {
c@5 257
c@5 258 auto featureset = b.initFeaturePairs(fs.size());
c@5 259 int ix = 0;
c@5 260 for (const auto &fsi : fs) {
c@5 261 auto fspair = featureset[ix];
c@5 262 fspair.setOutput(fsi.first);
c@5 263 auto featurelist = fspair.initFeatures(fsi.second.size());
c@5 264 for (size_t j = 0; j < fsi.second.size(); ++j) {
c@5 265 auto feature = featurelist[j];
c@5 266 buildFeature(feature, fsi.second[j]);
c@5 267 }
c@5 268 ++ix;
c@5 269 }
c@5 270 }
c@5 271
c@5 272 static void
c@5 273 readFeatureSet(Vamp::Plugin::FeatureSet &fs,
c@5 274 const FeatureSet::Reader &r) {
c@5 275
c@5 276 for (const auto &p: r.getFeaturePairs()) {
c@5 277 Vamp::Plugin::FeatureList vfl;
c@5 278 for (const auto &f: p.getFeatures()) {
c@5 279 Vamp::Plugin::Feature vf;
c@5 280 readFeature(vf, f);
c@5 281 vfl.push_back(vf);
c@5 282 }
c@5 283 fs[p.getOutput()] = vfl;
c@5 284 }
c@5 285 }
c@5 286
c@5 287 static InputDomain
c@5 288 fromInputDomain(Vamp::Plugin::InputDomain d) {
c@5 289 switch(d) {
c@5 290 case Vamp::Plugin::TimeDomain:
c@5 291 return InputDomain::TIME_DOMAIN;
c@5 292 case Vamp::Plugin::FrequencyDomain:
c@5 293 return InputDomain::FREQUENCY_DOMAIN;
c@5 294 default:
c@5 295 throw std::logic_error("unexpected Vamp InputDomain enum value");
c@5 296 }
c@5 297 }
c@5 298
c@5 299 static Vamp::Plugin::InputDomain
c@5 300 toInputDomain(InputDomain d) {
c@5 301 switch(d) {
c@5 302 case InputDomain::TIME_DOMAIN:
c@5 303 return Vamp::Plugin::TimeDomain;
c@5 304 case InputDomain::FREQUENCY_DOMAIN:
c@5 305 return Vamp::Plugin::FrequencyDomain;
c@5 306 default:
c@5 307 throw std::logic_error("unexpected Capnp InputDomain enum value");
c@5 308 }
c@5 309 }
c@5 310
c@5 311 static void
c@5 312 buildPluginStaticData(PluginStaticData::Builder &b,
c@5 313 const Vamp::HostExt::PluginStaticData &d) {
c@5 314
c@5 315 b.setPluginKey(d.pluginKey);
c@5 316
c@5 317 auto basic = b.initBasic();
c@5 318 buildBasicDescriptor(basic, d.basic);
c@5 319
c@5 320 b.setMaker(d.maker);
c@5 321 b.setCopyright(d.copyright);
c@5 322 b.setPluginVersion(d.pluginVersion);
c@5 323
c@5 324 auto clist = b.initCategory(d.category.size());
c@5 325 for (size_t i = 0; i < d.category.size(); ++i) {
c@5 326 clist.set(i, d.category[i]);
c@5 327 }
c@5 328
c@5 329 b.setMinChannelCount(d.minChannelCount);
c@5 330 b.setMaxChannelCount(d.maxChannelCount);
c@5 331
c@5 332 const auto &vparams = d.parameters;
c@5 333 auto plist = b.initParameters(vparams.size());
c@5 334 for (size_t i = 0; i < vparams.size(); ++i) {
c@5 335 auto pd = plist[i];
c@5 336 buildParameterDescriptor(pd, vparams[i]);
c@5 337 }
c@5 338
c@5 339 const auto &vprogs = d.programs;
c@5 340 auto pglist = b.initPrograms(vprogs.size());
c@5 341 for (size_t i = 0; i < vprogs.size(); ++i) {
c@5 342 pglist.set(i, vprogs[i]);
c@5 343 }
c@5 344
c@5 345 b.setInputDomain(fromInputDomain(d.inputDomain));
c@5 346
c@5 347 const auto &vouts = d.basicOutputInfo;
c@5 348 auto olist = b.initBasicOutputInfo(vouts.size());
c@5 349 for (size_t i = 0; i < vouts.size(); ++i) {
c@5 350 auto od = olist[i];
c@5 351 buildBasicDescriptor(od, vouts[i]);
c@5 352 }
c@5 353 }
c@5 354
c@5 355 static void
c@5 356 readPluginStaticData(Vamp::HostExt::PluginStaticData &d,
c@5 357 const PluginStaticData::Reader &r) {
c@5 358
c@5 359 d.pluginKey = r.getPluginKey();
c@5 360
c@5 361 readBasicDescriptor(d.basic, r.getBasic());
c@5 362
c@5 363 d.maker = r.getMaker();
c@5 364 d.copyright = r.getCopyright();
c@5 365 d.pluginVersion = r.getPluginVersion();
c@5 366
c@5 367 for (auto c: r.getCategory()) {
c@5 368 d.category.push_back(c);
c@5 369 }
c@5 370
c@5 371 d.minChannelCount = r.getMinChannelCount();
c@5 372 d.maxChannelCount = r.getMaxChannelCount();
c@5 373
c@5 374 for (auto p: r.getParameters()) {
c@5 375 Vamp::Plugin::ParameterDescriptor pd;
c@5 376 readParameterDescriptor(pd, p);
c@5 377 d.parameters.push_back(pd);
c@5 378 }
c@5 379
c@5 380 for (auto p: r.getPrograms()) {
c@5 381 d.programs.push_back(p);
c@5 382 }
c@5 383
c@5 384 d.inputDomain = toInputDomain(r.getInputDomain());
c@5 385
c@5 386 for (auto o: r.getBasicOutputInfo()) {
c@5 387 Vamp::HostExt::PluginStaticData::Basic b;
c@5 388 readBasicDescriptor(b, o);
c@5 389 d.basicOutputInfo.push_back(b);
c@5 390 }
c@5 391 }
c@5 392
c@5 393 static void
c@5 394 buildPluginConfiguration(PluginConfiguration::Builder &b,
c@5 395 const Vamp::HostExt::PluginConfiguration &c) {
c@5 396
c@5 397 const auto &vparams = c.parameterValues;
c@5 398 auto params = b.initParameterValues(vparams.size());
c@5 399 int i = 0;
c@5 400 for (const auto &pp : vparams) {
c@5 401 auto param = params[i++];
c@5 402 param.setParameter(pp.first);
c@5 403 param.setValue(pp.second);
c@5 404 }
c@5 405
c@5 406 b.setCurrentProgram(c.currentProgram);
c@5 407 b.setChannelCount(c.channelCount);
c@5 408 b.setStepSize(c.stepSize);
c@5 409 b.setBlockSize(c.blockSize);
c@5 410 }
c@5 411
c@5 412 static void
c@5 413 readPluginConfiguration(Vamp::HostExt::PluginConfiguration &c,
c@5 414 const PluginConfiguration::Reader &r) {
c@5 415
c@5 416 for (const auto &pp: r.getParameterValues()) {
c@5 417 c.parameterValues[pp.getParameter()] = pp.getValue();
c@5 418 }
c@5 419
c@5 420 c.currentProgram = r.getCurrentProgram();
c@5 421 c.channelCount = r.getChannelCount();
c@5 422 c.stepSize = r.getStepSize();
c@5 423 c.blockSize = r.getBlockSize();
c@5 424 }
c@5 425
c@5 426 static void
c@5 427 buildLoadRequest(LoadRequest::Builder &r,
c@5 428 const Vamp::HostExt::LoadRequest &req) {
c@5 429
c@5 430 r.setPluginKey(req.pluginKey);
c@5 431 r.setInputSampleRate(req.inputSampleRate);
c@5 432
c@5 433 std::vector<AdapterFlag> flags;
c@5 434 if (req.adapterFlags & Vamp::HostExt::PluginLoader::ADAPT_INPUT_DOMAIN) {
c@5 435 flags.push_back(AdapterFlag::ADAPT_INPUT_DOMAIN);
c@5 436 }
c@5 437 if (req.adapterFlags & Vamp::HostExt::PluginLoader::ADAPT_CHANNEL_COUNT) {
c@5 438 flags.push_back(AdapterFlag::ADAPT_CHANNEL_COUNT);
c@5 439 }
c@5 440 if (req.adapterFlags & Vamp::HostExt::PluginLoader::ADAPT_BUFFER_SIZE) {
c@5 441 flags.push_back(AdapterFlag::ADAPT_BUFFER_SIZE);
c@5 442 }
c@5 443
c@5 444 auto f = r.initAdapterFlags(flags.size());
c@5 445 for (size_t i = 0; i < flags.size(); ++i) {
c@5 446 f.set(i, flags[i]);
c@5 447 }
c@5 448 }
c@5 449
c@5 450 static void
c@5 451 readLoadRequest(Vamp::HostExt::LoadRequest &req,
c@5 452 const LoadRequest::Reader &r) {
c@5 453
c@5 454 req.pluginKey = r.getPluginKey();
c@5 455 req.inputSampleRate = r.getInputSampleRate();
c@5 456
c@5 457 int flags = 0;
c@5 458 for (const auto &a: r.getAdapterFlags()) {
c@5 459 if (a == AdapterFlag::ADAPT_INPUT_DOMAIN) {
c@5 460 flags |= Vamp::HostExt::PluginLoader::ADAPT_INPUT_DOMAIN;
c@5 461 }
c@5 462 if (a == AdapterFlag::ADAPT_CHANNEL_COUNT) {
c@5 463 flags |= Vamp::HostExt::PluginLoader::ADAPT_CHANNEL_COUNT;
c@5 464 }
c@5 465 if (a == AdapterFlag::ADAPT_BUFFER_SIZE) {
c@5 466 flags |= Vamp::HostExt::PluginLoader::ADAPT_BUFFER_SIZE;
c@5 467 }
c@5 468 }
c@5 469 req.adapterFlags = flags;
c@5 470 }
c@5 471 };
c@5 472
c@5 473 }
c@5 474
c@5 475