|
2 | 2 | #include <iostream> |
3 | 3 | #include <sstream> |
4 | 4 | #include <stdexcept> |
5 | | -#include <unordered_set> |
6 | 5 |
|
7 | 6 | #include "dsp.h" |
8 | 7 | #include "registry.h" |
9 | 8 | #include "json.hpp" |
10 | | -#include "lstm.h" |
11 | | -#include "convnet.h" |
12 | | -#include "wavenet.h" |
13 | 9 | #include "get_dsp.h" |
| 10 | +#include "model_config.h" |
14 | 11 |
|
15 | 12 | namespace nam |
16 | 13 | { |
@@ -146,62 +143,69 @@ std::unique_ptr<DSP> get_dsp(const nlohmann::json& config, dspData& returnedConf |
146 | 143 | return get_dsp(conf); |
147 | 144 | } |
148 | 145 |
|
149 | | -struct OptionalValue |
| 146 | +// ============================================================================= |
| 147 | +// Unified construction path |
| 148 | +// ============================================================================= |
| 149 | + |
| 150 | +std::unique_ptr<ModelConfig> parse_model_config_json(const std::string& architecture, const nlohmann::json& config, |
| 151 | + double sample_rate) |
| 152 | +{ |
| 153 | + return ConfigParserRegistry::instance().parse(architecture, config, sample_rate); |
| 154 | +} |
| 155 | + |
| 156 | +namespace |
150 | 157 | { |
151 | | - bool have = false; |
152 | | - double value = 0.0; |
153 | | -}; |
| 158 | + |
| 159 | +void apply_metadata(DSP& dsp, const ModelMetadata& metadata) |
| 160 | +{ |
| 161 | + if (metadata.loudness.has_value()) |
| 162 | + dsp.SetLoudness(metadata.loudness.value()); |
| 163 | + if (metadata.input_level.has_value()) |
| 164 | + dsp.SetInputLevel(metadata.input_level.value()); |
| 165 | + if (metadata.output_level.has_value()) |
| 166 | + dsp.SetOutputLevel(metadata.output_level.value()); |
| 167 | +} |
| 168 | + |
| 169 | +} // anonymous namespace |
| 170 | + |
| 171 | +std::unique_ptr<DSP> create_dsp(std::unique_ptr<ModelConfig> config, std::vector<float> weights, |
| 172 | + const ModelMetadata& metadata) |
| 173 | +{ |
| 174 | + auto out = config->create(std::move(weights), metadata.sample_rate); |
| 175 | + apply_metadata(*out, metadata); |
| 176 | + // "pre-warm" the model to settle initial conditions |
| 177 | + // Can this be removed now that it's part of Reset()? |
| 178 | + out->prewarm(); |
| 179 | + return out; |
| 180 | +} |
| 181 | + |
| 182 | +// ============================================================================= |
| 183 | +// get_dsp(dspData&) — now uses unified path |
| 184 | +// ============================================================================= |
154 | 185 |
|
155 | 186 | std::unique_ptr<DSP> get_dsp(dspData& conf) |
156 | 187 | { |
157 | 188 | verify_config_version(conf.version); |
158 | 189 |
|
159 | | - auto& architecture = conf.architecture; |
160 | | - nlohmann::json& config = conf.config; |
161 | | - std::vector<float>& weights = conf.weights; |
162 | | - OptionalValue loudness, inputLevel, outputLevel; |
163 | | - |
164 | | - auto AssignOptional = [&conf](const std::string key, OptionalValue& v) { |
165 | | - if (conf.metadata.find(key) != conf.metadata.end()) |
166 | | - { |
167 | | - if (!conf.metadata[key].is_null()) |
168 | | - { |
169 | | - v.value = conf.metadata[key]; |
170 | | - v.have = true; |
171 | | - } |
172 | | - } |
173 | | - }; |
| 190 | + // Extract metadata from JSON |
| 191 | + ModelMetadata metadata; |
| 192 | + metadata.version = conf.version; |
| 193 | + metadata.sample_rate = conf.expected_sample_rate; |
174 | 194 |
|
175 | 195 | if (!conf.metadata.is_null()) |
176 | 196 | { |
177 | | - AssignOptional("loudness", loudness); |
178 | | - AssignOptional("input_level_dbu", inputLevel); |
179 | | - AssignOptional("output_level_dbu", outputLevel); |
180 | | - } |
181 | | - const double expectedSampleRate = conf.expected_sample_rate; |
182 | | - |
183 | | - // Initialize using registry-based factory |
184 | | - std::unique_ptr<DSP> out = |
185 | | - nam::factory::FactoryRegistry::instance().create(architecture, config, weights, expectedSampleRate); |
186 | | - |
187 | | - if (loudness.have) |
188 | | - { |
189 | | - out->SetLoudness(loudness.value); |
190 | | - } |
191 | | - if (inputLevel.have) |
192 | | - { |
193 | | - out->SetInputLevel(inputLevel.value); |
194 | | - } |
195 | | - if (outputLevel.have) |
196 | | - { |
197 | | - out->SetOutputLevel(outputLevel.value); |
| 197 | + auto extract = [&conf](const std::string& key) -> std::optional<double> { |
| 198 | + if (conf.metadata.find(key) != conf.metadata.end() && !conf.metadata[key].is_null()) |
| 199 | + return conf.metadata[key].get<double>(); |
| 200 | + return std::nullopt; |
| 201 | + }; |
| 202 | + metadata.loudness = extract("loudness"); |
| 203 | + metadata.input_level = extract("input_level_dbu"); |
| 204 | + metadata.output_level = extract("output_level_dbu"); |
198 | 205 | } |
199 | 206 |
|
200 | | - // "pre-warm" the model to settle initial conditions |
201 | | - // Can this be removed now that it's part of Reset()? |
202 | | - out->prewarm(); |
203 | | - |
204 | | - return out; |
| 207 | + auto model_config = ConfigParserRegistry::instance().parse(conf.architecture, conf.config, conf.expected_sample_rate); |
| 208 | + return create_dsp(std::move(model_config), std::move(conf.weights), metadata); |
205 | 209 | } |
206 | 210 |
|
207 | 211 | double get_sample_rate_from_nam_file(const nlohmann::json& j) |
|
0 commit comments