From 2b86429f762d8d977ceb4d610189e7c01fc52ee8 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Mon, 25 Mar 2024 14:42:10 +0100 Subject: Linter fixes for audiocachefile.cc --- src/audio.h | 2 +- src/audiocacheeventhandler.cc | 6 ++-- src/audiocacheeventhandler.h | 2 +- src/audiocachefile.cc | 64 +++++++++++++++++++++---------------------- src/audiocachefile.h | 14 ++++++---- 5 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/audio.h b/src/audio.h index d022147..289f662 100644 --- a/src/audio.h +++ b/src/audio.h @@ -26,4 +26,4 @@ */ #pragma once -#include +#include "audiotypes.h" diff --git a/src/audiocacheeventhandler.cc b/src/audiocacheeventhandler.cc index a0327b5..e5c1bdc 100644 --- a/src/audiocacheeventhandler.cc +++ b/src/audiocacheeventhandler.cc @@ -115,7 +115,7 @@ void AudioCacheEventHandler::unlock() } void AudioCacheEventHandler::pushLoadNextEvent(AudioCacheFile* afile, - size_t channel, + size_t channel_index, size_t pos, sample_t* buffer, volatile bool* ready) { @@ -124,8 +124,8 @@ void AudioCacheEventHandler::pushLoadNextEvent(AudioCacheFile* afile, cache_event.pos = pos; cache_event.afile = afile; - CacheChannel c; - c.channel = channel; + CacheChannel c{}; + c.channel_index = channel_index; c.samples = buffer; *ready = false; diff --git a/src/audiocacheeventhandler.h b/src/audiocacheeventhandler.h index f80b4c8..8de812b 100644 --- a/src/audiocacheeventhandler.h +++ b/src/audiocacheeventhandler.h @@ -68,7 +68,7 @@ public: //! This methods are supplied to make this class lockable by std::lock_guard void unlock(); - void pushLoadNextEvent(AudioCacheFile* afile, size_t channel, + void pushLoadNextEvent(AudioCacheFile* afile, size_t channel_index, size_t pos, sample_t* buffer, volatile bool* ready); void pushCloseEvent(cacheid_t id); diff --git a/src/audiocachefile.cc b/src/audiocachefile.cc index 2c2888c..daf4fc1 100644 --- a/src/audiocachefile.cc +++ b/src/audiocachefile.cc @@ -26,23 +26,19 @@ */ #include "audiocachefile.h" -#include - -#include - +#include #include +#include -#include "audiocache.h" +#include AudioCacheFile::AudioCacheFile(const std::string& filename, std::vector& read_buffer) : filename(filename) , read_buffer(read_buffer) { - std::memset(&sf_info, 0, sizeof(SF_INFO)); - fh = sf_open(filename.c_str(), SFM_READ, &sf_info); - if(!fh) + if(fh == nullptr) { ERR(audiofile,"SNDFILE Error (%s): %s\n", filename.c_str(), sf_strerror(fh)); @@ -57,7 +53,7 @@ AudioCacheFile::AudioCacheFile(const std::string& filename, AudioCacheFile::~AudioCacheFile() { - if(fh) + if(fh != nullptr) { sf_close(fh); fh = nullptr; @@ -80,10 +76,10 @@ size_t AudioCacheFile::getChannelCount() const } void AudioCacheFile::readChunk(const CacheChannels& channels, - size_t pos, size_t num_samples) + std::size_t pos, std::size_t num_samples) { //assert(fh != nullptr); // File handle must never be nullptr - if(!fh) + if(fh == nullptr) { return; } @@ -95,9 +91,9 @@ void AudioCacheFile::readChunk(const CacheChannels& channels, return; } - sf_seek(fh, pos, SEEK_SET); + sf_seek(fh, static_cast(pos), SEEK_SET); - size_t size = sf_info.frames - pos; + auto size = sf_info.frames - pos; if(size > num_samples) { size = num_samples; @@ -108,38 +104,42 @@ void AudioCacheFile::readChunk(const CacheChannels& channels, read_buffer.resize(size * sf_info.channels); } - size_t read_size = sf_readf_float(fh, read_buffer.data(), size); - (void)read_size; + const size_t read_size = sf_readf_float(fh, read_buffer.data(), + static_cast(size)); + assert(read_size == size); - for(auto it = channels.begin(); it != channels.end(); ++it) + for(const auto& channel : channels) { - size_t channel = it->channel; - sample_t* data = it->samples; + auto channel_index = channel.channel_index; + auto* data = channel.samples; for (size_t i = 0; i < size; ++i) { - data[i] = read_buffer[(i * sf_info.channels) + channel]; + data[i] = read_buffer[(i * sf_info.channels) + channel_index]; // NOLINT: will be fixed with std::span } } - for(auto it = channels.begin(); it != channels.end(); ++it) + for(const auto & channel : channels) { - *(it->ready) = true; + *(channel.ready) = true; } } AudioCacheFile& AudioCacheFiles::getFile(const std::string& filename) { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); - auto it = audiofiles.find(filename); - if(it == audiofiles.end()) + auto audiofile_it = audiofiles.find(filename); + if(audiofile_it == audiofiles.end()) { // Construct a new AudioCacheFile in place. The in place construction is relevant. - it = audiofiles.emplace(std::piecewise_construct, std::make_tuple(filename), - std::make_tuple(filename, std::ref(read_buffer))).first; + audiofile_it = + audiofiles.emplace(std::piecewise_construct, + std::make_tuple(filename), + std::make_tuple(filename, + std::ref(read_buffer))).first; // FIXME: This must be possible to do in a more easy-to-read way! } - auto& cache_audio_file = it->second; + auto& cache_audio_file = audiofile_it->second; // Increase ref count. ++cache_audio_file.ref; @@ -149,22 +149,22 @@ AudioCacheFile& AudioCacheFiles::getFile(const std::string& filename) void AudioCacheFiles::releaseFile(const std::string& filename) { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); - auto it = audiofiles.find(filename); - if(it == audiofiles.end()) + auto audiofile_it = audiofiles.find(filename); + if(audiofile_it == audiofiles.end()) { assert(false); // This should never happen! return; // not open } - auto& audiofile = it->second; + auto& audiofile = audiofile_it->second; assert(audiofile.ref); // If ref is not > 0 it shouldn't be in the map. --audiofile.ref; if(audiofile.ref == 0) { - audiofiles.erase(it); + audiofiles.erase(audiofile_it); } } diff --git a/src/audiocachefile.h b/src/audiocachefile.h index 66a6529..63170cf 100644 --- a/src/audiocachefile.h +++ b/src/audiocachefile.h @@ -31,16 +31,17 @@ #include #include #include +#include #include -#include +#include "audiotypes.h" //! Channel data container in the cache world. class CacheChannel { public: - size_t channel; //< Channel number + size_t channel_index; //< Channel number sample_t* samples; //< Sample buffer pointer. size_t num_samples; //< Number of samples in the sample buffer volatile bool* ready; //< Is set to true when the loading is done. @@ -72,12 +73,13 @@ public: size_t getChannelCount() const; //! Read audio data from the file into the supplied channel caches. - void readChunk(const CacheChannels& channels, size_t pos, size_t num_samples); + void readChunk(const CacheChannels& channels, std::size_t pos, + std::size_t num_samples); private: - int ref{0}; - SNDFILE* fh{nullptr}; - SF_INFO sf_info; + int ref{}; + SNDFILE* fh{}; + SF_INFO sf_info{}; std::string filename; std::vector& read_buffer; }; -- cgit v1.2.3