From c21124cffa63b4bb86a66de51499a2b9da77332e Mon Sep 17 00:00:00 2001 From: HappyDOGE <28511119+HappyDOGE@users.noreply.github.com> Date: Tue, 4 Jan 2022 16:56:03 +0300 Subject: fix typo and improve performance --- NorthstarDedicatedTest/audio.cpp | 28 +++++++++++++++++++--------- NorthstarDedicatedTest/audio.h | 2 +- 2 files changed, 20 insertions(+), 10 deletions(-) (limited to 'NorthstarDedicatedTest') diff --git a/NorthstarDedicatedTest/audio.cpp b/NorthstarDedicatedTest/audio.cpp index 6559a12c..5aa7354f 100644 --- a/NorthstarDedicatedTest/audio.cpp +++ b/NorthstarDedicatedTest/audio.cpp @@ -173,8 +173,18 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa continue; } - // Read file into a vector and add it to the samples list. - Samples.push_back(std::vector((std::istreambuf_iterator(wavStream)), std::istreambuf_iterator())); + // Get file size. + wavStream.seekg(0, std::ios::end); + size_t fileSize = wavStream.tellg(); + wavStream.seekg(0, std::ios::beg); + + // Allocate enough memory for the file. + uint8_t* data = new uint8_t[fileSize]; + + // Read the file. + wavStream.read(data, fileSize); + + Samples.push_back({ fileSize, std::unique_ptr(data) }); // Close the file. wavStream.close(); @@ -292,7 +302,7 @@ bool ShouldPlayAudioEvent(const char* eventName, const std::shared_ptr* vec = NULL; + std::pair>* dat = NULL; switch (overrideData->Strategy) { case AudioSelectionStrategy::RANDOM: - vec = &*select_randomly(overrideData->Samples.begin(), overrideData->Samples.end()); + dat = &*select_randomly(overrideData->Samples.begin(), overrideData->Samples.end()); break; case AudioSelectionStrategy::SEQUENTIAL: default: - vec = &overrideData->Samples[overrideData->CurrentIndex++]; + dat = &overrideData->Samples[overrideData->CurrentIndex++]; if (overrideData->CurrentIndex >= overrideData->Samples.size()) overrideData->CurrentIndex = 0; // reset back to the first sample entry break; } - if (!vec) + if (!dat) spdlog::warn("Could not get sample data from override struct for event {}! Shouldn't happen", eventName); else { - data = vec->data(); - dataLength = vec->size(); + data = dat->second.get(); + dataLength = dat->first; } } diff --git a/NorthstarDedicatedTest/audio.h b/NorthstarDedicatedTest/audio.h index 67fe342f..d53f5317 100644 --- a/NorthstarDedicatedTest/audio.h +++ b/NorthstarDedicatedTest/audio.h @@ -24,7 +24,7 @@ public: std::vector EventIds = {}; std::vector> EventIdsRegex = {}; - std::vector> Samples = {}; + std::vector>> Samples = {}; AudioSelectionStrategy Strategy = AudioSelectionStrategy::SEQUENTIAL; size_t CurrentIndex = 0; -- cgit v1.2.3