Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature unittests #362

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions NeuralAmpModeler/NeuralAmpModelerPreset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "NeuralAmpModelerPreset.h"
#include <filesystem>
#include <fstream>
#include <json.hpp>
#include <wdlstring.h>


NeuralAmpModelerPreset::NeuralAmpModelerPreset(std::string name, std::string desc, std::string ampPath,
std::string irPath, std::vector<float> params)
: _name(std::move(name))
, _description(std::move(desc))
, _amp(std::move(ampPath))
, _ir(std::move(irPath))
, _params(std::move(params))
{
_version = CURRENT_PRESET_VERSION; // future: handle preset versioning
}

const NeuralAmpModelerPreset* NeuralAmpModelerPreset::Deserialize(const WDL_String& presetPath,
std::string& errorMessage)
{
NeuralAmpModelerPreset* result = nullptr;

try
{
auto path = std::filesystem::u8path(presetPath.Get());
auto pathDirectory = path.parent_path();
std::ifstream i(path);
nlohmann::json j;
i >> j;
auto version = j["version"];

auto presetName = j["name"];
auto presetDescription = j["description"];
auto ampModel = static_cast<std::string>(j["amp"]);
auto ampIR = static_cast<std::string>(j["ir"]);
std::filesystem::path pAmpModel(ampModel);
std::filesystem::path pAmpIR(ampIR);

if( pAmpModel.is_relative()) pAmpModel = absolute(pathDirectory / ampModel);
if( pAmpIR.is_relative()) pAmpIR = absolute(pathDirectory / ampIR);
// MAKE pAmpModel an absolute path

if (j.find("params") != j.end())
{
auto params_list = j["params"];
std::vector<float> params;
for (auto& it : params_list)
params.push_back(it);
result = new NeuralAmpModelerPreset(presetName, presetDescription, pAmpModel.string(), pAmpIR.string(), params);
}
else
throw std::runtime_error("Corrupted preset file is missing params.");
}
catch (std::exception& e)
{
errorMessage = e.what();
result = nullptr;
}

return result;
}

// Generate a method that writes json parameters to a file using nlohmann::json using the same parameters as implemented
// in LoadFrom()
bool NeuralAmpModelerPreset::Serialize(const NeuralAmpModelerPreset& preset, const WDL_String& presetPath, std::string& errorMessage)
{
// Create a json instance from the preset object
try
{
auto presetFilePath = std::filesystem::u8path(presetPath.Get());
// force extension of presetPath to be ".nps"
if (presetFilePath.extension() != ".nps") presetFilePath.replace_extension(".nps");
const auto presetDirectoryPath = presetFilePath.parent_path();

// First, try to make the path relative to the preset file
auto optimizedPathtoAMP = std::filesystem::relative( preset.AmpPath(), presetDirectoryPath);
auto optimizedPathtoIR = std::filesystem::relative( preset.IrPath(), presetDirectoryPath);

// if the path can't be made relative, then it's not in the same directory as the preset file, so use absolute path
if (optimizedPathtoAMP.empty()) optimizedPathtoAMP = std::filesystem::absolute( preset.AmpPath());
if (optimizedPathtoIR.empty()) optimizedPathtoIR = std::filesystem::absolute( preset.IrPath());

std::ofstream o(presetFilePath);

nlohmann::json j;
// Write all the attributes of the preset object to the json instance
j["version"] = preset.Version();
j["name"] = preset.Name();
j["description"] = preset.Description();
j["amp"] = optimizedPathtoAMP.string();
j["ir"] = optimizedPathtoIR.string();
j["params"] = preset.Params();
// Write the json instance to the file
o << std::setw(4) << j << std::endl;
}
catch (const std::exception& e)
{
errorMessage = e.what();
return false;
}

return true;
}
39 changes: 39 additions & 0 deletions NeuralAmpModeler/NeuralAmpModelerPreset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
#include <string>
#include <iostream>
#include <vector>
#include <wdlstring.h>
/**
* \brief a preset version number is used to handle preset versioning in the future,
* it must be and increment of 1 for each new version necessiting a preset format change
*/
constexpr uint32_t CURRENT_PRESET_VERSION = 1;

class NeuralAmpModelerPreset
{
public:
NeuralAmpModelerPreset(std::string name, std::string desc, std::string ampPath, std::string irPath,
std::vector<float> params);

static const NeuralAmpModelerPreset* Deserialize(const WDL_String& presetPath, std::string& errorMessage);
static bool Serialize(const NeuralAmpModelerPreset& preset, const WDL_String& presetPath, std::string& errorMessage);
bool Serialize(const WDL_String& presetPath, std::string& errorMessage) const
{
return Serialize(*this, presetPath, errorMessage);
}

const std::string& Name() const { return _name; }
const std::string& Description() const { return _description; }
const std::string& AmpPath() const { return _amp; }
const std::string& IrPath() const { return _ir; }
const std::vector<float>& Params() const { return _params; }
uint32_t Version() const { return _version; }

protected:
std::string _name;
std::string _description;
std::string _amp;
std::string _ir;
std::vector<float> _params;
uint32_t _version;
};
24 changes: 24 additions & 0 deletions NeuralAmpModeler/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
project(UnitTesting)
Include(FetchContent)

FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.0.1 # or a later release
)

FetchContent_MakeAvailable(Catch2)
include_directories(
${CMAKE_SOURCE_DIR}/..
${CMAKE_SOURCE_DIR}/../../iPlug2/Dependencies/Extras/nlohmann
${CMAKE_SOURCE_DIR}/../../iPlug2/WDL
)

add_executable(tests
TestMain.cpp
TestNeuralAmpModelerPreset.cpp
../NeuralAmpModelerPreset.h
../NeuralAmpModelerPreset.cpp
)
set_property(TARGET tests PROPERTY CXX_STANDARD 17)
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
4 changes: 4 additions & 0 deletions NeuralAmpModeler/UnitTests/CreateProject-mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#/bin/bash
echo "Using cmake to generate Xcode unit test project in ../build-mac ..."
mkdir -p ../build-mac
cmake -G Xcode -S . -B ../build-mac
15 changes: 15 additions & 0 deletions NeuralAmpModeler/UnitTests/CreateProject-win.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@echo off
setlocal EnableExtensions EnableDelayedExpansion

echo Generating visual studio 2022 test project in ..\build-win. cmake must be in your PATH

set CMKDEF="C:\Program Files\CMake\bin\cmake.exe"
set CMKCMD=cmake.exe

if exist %CMKDEF% (
set CMK=%CMKDEF%
) else (
set CMK=%CMKCMD%
)

%CMK% -G "Visual Studio 17 2022" -S . -B..\build-win\test
8 changes: 8 additions & 0 deletions NeuralAmpModeler/UnitTests/HowTo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- Prerequisites:
You must have cmake 3.10 or newer installed in your system.

- Windows / Visual Studio 2022
Run CreateProject-win.cmd

- Mac OSX / Xcode:
Run ./CreateProject-mac.sh
Loading