2014-03-22 15:29:21 +01:00
|
|
|
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-02-25 09:26:29 +01:00
|
|
|
//
|
2014-03-22 15:29:21 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2006-02-25 09:26:29 +01:00
|
|
|
//
|
2014-03-22 15:29:21 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2006-02-25 09:26:29 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
#include "include/aegisub/audio_provider.h"
|
2012-12-23 00:18:38 +01:00
|
|
|
|
2012-01-08 02:33:39 +01:00
|
|
|
#include "audio_controller.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "compat.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/background_runner.h>
|
2014-03-21 02:51:08 +01:00
|
|
|
#include <libaegisub/file_mapping.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
|
|
|
#include <libaegisub/path.h>
|
|
|
|
#include <libaegisub/util.h>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
2014-03-22 15:29:21 +01:00
|
|
|
#include <boost/format.hpp>
|
|
|
|
#include <boost/interprocess/detail/os_thread_functions.hpp>
|
2014-03-24 03:30:03 +01:00
|
|
|
#include <wx/intl.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
namespace {
|
|
|
|
class HDAudioProvider final : public AudioProviderWrapper {
|
|
|
|
std::unique_ptr<agi::temp_file_mapping> file;
|
2012-06-15 15:08:39 +02:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const override {
|
|
|
|
start *= channels * bytes_per_sample;
|
|
|
|
count *= channels * bytes_per_sample;
|
|
|
|
memcpy(buf, file->read(start, count), count);
|
|
|
|
}
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
public:
|
|
|
|
HDAudioProvider(std::unique_ptr<AudioProvider> src, agi::BackgroundRunner *br)
|
|
|
|
: AudioProviderWrapper(std::move(src))
|
|
|
|
{
|
|
|
|
auto path = OPT_GET("Audio/Cache/HD/Location")->GetString();
|
|
|
|
if (path == "default")
|
|
|
|
path = "?temp";
|
|
|
|
auto cache_dir = config::path->MakeAbsolute(config::path->Decode(path), "?temp");
|
2014-03-22 15:29:21 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
auto bps = bytes_per_sample * channels;
|
2014-03-22 15:29:21 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
// Check free space
|
|
|
|
if ((uint64_t)num_samples * bps > agi::fs::FreeSpace(cache_dir))
|
|
|
|
throw agi::AudioCacheOpenError("Not enough free disk space in " + cache_dir.string() + " to cache the audio", nullptr);
|
2014-03-22 15:29:21 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
auto filename = str(boost::format("audio-%lld-%lld")
|
|
|
|
% (long long)time(nullptr)
|
|
|
|
% (long long)boost::interprocess::ipcdetail::get_current_process_id());
|
|
|
|
|
|
|
|
file = agi::util::make_unique<agi::temp_file_mapping>(cache_dir / filename, num_samples * bps);
|
|
|
|
br->Run([&] (agi::ProgressSink *ps) {
|
2014-03-25 17:51:38 +01:00
|
|
|
ps->SetTitle(from_wx(_("Load audio")));
|
2014-03-24 03:30:03 +01:00
|
|
|
ps->SetMessage(from_wx(_("Reading to Hard Disk cache")));
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
int64_t block = 65536;
|
|
|
|
for (int64_t i = 0; i < num_samples; i += block) {
|
|
|
|
block = std::min(block, num_samples - i);
|
|
|
|
source->GetAudio(file->write(i * bps, block * bps), i, block);
|
|
|
|
ps->SetProgress(i, num_samples);
|
|
|
|
if (ps->IsCancelled()) return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2006-12-26 03:05:35 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
std::unique_ptr<AudioProvider> CreateHDAudioProvider(std::unique_ptr<AudioProvider> src, agi::BackgroundRunner *br) {
|
|
|
|
return agi::util::make_unique<HDAudioProvider>(std::move(src), br);
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|