Rewrite the audio format converters, splitting each conversion into its own class

Originally committed to SVN as r5796.
This commit is contained in:
Thomas Goyne 2011-10-29 03:46:36 +00:00
parent 9a8fb66fbf
commit 7f06849531
8 changed files with 157 additions and 437 deletions

View File

@ -465,14 +465,6 @@
RelativePath="..\..\src\audio_provider_convert.h" RelativePath="..\..\src\audio_provider_convert.h"
> >
</File> </File>
<File
RelativePath="..\..\src\audio_provider_downmix.cpp"
>
</File>
<File
RelativePath="..\..\src\audio_provider_downmix.h"
>
</File>
<File <File
RelativePath="..\..\src\audio_provider_dummy.cpp" RelativePath="..\..\src\audio_provider_dummy.cpp"
> >

View File

@ -76,7 +76,6 @@
<ClInclude Include="$(SrcDir)audio_player_pulse.h" /> <ClInclude Include="$(SrcDir)audio_player_pulse.h" />
<ClInclude Include="$(SrcDir)audio_provider_avs.h" /> <ClInclude Include="$(SrcDir)audio_provider_avs.h" />
<ClInclude Include="$(SrcDir)audio_provider_convert.h" /> <ClInclude Include="$(SrcDir)audio_provider_convert.h" />
<ClInclude Include="$(SrcDir)audio_provider_downmix.h" />
<ClInclude Include="$(SrcDir)audio_provider_dummy.h" /> <ClInclude Include="$(SrcDir)audio_provider_dummy.h" />
<ClInclude Include="$(SrcDir)audio_provider_ffmpegsource.h" /> <ClInclude Include="$(SrcDir)audio_provider_ffmpegsource.h" />
<ClInclude Include="$(SrcDir)audio_provider_hd.h" /> <ClInclude Include="$(SrcDir)audio_provider_hd.h" />
@ -252,7 +251,6 @@
<ClCompile Include="$(SrcDir)audio_provider.cpp" /> <ClCompile Include="$(SrcDir)audio_provider.cpp" />
<ClCompile Include="$(SrcDir)audio_provider_avs.cpp" /> <ClCompile Include="$(SrcDir)audio_provider_avs.cpp" />
<ClCompile Include="$(SrcDir)audio_provider_convert.cpp" /> <ClCompile Include="$(SrcDir)audio_provider_convert.cpp" />
<ClCompile Include="$(SrcDir)audio_provider_downmix.cpp" />
<ClCompile Include="$(SrcDir)audio_provider_dummy.cpp" /> <ClCompile Include="$(SrcDir)audio_provider_dummy.cpp" />
<ClCompile Include="$(SrcDir)audio_provider_ffmpegsource.cpp" /> <ClCompile Include="$(SrcDir)audio_provider_ffmpegsource.cpp" />
<ClCompile Include="$(SrcDir)audio_provider_hd.cpp" /> <ClCompile Include="$(SrcDir)audio_provider_hd.cpp" />

View File

@ -183,9 +183,6 @@
<ClInclude Include="$(SrcDir)audio_provider_convert.h"> <ClInclude Include="$(SrcDir)audio_provider_convert.h">
<Filter>Audio\Providers</Filter> <Filter>Audio\Providers</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="$(SrcDir)audio_provider_downmix.h">
<Filter>Audio\Providers</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)audio_provider_dummy.h"> <ClInclude Include="$(SrcDir)audio_provider_dummy.h">
<Filter>Audio\Providers</Filter> <Filter>Audio\Providers</Filter>
</ClInclude> </ClInclude>
@ -692,9 +689,6 @@
<ClCompile Include="$(SrcDir)audio_provider_convert.cpp"> <ClCompile Include="$(SrcDir)audio_provider_convert.cpp">
<Filter>Audio\Providers</Filter> <Filter>Audio\Providers</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="$(SrcDir)audio_provider_downmix.cpp">
<Filter>Audio\Providers</Filter>
</ClCompile>
<ClCompile Include="$(SrcDir)audio_provider_dummy.cpp"> <ClCompile Include="$(SrcDir)audio_provider_dummy.cpp">
<Filter>Audio\Providers</Filter> <Filter>Audio\Providers</Filter>
</ClCompile> </ClCompile>

