2006-02-25 09:39:15 +01:00
|
|
|
// Copyright (c) 2005-2006, Rodrigo Braz Monteiro, Fredrik Mellbin
|
2006-02-25 09:26:29 +01:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file audio_provider_hd.cpp
|
|
|
|
/// @brief Caching audio provider using a file for backing
|
|
|
|
/// @ingroup audio_input
|
|
|
|
///
|
2006-02-25 09:26:29 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-12-23 00:18:38 +01:00
|
|
|
#include "audio_provider_hd.h"
|
|
|
|
|
2012-01-08 02:33:39 +01:00
|
|
|
#include "audio_controller.h"
|
2011-12-22 22:26:21 +01:00
|
|
|
#include "audio_provider_pcm.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "compat.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "utils.h"
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/access.h>
|
|
|
|
#include <libaegisub/background_runner.h>
|
|
|
|
#include <libaegisub/fs.h>
|
|
|
|
#include <libaegisub/io.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>
|
2013-11-19 17:59:27 +01:00
|
|
|
#include <fstream>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
namespace {
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::path cache_dir() {
|
|
|
|
std::string path = OPT_GET("Audio/Cache/HD/Location")->GetString();
|
2011-12-22 22:26:21 +01:00
|
|
|
if (path == "default")
|
2013-01-04 16:01:50 +01:00
|
|
|
path = "?temp";
|
2012-06-15 15:08:39 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
return config::path->MakeAbsolute(config::path->Decode(path), "?temp");
|
2011-12-22 22:26:21 +01:00
|
|
|
}
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::path cache_path() {
|
|
|
|
std::string pattern = OPT_GET("Audio/Cache/HD/Name")->GetString();
|
|
|
|
if (!boost::contains(pattern, "%02i")) pattern = "audio%02i.tmp";
|
|
|
|
boost::replace_all(pattern, "%02i", "%%%%-%%%%-%%%%-%%%%");
|
|
|
|
return unique_path(cache_dir()/pattern);
|
2011-12-22 22:26:21 +01:00
|
|
|
}
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
/// A PCM audio provider for raw dumps with no header
|
|
|
|
class RawAudioProvider : public PCMAudioProvider {
|
|
|
|
public:
|
2013-01-04 16:01:50 +01:00
|
|
|
RawAudioProvider(agi::fs::path const& cache_filename, AudioProvider *src)
|
2011-12-22 22:26:21 +01:00
|
|
|
: PCMAudioProvider(cache_filename)
|
|
|
|
{
|
|
|
|
bytes_per_sample = src->GetBytesPerSample();
|
|
|
|
num_samples = src->GetNumSamples();
|
|
|
|
channels = src->GetChannels();
|
|
|
|
sample_rate = src->GetSampleRate();
|
|
|
|
filename = src->GetFilename();
|
2012-06-13 17:58:28 +02:00
|
|
|
float_samples = src->AreSamplesFloat();
|
2011-12-22 22:26:21 +01:00
|
|
|
|
|
|
|
IndexPoint p = { 0, 0, num_samples };
|
|
|
|
index_points.push_back(p);
|
|
|
|
}
|
|
|
|
};
|
2006-02-25 09:39:15 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-27 05:18:29 +02:00
|
|
|
HDAudioProvider::HDAudioProvider(std::unique_ptr<AudioProvider> src, agi::BackgroundRunner *br)
|
|
|
|
: AudioProviderWrapper(std::move(src))
|
|
|
|
{
|
2012-06-15 15:08:45 +02:00
|
|
|
// Check free space
|
2013-01-04 16:01:50 +01:00
|
|
|
if ((uint64_t)num_samples * channels * bytes_per_sample > agi::fs::FreeSpace(cache_dir()))
|
2013-11-21 18:13:36 +01:00
|
|
|
throw agi::AudioCacheOpenError("Not enough free disk space in " + cache_dir().string() + " to cache the audio", nullptr);
|
2012-06-15 15:08:45 +02:00
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
diskCacheFilename = cache_path();
|
|
|
|
|
|
|
|
try {
|
|
|
|
{
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::io::Save out(diskCacheFilename, true);
|
2013-09-27 05:18:29 +02:00
|
|
|
br->Run(bind(&HDAudioProvider::FillCache, this, source.get(), &out.Get(), std::placeholders::_1));
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|
2013-09-27 05:18:29 +02:00
|
|
|
cache_provider = agi::util::make_unique<RawAudioProvider>(diskCacheFilename, source.get());
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|
2011-12-22 22:26:21 +01:00
|
|
|
catch (...) {
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::Remove(diskCacheFilename);
|
2011-12-22 22:26:21 +01:00
|
|
|
throw;
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
HDAudioProvider::~HDAudioProvider() {
|
|
|
|
cache_provider.reset(); // explicitly close the file so we can delete it
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::Remove(diskCacheFilename);
|
2011-12-22 22:26:21 +01:00
|
|
|
}
|
2006-12-26 03:05:35 +01:00
|
|
|
|
2012-08-18 05:13:44 +02:00
|
|
|
void HDAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const {
|
2011-12-22 22:26:21 +01:00
|
|
|
cache_provider->GetAudio(buf, start, count);
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
void HDAudioProvider::FillCache(AudioProvider *src, std::ofstream *out, agi::ProgressSink *ps) {
|
2012-12-23 00:18:38 +01:00
|
|
|
ps->SetMessage(from_wx(_("Reading to Hard Disk cache")));
|
2011-12-22 22:26:21 +01:00
|
|
|
|
|
|
|
int64_t block = 65536;
|
|
|
|
std::vector<char> read_buf;
|
|
|
|
read_buf.resize(block * channels * bytes_per_sample);
|
|
|
|
|
|
|
|
for (int64_t i = 0; i < num_samples; i += block) {
|
|
|
|
block = std::min(block, num_samples - i);
|
|
|
|
src->GetAudio(&read_buf[0], i, block);
|
|
|
|
out->write(&read_buf[0], block * channels * bytes_per_sample);
|
|
|
|
ps->SetProgress(i, num_samples);
|
|
|
|
|
|
|
|
if (ps->IsCancelled()) return;
|
2006-12-26 04:00:15 +01:00
|
|
|
}
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|