Skip to content
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
6 changes: 5 additions & 1 deletion NAM/convnet.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include <algorithm> // std::max_element
#include <algorithm>
#include <cmath> // pow, tanh, expf
#include <filesystem>
#include <fstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <version>

#if defined(__cpp_lib_filesystem)
#include <filesystem>
#endif

#include "dsp.h"
#include "registry.h"
Expand Down
6 changes: 5 additions & 1 deletion NAM/convnet.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#pragma once

#include <filesystem>
#include <iterator>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <version>

#if defined(__cpp_lib_filesystem)
#include <filesystem>
#endif

#include <Eigen/Dense>

Expand Down
6 changes: 5 additions & 1 deletion NAM/dsp.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#include <algorithm> // std::max_element
#include <cmath> // pow, tanh, expf
#include <cstring>
#include <filesystem>
#include <fstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <version>

#if defined(__cpp_lib_filesystem)
#include <filesystem>
#endif

#include "dsp.h"
#define tanh_impl_ std::tanh
Expand Down
8 changes: 7 additions & 1 deletion NAM/dsp.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#pragma once

#include <atomic>
#include <filesystem>
#include <iterator>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <version>

#if defined(__cpp_lib_filesystem)
#include <filesystem>
#endif

#include <Eigen/Dense>

Expand Down Expand Up @@ -371,12 +375,14 @@ struct dspData
/// \param version Config version string to verify
void verify_config_version(const std::string version);

#if defined(__cpp_lib_filesystem)
/// \brief Legacy loader for directory-style DSPs
///
/// Loads models from a directory structure (older format).
/// \param dirname Path to the directory containing the model
/// \return Unique pointer to a DSP object
std::unique_ptr<DSP> get_dsp_legacy(const std::filesystem::path dirname);
#endif
}; // namespace nam

#include "linear.h"
4 changes: 4 additions & 0 deletions NAM/get_dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,21 @@ void populate_dsp_data(const nlohmann::json& config, dspData& returnedConfig)
returnedConfig.expected_sample_rate = nam::get_sample_rate_from_nam_file(config);
}

#if defined(__cpp_lib_filesystem)
std::unique_ptr<DSP> get_dsp(const std::filesystem::path config_filename, DspLoadOptions options)
{
dspData temp;
return get_dsp(config_filename, temp, options);
}
#endif

std::unique_ptr<DSP> get_dsp(const nlohmann::json& config, DspLoadOptions options)
{
dspData temp;
return get_dsp(config, temp, options);
}

#if defined(__cpp_lib_filesystem)
std::unique_ptr<DSP> get_dsp(const std::filesystem::path config_filename, dspData& returnedConfig,
DspLoadOptions options)
{
Expand Down Expand Up @@ -202,6 +205,7 @@ std::unique_ptr<DSP> get_dsp(const std::filesystem::path config_filename, dspDat

return get_dsp(conf, options);
}
#endif

std::unique_ptr<DSP> get_dsp(const nlohmann::json& config, dspData& returnedConfig, DspLoadOptions options)
{
Expand Down
5 changes: 5 additions & 0 deletions NAM/get_dsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>
#include <optional>
#include <vector>
#include <version>

#include "dsp.h"
#include "nam_file.h"
Expand Down Expand Up @@ -77,6 +78,7 @@ struct DspLoadOptions
std::optional<bool> prewarm = std::nullopt;
};

#if defined(__cpp_lib_filesystem)
/// \brief Get NAM from a .nam model or mono/stereo .wav impulse response
/// \param config_filename Path to the .nam model or .wav impulse response file
/// \param options Loading options
Expand All @@ -88,13 +90,15 @@ struct DspLoadOptions
/// \throws std::runtime_error If a WAV file is invalid, empty, has more than two channels, or uses an unsupported format
/// \throws NamFileValidationError If a .nam file cannot be read or does not contain a minimally valid configuration
std::unique_ptr<DSP> get_dsp(const std::filesystem::path config_filename, DspLoadOptions options = DspLoadOptions());
#endif

/// \brief Get NAM from a provided configuration struct
/// \param conf DSP data structure containing model configuration and weights
/// \param options Loading options
/// \return Unique pointer to a DSP object
std::unique_ptr<DSP> get_dsp(dspData& conf, DspLoadOptions options = DspLoadOptions());

#if defined(__cpp_lib_filesystem)
/// \brief Get NAM from a .nam model or mono/stereo .wav impulse response and store its configuration
///
/// Creates an instance of DSP and also returns a dspData struct that holds the data of the model.
Expand All @@ -110,6 +114,7 @@ std::unique_ptr<DSP> get_dsp(dspData& conf, DspLoadOptions options = DspLoadOpti
/// \throws NamFileValidationError If a .nam file cannot be read or does not contain a minimally valid configuration
std::unique_ptr<DSP> get_dsp(const std::filesystem::path config_filename, dspData& returnedConfig,
DspLoadOptions options = DspLoadOptions());
#endif

/// \brief Get NAM from a provided configuration JSON object
/// \param config JSON configuration object
Expand Down
2 changes: 2 additions & 0 deletions NAM/nam_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <fstream>
#include <string>

#if defined(__cpp_lib_filesystem)
namespace nam
{
nlohmann::json validate_nam_file(const std::filesystem::path& filename)
Expand Down Expand Up @@ -39,3 +40,4 @@ nlohmann::json validate_nam_file(const std::filesystem::path& filename)
return config;
}
} // namespace nam
#endif
8 changes: 7 additions & 1 deletion NAM/nam_file.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#pragma once

#include <filesystem>
#include <stdexcept>
#include <version>

#if defined(__cpp_lib_filesystem)
#include <filesystem>
#endif

#include "json.hpp"

Expand All @@ -14,9 +18,11 @@ class NamFileValidationError : public std::runtime_error
using std::runtime_error::runtime_error;
};

