Skip to content

Commit

Permalink
LV2 parameter: QHash for UnitsHint lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Nov 12, 2022
1 parent e71113c commit 5280012
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 54 deletions.
85 changes: 33 additions & 52 deletions src/effects/backends/effectmanifestparameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,60 +67,41 @@ class EffectManifestParameter {
Time, // units?
};

static UnitsHint lv2UnitToUnitsHint(const QString& lv2Unit) {
// Add custom LV2 units here (with correct case) and also
// in unitsHintToString()
if (lv2Unit == QLatin1String("bar")) {
return UnitsHint::Bar;
} else if (lv2Unit == QLatin1String("beat")) {
return UnitsHint::Beat;
} else if (lv2Unit == QLatin1String("bpm")) {
return UnitsHint::BPM;
} else if (lv2Unit == QLatin1String("cent")) {
return UnitsHint::Cent;
} else if (lv2Unit == QLatin1String("cm")) {
return UnitsHint::Centimetre;
} else if (lv2Unit == QLatin1String("coef")) {
return UnitsHint::Coefficient;
} else if (lv2Unit == QLatin1String("db")) {
return UnitsHint::Decibel;
} else if (lv2Unit == QLatin1String("degree")) {
return UnitsHint::Degree;
} else if (lv2Unit == QLatin1String("frame")) {
return UnitsHint::Frame;
} else if (lv2Unit == QLatin1String("hz")) {
return UnitsHint::Hertz;
} else if (lv2Unit == QLatin1String("inch")) {
return UnitsHint::Inch;
} else if (lv2Unit == QLatin1String("khz")) {
return UnitsHint::KiloHertz;
} else if (lv2Unit == QLatin1String("km")) {
return UnitsHint::Kilometer;
} else if (lv2Unit == QLatin1String("m")) {
return UnitsHint::Meter;
} else if (lv2Unit == QLatin1String("mhz")) {
return UnitsHint::MegaHertz;
} else if (lv2Unit == QLatin1String("midiNote")) {
return UnitsHint::Midinote;
} else if (lv2Unit == QLatin1String("mile")) {
return UnitsHint::Mile;
} else if (lv2Unit == QLatin1String("min")) {
return UnitsHint::Minute;
} else if (lv2Unit == QLatin1String("mm")) {
return UnitsHint::Millmeter;
} else if (lv2Unit == QLatin1String("ms")) {
return UnitsHint::Millisecond;
} else if (lv2Unit == QLatin1String("oct")) {
return UnitsHint::Octave;
} else if (lv2Unit == QLatin1String("pc")) {
return UnitsHint::Percentage;
} else if (lv2Unit == QLatin1String("s")) {
return UnitsHint::Seconds;
} else if (lv2Unit == QLatin1String("semitone12TET")) {
return UnitsHint::Semitone12tet;
} else {
const QHash<QString, UnitsHint> lv2UnitToUnitsHintHash{
// Add custom LV2 units here (with correct case) and also
// in unitsHintStringHash()
{QString("bar"), UnitsHint::Bar},
{QString("beat"), UnitsHint::Beat},
{QString("bpm"), UnitsHint::BPM},
{QString("cent"), UnitsHint::Cent},
{QString("cm"), UnitsHint::Centimetre},
{QString("coef"), UnitsHint::Coefficient},
{QString("db"), UnitsHint::Decibel},
{QString("degree"), UnitsHint::Degree},
{QString("frame"), UnitsHint::Frame},
{QString("hz"), UnitsHint::Hertz},
{QString("inch"), UnitsHint::Inch},
{QString("khz"), UnitsHint::KiloHertz},
{QString("km"), UnitsHint::Kilometer},
{QString("m"), UnitsHint::Meter},
{QString("mhz"), UnitsHint::MegaHertz},
{QString("midiNote"), UnitsHint::Midinote},
{QString("mile"), UnitsHint::Mile},
{QString("min"), UnitsHint::Minute},
{QString("mm"), UnitsHint::Millmeter},
{QString("ms"), UnitsHint::Millisecond},
{QString("oct"), UnitsHint::Octave},
{QString("pc"), UnitsHint::Percentage},
{QString("s"), UnitsHint::Seconds},
{QString("semitone12TET"), UnitsHint::Semitone12tet}};

UnitsHint lv2UnitToUnitsHint(const QString& lv2) {
QHash<QString, UnitsHint>::const_iterator uHintIt =
lv2UnitToUnitsHintHash.find(lv2);
if (uHintIt == lv2UnitToUnitsHintHash.constEnd()) {
return UnitsHint::Unknown;
}
return uHintIt.value();
}

const QHash<UnitsHint, QString> unitsHintStringHash{
Expand Down
4 changes: 2 additions & 2 deletions src/effects/backends/lv2/lv2manifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ LV2Manifest::LV2Manifest(LilvWorld* world,
QString unitStr = lilv_node_as_uri(unit);
// that starts with the 'units' prefix, isolate the identifier
if (unitStr.startsWith(lilv_node_as_string(properties["unit_prefix"]))) {
unitsHint = EffectManifestParameter::lv2UnitToUnitsHint(
unitsHint = param->lv2UnitToUnitsHint(
unitStr.remove(lilv_node_as_string(properties["unit_prefix"])));
}
} else { // Try to extract the custom unit symbol string
Expand All @@ -93,7 +93,7 @@ LV2Manifest::LV2Manifest(LilvWorld* world,
// Accepted custom units needs to be 'whitelisted' in
// EffectManifestParameter::lv2UnitToUnitsHint and added to
// EffectManifestParameter::unitsHintStringHash
unitsHint = EffectManifestParameter::lv2UnitToUnitsHint(
unitsHint = param->lv2UnitToUnitsHint(
lilv_node_as_string(customUnit));
if (lv2ParamDebug &&
unitsHint == EffectManifestParameter::UnitsHint::Unknown &&
Expand Down

0 comments on commit 5280012

Please sign in to comment.