diff options
-rw-r--r-- | src/audiocachefile.cc | 25 | ||||
-rw-r--r-- | 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<sample_t>& 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 <string> #include <list> #include <map> +#include <vector> #include <mutex> #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<CacheChannel>; @@ -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<sample_t>& read_buffer); //! Closes file handle. ~AudioCacheFile(); @@ -78,6 +79,7 @@ private: SNDFILE* fh{nullptr}; SF_INFO sf_info; std::string filename; + std::vector<sample_t>& read_buffer; }; class AudioCacheFiles { @@ -95,4 +97,5 @@ public: protected: std::map<std::string, AudioCacheFile*> audiofiles; std::mutex mutex; + std::vector<sample_t> read_buffer; }; |