diff --git a/NAM/activations.cpp b/NAM/activations.cpp index 50b8222..92f1d8d 100644 --- a/NAM/activations.cpp +++ b/NAM/activations.cpp @@ -1,19 +1,19 @@ #include "activations.h" -activations::ActivationTanh _TANH = activations::ActivationTanh(); -activations::ActivationFastTanh _FAST_TANH = activations::ActivationFastTanh(); -activations::ActivationHardTanh _HARD_TANH = activations::ActivationHardTanh(); -activations::ActivationReLU _RELU = activations::ActivationReLU(); -activations::ActivationSigmoid _SIGMOID = activations::ActivationSigmoid(); +nam::activations::ActivationTanh _TANH = nam::activations::ActivationTanh(); +nam::activations::ActivationFastTanh _FAST_TANH = nam::activations::ActivationFastTanh(); +nam::activations::ActivationHardTanh _HARD_TANH = nam::activations::ActivationHardTanh(); +nam::activations::ActivationReLU _RELU = nam::activations::ActivationReLU(); +nam::activations::ActivationSigmoid _SIGMOID = nam::activations::ActivationSigmoid(); -bool activations::Activation::using_fast_tanh = false; +bool nam::activations::Activation::using_fast_tanh = false; -std::unordered_map activations::Activation::_activations = +std::unordered_map nam::activations::Activation::_activations = {{"Tanh", &_TANH}, {"Hardtanh", &_HARD_TANH}, {"Fasttanh", &_FAST_TANH}, {"ReLU", &_RELU}, {"Sigmoid", &_SIGMOID}}; -activations::Activation* tanh_bak = nullptr; +nam::activations::Activation* tanh_bak = nullptr; -activations::Activation* activations::Activation::get_activation(const std::string name) +nam::activations::Activation* nam::activations::Activation::get_activation(const std::string name) { if (_activations.find(name) == _activations.end()) return nullptr; @@ -21,9 +21,9 @@ activations::Activation* activations::Activation::get_activation(const std::stri return _activations[name]; } -void activations::Activation::enable_fast_tanh() +void nam::activations::Activation::enable_fast_tanh() { - activations::Activation::using_fast_tanh = true; + nam::activations::Activation::using_fast_tanh = true; if (_activations["Tanh"] != _activations["Fasttanh"]) { @@ -32,9 +32,9 @@ void activations::Activation::enable_fast_tanh() } } -void activations::Activation::disable_fast_tanh() +void nam::activations::Activation::disable_fast_tanh() { - activations::Activation::using_fast_tanh = false; + nam::activations::Activation::using_fast_tanh = false; if (_activations["Tanh"] == _activations["Fasttanh"]) { diff --git a/NAM/activations.h b/NAM/activations.h index 73db59a..e9afc33 100644 --- a/NAM/activations.h +++ b/NAM/activations.h @@ -5,6 +5,8 @@ #include #include +namespace nam +{ namespace activations { inline float relu(float x) @@ -119,5 +121,5 @@ class ActivationSigmoid : public Activation } } }; - -}; // namespace activations \ No newline at end of file +}; // namespace activations +}; // namespace nam diff --git a/NAM/convnet.cpp b/NAM/convnet.cpp index d99d7c9..917f93a 100644 --- a/NAM/convnet.cpp +++ b/NAM/convnet.cpp @@ -12,7 +12,7 @@ #include "util.h" #include "convnet.h" -convnet::BatchNorm::BatchNorm(const int dim, std::vector::iterator& params) +nam::convnet::BatchNorm::BatchNorm(const int dim, std::vector::iterator& params) { // Extract from param buffer Eigen::VectorXf running_mean(dim); @@ -37,7 +37,7 @@ convnet::BatchNorm::BatchNorm(const int dim, std::vector::iterator& param this->loc = _bias - this->scale.cwiseProduct(running_mean); } -void convnet::BatchNorm::process_(Eigen::MatrixXf& x, const long i_start, const long i_end) const +void nam::convnet::BatchNorm::process_(Eigen::MatrixXf& x, const long i_start, const long i_end) const { // todo using colwise? // #speed but conv probably dominates @@ -48,9 +48,9 @@ void convnet::BatchNorm::process_(Eigen::MatrixXf& x, const long i_start, const } } -void convnet::ConvNetBlock::set_params_(const int in_channels, const int out_channels, const int _dilation, - const bool batchnorm, const std::string activation, - std::vector::iterator& params) +void nam::convnet::ConvNetBlock::set_params_(const int in_channels, const int out_channels, const int _dilation, + const bool batchnorm, const std::string activation, + std::vector::iterator& params) { this->_batchnorm = batchnorm; // HACK 2 kernel @@ -60,8 +60,8 @@ void convnet::ConvNetBlock::set_params_(const int in_channels, const int out_cha this->activation = activations::Activation::get_activation(activation); } -void convnet::ConvNetBlock::process_(const Eigen::MatrixXf& input, Eigen::MatrixXf& output, const long i_start, - const long i_end) const +void nam::convnet::ConvNetBlock::process_(const Eigen::MatrixXf& input, Eigen::MatrixXf& output, const long i_start, + const long i_end) const { const long ncols = i_end - i_start; this->conv.process_(input, output, i_start, ncols, i_start); @@ -71,12 +71,12 @@ void convnet::ConvNetBlock::process_(const Eigen::MatrixXf& input, Eigen::Matrix this->activation->apply(output.middleCols(i_start, ncols)); } -long convnet::ConvNetBlock::get_out_channels() const +long nam::convnet::ConvNetBlock::get_out_channels() const { return this->conv.get_out_channels(); } -convnet::_Head::_Head(const int channels, std::vector::iterator& params) +nam::convnet::_Head::_Head(const int channels, std::vector::iterator& params) { this->_weight.resize(channels); for (int i = 0; i < channels; i++) @@ -84,8 +84,8 @@ convnet::_Head::_Head(const int channels, std::vector::iterator& params) this->_bias = *(params++); } -void convnet::_Head::process_(const Eigen::MatrixXf& input, Eigen::VectorXf& output, const long i_start, - const long i_end) const +void nam::convnet::_Head::process_(const Eigen::MatrixXf& input, Eigen::VectorXf& output, const long i_start, + const long i_end) const { const long length = i_end - i_start; output.resize(length); @@ -93,8 +93,9 @@ void convnet::_Head::process_(const Eigen::MatrixXf& input, Eigen::VectorXf& out output(i) = this->_bias + input.col(j).dot(this->_weight); } -convnet::ConvNet::ConvNet(const int channels, const std::vector& dilations, const bool batchnorm, - const std::string activation, std::vector& params, const double expected_sample_rate) +nam::convnet::ConvNet::ConvNet(const int channels, const std::vector& dilations, const bool batchnorm, + const std::string activation, std::vector& params, + const double expected_sample_rate) : Buffer(*std::max_element(dilations.begin(), dilations.end()), expected_sample_rate) { this->_verify_params(channels, dilations, batchnorm, params.size()); @@ -116,7 +117,7 @@ convnet::ConvNet::ConvNet(const int channels, const std::vector& dilations, } -void convnet::ConvNet::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) +void nam::convnet::ConvNet::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) { this->_update_buffers_(input, num_frames); @@ -135,13 +136,13 @@ void convnet::ConvNet::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int output[s] = this->_head_output(s); } -void convnet::ConvNet::_verify_params(const int channels, const std::vector& dilations, const bool batchnorm, - const size_t actual_params) +void nam::convnet::ConvNet::_verify_params(const int channels, const std::vector& dilations, const bool batchnorm, + const size_t actual_params) { // TODO } -void convnet::ConvNet::_update_buffers_(NAM_SAMPLE* input, const int num_frames) +void nam::convnet::ConvNet::_update_buffers_(NAM_SAMPLE* input, const int num_frames) { this->Buffer::_update_buffers_(input, num_frames); @@ -163,7 +164,7 @@ void convnet::ConvNet::_update_buffers_(NAM_SAMPLE* input, const int num_frames) } } -void convnet::ConvNet::_rewind_buffers_() +void nam::convnet::ConvNet::_rewind_buffers_() { // Need to rewind the block vals first because Buffer::rewind_buffers() // resets the offset index diff --git a/NAM/convnet.h b/NAM/convnet.h index 174c6f5..fe8ab0a 100644 --- a/NAM/convnet.h +++ b/NAM/convnet.h @@ -9,6 +9,8 @@ #include +namespace nam +{ namespace convnet { // Custom Conv that avoids re-computing on pieces of the input and trusts @@ -82,3 +84,4 @@ class ConvNet : public Buffer void process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) override; }; }; // namespace convnet +}; // namespace nam diff --git a/NAM/dsp.cpp b/NAM/dsp.cpp index 09f0663..c2d26da 100644 --- a/NAM/dsp.cpp +++ b/NAM/dsp.cpp @@ -16,12 +16,12 @@ constexpr const long _INPUT_BUFFER_SAFETY_FACTOR = 32; -DSP::DSP(const double expected_sample_rate) +nam::DSP::DSP(const double expected_sample_rate) : mExpectedSampleRate(expected_sample_rate) { } -void DSP::prewarm() +void nam::DSP::prewarm() { if (_prewarm_samples == 0) return; @@ -38,14 +38,14 @@ void DSP::prewarm() } } -void DSP::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) +void nam::DSP::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) { // Default implementation is the null operation for (size_t i = 0; i < num_frames; i++) output[i] = input[i]; } -double DSP::GetLoudness() const +double nam::DSP::GetLoudness() const { if (!HasLoudness()) { @@ -54,15 +54,15 @@ double DSP::GetLoudness() const return mLoudness; } -void DSP::SetLoudness(const double loudness) +void nam::DSP::SetLoudness(const double loudness) { mLoudness = loudness; mHasLoudness = true; } -void DSP::finalize_(const int num_frames) {} +void nam::DSP::finalize_(const int num_frames) {} -void DSP::_get_params_(const std::unordered_map& input_params) +void nam::DSP::_get_params_(const std::unordered_map& input_params) { this->_stale_params = false; for (auto it = input_params.begin(); it != input_params.end(); ++it) @@ -79,18 +79,18 @@ void DSP::_get_params_(const std::unordered_map& input_para // Buffer ===================================================================== -Buffer::Buffer(const int receptive_field, const double expected_sample_rate) -: DSP(expected_sample_rate) +nam::Buffer::Buffer(const int receptive_field, const double expected_sample_rate) +: nam::DSP(expected_sample_rate) { this->_set_receptive_field(receptive_field); } -void Buffer::_set_receptive_field(const int new_receptive_field) +void nam::Buffer::_set_receptive_field(const int new_receptive_field) { this->_set_receptive_field(new_receptive_field, _INPUT_BUFFER_SAFETY_FACTOR * new_receptive_field); }; -void Buffer::_set_receptive_field(const int new_receptive_field, const int input_buffer_size) +void nam::Buffer::_set_receptive_field(const int new_receptive_field, const int input_buffer_size) { this->_receptive_field = new_receptive_field; this->_input_buffer.resize(input_buffer_size); @@ -98,7 +98,7 @@ void Buffer::_set_receptive_field(const int new_receptive_field, const int input this->_reset_input_buffer(); } -void Buffer::_update_buffers_(NAM_SAMPLE* input, const int num_frames) +void nam::Buffer::_update_buffers_(NAM_SAMPLE* input, const int num_frames) { // Make sure that the buffer is big enough for the receptive field and the // frames needed! @@ -126,7 +126,7 @@ void Buffer::_update_buffers_(NAM_SAMPLE* input, const int num_frames) std::fill(this->_output_buffer.begin(), this->_output_buffer.end(), 0.0f); } -void Buffer::_rewind_buffers_() +void nam::Buffer::_rewind_buffers_() { // Copy the input buffer back // RF-1 samples because we've got at least one new one inbound. @@ -140,22 +140,22 @@ void Buffer::_rewind_buffers_() this->_input_buffer_offset = this->_receptive_field; } -void Buffer::_reset_input_buffer() +void nam::Buffer::_reset_input_buffer() { this->_input_buffer_offset = this->_receptive_field; } -void Buffer::finalize_(const int num_frames) +void nam::Buffer::finalize_(const int num_frames) { - this->DSP::finalize_(num_frames); + this->nam::DSP::finalize_(num_frames); this->_input_buffer_offset += num_frames; } // Linear ===================================================================== -Linear::Linear(const int receptive_field, const bool _bias, const std::vector& params, - const double expected_sample_rate) -: Buffer(receptive_field, expected_sample_rate) +nam::Linear::Linear(const int receptive_field, const bool _bias, const std::vector& params, + const double expected_sample_rate) +: nam::Buffer(receptive_field, expected_sample_rate) { if ((int)params.size() != (receptive_field + (_bias ? 1 : 0))) throw std::runtime_error( @@ -169,9 +169,9 @@ Linear::Linear(const int receptive_field, const bool _bias, const std::vector_bias = _bias ? params[receptive_field] : (float)0.0; } -void Linear::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) +void nam::Linear::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) { - this->Buffer::_update_buffers_(input, num_frames); + this->nam::Buffer::_update_buffers_(input, num_frames); // Main computation! for (size_t i = 0; i < num_frames; i++) @@ -184,7 +184,7 @@ void Linear::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames // NN modules ================================================================= -void Conv1D::set_params_(std::vector::iterator& params) +void nam::Conv1D::set_params_(std::vector::iterator& params) { if (this->_weight.size() > 0) { @@ -200,8 +200,8 @@ void Conv1D::set_params_(std::vector::iterator& params) this->_bias(i) = *(params++); } -void Conv1D::set_size_(const int in_channels, const int out_channels, const int kernel_size, const bool do_bias, - const int _dilation) +void nam::Conv1D::set_size_(const int in_channels, const int out_channels, const int kernel_size, const bool do_bias, + const int _dilation) { this->_weight.resize(kernel_size); for (size_t i = 0; i < this->_weight.size(); i++) @@ -214,15 +214,15 @@ void Conv1D::set_size_(const int in_channels, const int out_channels, const int this->_dilation = _dilation; } -void Conv1D::set_size_and_params_(const int in_channels, const int out_channels, const int kernel_size, - const int _dilation, const bool do_bias, std::vector::iterator& params) +void nam::Conv1D::set_size_and_params_(const int in_channels, const int out_channels, const int kernel_size, + const int _dilation, const bool do_bias, std::vector::iterator& params) { this->set_size_(in_channels, out_channels, kernel_size, do_bias, _dilation); this->set_params_(params); } -void Conv1D::process_(const Eigen::MatrixXf& input, Eigen::MatrixXf& output, const long i_start, const long ncols, - const long j_start) const +void nam::Conv1D::process_(const Eigen::MatrixXf& input, Eigen::MatrixXf& output, const long i_start, const long ncols, + const long j_start) const { // This is the clever part ;) for (size_t k = 0; k < this->_weight.size(); k++) @@ -237,7 +237,7 @@ void Conv1D::process_(const Eigen::MatrixXf& input, Eigen::MatrixXf& output, con output.middleCols(j_start, ncols).colwise() += this->_bias; } -long Conv1D::get_num_params() const +long nam::Conv1D::get_num_params() const { long num_params = this->_bias.size(); for (size_t i = 0; i < this->_weight.size(); i++) @@ -245,7 +245,7 @@ long Conv1D::get_num_params() const return num_params; } -Conv1x1::Conv1x1(const int in_channels, const int out_channels, const bool _bias) +nam::Conv1x1::Conv1x1(const int in_channels, const int out_channels, const bool _bias) { this->_weight.resize(out_channels, in_channels); this->_do_bias = _bias; @@ -253,7 +253,7 @@ Conv1x1::Conv1x1(const int in_channels, const int out_channels, const bool _bias this->_bias.resize(out_channels); } -void Conv1x1::set_params_(std::vector::iterator& params) +void nam::Conv1x1::set_params_(std::vector::iterator& params) { for (int i = 0; i < this->_weight.rows(); i++) for (int j = 0; j < this->_weight.cols(); j++) @@ -263,7 +263,7 @@ void Conv1x1::set_params_(std::vector::iterator& params) this->_bias(i) = *(params++); } -Eigen::MatrixXf Conv1x1::process(const Eigen::MatrixXf& input) const +Eigen::MatrixXf nam::Conv1x1::process(const Eigen::MatrixXf& input) const { if (this->_do_bias) return (this->_weight * input).colwise() + this->_bias; diff --git a/NAM/dsp.h b/NAM/dsp.h index 209821d..273bc90 100644 --- a/NAM/dsp.h +++ b/NAM/dsp.h @@ -21,6 +21,8 @@ // TODO clean this up and track a bool for whether it knows. #define NAM_UNKNOWN_EXPECTED_SAMPLE_RATE -1.0 +namespace nam +{ enum EArchitectures { kLinear = 0, @@ -227,3 +229,4 @@ std::unique_ptr get_dsp(const std::filesystem::path model_file, dspData& re std::unique_ptr get_dsp(dspData& conf); // Legacy loader for directory-type DSPs std::unique_ptr get_dsp_legacy(const std::filesystem::path dirname); +}; // namespace nam diff --git a/NAM/get_dsp.cpp b/NAM/get_dsp.cpp index 3c71c86..7e11a74 100644 --- a/NAM/get_dsp.cpp +++ b/NAM/get_dsp.cpp @@ -9,6 +9,8 @@ #include "convnet.h" #include "wavenet.h" +namespace nam +{ struct Version { int major; @@ -78,13 +80,6 @@ std::vector GetWeights(nlohmann::json const& j, const std::filesystem::pa throw std::runtime_error("Corrupted model file is missing weights."); } -std::unique_ptr get_dsp_legacy(const std::filesystem::path model_dir) -{ - auto config_filename = model_dir / std::filesystem::path("config.json"); - dspData temp; - return get_dsp(config_filename, temp); -} - std::unique_ptr get_dsp(const std::filesystem::path config_filename) { dspData temp; @@ -119,9 +114,9 @@ std::unique_ptr get_dsp(const std::filesystem::path config_filename, dspDat /*Copy to a new dsp_config object for get_dsp below, - since not sure if params actually get modified as being non-const references on some - model constructors inside get_dsp(dsp_config& conf). - We need to return unmodified version of dsp_config via returnedConfig.*/ + since not sure if params actually get modified as being non-const references on some + model constructors inside get_dsp(dsp_config& conf). + We need to return unmodified version of dsp_config via returnedConfig.*/ dspData conf = returnedConfig; return get_dsp(conf); @@ -217,3 +212,4 @@ std::unique_ptr get_dsp(dspData& conf) return out; } +}; // namespace nam diff --git a/NAM/lstm.cpp b/NAM/lstm.cpp index 9697c60..dfd6daa 100644 --- a/NAM/lstm.cpp +++ b/NAM/lstm.cpp @@ -4,7 +4,7 @@ #include "lstm.h" -lstm::LSTMCell::LSTMCell(const int input_size, const int hidden_size, std::vector::iterator& params) +nam::lstm::LSTMCell::LSTMCell(const int input_size, const int hidden_size, std::vector::iterator& params) { // Resize arrays this->_w.resize(4 * hidden_size, input_size + hidden_size); @@ -26,7 +26,7 @@ lstm::LSTMCell::LSTMCell(const int input_size, const int hidden_size, std::vecto this->_c[i] = *(params++); } -void lstm::LSTMCell::process_(const Eigen::VectorXf& x) +void nam::lstm::LSTMCell::process_(const Eigen::VectorXf& x) { const long hidden_size = this->_get_hidden_size(); const long input_size = this->_get_input_size(); @@ -63,8 +63,8 @@ void lstm::LSTMCell::process_(const Eigen::VectorXf& x) } } -lstm::LSTM::LSTM(const int num_layers, const int input_size, const int hidden_size, std::vector& params, - nlohmann::json& parametric, const double expected_sample_rate) +nam::lstm::LSTM::LSTM(const int num_layers, const int input_size, const int hidden_size, std::vector& params, + nlohmann::json& parametric, const double expected_sample_rate) : DSP(expected_sample_rate) { this->_init_parametric(parametric); @@ -78,7 +78,7 @@ lstm::LSTM::LSTM(const int num_layers, const int input_size, const int hidden_si assert(it == params.end()); } -void lstm::LSTM::_init_parametric(nlohmann::json& parametric) +void nam::lstm::LSTM::_init_parametric(nlohmann::json& parametric) { std::vector parametric_names; for (nlohmann::json::iterator it = parametric.begin(); it != parametric.end(); ++it) @@ -95,7 +95,7 @@ void lstm::LSTM::_init_parametric(nlohmann::json& parametric) this->_input_and_params.resize(1 + parametric.size()); // TODO amp parameters } -void lstm::LSTM::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) +void nam::lstm::LSTM::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) { // Get params into the input vector before starting if (this->_stale_params) @@ -109,7 +109,7 @@ void lstm::LSTM::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_fr output[i] = this->_process_sample(input[i]); } -float lstm::LSTM::_process_sample(const float x) +float nam::lstm::LSTM::_process_sample(const float x) { if (this->_layers.size() == 0) return x; diff --git a/NAM/lstm.h b/NAM/lstm.h index 2719a1c..c3b003d 100644 --- a/NAM/lstm.h +++ b/NAM/lstm.h @@ -9,6 +9,8 @@ #include "dsp.h" #include "json.hpp" +namespace nam +{ namespace lstm { // A Single LSTM cell @@ -70,3 +72,4 @@ class LSTM : public DSP Eigen::VectorXf _input_and_params; }; }; // namespace lstm +}; // namespace nam diff --git a/NAM/util.cpp b/NAM/util.cpp index b8ad1ea..93815fc 100644 --- a/NAM/util.cpp +++ b/NAM/util.cpp @@ -3,7 +3,7 @@ #include "util.h" -std::string util::lowercase(const std::string& s) +std::string nam::util::lowercase(const std::string& s) { std::string out(s); std::transform(s.begin(), s.end(), out.begin(), [](unsigned char c) { return std::tolower(c); }); diff --git a/NAM/util.h b/NAM/util.h index b95b48a..c0a5bd4 100644 --- a/NAM/util.h +++ b/NAM/util.h @@ -5,7 +5,10 @@ #include #include // Eigen::MatrixXf +namespace nam +{ namespace util { std::string lowercase(const std::string& s); }; // namespace util +}; // namespace nam diff --git a/NAM/wavenet.cpp b/NAM/wavenet.cpp index bbdbcfe..981938c 100644 --- a/NAM/wavenet.cpp +++ b/NAM/wavenet.cpp @@ -7,22 +7,22 @@ #include "wavenet.h" #include "util.h" -wavenet::_DilatedConv::_DilatedConv(const int in_channels, const int out_channels, const int kernel_size, - const int bias, const int dilation) +nam::wavenet::_DilatedConv::_DilatedConv(const int in_channels, const int out_channels, const int kernel_size, + const int bias, const int dilation) { this->set_size_(in_channels, out_channels, kernel_size, bias, dilation); } -void wavenet::_Layer::set_params_(std::vector::iterator& params) +void nam::wavenet::_Layer::set_params_(std::vector::iterator& params) { this->_conv.set_params_(params); this->_input_mixin.set_params_(params); this->_1x1.set_params_(params); } -void wavenet::_Layer::process_(const Eigen::MatrixXf& input, const Eigen::MatrixXf& condition, - Eigen::MatrixXf& head_input, Eigen::MatrixXf& output, const long i_start, - const long j_start) +void nam::wavenet::_Layer::process_(const Eigen::MatrixXf& input, const Eigen::MatrixXf& condition, + Eigen::MatrixXf& head_input, Eigen::MatrixXf& output, const long i_start, + const long j_start) { const long ncols = condition.cols(); const long channels = this->get_channels(); @@ -47,7 +47,7 @@ void wavenet::_Layer::process_(const Eigen::MatrixXf& input, const Eigen::Matrix output.middleCols(j_start, ncols) = input.middleCols(i_start, ncols) + this->_1x1.process(this->_z.topRows(channels)); } -void wavenet::_Layer::set_num_frames_(const long num_frames) +void nam::wavenet::_Layer::set_num_frames_(const long num_frames) { if (this->_z.rows() == this->_conv.get_out_channels() && this->_z.cols() == num_frames) return; // Already has correct size @@ -60,9 +60,9 @@ void wavenet::_Layer::set_num_frames_(const long num_frames) #define LAYER_ARRAY_BUFFER_SIZE 65536 -wavenet::_LayerArray::_LayerArray(const int input_size, const int condition_size, const int head_size, - const int channels, const int kernel_size, const std::vector& dilations, - const std::string activation, const bool gated, const bool head_bias) +nam::wavenet::_LayerArray::_LayerArray(const int input_size, const int condition_size, const int head_size, + const int channels, const int kernel_size, const std::vector& dilations, + const std::string activation, const bool gated, const bool head_bias) : _rechannel(input_size, channels, false) , _head_rechannel(channels, head_size, head_bias) { @@ -77,12 +77,12 @@ wavenet::_LayerArray::_LayerArray(const int input_size, const int condition_size this->_buffer_start = this->_get_receptive_field() - 1; } -void wavenet::_LayerArray::advance_buffers_(const int num_frames) +void nam::wavenet::_LayerArray::advance_buffers_(const int num_frames) { this->_buffer_start += num_frames; } -long wavenet::_LayerArray::get_receptive_field() const +long nam::wavenet::_LayerArray::get_receptive_field() const { long result = 0; for (size_t i = 0; i < this->_layers.size(); i++) @@ -90,7 +90,7 @@ long wavenet::_LayerArray::get_receptive_field() const return result; } -void wavenet::_LayerArray::prepare_for_frames_(const long num_frames) +void nam::wavenet::_LayerArray::prepare_for_frames_(const long num_frames) { // Example: // _buffer_start = 0 @@ -103,9 +103,9 @@ void wavenet::_LayerArray::prepare_for_frames_(const long num_frames) this->_rewind_buffers_(); } -void wavenet::_LayerArray::process_(const Eigen::MatrixXf& layer_inputs, const Eigen::MatrixXf& condition, - Eigen::MatrixXf& head_inputs, Eigen::MatrixXf& layer_outputs, - Eigen::MatrixXf& head_outputs) +void nam::wavenet::_LayerArray::process_(const Eigen::MatrixXf& layer_inputs, const Eigen::MatrixXf& condition, + Eigen::MatrixXf& head_inputs, Eigen::MatrixXf& layer_outputs, + Eigen::MatrixXf& head_outputs) { this->_layer_buffers[0].middleCols(this->_buffer_start, layer_inputs.cols()) = this->_rechannel.process(layer_inputs); const size_t last_layer = this->_layers.size() - 1; @@ -118,7 +118,7 @@ void wavenet::_LayerArray::process_(const Eigen::MatrixXf& layer_inputs, const E head_outputs = this->_head_rechannel.process(head_inputs); } -void wavenet::_LayerArray::set_num_frames_(const long num_frames) +void nam::wavenet::_LayerArray::set_num_frames_(const long num_frames) { // Wavenet checks for unchanged num_frames; if we made it here, there's // something to do. @@ -134,7 +134,7 @@ void wavenet::_LayerArray::set_num_frames_(const long num_frames) this->_layers[i].set_num_frames_(num_frames); } -void wavenet::_LayerArray::set_params_(std::vector::iterator& params) +void nam::wavenet::_LayerArray::set_params_(std::vector::iterator& params) { this->_rechannel.set_params_(params); for (size_t i = 0; i < this->_layers.size(); i++) @@ -142,12 +142,12 @@ void wavenet::_LayerArray::set_params_(std::vector::iterator& params) this->_head_rechannel.set_params_(params); } -long wavenet::_LayerArray::_get_channels() const +long nam::wavenet::_LayerArray::_get_channels() const { return this->_layers.size() > 0 ? this->_layers[0].get_channels() : 0; } -long wavenet::_LayerArray::_get_receptive_field() const +long nam::wavenet::_LayerArray::_get_receptive_field() const { // TODO remove this and use get_receptive_field() instead! long res = 1; @@ -156,7 +156,7 @@ long wavenet::_LayerArray::_get_receptive_field() const return res; } -void wavenet::_LayerArray::_rewind_buffers_() +void nam::wavenet::_LayerArray::_rewind_buffers_() // Consider wrapping instead... // Can make this smaller--largest dilation, not receptive field! { @@ -171,7 +171,7 @@ void wavenet::_LayerArray::_rewind_buffers_() // Head ======================================================================= -wavenet::_Head::_Head(const int input_size, const int num_layers, const int channels, const std::string activation) +nam::wavenet::_Head::_Head(const int input_size, const int num_layers, const int channels, const std::string activation) : _channels(channels) , _head(num_layers > 0 ? channels : input_size, 1, true) , _activation(activations::Activation::get_activation(activation)) @@ -187,13 +187,13 @@ wavenet::_Head::_Head(const int input_size, const int num_layers, const int chan } } -void wavenet::_Head::set_params_(std::vector::iterator& params) +void nam::wavenet::_Head::set_params_(std::vector::iterator& params) { for (size_t i = 0; i < this->_layers.size(); i++) this->_layers[i].set_params_(params); } -void wavenet::_Head::process_(Eigen::MatrixXf& inputs, Eigen::MatrixXf& outputs) +void nam::wavenet::_Head::process_(Eigen::MatrixXf& inputs, Eigen::MatrixXf& outputs) { const size_t num_layers = this->_layers.size(); this->_apply_activation_(inputs); @@ -213,7 +213,7 @@ void wavenet::_Head::process_(Eigen::MatrixXf& inputs, Eigen::MatrixXf& outputs) } } -void wavenet::_Head::set_num_frames_(const long num_frames) +void nam::wavenet::_Head::set_num_frames_(const long num_frames) { for (size_t i = 0; i < this->_buffers.size(); i++) { @@ -224,16 +224,16 @@ void wavenet::_Head::set_num_frames_(const long num_frames) } } -void wavenet::_Head::_apply_activation_(Eigen::MatrixXf& x) +void nam::wavenet::_Head::_apply_activation_(Eigen::MatrixXf& x) { this->_activation->apply(x); } // WaveNet ==================================================================== -wavenet::WaveNet::WaveNet(const std::vector& layer_array_params, const float head_scale, - const bool with_head, nlohmann::json parametric, std::vector params, - const double expected_sample_rate) +nam::wavenet::WaveNet::WaveNet(const std::vector& layer_array_params, + const float head_scale, const bool with_head, nlohmann::json parametric, + std::vector params, const double expected_sample_rate) : DSP(expected_sample_rate) , _num_frames(0) , _head_scale(head_scale) @@ -243,7 +243,7 @@ wavenet::WaveNet::WaveNet(const std::vector& layer_ar this->_init_parametric_(parametric); for (size_t i = 0; i < layer_array_params.size(); i++) { - this->_layer_arrays.push_back(wavenet::_LayerArray( + this->_layer_arrays.push_back(nam::wavenet::_LayerArray( layer_array_params[i].input_size, layer_array_params[i].condition_size, layer_array_params[i].head_size, layer_array_params[i].channels, layer_array_params[i].kernel_size, layer_array_params[i].dilations, layer_array_params[i].activation, layer_array_params[i].gated, layer_array_params[i].head_bias)); @@ -268,13 +268,13 @@ wavenet::WaveNet::WaveNet(const std::vector& layer_ar _prewarm_samples += this->_layer_arrays[i].get_receptive_field(); } -void wavenet::WaveNet::finalize_(const int num_frames) +void nam::wavenet::WaveNet::finalize_(const int num_frames) { this->DSP::finalize_(num_frames); this->_advance_buffers_(num_frames); } -void wavenet::WaveNet::set_params_(std::vector& params) +void nam::wavenet::WaveNet::set_params_(std::vector& params) { std::vector::iterator it = params.begin(); for (size_t i = 0; i < this->_layer_arrays.size(); i++) @@ -295,13 +295,13 @@ void wavenet::WaveNet::set_params_(std::vector& params) } } -void wavenet::WaveNet::_advance_buffers_(const int num_frames) +void nam::wavenet::WaveNet::_advance_buffers_(const int num_frames) { for (size_t i = 0; i < this->_layer_arrays.size(); i++) this->_layer_arrays[i].advance_buffers_(num_frames); } -void wavenet::WaveNet::_init_parametric_(nlohmann::json& parametric) +void nam::wavenet::WaveNet::_init_parametric_(nlohmann::json& parametric) { for (nlohmann::json::iterator it = parametric.begin(); it != parametric.end(); ++it) this->_param_names.push_back(it.key()); @@ -309,13 +309,13 @@ void wavenet::WaveNet::_init_parametric_(nlohmann::json& parametric) std::sort(this->_param_names.begin(), this->_param_names.end()); } -void wavenet::WaveNet::_prepare_for_frames_(const long num_frames) +void nam::wavenet::WaveNet::_prepare_for_frames_(const long num_frames) { for (size_t i = 0; i < this->_layer_arrays.size(); i++) this->_layer_arrays[i].prepare_for_frames_(num_frames); } -void wavenet::WaveNet::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) +void nam::wavenet::WaveNet::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int num_frames) { this->_set_num_frames_(num_frames); this->_prepare_for_frames_(num_frames); @@ -355,7 +355,7 @@ void wavenet::WaveNet::process(NAM_SAMPLE* input, NAM_SAMPLE* output, const int } } -void wavenet::WaveNet::_set_num_frames_(const long num_frames) +void nam::wavenet::WaveNet::_set_num_frames_(const long num_frames) { if (num_frames == this->_num_frames) return; diff --git a/NAM/wavenet.h b/NAM/wavenet.h index 981acb2..e1cb63f 100644 --- a/NAM/wavenet.h +++ b/NAM/wavenet.h @@ -8,6 +8,8 @@ #include "dsp.h" +namespace nam +{ namespace wavenet { // Rework the initialization API slightly. Merge w/ dsp.h later. @@ -205,3 +207,4 @@ class WaveNet : public DSP void _set_num_frames_(const long num_frames); }; }; // namespace wavenet +}; // namespace nam