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/
|
2008-01-19 03:18:08 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @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"
|
|
|
|
#include "include/aegisub/audio_provider.h"
|
|
|
|
|
|
|
|
#include <libaegisub/scoped_ptr.h>
|
|
|
|
|
|
|
|
/// 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();
|
|
|
|
}
|
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; }
|
|
|
|
wxString GetFilename() const { return source->GetFilename(); }
|
|
|
|
};
|
2008-01-19 03:18:08 +01:00
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
/// Endian swap converter for 16-bit audio
|
|
|
|
class EndianSwapAudioProvider : public AudioProviderConverter {
|
|
|
|
public:
|
|
|
|
EndianSwapAudioProvider(AudioProvider *src) : AudioProviderConverter(src) {
|
|
|
|
if (src->GetBytesPerSample() != 2)
|
|
|
|
throw agi::InternalError("EndianSwapAudioProvider only supports 16-bit audio", 0);
|
|
|
|
if (src->AreSamplesNativeEndian())
|
|
|
|
throw agi::InternalError("EndianSwapAudioProvider used on provider that doesn't need it", 0);
|
2008-01-25 21:53:12 +01:00
|
|
|
}
|
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
void GetAudio(void *buf, int64_t start, int64_t count) const {
|
|
|
|
source->GetAudio(buf, start, count);
|
|
|
|
count *= channels;
|
|
|
|
int16_t *buf16 = reinterpret_cast<int16_t*>(buf);
|
|
|
|
for (int64_t i = 0; i < count; ++i)
|
|
|
|
buf16[i] = Endian::Reverse((uint16_t)buf16[i]);
|
2008-01-25 21:53:12 +01:00
|
|
|
}
|
2011-10-29 05:46:36 +02:00
|
|
|
};
|
2008-01-25 21:53:12 +01:00
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
/// unsigned 8 bit -> signed machine-endian 16 bit audio converter
|
|
|
|
class UpconvertAudioProvider : public AudioProviderConverter {
|
|
|
|
public:
|
|
|
|
UpconvertAudioProvider(AudioProvider *src) : AudioProviderConverter(src) {
|
|
|
|
if (src->GetBytesPerSample() != 1)
|
|
|
|
throw agi::InternalError("UpconvertAudioProvider only supports 8-bit input", 0);
|
|
|
|
bytes_per_sample = 2;
|
2008-01-25 21:53:12 +01:00
|
|
|
}
|
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
void GetAudio(void *buf, int64_t start, int64_t count) const {
|
|
|
|
source->GetAudio(buf, start, count);
|
|
|
|
|
|
|
|
count *= channels;
|
|
|
|
int8_t *buf8 = reinterpret_cast<int8_t*>(buf);
|
|
|
|
// walking backwards so that the conversion can be done in place
|
|
|
|
for (int64_t i = count - 1; i >= 0; --i) {
|
|
|
|
buf8[i * 2 + 1] = 0;
|
|
|
|
buf8[i * 2] = buf8[i];
|
2009-06-05 01:02:29 +02:00
|
|
|
}
|
|
|
|
}
|
2011-10-29 05:46:36 +02:00
|
|
|
};
|
2008-01-25 21:53:12 +01:00
|
|
|
|
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 {
|
|
|
|
mutable std::vector<int16_t> src_buf;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
void GetAudio(void *buf, int64_t start, int64_t count) const {
|
|
|
|
if (count == 0) return;
|
2009-06-05 01:02:29 +02:00
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
src_buf.resize(count * src_channels);
|
|
|
|
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
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
void GetAudio(void *buf, int64_t start, int64_t count) const {
|
|
|
|
if (count == 0) return;
|
2008-01-25 21:53:12 +01:00
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
bool not_end = start + count < num_samples;
|
|
|
|
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
|
|
|
|
switch (provider->GetBytesPerSample()) {
|
|
|
|
case 1:
|
|
|
|
provider = new UpconvertAudioProvider(provider);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (!provider->AreSamplesNativeEndian())
|
|
|
|
provider = new EndianSwapAudioProvider(provider);
|
|
|
|
break;
|
|
|
|
default:
|
2010-08-02 10:18:45 +02:00
|
|
|
throw AudioOpenError("Audio format converter: audio with bitdepths greater than 16 bits/sample is currently unsupported");
|
2009-06-05 01:02:29 +02:00
|
|
|
}
|
2008-07-16 15:22:06 +02:00
|
|
|
|
2011-10-29 05:46:36 +02:00
|
|
|
// We currently only support mono audio
|
2008-07-16 15:22:06 +02:00
|
|
|
if (provider->GetChannels() != 1)
|
2011-10-29 05:46:36 +02:00
|
|
|
provider = new DownmixAudioProvider(provider);
|
|
|
|
|
|
|
|
// Some players don't like low sample rate audio
|
|
|
|
while (provider->GetSampleRate() < 32000)
|
|
|
|
provider = new SampleDoublingAudioProvider(provider);
|
2008-07-16 15:22:06 +02:00
|
|
|
|
|
|
|
return provider;
|
|
|
|
}
|