View File

@ -134,7 +134,6 @@ SRC += \
audio_player.cpp \ audio_player.cpp \
audio_provider.cpp \ audio_provider.cpp \
audio_provider_convert.cpp \ audio_provider_convert.cpp \
audio_provider_downmix.cpp \
audio_provider_hd.cpp \ audio_provider_hd.cpp \
audio_provider_pcm.cpp \ audio_provider_pcm.cpp \
audio_provider_ram.cpp \ audio_provider_ram.cpp \

View File

@ -1,29 +1,16 @@
// Copyright (c) 2008, Rodrigo Braz Monteiro // Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
// All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Permission to use, copy, modify, and distribute this software for any
// modification, are permitted provided that the following conditions are met: // purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
// //
// * Redistributions of source code must retain the above copyright notice, // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// this list of conditions and the following disclaimer. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// * Redistributions in binary form must reproduce the above copyright notice, // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// this list of conditions and the following disclaimer in the documentation // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// and/or other materials provided with the distribution. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// * Neither the name of the Aegisub Group nor the names of its contributors // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// may be used to endorse or promote products derived from this software // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// //
// Aegisub Project http://www.aegisub.org/ // Aegisub Project http://www.aegisub.org/
// //
@ -36,185 +23,164 @@
#include "config.h" #include "config.h"
#include "aegisub_endian.h"
#include "audio_provider_convert.h" #include "audio_provider_convert.h"
#include "audio_provider_downmix.h"
#include "aegisub_endian.h"
#include "include/aegisub/audio_provider.h"
/// @brief Constructor #include <libaegisub/scoped_ptr.h>
/// @param src
///
ConvertAudioProvider::ConvertAudioProvider(AudioProvider *src) : source(src) {
channels = source->GetChannels();
num_samples = source->GetNumSamples();
sample_rate = source->GetSampleRate();
bytes_per_sample = 2;
sampleMult = 1; /// Base class for all wrapping converters
if (sample_rate < 16000) sampleMult = 4; class AudioProviderConverter : public AudioProvider {
else if (sample_rate < 32000) sampleMult = 2; protected:
sample_rate *= sampleMult; agi::scoped_ptr<AudioProvider> source;
num_samples *= sampleMult; public:
} AudioProviderConverter(AudioProvider *src) : source(src) {
channels = source->GetChannels();
/// @brief Convert to 16-bit num_samples = source->GetNumSamples();
/// @param src sample_rate = source->GetSampleRate();
/// @param dst bytes_per_sample = source->GetBytesPerSample();
/// @param count
///
void ConvertAudioProvider::Make16Bit(const char *src, short *dst, int64_t count) const {
for (int64_t i=0;i<count;i++) {
dst[i] = (short(src[i])-128)*255;
} }
}
////////////////////// bool AreSamplesNativeEndian() const { return true; }
// Change sample rate wxString GetFilename() const { return source->GetFilename(); }
// This requres 16-bit input };
// The SampleConverter is a class overloading operator() with a function from short to short
template<class SampleConverter>
/// @brief DOCME /// Endian swap converter for 16-bit audio
/// @param src class EndianSwapAudioProvider : public AudioProviderConverter {
/// @param dst public:
/// @param count EndianSwapAudioProvider(AudioProvider *src) : AudioProviderConverter(src) {
/// @param converter if (src->GetBytesPerSample() != 2)
/// throw agi::InternalError("EndianSwapAudioProvider only supports 16-bit audio", 0);
void ConvertAudioProvider::ChangeSampleRate(const short *src, short *dst, int64_t count, const SampleConverter &converter) const { if (src->AreSamplesNativeEndian())
// Upsample by 2 throw agi::InternalError("EndianSwapAudioProvider used on provider that doesn't need it", 0);
if (sampleMult == 2) { }
int64_t size = count/2;
short cur; void GetAudio(void *buf, int64_t start, int64_t count) const {
short next = 0; source->GetAudio(buf, start, count);
for (int64_t i=0;i<size;i++) { count *= channels;
cur = next; int16_t *buf16 = reinterpret_cast<int16_t*>(buf);
next = converter(*src++); for (int64_t i = 0; i < count; ++i)
*(dst++) = cur; buf16[i] = Endian::Reverse((uint16_t)buf16[i]);
*(dst++) = (cur+next)/2; }
};
/// 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;
}
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];
} }
if (count%2) *(dst++) = next; }
};
/// 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;
} }
// Upsample by 4 void GetAudio(void *buf, int64_t start, int64_t count) const {
else if (sampleMult == 4) { if (count == 0) return;
int64_t size = count/4;
short cur;
short next = 0;
for (int64_t i=0;i<size;i++) {
cur = next;
next = converter(*src++);
*(dst++) = cur;
*(dst++) = (cur*3+next)/4;
*(dst++) = (cur+next)/2;
*(dst++) = (cur+next*3)/4;
}
for (int i=0;i<count%4;i++) *(dst++) = next;
}
// Nothing much to do, just ensure correct endedness src_buf.resize(count * src_channels);
else if (sampleMult == 1) { source->GetAudio(&src_buf[0], start, count);
int16_t *dst = reinterpret_cast<int16_t*>(buf);
// Just average the channels together
while (count-- > 0) { while (count-- > 0) {
*dst++ = converter(*src++); 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);
} }
} }
}
/// DOCME
struct NullSampleConverter {
inline short operator()(const short val) const {
return val;
}
}; };
/// DOCME /// Sample doubler with linear interpolation for the new samples
struct EndianSwapSampleConverter { /// Requires 16-bit mono input
inline short operator()(const short val) const { class SampleDoublingAudioProvider : public AudioProviderConverter {
return (short)Endian::Reverse((uint16_t)val); 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;
}
void GetAudio(void *buf, int64_t start, int64_t count) const {
if (count == 0) return;
bool not_end = start + count < num_samples;
int64_t src_count = count / 2;
source->GetAudio(buf, start / 2, src_count + not_end);
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];
}
if (count % 2)
buf16[count - 1] = buf16[src_count];
// 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);
}
}
}; };
/// @brief Get audio
/// @param destination
/// @param start
/// @param count
///
void ConvertAudioProvider::GetAudio(void *destination, int64_t start, int64_t count) const {
// Bits per sample
int srcBps = source->GetBytesPerSample();
// Nothing to do
if (sampleMult == 1 && srcBps == 2) {
source->GetAudio(destination,start,count);
}
// Convert
else {
// Allocate buffers with sufficient size for the entire operation
size_t fullSize = count;
int64_t srcCount = count / sampleMult;
short *buffer1 = NULL;
short *buffer2 = NULL;
short *last = NULL;
// Read audio
buffer1 = new short[fullSize * channels];
source->GetAudio(buffer1,start/sampleMult,srcCount);
// Convert from 8-bit to 16-bit
if (srcBps == 1) {
if (sampleMult == 1) {
Make16Bit((const char*)buffer1,(short*)destination,srcCount * channels);
}
else {
buffer2 = new short[fullSize * channels];
Make16Bit((const char*)buffer1,buffer2,srcCount * channels);
last = buffer2;
}
}
// Already 16-bit
else if (srcBps == 2) last = buffer1;
// Convert sample rate
if (sampleMult != 1 && source->AreSamplesNativeEndian()) {
ChangeSampleRate(last,(short*)destination,count * channels, NullSampleConverter());
}
else if (!source->AreSamplesNativeEndian()) {
ChangeSampleRate(last,(short*)destination,count * channels, EndianSwapSampleConverter());
}
delete [] buffer1;
delete [] buffer2;
}
}
/// @brief See if we need to downmix the number of channels
/// @param source_provider
///
AudioProvider *CreateConvertAudioProvider(AudioProvider *source_provider) { AudioProvider *CreateConvertAudioProvider(AudioProvider *source_provider) {
AudioProvider *provider = source_provider; AudioProvider *provider = source_provider;
// Aegisub requires 16 bit samples, // Ensure 16-bit audio with proper endianness
// some audio players break with low samplerates, switch (provider->GetBytesPerSample()) {
// everything breaks with wrong-ended samples. case 1:
if (provider->GetBytesPerSample() != 2 || provider = new UpconvertAudioProvider(provider);
provider->GetSampleRate() < 32000 || break;
!provider->AreSamplesNativeEndian()) case 2:
{ if (!provider->AreSamplesNativeEndian())
// @todo add support for more bitdepths (i.e. 24- and 32-bit audio) provider = new EndianSwapAudioProvider(provider);
if (provider->GetBytesPerSample() > 2) break;
default:
throw AudioOpenError("Audio format converter: audio with bitdepths greater than 16 bits/sample is currently unsupported"); throw AudioOpenError("Audio format converter: audio with bitdepths greater than 16 bits/sample is currently unsupported");
provider = new ConvertAudioProvider(provider);
} }
// We also require mono audio for historical reasons // We currently only support mono audio
if (provider->GetChannels() != 1) if (provider->GetChannels() != 1)
{ provider = new DownmixAudioProvider(provider);
provider = new DownmixingAudioProvider(provider);
} // Some players don't like low sample rate audio
while (provider->GetSampleRate() < 32000)
provider = new SampleDoublingAudioProvider(provider);
return provider; return provider;
} }

