From 7999bd2e087d5ee696899f1383c187b7e787069a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Fri, 29 Jan 2016 15:03:58 +0100 Subject: wip --- src/audiocachefile.cc | 25 ++++++++++--------------- src/audiocachefile.h | 7 +++++-- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/audiocachefile.cc b/src/audiocachefile.cc index 916ecb7..3d566e7 100644 --- a/src/audiocachefile.cc +++ b/src/audiocachefile.cc @@ -34,10 +34,11 @@ #include "audiocache.h" -AudioCacheFile::AudioCacheFile(const std::string& filename) - : filename(filename) +AudioCacheFile::AudioCacheFile(const std::string& filename, + std::vector& read_buffer) + : filename(filename), read_buffer(read_buffer) { - std::memset(&sf_info, 0, sizeof(SF_INFO)); + std::memset(&sf_info, 0, sizeof(SF_INFO)); // XXX Is this really necessary? fh = sf_open(filename.c_str(), SFM_READ, &sf_info); if(!fh) @@ -49,7 +50,7 @@ AudioCacheFile::AudioCacheFile(const std::string& filename) if(sf_info.frames == 0) { - printf("sf_info.frames == 0\n"); + printf("sf_info.frames == 0\n"); // XXX Shouldn't that be debug output? } } @@ -101,24 +102,18 @@ void AudioCacheFile::readChunk(const CacheChannels& channels, size = num_samples; } - static sample_t *read_buffer = nullptr; - static size_t read_buffer_size = 0; - - if((size * sf_info.channels) > read_buffer_size) + if((size * sf_info.channels) > read_buffer.size()) { - delete[] read_buffer; - read_buffer_size = size * sf_info.channels; - read_buffer = new sample_t[read_buffer_size]; - // TODO: This buffer is never free'd on app shutdown. + read_buffer.resize(size * sf_info.channels); } - size_t read_size = sf_readf_float(fh, read_buffer, size); + size_t read_size = sf_readf_float(fh, read_buffer.data(), size); (void)read_size; for(auto it = channels.begin(); it != channels.end(); ++it) { size_t channel = it->channel; - sample_t *data = it->samples; + sample_t* data = it->samples; for (size_t i = 0; i < size; ++i) { data[i] = read_buffer[(i * sf_info.channels) + channel]; @@ -140,7 +135,7 @@ AudioCacheFile& AudioCacheFiles::getFile(const std::string& filename) auto it = audiofiles.find(filename); if(it == audiofiles.end()) { - cacheAudioFile = new AudioCacheFile(filename); + cacheAudioFile = new AudioCacheFile(filename, read_buffer); // XXX why create a pointer and not move it into the map such that it is the owner? audiofiles.insert(std::make_pair(filename, cacheAudioFile)); } else diff --git a/src/audiocachefile.h b/src/audiocachefile.h index 9910563..8e40a2e 100644 --- a/src/audiocachefile.h +++ b/src/audiocachefile.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include "mutex.h" @@ -43,7 +44,7 @@ public: size_t channel; //< 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 tru when the loading is done. + volatile bool* ready; //< Is set to true when the loading is done. }; using CacheChannels = std::list; @@ -56,7 +57,7 @@ class AudioCacheFile { friend class TestableAudioCacheFiles; public: //! Create file handle for filename. - AudioCacheFile(const std::string& filename); + AudioCacheFile(const std::string& filename, std::vector& read_buffer); //! Closes file handle. ~AudioCacheFile(); @@ -78,6 +79,7 @@ private: SNDFILE* fh{nullptr}; SF_INFO sf_info; std::string filename; + std::vector& read_buffer; }; class AudioCacheFiles { @@ -95,4 +97,5 @@ public: protected: std::map audiofiles; std::mutex mutex; + std::vector read_buffer; }; -- cgit v1.2.3