#if defined(__cpp_lib_filesystem)
/// \brief Parse and validate a .nam file
/// \param filename Path to the .nam file
/// \return The parsed model configuration
/// \throws NamFileValidationError If the file cannot be read, parsed, or does not contain the minimum required fields
nlohmann::json validate_nam_file(const std::filesystem::path& filename);
#endif
} // namespace nam
2 changes: 2 additions & 0 deletions NAM/wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdexcept>
#include <string>

#if defined(__cpp_lib_filesystem)
namespace nam
{
namespace detail
Expand Down Expand Up @@ -142,3 +143,4 @@ std::vector<float> load_wav_ir(const std::filesystem::path& filename, double& sa
}
} // namespace detail
} // namespace nam
#endif
8 changes: 7 additions & 1 deletion NAM/wav.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#pragma once

#include <filesystem>
#include <vector>
#include <version>

#if defined(__cpp_lib_filesystem)
#include <filesystem>
#endif

#if defined(__cpp_lib_filesystem)
namespace nam
{
namespace detail
Expand All @@ -12,3 +17,4 @@ namespace detail
std::vector<float> load_wav_ir(const std::filesystem::path& filename, double& sample_rate, int& out_channels);
} // namespace detail
} // namespace nam
#endif
16 changes: 16 additions & 0 deletions tools/run_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
int main()
{
std::cout << "Running tests..." << std::endl;
#if defined(__cpp_lib_filesystem)
test_get_dsp_wav::test_formats_and_configuration();
test_get_dsp_wav::test_invalid_files();
#endif
// TODO Automatically loop, catch exceptions, log results

test_activations::TestFastTanh::test_core_function();
Expand Down Expand Up @@ -317,8 +319,10 @@ int main()

test_get_dsp::test_gets_input_level();
test_get_dsp::test_gets_output_level();
#if defined(__cpp_lib_filesystem)
test_get_dsp::test_empty_nam_file_throws_validation_error();
test_get_dsp::test_malformed_nam_file_throws_validation_error();
#endif
test_get_dsp::test_null_input_level();
test_get_dsp::test_null_output_level();
test_get_dsp::test_version_patch_one_beyond_supported();
Expand All @@ -332,12 +336,16 @@ int main()
test_get_dsp::test_get_dsp_prewarm_option_forces_constructor_reset_prewarm();
test_get_dsp::test_get_dsp_with_returned_config_constructs_once();

#if defined(__cpp_lib_filesystem)
test_nam_file::test_accepts_minimum_valid_file();
test_nam_file::test_rejects_non_object_json();
test_nam_file::test_rejects_missing_required_keys();
#endif

// Finally, some end-to-end tests.
#if defined(__cpp_lib_filesystem)
test_get_dsp::test_load_and_process_nam_files();
#endif

// Extensibility: external architecture registration and get_dsp (issue #230)
test_extensible::run_extensibility_tests();
Expand All @@ -352,14 +360,18 @@ int main()
test_container::test_container_last_max_value_must_cover_one();
test_container::test_container_unsorted_submodels_throws();
test_container::test_container_sample_rate_mismatch_throws();
#if defined(__cpp_lib_filesystem)
test_container::test_container_load_from_file();
#endif
test_container::test_container_default_is_max_size();
test_container::test_container_reset_only_resets_active_submodel();
test_container::test_container_switch_resets_before_activation();

// Sequential tests
test_sequential::test_sequential_loads_canonical_container_envelope();
#if defined(__cpp_lib_filesystem)
test_sequential::test_sequential_loads_from_file_path();
#endif
test_sequential::test_sequential_process_matches_manual_series();
test_sequential::test_sequential_process_is_realtime_safe_after_warmup();
test_sequential::test_sequential_rejects_blocks_larger_than_reset_maximum();
Expand All @@ -373,12 +385,15 @@ int main()
test_sequential::test_sequential_rejects_channel_mismatch();

// Render --slim tests
#if defined(__cpp_lib_filesystem)
test_render_slim::test_slim_changes_output();
test_render_slim::test_slim_rejects_non_slimmable();
test_render_slim::test_slim_boundary_values();
test_render_slim::test_slim_applied_before_processing();
#endif

// SlimmableWavenet tests
#if defined(__cpp_lib_filesystem)
test_slimmable_wavenet::test_loads_from_file();
test_slimmable_wavenet::test_implements_slimmable();
test_slimmable_wavenet::test_processes_audio();
Expand All @@ -387,6 +402,7 @@ int main()
test_slimmable_wavenet::test_default_is_max_size();
test_slimmable_wavenet::test_ratio_mapping();
test_slimmable_wavenet::test_slimmable_breakpoints();
#endif
test_slimmable_wavenet::test_from_json();
test_slimmable_wavenet::test_wavenet_without_slimmable_loads_as_regular();
test_slimmable_wavenet::test_unsupported_method_throws();
Expand Down
7 changes: 7 additions & 0 deletions tools/test/test_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
#include <stdexcept>
#include <string>
#include <vector>
#include <version>

#if defined(__cpp_lib_filesystem)
#include <filesystem>
#endif

#include "json.hpp"

Expand Down Expand Up @@ -370,13 +375,15 @@ void test_container_sample_rate_mismatch_throws()
std::cerr.rdbuf(originalCerr);
}