View File

@ -1,29 +1,16 @@
// Copyright (c) 2008, Rodrigo Braz Monteiro // Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
// All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Permission to use, copy, modify, and distribute this software for any
// modification, are permitted provided that the following conditions are met: // purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
// //
// * Redistributions of source code must retain the above copyright notice, // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// this list of conditions and the following disclaimer. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// * Redistributions in binary form must reproduce the above copyright notice, // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// this list of conditions and the following disclaimer in the documentation // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// and/or other materials provided with the distribution. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// * Neither the name of the Aegisub Group nor the names of its contributors // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// may be used to endorse or promote products derived from this software // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// //
// Aegisub Project http://www.aegisub.org/ // Aegisub Project http://www.aegisub.org/
// //
@ -34,37 +21,7 @@
/// @ingroup audio_input /// @ingroup audio_input
/// ///
#include "include/aegisub/audio_provider.h" class AudioProvider;
#ifndef AGI_PRE
#include <tr1/memory>
#endif
/// DOCME
/// @class ConvertAudioProvider
/// @brief DOCME
///
/// DOCME
class ConvertAudioProvider : public AudioProvider {
/// DOCME
int sampleMult;
/// DOCME
std::tr1::shared_ptr<AudioProvider> source;
void Make16Bit(const char *src, short *dst, int64_t count) const;
template<class SampleConverter>
void ChangeSampleRate(const short *src, short *dst, int64_t count, const SampleConverter &converter) const;
public:
ConvertAudioProvider(AudioProvider *source);
/// By its nature, the ConvertAudioProvider always delivers machine endian.
/// That's one of the points of it!
bool AreSamplesNativeEndian() const { return true; }
void GetAudio(void *buf, int64_t start, int64_t count) const;
wxString GetFilename() const { return source->GetFilename(); }
};
/// Get an audio provider which supplies audio in a format supported by Aegisub's players
AudioProvider *CreateConvertAudioProvider(AudioProvider *source_provider); AudioProvider *CreateConvertAudioProvider(AudioProvider *source_provider);

