2008-01-19 03:18:08 +01:00
|
|
|
// Copyright (c) 2008, Rodrigo Braz Monteiro
|
|
|
|
// 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.
|
|
|
|
//
|
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"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "aegisub_endian.h"
|
2008-01-19 03:18:08 +01:00
|
|
|
#include "audio_provider_convert.h"
|
2008-07-16 15:22:06 +02:00
|
|
|
#include "audio_provider_downmix.h"
|
2008-01-19 03:18:08 +01: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
|
|
|
|
|
|
|
/// @brief Constructor
|
|
|
|
/// @param src
|
|
|
|
///
|
2010-08-02 08:32:01 +02:00
|
|
|
ConvertAudioProvider::ConvertAudioProvider(AudioProvider *src) : source(src) {
|
2008-07-16 15:22:06 +02:00
|
|
|
channels = source->GetChannels();
|
2008-01-19 03:18:08 +01:00
|
|
|
num_samples = source->GetNumSamples();
|
|
|
|
sample_rate = source->GetSampleRate();
|
|
|
|
bytes_per_sample = 2;
|
2008-01-25 21:53:12 +01:00
|
|
|
|
|
|
|
sampleMult = 1;
|
|
|
|
if (sample_rate < 16000) sampleMult = 4;
|
|
|
|
else if (sample_rate < 32000) sampleMult = 2;
|
|
|
|
sample_rate *= sampleMult;
|
|
|
|
num_samples *= sampleMult;
|
2008-01-19 03:18:08 +01: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
|
|
|
/// @brief Convert to 16-bit
|
|
|
|
/// @param src
|
|
|
|
/// @param dst
|
|
|
|
/// @param count
|
|
|
|
///
|
2008-01-25 21:53:12 +01:00
|
|
|
void ConvertAudioProvider::Make16Bit(const char *src, short *dst, int64_t count) {
|
|
|
|
for (int64_t i=0;i<count;i++) {
|
2009-06-05 01:02:29 +02:00
|
|
|
dst[i] = (short(src[i])-128)*255;
|
2008-01-25 21:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
// Change sample rate
|
|
|
|
// This requres 16-bit input
|
2009-06-05 01:02:29 +02:00
|
|
|
// The SampleConverter is a class overloading operator() with a function from short to short
|
|
|
|
template<class SampleConverter>
|
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
|
|
|
|
|
|
|
/// @brief DOCME
|
|
|
|
/// @param src
|
|
|
|
/// @param dst
|
|
|
|
/// @param count
|
|
|
|
/// @param converter
|
|
|
|
///
|
2009-06-05 01:02:29 +02:00
|
|
|
void ConvertAudioProvider::ChangeSampleRate(const short *src, short *dst, int64_t count, const SampleConverter &converter) {
|
2008-01-25 21:53:12 +01:00
|
|
|
// Upsample by 2
|
|
|
|
if (sampleMult == 2) {
|
|
|
|
int64_t size = count/2;
|
|
|
|
short cur;
|
|
|
|
short next = 0;
|
|
|
|
for (int64_t i=0;i<size;i++) {
|
|
|
|
cur = next;
|
2009-06-05 01:02:29 +02:00
|
|
|
next = converter(*src++);
|
2008-01-25 21:53:12 +01:00
|
|
|
*(dst++) = cur;
|
|
|
|
*(dst++) = (cur+next)/2;
|
|
|
|
}
|
|
|
|
if (count%2) *(dst++) = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upsample by 4
|
|
|
|
else if (sampleMult == 4) {
|
|
|
|
int64_t size = count/4;
|
|
|
|
short cur;
|
|
|
|
short next = 0;
|
|
|
|
for (int64_t i=0;i<size;i++) {
|
|
|
|
cur = next;
|
2009-06-05 01:02:29 +02:00
|
|
|
next = converter(*src++);
|
2008-01-25 21:53:12 +01:00
|
|
|
*(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;
|
|
|
|
}
|
|
|
|
|
2009-06-05 01:02:29 +02:00
|
|
|
// Nothing much to do, just ensure correct endedness
|
|
|
|
else if (sampleMult == 1) {
|
|
|
|
while (count-- > 0) {
|
|
|
|
*dst++ = converter(*src++);
|
|
|
|
}
|
|
|
|
}
|
2008-01-25 21:53:12 +01: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
|
|
|
/// DOCME
|
2009-06-05 01:02:29 +02:00
|
|
|
struct NullSampleConverter {
|
|
|
|
inline short operator()(const short val) const {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
/// DOCME
|
2009-06-05 01:02:29 +02:00
|
|
|
struct EndianSwapSampleConverter {
|
|
|
|
inline short operator()(const short val) const {
|
|
|
|
return (short)Endian::Reverse((uint16_t)val);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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
|
|
|
/// @brief Get audio
|
|
|
|
/// @param destination
|
|
|
|
/// @param start
|
|
|
|
/// @param count
|
|
|
|
///
|
2008-01-19 03:18:08 +01:00
|
|
|
void ConvertAudioProvider::GetAudio(void *destination, int64_t start, int64_t count) {
|
2008-01-19 03:38:31 +01:00
|
|
|
// Bits per sample
|
|
|
|
int srcBps = source->GetBytesPerSample();
|
|
|
|
|
2008-01-25 21:53:12 +01:00
|
|
|
// Nothing to do
|
|
|
|
if (sampleMult == 1 && srcBps == 2) {
|
|
|
|
source->GetAudio(destination,start,count);
|
2008-01-19 03:18:08 +01:00
|
|
|
}
|
2008-01-19 03:38:31 +01:00
|
|
|
|
2008-01-25 21:53:12 +01:00
|
|
|
// 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
|
2009-05-15 02:48:43 +02:00
|
|
|
buffer1 = new short[fullSize * channels];
|
2008-01-25 21:53:12 +01:00
|
|
|
source->GetAudio(buffer1,start/sampleMult,srcCount);
|
2008-01-19 03:38:31 +01:00
|
|
|
|
2008-01-25 21:53:12 +01:00
|
|
|
// Convert from 8-bit to 16-bit
|
|
|
|
if (srcBps == 1) {
|
|
|
|
if (sampleMult == 1) {
|
2009-05-15 02:48:43 +02:00
|
|
|
Make16Bit((const char*)buffer1,(short*)destination,srcCount * channels);
|
2008-01-25 21:53:12 +01:00
|
|
|
}
|
|
|
|
else {
|
2009-05-15 02:48:43 +02:00
|
|
|
buffer2 = new short[fullSize * channels];
|
|
|
|
Make16Bit((const char*)buffer1,buffer2,srcCount * channels);
|
2008-01-25 21:53:12 +01:00
|
|
|
last = buffer2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Already 16-bit
|
|
|
|
else if (srcBps == 2) last = buffer1;
|
|
|
|
|
|
|
|
// Convert sample rate
|
2009-06-05 01:02:29 +02:00
|
|
|
if (sampleMult != 1 && source->AreSamplesNativeEndian()) {
|
|
|
|
ChangeSampleRate(last,(short*)destination,count * channels, NullSampleConverter());
|
|
|
|
}
|
|
|
|
else if (!source->AreSamplesNativeEndian()) {
|
|
|
|
ChangeSampleRate(last,(short*)destination,count * channels, EndianSwapSampleConverter());
|
2008-01-25 21:53:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
delete [] buffer1;
|
|
|
|
delete [] buffer2;
|
|
|
|
}
|
2008-01-19 03:18:08 +01:00
|
|
|
}
|
2008-07-16 15:22:06 +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
|
|
|
|
|
|
|
/// @brief See if we need to downmix the number of channels
|
|
|
|
/// @param source_provider
|
|
|
|
///
|
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
|
|
|
|
|
|
|
// Aegisub requires 16 bit samples,
|
|
|
|
// some audio players break with low samplerates,
|
|
|
|
// everything breaks with wrong-ended samples.
|
|
|
|
if (provider->GetBytesPerSample() != 2 ||
|
|
|
|
provider->GetSampleRate() < 32000 ||
|
|
|
|
!provider->AreSamplesNativeEndian())
|
|
|
|
{
|
2009-09-21 03:52:34 +02:00
|
|
|
// @todo add support for more bitdepths (i.e. 24- and 32-bit audio)
|
|
|
|
if (provider->GetBytesPerSample() > 2)
|
2010-08-02 08:32:01 +02:00
|
|
|
AudioOpenError("Audio format converter: audio with bitdepths greater than 16 bits/sample is currently unsupported");
|
2009-09-21 03:52:34 +02:00
|
|
|
|
2009-05-15 02:48:43 +02:00
|
|
|
provider = new ConvertAudioProvider(provider);
|
2009-06-05 01:02:29 +02:00
|
|
|
}
|
2008-07-16 15:22:06 +02:00
|
|
|
|
2009-06-05 01:02:29 +02:00
|
|
|
// We also require mono audio for historical reasons
|
2008-07-16 15:22:06 +02:00
|
|
|
if (provider->GetChannels() != 1)
|
2009-06-05 01:02:29 +02:00
|
|
|
{
|
2008-07-16 15:22:06 +02:00
|
|
|
provider = new DownmixingAudioProvider(provider);
|
2009-06-05 01:02:29 +02:00
|
|
|
}
|
2008-07-16 15:22:06 +02:00
|
|
|
|
|
|
|
return provider;
|
|
|
|
}
|