From 99e628a5ab8f31dfdc2e6dd67f51ebf319bcf589 Mon Sep 17 00:00:00 2001 From: sakertooth Date: Tue, 26 Sep 2023 22:07:13 -0400 Subject: [PATCH] Use array specialization for unique_ptr when managing DrumSynth data Also made it so that we don't create result before checking if we failed to decode the file, potentially saving us an allocation. --- src/core/SampleBuffer.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index 1f5000cc2..54b335cef 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -122,20 +122,21 @@ void SampleBuffer::decodeSampleSF(const QString& audioFile) void SampleBuffer::decodeSampleDS(const QString& audioFile) { - auto data = std::unique_ptr{}; + // Populated by DrumSynth::GetDSFileSamples int_sample_t* dataPtr = nullptr; auto ds = DrumSynth{}; const auto engineRate = Engine::audioEngine()->processingSampleRate(); const auto frames = ds.GetDSFileSamples(audioFile, dataPtr, DEFAULT_CHANNELS, engineRate); - data.reset(dataPtr); + const auto data = std::unique_ptr{dataPtr}; // NOLINT, we have to use a C-style array here + + if (frames <= 0 || !data) + { + throw std::runtime_error{"Decoding failure: failed to decode DrumSynth file."}; + } auto result = std::vector(frames); - if (frames > 0 && data != nullptr) - { - src_short_to_float_array(data.get(), &result[0][0], frames * DEFAULT_CHANNELS); - } - else { throw std::runtime_error{"Decoding failure: failed to decode DrumSynth file."}; } + src_short_to_float_array(data.get(), &result[0][0], frames * DEFAULT_CHANNELS); m_data = std::move(result); m_audioFile = audioFile;