Skip to content
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
37 changes: 37 additions & 0 deletions NAM/linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,43 @@ nam::Linear::Linear(const int in_channels, const int out_channels, const int rec

nam::Linear::~Linear() = default;

void nam::Linear::prewarm()
{
if (mMaxBufferSize == 0)
SetMaxBufferSize(NAM_DEFAULT_MAX_BUFFER_SIZE);

for (auto& input : this->_input_buffers)
std::fill(input.begin(), input.end(), 0.0f);
for (auto& output : this->_output_buffers)
std::fill(output.begin(), output.end(), 0.0f);
this->_reset_input_buffer();

if (this->_fft_state == nullptr)
return;

auto& state = *this->_fft_state;
state.sample_index = 0;
for (auto& channel : state.output_channels)
std::fill(channel.output_ring.begin(), channel.output_ring.end(), 0.0f);
for (auto& tier : state.tiers)
for (auto& channel : tier.channels)
{
std::fill(channel.input_time.begin(), channel.input_time.end(), 0.0f);
for (auto& spectrum : channel.input_spectra)
std::fill(spectrum.begin(), spectrum.end(), LinearFFTState::Complex{});
std::fill(channel.accumulator.begin(), channel.accumulator.end(), LinearFFTState::Complex{});
std::fill(channel.ifft_time.begin(), channel.ifft_time.end(), 0.0f);
// Restore the staggered tier phases used by a newly initialized model.
channel.input_pos = tier.runs_inline ? 0 : tier.block_size / 2;
channel.spectrum_write_index = 0;
channel.job_spectrum_write_index = 0;
channel.job_work_index = 0;
channel.job_ticks_remaining = 0;
channel.job_block_start = 0;
channel.job_active = false;
}
}

bool nam::Linear::SupportsArbitrarySampleRate()
{
const double training_rate = GetExpectedSampleRate();
Expand Down
9 changes: 9 additions & 0 deletions NAM/linear.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ class Linear : public Buffer

~Linear() override;

/// \brief Get the receptive field in samples at the current processing rate
int GetPrewarmSamples() override { return this->_receptive_field; }

/// \brief Clear convolution history directly, without processing silence
///
/// Preserves coefficients and FFT plans. May allocate if no maximum buffer
/// size has been configured yet.
void prewarm() override;

/// \brief Whether the training sample rate is known, finite, and positive
bool SupportsArbitrarySampleRate() override;

Expand Down
3 changes: 3 additions & 0 deletions tools/run_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ int main()
test_linear::test_channel_mappings();
test_linear::test_channel_validation();
test_linear::test_channel_mapping_in_place();
test_linear::test_prewarm_samples_tracks_receptive_field();
test_linear::test_prewarm_before_reset();
test_linear::test_prewarm_clears_convolution_state();
test_linear::test_arbitrary_sample_rate_capability();
test_linear::test_sample_rate_known_values();
test_linear::test_sample_rate_fractional_and_fft();
Expand Down
125 changes: 125 additions & 0 deletions tools/test/test_linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ namespace test_linear
namespace
{

class ProcessCountingLinear : public nam::Linear
{
public:
using nam::Linear::Linear;

void process(NAM_SAMPLE** input, NAM_SAMPLE** output, const int num_frames) override
{
++process_calls;
nam::Linear::process(input, output, num_frames);
}

int process_calls = 0;
};

std::vector<NAM_SAMPLE> process_model(nam::Linear& model, const std::vector<NAM_SAMPLE>& input,
const std::vector<int>& chunk_sizes)
{
Expand Down Expand Up @@ -100,6 +114,117 @@ void assert_process_realtime_safe(const int receptive_field, const nam::LinearIm

} // namespace

void test_prewarm_samples_tracks_receptive_field()
{
for (const auto implementation : {nam::LinearImplementation::Direct, nam::LinearImplementation::FFT})
{
ProcessCountingLinear linear(1, 1, 257, false, make_weights(257, false), 48000.0, implementation);
nam::DSP& model = linear;
assert(model.GetPrewarmSamples() == 257);
for (const double rate : {96000.0, 44100.0, 24000.0, 48000.0})
{
model.Reset(rate, 127);
assert(model.GetPrewarmSamples() == (int)std::ceil(257 * rate / 48000.0));
assert(linear.process_calls == 0);
}

nam::Linear unknown(1, 1, 257, false, make_weights(257, false), -1.0, implementation);
unknown.Reset(96000.0, 127);
assert(unknown.GetPrewarmSamples() == 257);
}
}

void test_prewarm_before_reset()
{
for (const auto implementation : {nam::LinearImplementation::Direct, nam::LinearImplementation::FFT})
{
const int taps = 1536;
const auto weights = make_weights(taps, true);
ProcessCountingLinear model(1, 1, taps, true, weights, 48000.0, implementation);
nam::DSP& dsp = model;
dsp.prewarm();
assert(model.GetMaxBufferSize() == NAM_DEFAULT_MAX_BUFFER_SIZE);
assert(model.process_calls == 0);
std::vector<NAM_SAMPLE> input(taps + 256, 0.0);
input[0] = 1.0;
const auto output = process_model(model, input, {1, 7, 31});
for (size_t i = 0; i < output.size(); ++i)
assert_near(output[i], weights.back() + (i < (size_t)taps ? weights[i] : 0.0), 1.0e-6);
}
}

void test_prewarm_clears_convolution_state()
{
for (const auto implementation : {nam::LinearImplementation::Direct, nam::LinearImplementation::FFT})
for (const auto shape : {std::pair<int, int>{1, 1}, {2, 2}, {1, 2}, {2, 1}})
{
const int taps = implementation == nam::LinearImplementation::Direct ? 17 : 4097;
const int inputs = shape.first, outputs = shape.second;
const int paths = std::max(inputs, outputs);
const int kernels = inputs == outputs ? 1 : paths;
std::vector<float> weights(taps * kernels, 0.0f);
for (int k = 0; k < kernels; ++k)
{
weights[k * taps] = 0.25f * (k + 1);
weights[k * taps + taps / 2] = -0.125f * (k + 1);
weights[k * taps + taps - 1] = 0.5f * (k + 1);
}
for (int ch = 0; ch < (inputs == outputs ? 1 : outputs); ++ch)
weights.push_back(0.0625f * (ch + 1));
ProcessCountingLinear model(inputs, outputs, taps, true, weights, 48000.0, implementation);
model.Reset(48000.0, 257);
std::vector<std::vector<NAM_SAMPLE>> input(inputs, std::vector<NAM_SAMPLE>(257));
std::vector<std::vector<NAM_SAMPLE>> output(outputs, std::vector<NAM_SAMPLE>(257));
std::vector<NAM_SAMPLE*> input_ptrs(inputs), output_ptrs(outputs);
for (int ch = 0; ch < inputs; ++ch)
input_ptrs[ch] = input[ch].data();
for (int ch = 0; ch < outputs; ++ch)
output_ptrs[ch] = output[ch].data();

for (const int dirty_samples : {1733, 8271})
{
for (int ch = 0; ch < inputs; ++ch)
std::fill(input[ch].begin(), input[ch].end(), ch + 1.0);
for (int offset = 0; offset < dirty_samples; offset += 257)
model.process(input_ptrs.data(), output_ptrs.data(), std::min(257, dirty_samples - offset));

const int process_calls = model.process_calls;
nam::DSP& dsp = model;
allocation_tracking::run_allocation_test_no_allocations(
nullptr, [&]() { dsp.prewarm(); }, nullptr, "Linear prewarm clears state without allocations");
assert(model.process_calls == process_calls);
assert(model.GetPrewarmSamples() == taps);

// A new impulse must reproduce only the original coefficients and bias,
// with no old input history, queued FFT work, or overlap-add output.
for (int ch = 0; ch < inputs; ++ch)
{
std::fill(input[ch].begin(), input[ch].end(), 0.0);
input[ch][0] = ch + 1.0;
}
const int chunks[] = {1, 7, 127, 257, 31};
for (int offset = 0, chunk = 0; offset < taps + 257; ++chunk)
{
const int count = std::min(chunks[chunk % 5], taps + 257 - offset);
model.process(input_ptrs.data(), output_ptrs.data(), count);
for (int ch = 0; ch < outputs; ++ch)
for (int i = 0; i < count; ++i)
{
double expected = 0.0625 * (inputs == outputs ? 1 : ch + 1);
for (int path = 0; path < paths; ++path)
if ((outputs == 1 ? 0 : path) == ch && offset + i < taps)
expected += (inputs == 1 ? 1.0 : path + 1.0)
* weights[(kernels == 1 ? 0 : path) * taps + offset + i];
assert_near(output[ch][i], expected, 1.0e-6);
}
for (auto& channel : input)
channel[0] = 0.0;
offset += count;
}
}
}
}

void test_direct_known_values()
{
const std::vector<float> weights{0.5f, -0.25f, 0.125f};
Expand Down
Loading