Skip to content

Commit

Permalink
Add GetDSP(const char* jsonStr)
Browse files Browse the repository at this point in the history
  • Loading branch information
olilarkin committed May 4, 2024
1 parent e090dfe commit 7ea2631
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
2 changes: 2 additions & 0 deletions NAM/dsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ void VerifyConfigVersion(const std::string& version);
std::unique_ptr<DSP> GetDSP(const std::filesystem::path& modelFile);
// Creates an instance of DSP. Also returns a dspData struct that holds the data of the model.
std::unique_ptr<DSP> GetDSP(const std::filesystem::path& modelFile, dspData& returnedConfig);
// Creates an instance of DSP. Also returns a dspData struct that holds the data of the model.
std::unique_ptr<DSP> GetDSP(const char* jsonStr, dspData& returnedConfig);
// Instantiates a DSP object from dsp_config struct.
std::unique_ptr<DSP> GetDSP(dspData& conf);
// Legacy loader for directory-type DSPs
Expand Down
58 changes: 43 additions & 15 deletions NAM/get_dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void VerifyConfigVersion(const std::string& versionStr)
}
}

std::vector<float> GetWeights(nlohmann::json const& j, const std::filesystem::path& config_path)
std::vector<float> GetWeights(nlohmann::json const& j)
{
if (j.find("weights") != j.end())
{
Expand All @@ -77,7 +77,7 @@ std::vector<float> GetWeights(nlohmann::json const& j, const std::filesystem::pa
return weights;
}
else
throw std::runtime_error("Corrupted model file is missing weights.");
throw std::runtime_error("Corrupted model is missing weights.");
}

std::unique_ptr<DSP> GetDSP(const std::filesystem::path& configFileName)
Expand All @@ -86,7 +86,7 @@ std::unique_ptr<DSP> GetDSP(const std::filesystem::path& configFileName)
return GetDSP(configFileName, temp);
}

std::unique_ptr<DSP> GetDSP(const std::filesystem::path& configFileName, dspData& returnedConfig)
std::unique_ptr<DSP> GetDSP(const std::filesystem::path& configFileName, dspData& config)
{
if (!std::filesystem::exists(configFileName))
throw std::runtime_error("Config JSON doesn't exist!\n");
Expand All @@ -96,28 +96,56 @@ std::unique_ptr<DSP> GetDSP(const std::filesystem::path& configFileName, dspData
VerifyConfigVersion(j["version"]);

auto architecture = j["architecture"];
nlohmann::json config = j["config"];
std::vector<float> weights = GetWeights(j, configFileName);

// Assign values to returnedConfig
returnedConfig.version = j["version"];
returnedConfig.architecture = j["architecture"];
returnedConfig.config = j["config"];
returnedConfig.metadata = j["metadata"];
returnedConfig.weights = weights;
std::vector<float> weights = GetWeights(j);

// Assign values to config
config.version = j["version"];
config.architecture = j["architecture"];
config.config = j["config"];
config.metadata = j["metadata"];
config.weights = weights;
if (j.find("sample_rate") != j.end())
returnedConfig.expectedSampleRate = j["sample_rate"];
config.expectedSampleRate = j["sample_rate"];
else
{
returnedConfig.expectedSampleRate = -1.0;
config.expectedSampleRate = -1.0;
}

/*Copy to a new dsp_config object for GetDSP below,
since not sure if weights actually get modified as being non-const references on some
model constructors inside GetDSP(dsp_config& conf).
We need to return unmodified version of dsp_config via returnedConfig.*/
dspData conf = config;

return GetDSP(conf);
}

std::unique_ptr<DSP> GetDSP(const char* jsonStr, dspData& config)
{
nlohmann::json j = nlohmann::json::parse(jsonStr);
VerifyConfigVersion(j["version"]);

auto architecture = j["architecture"];
std::vector<float> weights = GetWeights(j);

// Assign values to config
config.version = j["version"];
config.architecture = j["architecture"];
config.config = j["config"];
config.metadata = j["metadata"];
config.weights = weights;
if (j.find("sample_rate") != j.end())
config.expectedSampleRate = j["sample_rate"];
else
{
config.expectedSampleRate = -1.0;
}

/*Copy to a new dsp_config object for GetDSP below,
since not sure if weights actually get modified as being non-const references on some
model constructors inside GetDSP(dsp_config& conf).
We need to return unmodified version of dsp_config via returnedConfig.*/
dspData conf = returnedConfig;
dspData conf = config;

return GetDSP(conf);
}
Expand Down

0 comments on commit 7ea2631

Please sign in to comment.