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"
|
|
|
|
|
2006-12-26 04:00:15 +01:00
|
|
|
#include <wx/filefn.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#include <wx/filename.h>
|
|
|
|
|
2011-09-28 21:49:37 +02:00
|
|
|
#include <libaegisub/background_runner.h>
|
2011-12-22 22:26:21 +01:00
|
|
|
#include <libaegisub/io.h>
|
2011-09-28 21:49:37 +02:00
|
|
|
|
2006-02-25 09:39:15 +01:00
|
|
|
#include "audio_provider_hd.h"
|
2011-09-28 21:49:37 +02:00
|
|
|
|
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"
|
2008-10-29 01:18:13 +01:00
|
|
|
#include "main.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "standard_paths.h"
|
|
|
|
#include "utils.h"
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
namespace {
|
|
|
|
wxString cache_dir() {
|
|
|
|
wxString path = lagi_wxString(OPT_GET("Audio/Cache/HD/Location")->GetString());
|
|
|
|
if (path == "default")
|
2012-06-15 15:08:39 +02:00
|
|
|
path = "?temp/";
|
|
|
|
|
|
|
|
return DecodeRelativePath(StandardPaths::DecodePath(path), StandardPaths::DecodePath("?user/"));
|
2011-12-22 22:26:21 +01:00
|
|
|
}
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
wxString cache_path() {
|
|
|
|
wxString pattern = lagi_wxString(OPT_GET("Audio/Cache/HD/Name")->GetString());
|
|
|
|
if (pattern.Find("%02i") == wxNOT_FOUND) pattern = "audio%02i.tmp";
|
|
|
|
|
|
|
|
// Try from 00 to 99
|
|
|
|
for (int i=0;i<100;i++) {
|
|
|
|
// File exists?
|
2012-06-15 15:08:41 +02:00
|
|
|
wxFileName curNameTry(cache_dir(), wxString::Format(pattern, i));
|
2012-06-23 03:06:32 +02:00
|
|
|
#if wxCHECK_VERSION(2, 9, 4)
|
2012-06-15 15:08:41 +02:00
|
|
|
if (!curNameTry.Exists())
|
2012-06-23 03:06:32 +02:00
|
|
|
#else
|
|
|
|
if (!curNameTry.FileExists() && !curNameTry.DirExists())
|
|
|
|
#endif
|
2012-06-15 15:08:41 +02:00
|
|
|
return curNameTry.GetFullPath();
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|
2011-12-22 22:26:21 +01:00
|
|
|
return "";
|
|
|
|
}
|
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:
|
|
|
|
RawAudioProvider(wxString 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();
|
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
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
bool AreSamplesNativeEndian() const { return true; }
|
|
|
|
};
|
2006-02-25 09:39:15 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
HDAudioProvider::HDAudioProvider(AudioProvider *src, agi::BackgroundRunner *br) {
|
|
|
|
agi::scoped_ptr<AudioProvider> source(src);
|
|
|
|
assert(src->AreSamplesNativeEndian()); // Byteswapping should be done before caching
|
2011-09-28 21:47:40 +02:00
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
bytes_per_sample = source->GetBytesPerSample();
|
|
|
|
num_samples = source->GetNumSamples();
|
|
|
|
channels = source->GetChannels();
|
|
|
|
sample_rate = source->GetSampleRate();
|
|
|
|
filename = source->GetFilename();
|
2012-06-13 17:58:28 +02:00
|
|
|
float_samples = source->AreSamplesFloat();
|
2011-12-22 22:26:21 +01:00
|
|
|
|
2012-06-15 15:08:45 +02:00
|
|
|
// Check free space
|
|
|
|
wxDiskspaceSize_t freespace;
|
|
|
|
if (wxGetDiskSpace(cache_dir(), 0, &freespace)) {
|
|
|
|
if (num_samples * channels * bytes_per_sample > freespace)
|
|
|
|
throw agi::AudioCacheOpenError("Not enough free disk space in " + STD_STR(cache_dir()) + " to cache the audio", 0);
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:26:21 +01:00
|
|
|
diskCacheFilename = cache_path();
|
|
|
|
|
|
|
|
try {
|
|
|
|
{
|
|
|
|
agi::io::Save out(STD_STR(diskCacheFilename), true);
|
2012-09-25 01:35:27 +02:00
|
|
|
br->Run(bind(&HDAudioProvider::FillCache, this, src, &out.Get(), std::placeholders::_1));
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|
2011-12-22 22:26:21 +01:00
|
|
|
cache_provider.reset(new RawAudioProvider(diskCacheFilename, src));
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|
2011-12-22 22:26:21 +01:00
|
|
|
catch (...) {
|
|
|
|
wxRemoveFile(diskCacheFilename);
|
|
|
|
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
|
|
|
|
wxRemoveFile(diskCacheFilename);
|
|
|
|
}
|
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) {
|
|
|
|
ps->SetMessage(STD_STR(_("Reading to Hard Disk cache")));
|
|
|
|
|
|
|
|
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
|
|
|
}
|