Skip to content

Commit

Permalink
More efficient pre-warming using multiple-sample buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
sdatkinson committed Sep 8, 2024
1 parent 856fbb9 commit 4c8fea7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 14 additions & 6 deletions NAM/dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ void nam::DSP::prewarm()
if (prewarmSamples == 0)
return;

NAM_SAMPLE sample = 0;
NAM_SAMPLE* sample_ptr = &sample;
const size_t bufferSize = std::max(mMaxBufferSize, 1);
std::vector<NAM_SAMPLE> inputBuffer, outputBuffer;
inputBuffer.resize(bufferSize);
outputBuffer.resize(bufferSize);
for (auto it = inputBuffer.begin(); it != inputBuffer.end(); ++it)
{
(*it) = (NAM_SAMPLE)0.0;
}

// pre-warm the model for a model-specific number of samples
for (long i = 0; i < prewarmSamples; i++)
NAM_SAMPLE* inputPtr = inputBuffer.data();
NAM_SAMPLE* outputPtr = outputBuffer.data();
int samplesProcessed = 0;
while (samplesProcessed < prewarmSamples)
{
this->process(sample_ptr, sample_ptr, 1);
sample = 0;
this->process(inputPtr, outputPtr, bufferSize);
samplesProcessed += bufferSize;
}
}

Expand Down
8 changes: 4 additions & 4 deletions tools/benchmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ int main(int argc, char* argv[])
exit(1);
}

auto t1 = high_resolution_clock::now();

size_t bufferSize = AUDIO_BUFFER_SIZE;
model->Reset(model->GetExpectedSampleRate(), bufferSize);
Expand All @@ -49,17 +48,18 @@ int main(int argc, char* argv[])
{
inputBuffer[i] = 0.0;
}
// Run once outside to trigger any pre-allocation
model->process(inputBuffer, outputBuffer, AUDIO_BUFFER_SIZE);

std::cout << "Running benchmark\n";

auto t1 = high_resolution_clock::now();
for (size_t i = 0; i < numBuffers; i++)
{
model->process(inputBuffer, outputBuffer, AUDIO_BUFFER_SIZE);
}

auto t2 = high_resolution_clock::now();
std::cout << "Finished\n";

auto t2 = high_resolution_clock::now();

/* Getting number of milliseconds as an integer. */
auto ms_int = duration_cast<milliseconds>(t2 - t1);
Expand Down

0 comments on commit 4c8fea7

Please sign in to comment.