Audacity 3.2.0
DynamicRangeProcessorUtils.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 DynamicRangeProcessorUtils.cpp
7
8 Matthieu Hodgkinson
9
10**********************************************************************/
12#include <optional>
13#include <regex>
14#include <stdexcept>
15#include <unordered_map>
16
18{
19namespace
20{
21
22template <typename Struct> struct SettingDescription
23{
24 double Struct::*const mem {};
25 const char* const key {};
26};
27
28static const std::vector<SettingDescription<CompressorSettings>>
30 { &CompressorSettings::thresholdDb, "thresholdDb" },
31 { &CompressorSettings::makeupGainDb, "makeupGainDb" },
32 { &CompressorSettings::kneeWidthDb, "kneeWidthDb" },
33 { &CompressorSettings::compressionRatio, "compressionRatio" },
34 { &CompressorSettings::lookaheadMs, "lookaheadMs" },
35 { &CompressorSettings::attackMs, "attackMs" },
36 { &CompressorSettings::releaseMs, "releaseMs" },
37 { &CompressorSettings::showInput, "showInput" },
38 { &CompressorSettings::showOutput, "showOutput" },
39 { &CompressorSettings::showActual, "showActual" },
40 { &CompressorSettings::showTarget, "showTarget" },
41 };
42
43const std::vector<SettingDescription<LimiterSettings>>
45 { &LimiterSettings::thresholdDb, "thresholdDb" },
46 { &LimiterSettings::makeupTargetDb, "makeupTargetDb" },
47 { &LimiterSettings::kneeWidthDb, "kneeWidthDb" },
48 { &LimiterSettings::lookaheadMs, "lookaheadMs" },
49 { &LimiterSettings::releaseMs, "releaseMs" },
50 { &LimiterSettings::showInput, "showInput" },
51 { &LimiterSettings::showOutput, "showOutput" },
52 { &LimiterSettings::showActual, "showActual" },
53 { &LimiterSettings::showTarget, "showTarget" },
54 };
55
56template <typename Struct>
57std::optional<Struct> Deserialize(
58 const std::string& str,
59 const std::vector<SettingDescription<Struct>>& settings)
60{
61 Struct settingStruct;
62
63 const std::regex pattern(R"((\w+)=\"(-?\d+\.?\d*)\")");
64
65 // Create an unordered_map to store the key-value pairs
66 std::unordered_map<std::string, std::string> values;
67
68 const auto begin = std::sregex_iterator(str.begin(), str.end(), pattern);
69 const auto end = std::sregex_iterator();
70
71 for (auto it = begin; it != end; ++it)
72 {
73 const std::smatch match = *it;
74 values[match[1].str()] = match[2].str();
75 }
76
77 if (std::any_of(
78 settings.begin(), settings.end(), [&values](const auto& setting) {
79 return values.find(setting.key) == values.end();
80 }))
81 return {};
82
83 for (const auto& setting : settings)
84 try
85 {
86 settingStruct.*(setting.mem) = std::stod(values.at(setting.key));
87 }
88 catch (...)
89 {
90 return {};
91 }
92
93 return settingStruct;
94}
95
96template <typename Struct>
97std::vector<Preset<Struct>> GetPresets(
98 const std::vector<SettingDescription<Struct>>& settingDescriptions,
99 const std::vector<Detail::SerializedPreset>& serializedPresets)
100{
101 std::vector<Preset<Struct>> presets;
102 for (const auto& serialized : serializedPresets)
103 if (
104 const auto preset =
105 Deserialize<Struct>(serialized.settings, settingDescriptions))
106 presets.push_back({ serialized.name, *preset });
107
108 return presets;
109}
110} // namespace
111} // namespace DynamicRangeProcessorUtils
112
113std::vector<DynamicRangeProcessorUtils::CompressorPreset>
115{
116 return GetPresets<CompressorSettings>(
118}
119
120std::vector<DynamicRangeProcessorUtils::LimiterPreset>
122{
123 return GetPresets<LimiterSettings>(
125}
#define str(a)
const wxChar * values
static const AudacityProject::AttachedObjects::RegisteredFactory key
ReverbSettings preset
Definition: ReverbBase.cpp:25
static Settings & settings()
Definition: TrackInfo.cpp:51
const std::vector< SerializedPreset > serializedLimiterPresets
const std::vector< SerializedPreset > serializedCompressorPresets
std::optional< Struct > Deserialize(const std::string &str, const std::vector< SettingDescription< Struct > > &settings)
std::vector< Preset< Struct > > GetPresets(const std::vector< SettingDescription< Struct > > &settingDescriptions, const std::vector< Detail::SerializedPreset > &serializedPresets)
const std::vector< SettingDescription< LimiterSettings > > limiterSettingDescriptions
static const std::vector< SettingDescription< CompressorSettings > > compressorSettingDescriptions
DYNAMIC_RANGE_PROCESSOR_API std::vector< CompressorPreset > GetCompressorPresets()
DYNAMIC_RANGE_PROCESSOR_API std::vector< LimiterPreset > GetLimiterPresets()
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101