Use read_file_mapping directly in the HD audio provider

This commit is contained in:
Thomas Goyne 2014-03-20 18:51:08 -07:00
parent 6c14c9bee9
commit 00b4d6908f
2 changed files with 13 additions and 31 deletions

View File

@ -37,13 +37,13 @@
#include "audio_provider_hd.h" #include "audio_provider_hd.h"
#include "audio_controller.h" #include "audio_controller.h"
#include "audio_provider_pcm.h"
#include "compat.h" #include "compat.h"
#include "options.h" #include "options.h"
#include "utils.h" #include "utils.h"
#include <libaegisub/access.h> #include <libaegisub/access.h>
#include <libaegisub/background_runner.h> #include <libaegisub/background_runner.h>
#include <libaegisub/file_mapping.h>
#include <libaegisub/fs.h> #include <libaegisub/fs.h>
#include <libaegisub/io.h> #include <libaegisub/io.h>
#include <libaegisub/path.h> #include <libaegisub/path.h>
@ -69,56 +69,38 @@ agi::fs::path cache_path() {
boost::replace_all(pattern, "%02i", "%%%%-%%%%-%%%%-%%%%"); boost::replace_all(pattern, "%02i", "%%%%-%%%%-%%%%-%%%%");
return unique_path(cache_dir()/pattern); return unique_path(cache_dir()/pattern);
} }
/// A PCM audio provider for raw dumps with no header
class RawAudioProvider final : public PCMAudioProvider {
public:
RawAudioProvider(agi::fs::path const& cache_filename, AudioProvider *src)
: PCMAudioProvider(cache_filename)
{
bytes_per_sample = src->GetBytesPerSample();
num_samples = src->GetNumSamples();
channels = src->GetChannels();
sample_rate = src->GetSampleRate();
filename = src->GetFilename();
float_samples = src->AreSamplesFloat();
IndexPoint p = { 0, 0, num_samples };
index_points.push_back(p);
}
};
} }
HDAudioProvider::HDAudioProvider(std::unique_ptr<AudioProvider> src, agi::BackgroundRunner *br) HDAudioProvider::HDAudioProvider(std::unique_ptr<AudioProvider> src, agi::BackgroundRunner *br)
: AudioProviderWrapper(std::move(src)) : AudioProviderWrapper(std::move(src))
, cache_filename(cache_path())
{ {
// Check free space // Check free space
if ((uint64_t)num_samples * channels * bytes_per_sample > agi::fs::FreeSpace(cache_dir())) if ((uint64_t)num_samples * channels * bytes_per_sample > agi::fs::FreeSpace(cache_dir()))
throw agi::AudioCacheOpenError("Not enough free disk space in " + cache_dir().string() + " to cache the audio", nullptr); throw agi::AudioCacheOpenError("Not enough free disk space in " + cache_dir().string() + " to cache the audio", nullptr);
diskCacheFilename = cache_path();
try { try {
{ {
agi::io::Save out(diskCacheFilename, true); agi::io::Save out(cache_filename, true);
br->Run(bind(&HDAudioProvider::FillCache, this, source.get(), &out.Get(), std::placeholders::_1)); br->Run(bind(&HDAudioProvider::FillCache, this, source.get(), &out.Get(), std::placeholders::_1));
} }
cache_provider = agi::util::make_unique<RawAudioProvider>(diskCacheFilename, source.get()); file = agi::util::make_unique<agi::read_file_mapping>(cache_filename);
} }
catch (...) { catch (...) {
agi::fs::Remove(diskCacheFilename); agi::fs::Remove(cache_filename);
throw; throw;
} }
} }
HDAudioProvider::~HDAudioProvider() { HDAudioProvider::~HDAudioProvider() {
cache_provider.reset(); // explicitly close the file so we can delete it file.reset(); // explicitly close the file so we can delete it
agi::fs::Remove(diskCacheFilename); agi::fs::Remove(cache_filename);
} }
void HDAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const { void HDAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const {
cache_provider->GetAudio(buf, start, count); start *= channels * bytes_per_sample;
count *= channels * bytes_per_sample;
memcpy(buf, file->read(start, count), count);
} }
void HDAudioProvider::FillCache(AudioProvider *src, std::ofstream *out, agi::ProgressSink *ps) { void HDAudioProvider::FillCache(AudioProvider *src, std::ofstream *out, agi::ProgressSink *ps) {

View File

@ -37,13 +37,13 @@
namespace agi { namespace agi {
class BackgroundRunner; class BackgroundRunner;
class ProgressSink; class ProgressSink;
class read_file_mapping;
} }
class HDAudioProvider final : public AudioProviderWrapper { class HDAudioProvider final : public AudioProviderWrapper {
/// Name of the file which the decoded audio is written to /// Name of the file which the decoded audio is written to
agi::fs::path diskCacheFilename; agi::fs::path cache_filename;
/// Audio provider which reads from the decoded cache std::unique_ptr<agi::read_file_mapping> file;
std::unique_ptr<AudioProvider> cache_provider;
/// Fill the cache with all of the data from the source audio provider /// Fill the cache with all of the data from the source audio provider
/// @param src Audio data to cache /// @param src Audio data to cache