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

Allow NAM_SAMPLE_FLOAT to switch model input to float instead of double #48

Merged
merged 2 commits into from
Jun 23, 2023
Merged
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
9 changes: 5 additions & 4 deletions NAM/dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ DSP::DSP(const double loudness)
{
}

void DSP::process(double** inputs, double** outputs, const int num_channels, const int num_frames,
void DSP::process(NAM_SAMPLE** inputs, NAM_SAMPLE** outputs, const int num_channels, const int num_frames,
const double input_gain, const double output_gain,
const std::unordered_map<std::string, double>& params)
{
Expand Down Expand Up @@ -58,7 +58,7 @@ void DSP::_get_params_(const std::unordered_map<std::string, double>& input_para
}
}

void DSP::_apply_input_level_(double** inputs, const int num_channels, const int num_frames, const double gain)
void DSP::_apply_input_level_(NAM_SAMPLE** inputs, const int num_channels, const int num_frames, const double gain)
{
// Must match exactly; we're going to use the size of _input_post_gain later
// for num_frames.
Expand All @@ -83,13 +83,14 @@ void DSP::_process_core_()
this->_core_dsp_output[i] = this->_input_post_gain[i];
}

void DSP::_apply_output_level_(double** outputs, const int num_channels, const int num_frames, const double gain)
void DSP::_apply_output_level_(NAM_SAMPLE** outputs, const int num_channels, const int num_frames,
const double gain)
{
const double loudnessGain = pow(10.0, -(this->mLoudness - TARGET_DSP_LOUDNESS) / 20.0);
const double finalGain = this->mNormalizeOutputLoudness ? gain * loudnessGain : gain;
for (int c = 0; c < num_channels; c++)
for (int s = 0; s < num_frames; s++)
outputs[c][s] = double(finalGain * this->_core_dsp_output[s]);
outputs[c][s] = (NAM_SAMPLE)(finalGain * this->_core_dsp_output[s]);
}

// Buffer =====================================================================
Expand Down
12 changes: 9 additions & 3 deletions NAM/dsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
#include "activations.h"
#include "json.hpp"

#ifdef NAM_SAMPLE_FLOAT
#define NAM_SAMPLE float
#else
#define NAM_SAMPLE double
#endif

enum EArchitectures
{
kLinear = 0,
Expand Down Expand Up @@ -51,7 +57,7 @@ class DSP
// 3. The core DSP algorithm is run (This is what should probably be
// overridden in subclasses).
// 4. The output level is applied and the result stored to `output`.
virtual void process(double** inputs, double** outputs, const int num_channels, const int num_frames,
virtual void process(NAM_SAMPLE** inputs, NAM_SAMPLE** outputs, const int num_channels, const int num_frames,
const double input_gain, const double output_gain,
const std::unordered_map<std::string, double>& params);
// Anything to take care of before next buffer comes in.
Expand Down Expand Up @@ -87,7 +93,7 @@ class DSP

// Apply the input gain
// Result populates this->_input_post_gain
void _apply_input_level_(double** inputs, const int num_channels, const int num_frames, const double gain);
void _apply_input_level_(NAM_SAMPLE** inputs, const int num_channels, const int num_frames, const double gain);

// i.e. ensure the size is correct.
void _ensure_core_dsp_output_ready_();
Expand All @@ -98,7 +104,7 @@ class DSP
virtual void _process_core_();

// Copy this->_core_dsp_output to output and apply the output volume
void _apply_output_level_(double** outputs, const int num_channels, const int num_frames, const double gain);
void _apply_output_level_(NAM_SAMPLE** outputs, const int num_channels, const int num_frames, const double gain);
};

// Class where an input buffer is kept so that long-time effects can be
Expand Down