forked from mia/Aegisub
Use stable_vector for AudioProviderRam's cache
stable_vector allocates its elements non-contiguously (as is required for stability), while still giving O(1) indexing.
This commit is contained in:
parent
433368dc58
commit
e270dc9aec
4 changed files with 30 additions and 49 deletions
|
@ -28,6 +28,11 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Common C++
|
// Common C++
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable:4996)
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
@ -47,6 +52,9 @@
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Boost
|
// Boost
|
||||||
#include <boost/container/list.hpp>
|
#include <boost/container/list.hpp>
|
||||||
|
|
|
@ -44,33 +44,22 @@
|
||||||
#include <libaegisub/background_runner.h>
|
#include <libaegisub/background_runner.h>
|
||||||
#include <libaegisub/scoped_ptr.h>
|
#include <libaegisub/scoped_ptr.h>
|
||||||
|
|
||||||
#define CacheBits ((22))
|
#define CacheBits 22
|
||||||
|
#define CacheBlockSize (1 << CacheBits)
|
||||||
|
|
||||||
#define CacheBlockSize ((1 << CacheBits))
|
RAMAudioProvider::RAMAudioProvider(AudioProvider *src, agi::BackgroundRunner *br)
|
||||||
|
{
|
||||||
RAMAudioProvider::RAMAudioProvider(AudioProvider *src, agi::BackgroundRunner *br) {
|
|
||||||
agi::scoped_ptr<AudioProvider> source(src);
|
agi::scoped_ptr<AudioProvider> source(src);
|
||||||
|
|
||||||
samples_native_endian = source->AreSamplesNativeEndian();
|
|
||||||
|
|
||||||
// Allocate cache
|
|
||||||
int64_t ssize = source->GetNumSamples() * source->GetBytesPerSample();
|
|
||||||
blockcount = (ssize + CacheBlockSize - 1) >> CacheBits;
|
|
||||||
blockcache = new char*[blockcount];
|
|
||||||
memset(blockcache, 0, blockcount * sizeof(char*));
|
|
||||||
|
|
||||||
// Allocate cache blocks
|
|
||||||
try {
|
try {
|
||||||
for (int i = 0; i < blockcount; i++) {
|
blockcache.resize((src->GetNumSamples() * src->GetBytesPerSample() + CacheBlockSize - 1) >> CacheBits);
|
||||||
blockcache[i] = new char[std::min<size_t>(CacheBlockSize, ssize - i * CacheBlockSize)];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (std::bad_alloc const&) {
|
catch (std::bad_alloc const&) {
|
||||||
Clear();
|
|
||||||
throw agi::AudioCacheOpenError("Couldn't open audio, not enough ram available.", 0);
|
throw agi::AudioCacheOpenError("Couldn't open audio, not enough ram available.", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy parameters
|
// Copy parameters
|
||||||
|
samples_native_endian = source->AreSamplesNativeEndian();
|
||||||
bytes_per_sample = source->GetBytesPerSample();
|
bytes_per_sample = source->GetBytesPerSample();
|
||||||
num_samples = source->GetNumSamples();
|
num_samples = source->GetNumSamples();
|
||||||
channels = source->GetChannels();
|
channels = source->GetChannels();
|
||||||
|
@ -81,51 +70,34 @@ RAMAudioProvider::RAMAudioProvider(AudioProvider *src, agi::BackgroundRunner *br
|
||||||
br->Run(std::bind(&RAMAudioProvider::FillCache, this, src, std::placeholders::_1));
|
br->Run(std::bind(&RAMAudioProvider::FillCache, this, src, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
RAMAudioProvider::~RAMAudioProvider() {
|
|
||||||
Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RAMAudioProvider::FillCache(AudioProvider *source, agi::ProgressSink *ps) {
|
void RAMAudioProvider::FillCache(AudioProvider *source, agi::ProgressSink *ps) {
|
||||||
ps->SetMessage(STD_STR(_("Reading into RAM")));
|
ps->SetMessage(STD_STR(_("Reading into RAM")));
|
||||||
|
|
||||||
int64_t readsize = CacheBlockSize / source->GetBytesPerSample();
|
int64_t readsize = CacheBlockSize / source->GetBytesPerSample();
|
||||||
for (int i = 0; i < blockcount; i++) {
|
for (size_t i = 0; i < blockcache.size(); i++) {
|
||||||
source->GetAudio((char*)blockcache[i], i * readsize, std::min(readsize, num_samples - i * readsize));
|
source->GetAudio(&blockcache[i][0], i * readsize, std::min(readsize, num_samples - i * readsize));
|
||||||
|
|
||||||
ps->SetProgress(i, (blockcount - 1));
|
ps->SetProgress(i, blockcache.size() - 1);
|
||||||
if (ps->IsCancelled()) {
|
if (ps->IsCancelled()) {
|
||||||
Clear();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RAMAudioProvider::Clear() {
|
|
||||||
// Free ram cache
|
|
||||||
if (blockcache) {
|
|
||||||
for (int i = 0; i < blockcount; i++) {
|
|
||||||
delete [] blockcache[i];
|
|
||||||
}
|
|
||||||
delete [] blockcache;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RAMAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const {
|
void RAMAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const {
|
||||||
// Prepare copy
|
char *charbuf = static_cast<char *>(buf);
|
||||||
char *charbuf = (char *)buf;
|
int i = (start * bytes_per_sample) >> CacheBits;
|
||||||
int i = (start*bytes_per_sample) >> CacheBits;
|
int start_offset = (start * bytes_per_sample) & (CacheBlockSize-1);
|
||||||
int start_offset = (start*bytes_per_sample) & (CacheBlockSize-1);
|
int64_t bytesremaining = count * bytes_per_sample;
|
||||||
int64_t bytesremaining = count*bytes_per_sample;
|
|
||||||
|
|
||||||
// Copy
|
|
||||||
while (bytesremaining) {
|
while (bytesremaining) {
|
||||||
int readsize = std::min<int>(bytesremaining, CacheBlockSize - start_offset);
|
int readsize = std::min<int>(bytesremaining, CacheBlockSize - start_offset);
|
||||||
|
|
||||||
memcpy(charbuf,(char *)(blockcache[i++]+start_offset),readsize);
|
memcpy(charbuf, &blockcache[i++][start_offset], readsize);
|
||||||
|
|
||||||
charbuf+=readsize;
|
charbuf += readsize;
|
||||||
|
|
||||||
start_offset=0;
|
start_offset = 0;
|
||||||
bytesremaining-=readsize;
|
bytesremaining -= readsize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,23 +34,22 @@
|
||||||
|
|
||||||
#include "include/aegisub/audio_provider.h"
|
#include "include/aegisub/audio_provider.h"
|
||||||
|
|
||||||
|
#include <boost/container/stable_vector.hpp>
|
||||||
|
|
||||||
namespace agi {
|
namespace agi {
|
||||||
class BackgroundRunner;
|
class BackgroundRunner;
|
||||||
class ProgressSink;
|
class ProgressSink;
|
||||||
}
|
}
|
||||||
|
|
||||||
class RAMAudioProvider : public AudioProvider {
|
class RAMAudioProvider : public AudioProvider {
|
||||||
char** blockcache;
|
boost::container::stable_vector<char[1 << 22]> blockcache;
|
||||||
int blockcount;
|
|
||||||
bool samples_native_endian;
|
bool samples_native_endian;
|
||||||
|
|
||||||
void Clear();
|
|
||||||
void FillCache(AudioProvider *source, agi::ProgressSink *ps);
|
void FillCache(AudioProvider *source, agi::ProgressSink *ps);
|
||||||
void FillBuffer(void *buf, int64_t start, int64_t count) const;
|
void FillBuffer(void *buf, int64_t start, int64_t count) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RAMAudioProvider(AudioProvider *source, agi::BackgroundRunner *br);
|
RAMAudioProvider(AudioProvider *source, agi::BackgroundRunner *br);
|
||||||
~RAMAudioProvider();
|
|
||||||
|
|
||||||
bool AreSamplesNativeEndian() const { return samples_native_endian; }
|
bool AreSamplesNativeEndian() const { return samples_native_endian; }
|
||||||
};
|
};
|
||||||
|
|
|
@ -68,6 +68,8 @@ public:
|
||||||
bool AreSamplesFloat() const { return float_samples; }
|
bool AreSamplesFloat() const { return float_samples; }
|
||||||
virtual bool AreSamplesNativeEndian() const = 0;
|
virtual bool AreSamplesNativeEndian() const = 0;
|
||||||
|
|
||||||
|
virtual int64_t GetSamplesLoaded() const { return num_samples; }
|
||||||
|
|
||||||
/// @brief Does this provider benefit from external caching?
|
/// @brief Does this provider benefit from external caching?
|
||||||
virtual bool NeedsCache() const { return false; }
|
virtual bool NeedsCache() const { return false; }
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue