Skip to content

Commit cd92997

Browse files
authored
[BUGFIX] Handle when calibration fields are present but null-valued (#130)
* Fix when dBu metadata are present but null. Needs tests. * Tests
1 parent 24ed91d commit cd92997

File tree

5 files changed

+95
-4
lines changed

5 files changed

+95
-4
lines changed

NAM/get_dsp.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ std::unique_ptr<DSP> get_dsp(dspData& conf)
137137
auto AssignOptional = [&conf](const std::string key, OptionalValue& v) {
138138
if (conf.metadata.find(key) != conf.metadata.end())
139139
{
140-
v.value = conf.metadata[key];
141-
v.have = true;
140+
if (!conf.metadata[key].is_null())
141+
{
142+
v.value = conf.metadata[key];
143+
v.have = true;
144+
}
142145
}
143146
};
144147

@@ -205,6 +208,7 @@ std::unique_ptr<DSP> get_dsp(dspData& conf)
205208
}
206209

207210
// "pre-warm" the model to settle initial conditions
211+
// Can this be removed now that it's part of Reset()?
208212
out->prewarm();
209213

210214
return out;

NAM/get_dsp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <fstream>
22

3+
#include "dsp.h"
4+
35
namespace nam
46
{
57
// Get NAM from a .nam file at the provided location

tools/run_tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ int main()
1111
std::cout << "Running tests..." << std::endl;
1212
// TODO Automatically loop, catch exceptions, log results
1313

14+
test_activations::TestFastTanh::test_core_function();
15+
test_activations::TestFastTanh::test_get_by_init();
16+
test_activations::TestFastTanh::test_get_by_str();
17+
1418
test_activations::TestLeakyReLU::test_core_function();
1519
test_activations::TestLeakyReLU::test_get_by_init();
1620
test_activations::TestLeakyReLU::test_get_by_str();
@@ -25,6 +29,8 @@ int main()
2529

2630
test_get_dsp::test_gets_input_level();
2731
test_get_dsp::test_gets_output_level();
32+
test_get_dsp::test_null_input_level();
33+
test_get_dsp::test_null_output_level();
2834

2935
std::cout << "Success!" << std::endl;
3036
return 0;

tools/test/test_activations.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,60 @@
1313

1414
namespace test_activations
1515
{
16+
// TODO get nonzero cases
17+
class TestFastTanh
18+
{
19+
public:
20+
static void test_core_function()
21+
{
22+
auto TestCase = [](float input, float expectedOutput) {
23+
float actualOutput = nam::activations::fast_tanh(input);
24+
assert(actualOutput == expectedOutput);
25+
};
26+
// A few snapshot tests
27+
TestCase(0.0f, 0.0f);
28+
// TestCase(1.0f, 1.0f);
29+
// TestCase(-1.0f, -0.01f);
30+
};
31+
32+
static void test_get_by_init()
33+
{
34+
auto a = nam::activations::ActivationLeakyReLU();
35+
_test_class(&a);
36+
}
37+
38+
// Get the singleton and test it
39+
static void test_get_by_str()
40+
{
41+
const std::string name = "Fasttanh";
42+
auto a = nam::activations::Activation::get_activation(name);
43+
_test_class(a);
44+
}
45+
46+
private:
47+
// Put the class through its paces
48+
static void _test_class(nam::activations::Activation* a)
49+
{
50+
std::vector<float> inputs, expectedOutputs;
51+
52+
inputs.push_back(0.0f);
53+
expectedOutputs.push_back(0.0f);
54+
55+
// inputs.push_back(1.0f);
56+
// expectedOutputs.push_back(1.0f);
57+
58+
// inputs.push_back(-1.0f);
59+
// expectedOutputs.push_back(-0.01f);
60+
61+
a->apply(inputs.data(), (long)inputs.size());
62+
for (auto itActual = inputs.begin(), itExpected = expectedOutputs.begin(); itActual != inputs.end();
63+
++itActual, ++itExpected)
64+
{
65+
assert(*itActual == *itExpected);
66+
}
67+
};
68+
};
69+
1670
class TestLeakyReLU
1771
{
1872
public:

tools/test/test_get_dsp.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace test_get_dsp
1111
{
1212
// Config
13-
const std::string configStr =
13+
const std::string basicConfigStr =
1414
R"({"version": "0.5.4", "metadata": {"date": {"year": 2024, "month": 10, "day": 9, "hour": 18, "minute": 44, "second": 41}, "loudness": -37.8406867980957, "gain": 0.13508800804658277, "name": "Test LSTM", "modeled_by": "Steve", "gear_type": "amp", "gear_make": "Darkglass Electronics", "gear_model": "Microtubes 900 v2", "tone_type": "clean", "input_level_dbu": 18.3, "output_level_dbu": 12.3, "training": {"settings": {"ignore_checks": false}, "data": {"latency": {"manual": null, "calibration": {"algorithm_version": 1, "delays": [-16], "safety_factor": 1, "recommended": -17, "warnings": {"matches_lookahead": false, "disagreement_too_high": false}}}, "checks": {"version": 3, "passed": true}}, "validation_esr": null}}, "architecture": "LSTM", "config": {"input_size": 1, "hidden_size": 3, "num_layers": 1}, "weights": [-0.21677088737487793, -0.6683622002601624, -0.2560940980911255, -0.3588429093360901, 0.17952610552310944, 0.19445613026618958, -0.01662646047770977, 0.5353694558143616, -0.2536540627479553, -0.5132213234901428, -0.020476307719945908, 0.08592455089092255, -0.6891753673553467, 0.3627359867095947, 0.008421811275184155, 0.3113192617893219, 0.14251480996608734, 0.07989779114723206, -0.18211324512958527, 0.7118963003158569, 0.41084015369415283, -0.6571938395500183, -0.13214066624641418, -0.2698603868484497, 0.49387243390083313, -0.3491725027561188, 0.6353667974472046, -0.5005152225494385, 0.2052856683731079, -0.4301638901233673, -0.15770092606544495, -0.7181791067123413, 0.056290093809366226, -0.49049463868141174, 0.6623441576957703, 0.09029324352741241, 0.34005245566368103, 0.16416560113430023, 0.15520110726356506, -0.4155678153038025, -0.36928507685661316, 0.3211132884025574, -0.6769840121269226, -0.1575538069009781, 0.05268515646457672, -0.4191459119319916, 0.599330484867096, 0.21518059074878693, -4.246325492858887, -3.315647840499878, -4.328850746154785, 4.496089458465576, 5.015639305114746, 3.6492037773132324, 0.14431169629096985, -0.6633821725845337, 0.11673200130462646, -0.1418764889240265, -0.4897872805595398, -0.8689419031143188, -0.06714004278182983, -0.4450395107269287, -0.02142983116209507, -0.15136894583702087, -2.775207996368408, -0.08681213855743408, 0.05702732503414154, 0.670292317867279, 0.31442636251449585, 0.30793967843055725], "sample_rate": 48000})";
1515

1616
// Copied over but shouldn't be publicly-exposed.
@@ -27,7 +27,7 @@ std::vector<float> GetWeights(nlohmann::json const& j)
2727
}
2828
}
2929

30-
nam::dspData _GetConfig()
30+
nam::dspData _GetConfig(const std::string& configStr = basicConfigStr)
3131
{
3232
nlohmann::json j = nlohmann::json::parse(configStr);
3333

@@ -54,4 +54,29 @@ void test_gets_output_level()
5454
std::unique_ptr<nam::DSP> dsp = get_dsp(config);
5555
assert(dsp->HasOutputLevel());
5656
}
57+
58+
void test_null_input_level()
59+
{
60+
// Issue 129
61+
const std::string configStr =
62+
R"({"version": "0.5.4", "metadata": {"date": {"year": 2024, "month": 10, "day": 9, "hour": 18, "minute": 44, "second": 41}, "loudness": -37.8406867980957, "gain": 0.13508800804658277, "name": "Test LSTM", "modeled_by": "Steve", "gear_type": "amp", "gear_make": "Darkglass Electronics", "gear_model": "Microtubes 900 v2", "tone_type": "clean", "input_level_dbu": null, "output_level_dbu": 12.3, "training": {"settings": {"ignore_checks": false}, "data": {"latency": {"manual": null, "calibration": {"algorithm_version": 1, "delays": [-16], "safety_factor": 1, "recommended": -17, "warnings": {"matches_lookahead": false, "disagreement_too_high": false}}}, "checks": {"version": 3, "passed": true}}, "validation_esr": null}}, "architecture": "LSTM", "config": {"input_size": 1, "hidden_size": 3, "num_layers": 1}, "weights": [-0.21677088737487793, -0.6683622002601624, -0.2560940980911255, -0.3588429093360901, 0.17952610552310944, 0.19445613026618958, -0.01662646047770977, 0.5353694558143616, -0.2536540627479553, -0.5132213234901428, -0.020476307719945908, 0.08592455089092255, -0.6891753673553467, 0.3627359867095947, 0.008421811275184155, 0.3113192617893219, 0.14251480996608734, 0.07989779114723206, -0.18211324512958527, 0.7118963003158569, 0.41084015369415283, -0.6571938395500183, -0.13214066624641418, -0.2698603868484497, 0.49387243390083313, -0.3491725027561188, 0.6353667974472046, -0.5005152225494385, 0.2052856683731079, -0.4301638901233673, -0.15770092606544495, -0.7181791067123413, 0.056290093809366226, -0.49049463868141174, 0.6623441576957703, 0.09029324352741241, 0.34005245566368103, 0.16416560113430023, 0.15520110726356506, -0.4155678153038025, -0.36928507685661316, 0.3211132884025574, -0.6769840121269226, -0.1575538069009781, 0.05268515646457672, -0.4191459119319916, 0.599330484867096, 0.21518059074878693, -4.246325492858887, -3.315647840499878, -4.328850746154785, 4.496089458465576, 5.015639305114746, 3.6492037773132324, 0.14431169629096985, -0.6633821725845337, 0.11673200130462646, -0.1418764889240265, -0.4897872805595398, -0.8689419031143188, -0.06714004278182983, -0.4450395107269287, -0.02142983116209507, -0.15136894583702087, -2.775207996368408, -0.08681213855743408, 0.05702732503414154, 0.670292317867279, 0.31442636251449585, 0.30793967843055725], "sample_rate": 48000})";
63+
nam::dspData config = _GetConfig(configStr);
64+
// The first part of this is that the following line doesn't fail:
65+
std::unique_ptr<nam::DSP> dsp = get_dsp(config);
66+
67+
assert(!dsp->HasInputLevel());
68+
assert(dsp->HasOutputLevel());
69+
}
70+
71+
void test_null_output_level()
72+
{
73+
// Issue 129
74+
const std::string configStr =
75+
R"({"version": "0.5.4", "metadata": {"date": {"year": 2024, "month": 10, "day": 9, "hour": 18, "minute": 44, "second": 41}, "loudness": -37.8406867980957, "gain": 0.13508800804658277, "name": "Test LSTM", "modeled_by": "Steve", "gear_type": "amp", "gear_make": "Darkglass Electronics", "gear_model": "Microtubes 900 v2", "tone_type": "clean", "input_level_dbu": 19.0, "output_level_dbu": null, "training": {"settings": {"ignore_checks": false}, "data": {"latency": {"manual": null, "calibration": {"algorithm_version": 1, "delays": [-16], "safety_factor": 1, "recommended": -17, "warnings": {"matches_lookahead": false, "disagreement_too_high": false}}}, "checks": {"version": 3, "passed": true}}, "validation_esr": null}}, "architecture": "LSTM", "config": {"input_size": 1, "hidden_size": 3, "num_layers": 1}, "weights": [-0.21677088737487793, -0.6683622002601624, -0.2560940980911255, -0.3588429093360901, 0.17952610552310944, 0.19445613026618958, -0.01662646047770977, 0.5353694558143616, -0.2536540627479553, -0.5132213234901428, -0.020476307719945908, 0.08592455089092255, -0.6891753673553467, 0.3627359867095947, 0.008421811275184155, 0.3113192617893219, 0.14251480996608734, 0.07989779114723206, -0.18211324512958527, 0.7118963003158569, 0.41084015369415283, -0.6571938395500183, -0.13214066624641418, -0.2698603868484497, 0.49387243390083313, -0.3491725027561188, 0.6353667974472046, -0.5005152225494385, 0.2052856683731079, -0.4301638901233673, -0.15770092606544495, -0.7181791067123413, 0.056290093809366226, -0.49049463868141174, 0.6623441576957703, 0.09029324352741241, 0.34005245566368103, 0.16416560113430023, 0.15520110726356506, -0.4155678153038025, -0.36928507685661316, 0.3211132884025574, -0.6769840121269226, -0.1575538069009781, 0.05268515646457672, -0.4191459119319916, 0.599330484867096, 0.21518059074878693, -4.246325492858887, -3.315647840499878, -4.328850746154785, 4.496089458465576, 5.015639305114746, 3.6492037773132324, 0.14431169629096985, -0.6633821725845337, 0.11673200130462646, -0.1418764889240265, -0.4897872805595398, -0.8689419031143188, -0.06714004278182983, -0.4450395107269287, -0.02142983116209507, -0.15136894583702087, -2.775207996368408, -0.08681213855743408, 0.05702732503414154, 0.670292317867279, 0.31442636251449585, 0.30793967843055725], "sample_rate": 48000})";
76+
nam::dspData config = _GetConfig(configStr);
77+
// The first part of this is that the following line doesn't fail:
78+
std::unique_ptr<nam::DSP> dsp = get_dsp(config);
79+
assert(dsp->HasInputLevel());
80+
assert(!dsp->HasOutputLevel());
81+
}
5782
}; // namespace test_get_dsp

0 commit comments

Comments
 (0)