2011-10-29 05:46:36 +02:00
|
|
|
// Copyright (c) 2011, 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/
|
|
|
|
|
|
|
|
/// @file audio_provider_convert.cpp
|
|
|
|
/// @brief Intermediate sample format-converting audio provider
|
|
|
|
/// @ingroup audio_input
|
|
|
|
///
|
2008-01-19 03:18:08 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-01-19 03:18:08 +01:00
|
|
|
#include "audio_provider_convert.h"
|
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
#include "aegisub_endian.h"
|
2012-01-08 02:33:39 +01:00
|
|
|
#include "audio_controller.h"
|
2011-10-29 05:46:36 +02:00
|
|
|
#include "include/aegisub/audio_provider.h"
|
|
|
|
|
|
|
|
#include <libaegisub/scoped_ptr.h>
|
2012-12-27 17:42:10 +01:00
|
|
|
#include <libaegisub/log.h>
|
2011-10-29 05:46:36 +02:00
|
|
|
|
2012-06-13 17:58:28 +02:00
|
|
|
#include <limits>
|
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
/// Base class for all wrapping converters
|
|
|
|
class AudioProviderConverter : public AudioProvider {
|
|
|
|
protected:
|
|
|
|
agi::scoped_ptr<AudioProvider> source;
|
|
|
|
public:
|
|
|
|
AudioProviderConverter(AudioProvider *src) : source(src) {
|
|
|
|
channels = source->GetChannels();
|
|
|
|
num_samples = source->GetNumSamples();
|
|
|
|
sample_rate = source->GetSampleRate();
|
|
|
|
bytes_per_sample = source->GetBytesPerSample();
|
2012-06-13 17:58:28 +02:00
|
|
|
float_samples = source->AreSamplesFloat();
|
2011-10-29 05:46:36 +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
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
bool AreSamplesNativeEndian() const { return true; }
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::path GetFilename() const { return source->GetFilename(); }
|
2011-10-29 05:46:36 +02:00
|
|
|
};
|
2008-01-19 03:18:08 +01:00
|
|
|
|
2012-06-13 17:58:28 +02:00
|
|
|
/// Anything integral -> 16 bit signed machine-endian audio converter
|
2011-10-29 05:46:51 +02:00
|
|
|
template<class Target>
|
|
|
|
class BitdepthConvertAudioProvider : public AudioProviderConverter {
|
2011-10-29 05:46:44 +02:00
|
|
|
int src_bytes_per_sample;
|
|
|
|
bool src_is_native_endian;
|
|
|
|
public:
|
2011-10-29 05:46:51 +02:00
|
|
|
BitdepthConvertAudioProvider(AudioProvider *src) : AudioProviderConverter(src) {
|
|
|
|
if (bytes_per_sample > 8)
|
2012-01-08 02:33:39 +01:00
|
|
|
throw agi::AudioProviderOpenError("Audio format converter: audio with bitdepths greater than 64 bits/sample is currently unsupported", 0);
|
2011-10-29 05:46:44 +02:00
|
|
|
|
|
|
|
src_is_native_endian = src->AreSamplesNativeEndian();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-18 05:13:44 +02:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const {
|
2011-10-29 05:47:02 +02:00
|
|
|
std::vector<char> src_buf(count * src_bytes_per_sample * channels);
|
2011-10-29 05:46:44 +02:00
|
|
|
source->GetAudio(&src_buf[0], start, count);
|
|
|
|
|
|
|
|
int16_t *dest = reinterpret_cast<int16_t*>(buf);
|
|
|
|
|
|
|
|
for (int64_t i = 0; i < count * channels; ++i) {
|
|
|
|
int64_t sample = 0;
|
|
|
|
char *sample_ptr = (char*)&sample;
|
|
|
|
char *src = &src_buf[i * src_bytes_per_sample];
|
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)
|
|
|
|
*sample_ptr = static_cast<uint8_t>(*src) - 127;
|
|
|
|
else if (src_is_native_endian) {
|
2011-10-29 05:46:44 +02:00
|
|
|
#ifdef HAVE_LITTLE_ENDIAN
|
|
|
|
memcpy(sample_ptr, src, src_bytes_per_sample);
|
|
|
|
#else
|
|
|
|
memcpy(sample_ptr + sizeof(int64_t) - src_bytes_per_sample, src, src_bytes_per_sample);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (int byte_index = 0; i < src_bytes_per_sample; ++i) {
|
|
|
|
#ifdef HAVE_LITTLE_ENDIAN
|
|
|
|
sample_ptr[byte_index] = src[src_bytes_per_sample - byte_index - 1];
|
|
|
|
#else
|
|
|
|
sample_ptr[sizeof(int64_t) - byte_index - 1] = src[byte_index];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-07 07:18:34 +01:00
|
|
|
if (static_cast<size_t>(src_bytes_per_sample) > sizeof(Target))
|
2011-10-29 05:46:51 +02:00
|
|
|
sample >>= (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))
|
2011-10-29 05:46:51 +02:00
|
|
|
sample <<= (sizeof(Target) - src_bytes_per_sample ) * 8;
|
|
|
|
|
|
|
|
dest[i] = (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>
|
|
|
|
class FloatConvertAudioProvider : public AudioProviderConverter {
|
|
|
|
public:
|
|
|
|
FloatConvertAudioProvider(AudioProvider *src) : AudioProviderConverter(src) {
|
|
|
|
if (!src->AreSamplesNativeEndian())
|
|
|
|
throw agi::AudioProviderOpenError("Audio format converter: Float audio with non-native endianness is currently unsupported.", 0);
|
|
|
|
bytes_per_sample = sizeof(Target);
|
|
|
|
float_samples = false;
|
|
|
|
}
|
|
|
|
|
2012-08-18 05:13:44 +02:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const {
|
2012-06-13 17:58:28 +02:00
|
|
|
std::vector<Source> src_buf(count * channels);
|
|
|
|
source->GetAudio(&src_buf[0], start, count);
|
|
|
|
|
|
|
|
Target *dest = reinterpret_cast<Target*>(buf);
|
|
|
|
|
|
|
|
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
|
|
|
|
class DownmixAudioProvider : public AudioProviderConverter {
|
|
|
|
int src_channels;
|
|
|
|
public:
|
|
|
|
DownmixAudioProvider(AudioProvider *src) : AudioProviderConverter(src) {
|
|
|
|
if (bytes_per_sample != 2)
|
|
|
|
throw agi::InternalError("DownmixAudioProvider requires 16-bit input", 0);
|
|
|
|
if (channels == 1)
|
|
|
|
throw agi::InternalError("DownmixAudioProvider requires multi-channel input", 0);
|
|
|
|
src_channels = channels;
|
|
|
|
channels = 1;
|
2009-06-05 01:02:29 +02:00
|
|
|
}
|
|
|
|
|
2012-08-18 05:13:44 +02:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const {
|
2011-10-29 05:46:36 +02:00
|
|
|
if (count == 0) return;
|
2009-06-05 01:02:29 +02:00
|
|
|
|
2011-10-29 05:47:02 +02:00
|
|
|
std::vector<int16_t> src_buf(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
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
int16_t *dst = reinterpret_cast<int16_t*>(buf);
|
|
|
|
// 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
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
/// Sample doubler with linear interpolation for the new samples
|
|
|
|
/// Requires 16-bit mono input
|
|
|
|
class SampleDoublingAudioProvider : public AudioProviderConverter {
|
|
|
|
public:
|
|
|
|
SampleDoublingAudioProvider(AudioProvider *src) : AudioProviderConverter(src) {
|
|
|
|
if (src->GetBytesPerSample() != 2)
|
|
|
|
throw agi::InternalError("UpsampleAudioProvider requires 16-bit input", 0);
|
|
|
|
if (src->GetChannels() != 1)
|
|
|
|
throw agi::InternalError("UpsampleAudioProvider requires mono input", 0);
|
|
|
|
|
|
|
|
sample_rate *= 2;
|
|
|
|
num_samples *= 2;
|
2008-01-19 03:18:08 +01:00
|
|
|
}
|
2008-01-19 03:38:31 +01:00
|
|
|
|
2012-08-18 05:13:44 +02:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const {
|
2011-10-29 05:46:36 +02:00
|
|
|
if (count == 0) return;
|
2008-01-25 21:53:12 +01:00
|
|
|
|
2011-12-26 23:20:49 +01:00
|
|
|
int not_end = start + count < num_samples;
|
2011-10-29 05:46:36 +02:00
|
|
|
int64_t src_count = count / 2;
|
|
|
|
source->GetAudio(buf, start / 2, src_count + not_end);
|
2008-01-25 21:53:12 +01:00
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
int16_t *buf16 = reinterpret_cast<int16_t*>(buf);
|
|
|
|
|
|
|
|
if (!not_end) {
|
|
|
|
// We weren't able to request a sample past the end so just
|
|
|
|
// duplicate the last sample
|
|
|
|
buf16[src_count] = buf16[src_count + 1];
|
2008-01-25 21:53:12 +01:00
|
|
|
}
|
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
if (count % 2)
|
|
|
|
buf16[count - 1] = buf16[src_count];
|
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
|
|
|
|
for (int64_t i = src_count - 1; i >= 0; --i) {
|
|
|
|
buf16[i * 2] = buf16[i];
|
|
|
|
buf16[i * 2 + 1] = (int16_t)(((int32_t)buf16[i] + buf16[i + 1]) / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
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
|
|
|
|
2008-07-16 15:22:06 +02:00
|
|
|
AudioProvider *CreateConvertAudioProvider(AudioProvider *source_provider) {
|
|
|
|
AudioProvider *provider = source_provider;
|
2009-06-05 01:02:29 +02:00
|
|
|
|
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))
|
|
|
|
provider = new FloatConvertAudioProvider<float, int16_t>(provider);
|
|
|
|
else
|
|
|
|
provider = new FloatConvertAudioProvider<double, int16_t>(provider);
|
|
|
|
}
|
2012-12-27 17:42:10 +01:00
|
|
|
if (provider->GetBytesPerSample() != 2 || !provider->AreSamplesNativeEndian()) {
|
|
|
|
LOG_D("audio_provider") << "Converting " << provider->GetBytesPerSample() << " bytes per sample or wrong endian to S16";
|
2011-10-29 05:46:51 +02:00
|
|
|
provider = new BitdepthConvertAudioProvider<int16_t>(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";
|
2011-10-29 05:46:36 +02:00
|
|
|
provider = new DownmixAudioProvider(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";
|
2011-10-29 05:46:36 +02:00
|
|
|
provider = new SampleDoublingAudioProvider(provider);
|
2012-12-27 17:42:10 +01:00
|
|
|
}
|
2008-07-16 15:22:06 +02:00
|
|
|
|
|
|
|
return provider;
|
|
|
|
}
|