2014-07-09 16:22:49 +02:00
|
|
|
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
|
2008-01-19 03:18:08 +01:00
|
|
|
//
|
2011-10-29 05:46:36 +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.
|
2008-01-19 03:18:08 +01:00
|
|
|
//
|
2011-10-29 05:46:36 +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.
|
2008-01-19 03:18:08 +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-10-29 05:46:36 +02:00
|
|
|
|
2012-12-27 17:42:10 +01:00
|
|
|
#include <libaegisub/log.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2011-10-29 05:46:36 +02:00
|
|
|
|
2012-06-13 17:58:28 +02:00
|
|
|
#include <limits>
|
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
using namespace agi;
|
|
|
|
|
2012-06-13 17:58:28 +02:00
|
|
|
/// Anything integral -> 16 bit signed machine-endian audio converter
|
2014-07-09 16:22:49 +02:00
|
|
|
namespace {
|
2011-10-29 05:46:51 +02:00
|
|
|
template<class Target>
|
2014-03-13 02:39:07 +01:00
|
|
|
class BitdepthConvertAudioProvider final : public AudioProviderWrapper {
|
2011-10-29 05:46:44 +02:00
|
|
|
int src_bytes_per_sample;
|
2014-07-10 17:11:59 +02:00
|
|
|
mutable std::vector<uint8_t> src_buf;
|
|
|
|
|
2011-10-29 05:46:44 +02:00
|
|
|
public:
|
2013-09-27 05:18:29 +02:00
|
|
|
BitdepthConvertAudioProvider(std::unique_ptr<AudioProvider> src) : AudioProviderWrapper(std::move(src)) {
|
2011-10-29 05:46:51 +02:00
|
|
|
if (bytes_per_sample > 8)
|
2014-07-09 16:22:49 +02:00
|
|
|
throw AudioProviderError("Audio format converter: audio with bitdepths greater than 64 bits/sample is currently unsupported");
|
2011-10-29 05:46:44 +02:00
|
|
|
|
|
|
|
src_bytes_per_sample = bytes_per_sample;
|
2011-10-29 05:46:51 +02:00
|
|
|
bytes_per_sample = sizeof(Target);
|
2011-10-29 05:46:44 +02:00
|
|
|
}
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const override {
|
2014-07-10 17:11:59 +02:00
|
|
|
src_buf.resize(count * src_bytes_per_sample * channels);
|
2014-07-09 16:22:49 +02:00
|
|
|
source->GetAudio(src_buf.data(), start, count);
|
2011-10-29 05:46:44 +02:00
|
|
|
|
2014-07-10 15:50:50 +02:00
|
|
|
auto dest = static_cast<int16_t*>(buf);
|
2011-10-29 05:46:44 +02:00
|
|
|
|
|
|
|
for (int64_t i = 0; i < count * channels; ++i) {
|
|
|
|
int64_t sample = 0;
|
2011-10-29 05:46:51 +02:00
|
|
|
|
|
|
|
// 8 bits per sample is assumed to be unsigned with a bias of 127,
|
|
|
|
// while everything else is assumed to be signed with zero bias
|
|
|
|
if (src_bytes_per_sample == 1)
|
2014-07-10 15:50:50 +02:00
|
|
|
sample = src_buf[i] - 128;
|
2014-07-09 16:22:49 +02:00
|
|
|
else {
|
2014-07-10 15:50:50 +02:00
|
|
|
for (int j = src_bytes_per_sample; j > 0; --j) {
|
2014-07-09 16:22:49 +02:00
|
|
|
sample <<= 8;
|
2014-07-10 15:50:50 +02:00
|
|
|
sample += src_buf[i * src_bytes_per_sample + j - 1];
|
2014-07-09 16:22:49 +02:00
|
|
|
}
|
|
|
|
}
|
2011-10-29 05:46:44 +02:00
|
|
|
|
2011-11-07 07:18:34 +01:00
|
|
|
if (static_cast<size_t>(src_bytes_per_sample) > sizeof(Target))
|
2014-07-10 15:50:50 +02:00
|
|
|
sample /= 1 << (src_bytes_per_sample - sizeof(Target)) * 8;
|
2011-11-07 07:18:34 +01:00
|
|
|
else if (static_cast<size_t>(src_bytes_per_sample) < sizeof(Target))
|
2014-07-10 15:50:50 +02:00
|
|
|
sample *= 1 << (sizeof(Target) - src_bytes_per_sample ) * 8;
|
2011-10-29 05:46:51 +02:00
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
dest[i] = static_cast<Target>(sample);
|
2011-10-29 05:46:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-13 17:58:28 +02:00
|
|
|
/// Floating point -> 16 bit signed machine-endian audio converter
|
|
|
|
template<class Source, class Target>
|
2014-03-13 02:39:07 +01:00
|
|
|
class FloatConvertAudioProvider final : public AudioProviderWrapper {
|
2014-07-10 17:11:59 +02:00
|
|
|
mutable std::vector<Source> src_buf;
|
|
|
|
|
2012-06-13 17:58:28 +02:00
|
|
|
public:
|
2013-09-27 05:18:29 +02:00
|
|
|
FloatConvertAudioProvider(std::unique_ptr<AudioProvider> src) : AudioProviderWrapper(std::move(src)) {
|
2012-06-13 17:58:28 +02:00
|
|
|
bytes_per_sample = sizeof(Target);
|
|
|
|
float_samples = false;
|
|
|
|
}
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const override {
|
2014-07-10 17:11:59 +02:00
|
|
|
src_buf.resize(count * channels);
|
2012-06-13 17:58:28 +02:00
|
|
|
source->GetAudio(&src_buf[0], start, count);
|
|
|
|
|
2014-07-10 17:11:59 +02:00
|
|
|
auto dest = static_cast<Target*>(buf);
|
2012-06-13 17:58:28 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < static_cast<size_t>(count * channels); ++i) {
|
|
|
|
Source expanded;
|
|
|
|
if (src_buf[i] < 0)
|
|
|
|
expanded = static_cast<Target>(-src_buf[i] * std::numeric_limits<Target>::min());
|
|
|
|
else
|
|
|
|
expanded = static_cast<Target>(src_buf[i] * std::numeric_limits<Target>::max());
|
|
|
|
|
|
|
|
if (expanded < std::numeric_limits<Target>::min())
|
|
|
|
dest[i] = std::numeric_limits<Target>::min();
|
|
|
|
else if (expanded > std::numeric_limits<Target>::max())
|
|
|
|
dest[i] = std::numeric_limits<Target>::max();
|
|
|
|
else
|
|
|
|
dest[i] = static_cast<Target>(expanded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
/// Non-mono 16-bit signed machine-endian -> mono 16-bit signed machine endian converter
|
2014-03-13 02:39:07 +01:00
|
|
|
class DownmixAudioProvider final : public AudioProviderWrapper {
|
2011-10-29 05:46:36 +02:00
|
|
|
int src_channels;
|
2014-07-10 17:11:59 +02:00
|
|
|
mutable std::vector<int16_t> src_buf;
|
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
public:
|
2013-09-27 05:18:29 +02:00
|
|
|
DownmixAudioProvider(std::unique_ptr<AudioProvider> src) : AudioProviderWrapper(std::move(src)) {
|
2011-10-29 05:46:36 +02:00
|
|
|
src_channels = channels;
|
|
|
|
channels = 1;
|
2009-06-05 01:02:29 +02:00
|
|
|
}
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const override {
|
2014-07-10 17:11:59 +02:00
|
|
|
src_buf.resize(count * src_channels);
|
2011-10-29 05:46:36 +02:00
|
|
|
source->GetAudio(&src_buf[0], start, count);
|
2009-06-05 01:02:29 +02:00
|
|
|
|
2014-07-10 17:11:59 +02:00
|
|
|
auto dst = static_cast<int16_t*>(buf);
|
2011-10-29 05:46:36 +02:00
|
|
|
// Just average the channels together
|
|
|
|
while (count-- > 0) {
|
|
|
|
int sum = 0;
|
|
|
|
for (int c = 0; c < src_channels; ++c)
|
|
|
|
sum += src_buf[count * src_channels + c];
|
|
|
|
dst[count] = static_cast<int16_t>(sum / src_channels);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2008-01-19 03:38:31 +01:00
|
|
|
|
2014-07-10 17:11:59 +02:00
|
|
|
/// Sample doubler with linear interpolation for the samples provider
|
2011-10-29 05:46:36 +02:00
|
|
|
/// Requires 16-bit mono input
|
2014-03-13 02:39:07 +01:00
|
|
|
class SampleDoublingAudioProvider final : public AudioProviderWrapper {
|
2011-10-29 05:46:36 +02:00
|
|
|
public:
|
2013-09-27 05:18:29 +02:00
|
|
|
SampleDoublingAudioProvider(std::unique_ptr<AudioProvider> src) : AudioProviderWrapper(std::move(src)) {
|
2011-10-29 05:46:36 +02:00
|
|
|
sample_rate *= 2;
|
|
|
|
num_samples *= 2;
|
2014-04-22 21:34:20 +02:00
|
|
|
decoded_samples = decoded_samples * 2;
|
2008-01-19 03:18:08 +01:00
|
|
|
}
|
2008-01-19 03:38:31 +01:00
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const override {
|
2014-07-10 19:14:54 +02:00
|
|
|
int16_t *src, *dst = static_cast<int16_t *>(buf);
|
2008-01-25 21:53:12 +01:00
|
|
|
|
2014-07-10 19:14:54 +02:00
|
|
|
// We need to always get at least two samples to be able to interpolate
|
|
|
|
int16_t srcbuf[2];
|
|
|
|
if (count == 1) {
|
|
|
|
source->GetAudio(srcbuf, start / 2, 2);
|
|
|
|
src = srcbuf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
source->GetAudio(buf, start / 2, (start + count) / 2 - start / 2 + 1);
|
|
|
|
src = dst;
|
2008-01-25 21:53:12 +01:00
|
|
|
}
|
2008-07-16 15:22:06 +02:00
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
// walking backwards so that the conversion can be done in place
|
2014-07-10 19:14:54 +02:00
|
|
|
for (; count > 0; --count) {
|
|
|
|
auto src_index = (start + count - 1) / 2 - start / 2;
|
|
|
|
auto i = count - 1;
|
|
|
|
if ((start + i) & 1)
|
|
|
|
dst[i] = (int16_t)(((int32_t)src[src_index] + src[src_index + 1]) / 2);
|
|
|
|
else
|
|
|
|
dst[i] = src[src_index];
|
2011-10-29 05:46:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-07-09 16:22:49 +02:00
|
|
|
}
|
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-07-09 16:22:49 +02:00
|
|
|
namespace agi {
|
2013-09-16 15:43:17 +02:00
|
|
|
std::unique_ptr<AudioProvider> CreateConvertAudioProvider(std::unique_ptr<AudioProvider> provider) {
|
2011-10-29 05:46:36 +02:00
|
|
|
// Ensure 16-bit audio with proper endianness
|
2012-06-13 17:58:28 +02:00
|
|
|
if (provider->AreSamplesFloat()) {
|
2012-12-27 17:42:10 +01:00
|
|
|
LOG_D("audio_provider") << "Converting float to S16";
|
2012-06-13 17:58:28 +02:00
|
|
|
if (provider->GetBytesPerSample() == sizeof(float))
|
2014-04-23 22:53:24 +02:00
|
|
|
provider = agi::make_unique<FloatConvertAudioProvider<float, int16_t>>(std::move(provider));
|
2012-06-13 17:58:28 +02:00
|
|
|
else
|
2014-04-23 22:53:24 +02:00
|
|
|
provider = agi::make_unique<FloatConvertAudioProvider<double, int16_t>>(std::move(provider));
|
2012-06-13 17:58:28 +02:00
|
|
|
}
|
2013-06-12 05:09:45 +02:00
|
|
|
if (provider->GetBytesPerSample() != 2) {
|
2012-12-27 17:42:10 +01:00
|
|
|
LOG_D("audio_provider") << "Converting " << provider->GetBytesPerSample() << " bytes per sample or wrong endian to S16";
|
2014-04-23 22:53:24 +02:00
|
|
|
provider = agi::make_unique<BitdepthConvertAudioProvider<int16_t>>(std::move(provider));
|
2012-12-27 17:42:10 +01:00
|
|
|
}
|
2008-07-16 15:22:06 +02:00
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
// We currently only support mono audio
|
2012-12-27 17:42:10 +01:00
|
|
|
if (provider->GetChannels() != 1) {
|
|
|
|
LOG_D("audio_provider") << "Downmixing to mono from " << provider->GetChannels() << " channels";
|
2014-04-23 22:53:24 +02:00
|
|
|
provider = agi::make_unique<DownmixAudioProvider>(std::move(provider));
|
2012-12-27 17:42:10 +01:00
|
|
|
}
|
2011-10-29 05:46:36 +02:00
|
|
|
|
|
|
|
// Some players don't like low sample rate audio
|
2012-12-27 17:42:10 +01:00
|
|
|
while (provider->GetSampleRate() < 32000) {
|
|
|
|
LOG_D("audio_provider") << "Doubling sample rate";
|
2014-04-23 22:53:24 +02:00
|
|
|
provider = agi::make_unique<SampleDoublingAudioProvider>(std::move(provider));
|
2012-12-27 17:42:10 +01:00
|
|
|
}
|
2008-07-16 15:22:06 +02:00
|
|
|
|
2013-09-16 15:43:17 +02:00
|
|
|
return provider;
|
2008-07-16 15:22:06 +02:00
|
|
|
}
|
2014-07-09 16:22:49 +02:00
|
|
|
}
|