#if defined(__cpp_lib_filesystem)
void test_container_load_from_file()
{
std::filesystem::path path("example_models/slimmable_container.nam");
auto dsp = nam::get_dsp(path);
assert(dsp != nullptr);
process_and_verify(dsp.get(), 3, 64);
}
#endif

void test_container_default_is_max_size()
{
Expand Down
10 changes: 9 additions & 1 deletion tools/test/test_extensible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
#include <cassert>
#include <cmath>
#include <fstream>
#include <filesystem>
#include <memory>
#include <string>
#include <vector>
#include <version>

#if defined(__cpp_lib_filesystem)
#include <filesystem>
#endif

#include "json.hpp"

Expand Down Expand Up @@ -89,6 +93,7 @@ static void test_get_dsp_detects_dummy_architecture_from_json()
}
}

#if defined(__cpp_lib_filesystem)
static void test_get_dsp_detects_dummy_architecture_from_nam_file()
{
const std::string namContent = R"({
Expand Down Expand Up @@ -132,11 +137,14 @@ static void test_get_dsp_detects_dummy_architecture_from_nam_file()

std::filesystem::remove(path);
}
#endif

void run_extensibility_tests()
{
test_get_dsp_detects_dummy_architecture_from_json();
#if defined(__cpp_lib_filesystem)
test_get_dsp_detects_dummy_architecture_from_nam_file();
#endif
}

} // namespace test_extensible
Loading