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;
|
|
|
|
|
2019-10-30 04:49:55 +01:00
|
|
|
/// Anything -> mono 16 bit signed machine-endian audio converter
|
2014-07-09 16:22:49 +02:00
|
|
|
namespace {
|
2019-10-30 04:49:55 +01:00
|
|
|
class ConvertAudioProvider final : public AudioProviderWrapper {
|
2011-10-29 05:46:44 +02:00
|
|
|
public:
|
2019-10-30 04:49:55 +01:00
|
|
|
ConvertAudioProvider(std::unique_ptr<AudioProvider> src) : AudioProviderWrapper(std::move(src)) {
|
2012-06-13 17:58:28 +02:00
|
|
|
float_samples = false;
|
2011-10-29 05:46:36 +02:00
|
|
|
channels = 1;
|
2019-10-30 04:49:55 +01:00
|
|
|
bytes_per_sample = sizeof(int16_t);
|
2009-06-05 01:02:29 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 04:49:55 +01:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const override {
|
|
|
|
source->GetInt16MonoAudio(reinterpret_cast<int16_t*>(buf), start, count);
|
2011-10-29 05:46:36 +02:00
|
|
|
}
|
|
|
|
};
|
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
|
2019-10-30 04:49:55 +01:00
|
|
|
if (provider->AreSamplesFloat())
|
2012-12-27 17:42:10 +01:00
|
|
|
LOG_D("audio_provider") << "Converting float to S16";
|
2019-10-30 04:49:55 +01:00
|
|
|
else if (provider->GetBytesPerSample() != 2)
|
|
|
|
LOG_D("audio_provider") << "Converting " << provider->GetBytesPerSample() << " bytes per sample to S16";
|
2008-07-16 15:22:06 +02:00
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
// We currently only support mono audio
|
2019-10-30 04:49:55 +01:00
|
|
|
if (provider->GetChannels() != 1)
|
2012-12-27 17:42:10 +01:00
|
|
|
LOG_D("audio_provider") << "Downmixing to mono from " << provider->GetChannels() << " channels";
|
2019-10-30 04:49:55 +01:00
|
|
|
|
|
|
|
provider = agi::make_unique<ConvertAudioProvider>(std::move(provider));
|
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
|
|
|
}
|