summaryrefslogtreecommitdiff
path: root/src/audiocachefile.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/audiocachefile.cc')
-rw-r--r--src/audiocachefile.cc35
1 files changed, 15 insertions, 20 deletions
diff --git a/src/audiocachefile.cc b/src/audiocachefile.cc
index 916ecb7..91fcecf 100644
--- a/src/audiocachefile.cc
+++ b/src/audiocachefile.cc
@@ -34,8 +34,9 @@
#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));
@@ -49,7 +50,7 @@ AudioCacheFile::AudioCacheFile(const std::string& filename)
if(sf_info.frames == 0)
{
- printf("sf_info.frames == 0\n");
+ WARN(cache, "sf_info.frames == 0\n");
}
}
@@ -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];
@@ -135,25 +130,25 @@ AudioCacheFile& AudioCacheFiles::getFile(const std::string& filename)
{
std::lock_guard<std::mutex> lock(mutex);
- AudioCacheFile* cacheAudioFile = nullptr;
+ AudioCacheFile* cache_audio_file = nullptr;
auto it = audiofiles.find(filename);
if(it == audiofiles.end())
{
- cacheAudioFile = new AudioCacheFile(filename);
- audiofiles.insert(std::make_pair(filename, cacheAudioFile));
+ cache_audio_file = new AudioCacheFile(filename, read_buffer);
+ audiofiles.insert(std::make_pair(filename, cache_audio_file));
}
else
{
- cacheAudioFile = it->second;
+ cache_audio_file = it->second;
}
- assert(cacheAudioFile);
+ assert(cache_audio_file);
// Increase ref count.
- ++cacheAudioFile->ref;
+ ++cache_audio_file->ref;
- return *cacheAudioFile;
+ return *cache_audio_file;
}
void AudioCacheFiles::releaseFile(const std::string& filename)