From 2f1a777c5f5796232bc70512c08a44a3821ed953 Mon Sep 17 00:00:00 2001 From: Steven Atkinson Date: Thu, 15 Jun 2023 19:51:12 -0700 Subject: [PATCH] Fix memory issues --- NAM/get_dsp.cpp | 6 ++--- dsp/ImpulseResponse.cpp | 4 ++-- dsp/wav.cpp | 20 +++++----------- tools/benchmodel.cpp | 52 ++++++++++++++++++++--------------------- 4 files changed, 37 insertions(+), 45 deletions(-) diff --git a/NAM/get_dsp.cpp b/NAM/get_dsp.cpp index f2791cc..fa447f8 100644 --- a/NAM/get_dsp.cpp +++ b/NAM/get_dsp.cpp @@ -123,9 +123,9 @@ std::unique_ptr get_dsp(dspData& conf) { verify_config_version(conf.version); - auto &architecture = conf.architecture; - nlohmann::json &config = conf.config; - std::vector ¶ms = conf.params; + auto& architecture = conf.architecture; + nlohmann::json& config = conf.config; + std::vector& params = conf.params; bool haveLoudness = false; double loudness = TARGET_DSP_LOUDNESS; diff --git a/dsp/ImpulseResponse.cpp b/dsp/ImpulseResponse.cpp index 9756aa3..b23d236 100644 --- a/dsp/ImpulseResponse.cpp +++ b/dsp/ImpulseResponse.cpp @@ -49,7 +49,7 @@ void dsp::ImpulseResponse::_SetWeights(const double sampleRate) if (this->mRawAudioSampleRate == sampleRate) { this->mResampled.resize(this->mRawAudio.size()); - memcpy(this->mResampled.data(), this->mRawAudio.data(), this->mResampled.size()); + memcpy(this->mResampled.data(), this->mRawAudio.data(), sizeof(float) * this->mResampled.size()); } else { @@ -58,7 +58,7 @@ void dsp::ImpulseResponse::_SetWeights(const double sampleRate) padded.resize(this->mRawAudio.size() + 2); padded[0] = 0.0f; padded[padded.size() - 1] = 0.0f; - memcpy(padded.data() + 1, this->mRawAudio.data(), this->mRawAudio.size()); + memcpy(padded.data() + 1, this->mRawAudio.data(), sizeof(float) * this->mRawAudio.size()); dsp::ResampleCubic(padded, this->mRawAudioSampleRate, sampleRate, 0.0, this->mResampled); } // Simple implementation w/ no resample... diff --git a/dsp/wav.cpp b/dsp/wav.cpp index 6c6ed47..864ca12 100644 --- a/dsp/wav.cpp +++ b/dsp/wav.cpp @@ -47,31 +47,23 @@ std::string dsp::wav::GetMsgForLoadReturnCode(LoadReturnCode retCode) { case (LoadReturnCode::ERROR_OPENING): message << "Failed to open file (is it being used by another " - "program?)"; + "program?)"; break; case (LoadReturnCode::ERROR_NOT_RIFF): message << "File is not a WAV file."; break; case (LoadReturnCode::ERROR_NOT_WAVE): message << "File is not a WAV file."; break; - case (LoadReturnCode::ERROR_MISSING_FMT): - message << "File is missing expected format chunk."; - break; + case (LoadReturnCode::ERROR_MISSING_FMT): message << "File is missing expected format chunk."; break; case (LoadReturnCode::ERROR_INVALID_FILE): message << "WAV file contents are invalid."; break; - case (LoadReturnCode::ERROR_UNSUPPORTED_FORMAT_ALAW): - message << "Unsupported file format \"A-law\""; - break; - case (LoadReturnCode::ERROR_UNSUPPORTED_FORMAT_MULAW): - message << "Unsupported file format \"mu-law\""; - break; + case (LoadReturnCode::ERROR_UNSUPPORTED_FORMAT_ALAW): message << "Unsupported file format \"A-law\""; break; + case (LoadReturnCode::ERROR_UNSUPPORTED_FORMAT_MULAW): message << "Unsupported file format \"mu-law\""; break; case (LoadReturnCode::ERROR_UNSUPPORTED_FORMAT_EXTENSIBLE): message << "Unsupported file format \"extensible\""; break; case (LoadReturnCode::ERROR_NOT_MONO): message << "File is not mono."; break; - case (LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE): - message << "Unsupported bits per sample"; - break; + case (LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE): message << "Unsupported bits per sample"; break; case (dsp::wav::LoadReturnCode::ERROR_OTHER): message << "???"; break; default: message << "???"; break; } - + return message.str(); } diff --git a/tools/benchmodel.cpp b/tools/benchmodel.cpp index 186ccf9..6d8698d 100644 --- a/tools/benchmodel.cpp +++ b/tools/benchmodel.cpp @@ -4,9 +4,9 @@ #include "NAM/dsp.h" -using std::chrono::high_resolution_clock; -using std::chrono::duration_cast; using std::chrono::duration; +using std::chrono::duration_cast; +using std::chrono::high_resolution_clock; using std::chrono::milliseconds; #define AUDIO_BUFFER_SIZE 64 @@ -31,41 +31,41 @@ int main(int argc, char* argv[]) std::unique_ptr model; - model.reset(); - model = std::move(get_dsp(modelPath)); + model.reset(); + model = std::move(get_dsp(modelPath)); - if (model == nullptr) - { - std::cerr << "Failed to load model\n"; + if (model == nullptr) + { + std::cerr << "Failed to load model\n"; - exit(1); - } + exit(1); + } - auto t1 = high_resolution_clock::now(); + auto t1 = high_resolution_clock::now(); - size_t bufferSize = 64; - size_t numBuffers = (48000 / 64) * 2; + size_t bufferSize = 64; + size_t numBuffers = (48000 / 64) * 2; - std::cout << "Running benchmark\n"; + std::cout << "Running benchmark\n"; - for (size_t i = 0; i < numBuffers; i++) - { - model->process(buffers, buffers, 1, AUDIO_BUFFER_SIZE, 1.0, 1.0, mNAMParams); - model->finalize_(AUDIO_BUFFER_SIZE); - } + for (size_t i = 0; i < numBuffers; i++) + { + model->process(buffers, buffers, 1, AUDIO_BUFFER_SIZE, 1.0, 1.0, mNAMParams); + model->finalize_(AUDIO_BUFFER_SIZE); + } - std::cout << "Finished\n"; + std::cout << "Finished\n"; - auto t2 = high_resolution_clock::now(); + auto t2 = high_resolution_clock::now(); - /* Getting number of milliseconds as an integer. */ - auto ms_int = duration_cast(t2 - t1); + /* Getting number of milliseconds as an integer. */ + auto ms_int = duration_cast(t2 - t1); - /* Getting number of milliseconds as a double. */ - duration ms_double = t2 - t1; + /* Getting number of milliseconds as a double. */ + duration ms_double = t2 - t1; - std::cout << ms_int.count() << "ms\n"; - std::cout << ms_double.count() << "ms\n"; + std::cout << ms_int.count() << "ms\n"; + std::cout << ms_double.count() << "ms\n"; } else {