From 64fb39b13a9d3548f2d8f8b1c68c85688c76a1bf Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 26 Dec 2016 23:33:38 +0100 Subject: [PATCH] Sort LV2 scale points, fixes #141 --- source/utils/CarlaLv2Utils.hpp | 36 +++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/source/utils/CarlaLv2Utils.hpp b/source/utils/CarlaLv2Utils.hpp index 0b78337db..c5d44215f 100644 --- a/source/utils/CarlaLv2Utils.hpp +++ b/source/utils/CarlaLv2Utils.hpp @@ -1,6 +1,6 @@ /* * Carla LV2 utils - * Copyright (C) 2011-2014 Filipe Coelho + * Copyright (C) 2011-2016 Filipe Coelho * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -84,6 +84,10 @@ # include "juce_core/juce_core.h" #endif +// used for scalepoint sorting +#include +typedef std::map LilvScalePointMap; + // ----------------------------------------------------------------------- // Define namespaces and missing prefixes @@ -1124,18 +1128,36 @@ const LV2_RDF_Descriptor* lv2_rdf_new(const LV2_URI uri, const bool loadPresets) rdfPort->ScalePointCount = lilvScalePoints.size(); rdfPort->ScalePoints = new LV2_RDF_PortScalePoint[rdfPort->ScalePointCount]; - uint32_t h = 0; + // get all scalepoints and sort them by value + LilvScalePointMap sortedpoints; + LILV_FOREACH(scale_points, it, lilvScalePoints) { - CARLA_SAFE_ASSERT_BREAK(h < rdfPort->ScalePointCount); - Lilv::ScalePoint lilvScalePoint(lilvScalePoints.get(it)); + + CARLA_SAFE_ASSERT_CONTINUE(lilvScalePoint.get_label() != nullptr); + + if (const LilvNode* const valuenode = lilvScalePoint.get_value()) + { + const double valueid = lilv_node_as_float(valuenode); + sortedpoints[valueid] = lilvScalePoint.me; + } + } + + // now safe to store, sorted by using std::map + uint32_t h = 0; + for (LilvScalePointMap::iterator it=sortedpoints.begin(), end=sortedpoints.end(); it != end; ++it) + { + CARLA_SAFE_ASSERT_BREAK(h < rdfPort->ScalePointCount); LV2_RDF_PortScalePoint* const rdfScalePoint(&rdfPort->ScalePoints[h++]); - if (const char* const label = lilv_node_as_string(lilvScalePoint.get_label())) - rdfScalePoint->Label = carla_strdup(label); + const LilvScalePoint* const scalepoint = it->second; + + const LilvNode* const xlabel = lilv_scale_point_get_label(scalepoint); + const LilvNode* const xvalue = lilv_scale_point_get_value(scalepoint); - rdfScalePoint->Value = Lilv::Node(lilvScalePoint.get_value()).as_float(); + rdfScalePoint->Label = carla_strdup(lilv_node_as_string(xlabel)); + rdfScalePoint->Value = lilv_node_as_float(xvalue); } }