View File

@ -1,125 +0,0 @@
// Copyright (c) 2007-2008, Niels Martin Hansen
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Aegisub Project http://www.aegisub.org/
//
// $Id$
/// @file audio_provider_downmix.cpp
/// @brief Intermediate audio provider downmixing the signal to mono
/// @ingroup audio_input
///
//////////////////
// Headers
#include "config.h"
#include "audio_provider_downmix.h"
/// @brief Constructor
/// @param source
///
DownmixingAudioProvider::DownmixingAudioProvider(AudioProvider *source) : provider(source) {
filename = source->GetFilename();
channels = 1; // target
src_channels = source->GetChannels();
num_samples = source->GetNumSamples();
bytes_per_sample = source->GetBytesPerSample();
sample_rate = source->GetSampleRate();
if (!(bytes_per_sample == 1 || bytes_per_sample == 2))
throw AudioOpenError("Downmixing Audio Provider: Can only downmix 8 and 16 bit audio");
if (!source->AreSamplesNativeEndian())
throw AudioOpenError("Downmixing Audio Provider: Source must have machine endian samples");
}
/// @brief Actual work happens here
/// @param buf
/// @param start
/// @param count
///
void DownmixingAudioProvider::GetAudio(void *buf, int64_t start, int64_t count) const {
if (count == 0) return;
// We can do this ourselves
if (start >= num_samples) {
if (bytes_per_sample == 1)
// 8 bit formats are usually unsigned with bias 127
memset(buf, 127, count);
else
// While everything else is signed
memset(buf, 0, count*bytes_per_sample);
return;
}
// So alloc some temporary memory for this
// Depending on use, this might be made faster by using
// a pre-allocced block of memory...?
char *tmp = new char[count*bytes_per_sample*src_channels];
try {
provider->GetAudio(tmp, start, count);
}
catch (...) {
delete tmp;
throw;
}
// Now downmix
// Just average the samples over the channels (really bad if they're out of phase!)
// XXX: Assuming here that sample data are in machine endian, an upstream provider should ensure that
if (bytes_per_sample == 1) {
uint8_t *src = (uint8_t *)tmp;
uint8_t *dst = (uint8_t *)buf;
while (count > 0) {
int sum = 0;
for (int c = 0; c < src_channels; c++)
sum += *(src++);
*(dst++) = (uint8_t)(sum / src_channels);
count--;
}
}
else if (bytes_per_sample == 2) {
int16_t *src = (int16_t *)tmp;
int16_t *dst = (int16_t *)buf;
while (count > 0) {
int sum = 0;
for (int c = 0; c < src_channels; c++)
sum += *(src++);
*(dst++) = (int16_t)(sum / src_channels);
count--;
}
}
// Done downmixing, free the work buffer
delete[] tmp;
}

View File

@ -1,61 +0,0 @@
// Copyright (c) 2007-2008, Niels Martin Hansen
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Aegisub Project http://www.aegisub.org/
//
// $Id$
/// @file audio_provider_downmix.h
/// @see audio_provider_downmix.cpp
/// @ingroup audio_input
///
#include "include/aegisub/audio_provider.h"
#ifndef AGI_PRE
#include <tr1/memory>
#endif
/// DOCME
/// @class DownmixingAudioProvider
/// @brief DOCME
///
/// DOCME
class DownmixingAudioProvider : public AudioProvider {
std::tr1::shared_ptr<AudioProvider> provider;
/// DOCME
int src_channels;
public:
DownmixingAudioProvider(AudioProvider *source);
/// @brief Downmixing requires samples to be native endian beforehand
///
bool AreSamplesNativeEndian() const { return true; }
void GetAudio(void *buf, int64_t start, int64_t count) const;
};