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-07-09 16:22:49 +02:00
|
|
|
#include "libaegisub/audio/provider.h"
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2014-03-21 02:51:08 +01:00
|
|
|
#include <libaegisub/file_mapping.h>
|
2014-05-29 00:19:05 +02:00
|
|
|
#include <libaegisub/format.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
|
|
|
#include <libaegisub/path.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2014-05-23 00:40:16 +02:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2014-03-22 15:29:21 +01:00
|
|
|
#include <boost/interprocess/detail/os_thread_functions.hpp>
|
2014-07-09 16:22:49 +02:00
|
|
|
#include <ctime>
|
2014-04-22 21:34:20 +02:00
|
|
|
#include <thread>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
namespace {
|
2014-07-09 16:22:49 +02:00
|
|
|
using namespace agi;
|
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
class HDAudioProvider final : public AudioProviderWrapper {
|
2014-07-09 16:22:49 +02:00
|
|
|
mutable temp_file_mapping file;
|
2014-04-22 21:34:20 +02:00
|
|
|
std::atomic<bool> cancelled = {false};
|
|
|
|
std::thread decoder;
|
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 {
|
2014-04-22 21:34:20 +02:00
|
|
|
auto missing = std::min(count, start + count - decoded_samples);
|
|
|
|
if (missing > 0) {
|
|
|
|
memset(static_cast<int16_t*>(buf) + count - missing, 0, missing * bytes_per_sample);
|
|
|
|
count -= missing;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0) {
|
2022-08-10 15:09:41 +02:00
|
|
|
start *= bytes_per_sample * channels;
|
|
|
|
count *= bytes_per_sample * channels;
|
2014-07-09 16:22:49 +02:00
|
|
|
memcpy(buf, file.read(start, count), count);
|
2014-04-22 21:34:20 +02:00
|
|
|
}
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
fs::path CacheFilename(fs::path const& dir) {
|
|
|
|
// Check free space
|
2022-08-10 15:09:41 +02:00
|
|
|
if ((uint64_t)num_samples * bytes_per_sample * channels > fs::FreeSpace(dir))
|
2014-07-09 16:22:49 +02:00
|
|
|
throw AudioProviderError("Not enough free disk space in " + dir.string() + " to cache the audio");
|
|
|
|
|
|
|
|
return format("audio-%lld-%lld", time(nullptr),
|
|
|
|
boost::interprocess::ipcdetail::get_current_process_id());
|
|
|
|
}
|
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
public:
|
2014-07-09 16:22:49 +02:00
|
|
|
HDAudioProvider(std::unique_ptr<AudioProvider> src, agi::fs::path const& dir)
|
2014-03-24 03:30:03 +01:00
|
|
|
: AudioProviderWrapper(std::move(src))
|
2022-08-10 15:09:41 +02:00
|
|
|
, file(dir / CacheFilename(dir), num_samples * bytes_per_sample * channels)
|
2014-03-24 03:30:03 +01:00
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
decoded_samples = 0;
|
|
|
|
decoder = std::thread([&] {
|
2014-03-24 03:30:03 +01:00
|
|
|
int64_t block = 65536;
|
|
|
|
for (int64_t i = 0; i < num_samples; i += block) {
|
2014-04-22 21:34:20 +02:00
|
|
|
if (cancelled) break;
|
2014-03-24 03:30:03 +01:00
|
|
|
block = std::min(block, num_samples - i);
|
2014-07-09 16:22:49 +02:00
|
|
|
source->GetAudio(file.write(i * bytes_per_sample, block * bytes_per_sample), i, block);
|
2014-04-22 21:34:20 +02:00
|
|
|
decoded_samples += block;
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-04-22 21:34:20 +02:00
|
|
|
|
|
|
|
~HDAudioProvider() {
|
|
|
|
cancelled = true;
|
|
|
|
decoder.join();
|
|
|
|
}
|
2014-03-24 03:30:03 +01:00
|
|
|
};
|
|
|
|
}
|
2006-12-26 03:05:35 +01:00
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
namespace agi {
|
|
|
|
std::unique_ptr<AudioProvider> CreateHDAudioProvider(std::unique_ptr<AudioProvider> src, agi::fs::path const& dir) {
|
2014-07-10 21:55:21 +02:00
|
|
|
return agi::make_unique<HDAudioProvider>(std::move(src), dir);
|
2014-07-09 16:22:49 +02:00
|
|
|
}
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|