2014-07-09 16:22:49 +02:00
|
|
|
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-02-25 09:26:29 +01:00
|
|
|
//
|
2014-07-09 16:22:49 +02: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-07-09 16:22:49 +02: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"
|
2011-09-28 21:49:37 +02:00
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
#include "libaegisub/make_unique.h"
|
2012-03-25 06:05:38 +02:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
#include <array>
|
|
|
|
#include <boost/container/stable_vector.hpp>
|
2014-04-22 21:34:20 +02:00
|
|
|
#include <thread>
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
namespace {
|
2014-07-09 16:22:49 +02:00
|
|
|
using namespace agi;
|
2006-02-25 09:26:29 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
#define CacheBits 22
|
|
|
|
#define CacheBlockSize (1 << CacheBits)
|
2006-02-25 09:26:29 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
class RAMAudioProvider final : public AudioProviderWrapper {
|
|
|
|
#ifdef _MSC_VER
|
2014-04-22 21:34:20 +02:00
|
|
|
boost::container::stable_vector<char[CacheBlockSize]> blockcache;
|
2014-03-24 03:30:03 +01:00
|
|
|
#else
|
2014-04-22 21:34:20 +02:00
|
|
|
boost::container::stable_vector<std::array<char, CacheBlockSize>> blockcache;
|
2014-03-24 03:30:03 +01:00
|
|
|
#endif
|
2014-04-22 21:34:20 +02:00
|
|
|
std::atomic<bool> cancelled = {false};
|
|
|
|
std::thread decoder;
|
2014-03-24 03:30:03 +01:00
|
|
|
|
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const override;
|
|
|
|
|
|
|
|
public:
|
2014-04-22 21:34:20 +02:00
|
|
|
RAMAudioProvider(std::unique_ptr<AudioProvider> src)
|
2014-03-24 03:30:03 +01:00
|
|
|
: AudioProviderWrapper(std::move(src))
|
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
decoded_samples = 0;
|
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
try {
|
2019-10-30 05:47:45 +01:00
|
|
|
blockcache.resize((num_samples * bytes_per_sample * channels + CacheBlockSize - 1) >> CacheBits);
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|
|
|
|
catch (std::bad_alloc const&) {
|
2014-07-09 16:22:49 +02:00
|
|
|
throw AudioProviderError("Not enough memory available to cache in RAM");
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|
2011-09-28 21:49:37 +02:00
|
|
|
|
2014-04-22 21:34:20 +02:00
|
|
|
decoder = std::thread([&] {
|
2019-10-30 05:47:45 +01:00
|
|
|
int64_t readsize = CacheBlockSize / bytes_per_sample / channels;
|
2014-03-24 03:30:03 +01:00
|
|
|
for (size_t i = 0; i < blockcache.size(); i++) {
|
2014-04-22 21:34:20 +02:00
|
|
|
if (cancelled) break;
|
|
|
|
auto actual_read = std::min<int64_t>(readsize, num_samples - i * readsize);
|
|
|
|
source->GetAudio(&blockcache[i][0], i * readsize, actual_read);
|
|
|
|
decoded_samples += actual_read;
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|
|
|
|
});
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
2014-04-22 21:34:20 +02:00
|
|
|
|
|
|
|
~RAMAudioProvider() {
|
|
|
|
cancelled = true;
|
|
|
|
decoder.join();
|
|
|
|
}
|
2014-03-24 03:30:03 +01:00
|
|
|
};
|
2011-09-28 21:47:40 +02:00
|
|
|
|
2012-08-18 05:13:44 +02:00
|
|
|
void RAMAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const {
|
2014-04-25 03:51:21 +02:00
|
|
|
auto charbuf = static_cast<char *>(buf);
|
2019-10-30 05:47:45 +01:00
|
|
|
for (int64_t bytes_remaining = count * bytes_per_sample * channels; bytes_remaining; ) {
|
2014-04-22 21:34:20 +02:00
|
|
|
if (start >= decoded_samples) {
|
|
|
|
memset(charbuf, 0, bytes_remaining);
|
|
|
|
break;
|
|
|
|
}
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2019-11-02 07:26:36 +01:00
|
|
|
const int64_t samples_per_block = CacheBlockSize / bytes_per_sample / channels;
|
|
|
|
|
|
|
|
const size_t i = start / samples_per_block;
|
|
|
|
const int start_offset = (start % samples_per_block) * bytes_per_sample * channels;
|
|
|
|
const int read_size = std::min<int>(bytes_remaining, samples_per_block * bytes_per_sample * channels - start_offset);
|
2006-02-25 09:26:29 +01:00
|
|
|
|
2014-04-25 03:51:21 +02:00
|
|
|
memcpy(charbuf, &blockcache[i][start_offset], read_size);
|
2014-04-22 21:34:20 +02:00
|
|
|
charbuf += read_size;
|
|
|
|
bytes_remaining -= read_size;
|
2019-10-30 05:47:45 +01:00
|
|
|
start += read_size / bytes_per_sample / channels;
|
2006-02-25 09:26:29 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
namespace agi {
|
2014-04-22 21:34:20 +02:00
|
|
|
std::unique_ptr<AudioProvider> CreateRAMAudioProvider(std::unique_ptr<AudioProvider> src) {
|
2014-07-10 21:55:21 +02:00
|
|
|
return agi::make_unique<RAMAudioProvider>(std::move(src));
|
2014-07-09 16:22:49 +02:00
|
|
|
}
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|