audio-drain/audio/drain/ChannelReorder.cpp

209 lines
6.6 KiB
C++
Raw Permalink Normal View History

/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
2016-10-02 21:41:55 +02:00
#include <audio/drain/ChannelReorder.hpp>
#include "debug.hpp"
2015-04-10 23:00:13 +02:00
audio::drain::ChannelReorder::ChannelReorder() {
}
2015-01-29 21:46:01 +01:00
2015-04-10 23:00:13 +02:00
void audio::drain::ChannelReorder::init() {
audio::drain::Algo::init();
2015-02-01 22:22:42 +01:00
m_type = "ChannelReorder";
2015-01-28 22:07:11 +01:00
}
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<audio::drain::ChannelReorder> audio::drain::ChannelReorder::create() {
ememory::SharedPtr<audio::drain::ChannelReorder> tmp(ETK_NEW(audio::drain::ChannelReorder));
2015-01-29 21:46:01 +01:00
tmp->init();
return tmp;
}
2015-04-10 23:00:13 +02:00
void audio::drain::ChannelReorder::configurationChange() {
audio::drain::AutoLogInOut("ChannelReorder (config)");
audio::drain::Algo::configurationChange();
if (m_input.getFormat() != m_output.getFormat()) {
DRAIN_ERROR("can not support Format Change ...");
m_needProcess = false;
}
if (m_input.getFrequency() != m_output.getFrequency()) {
DRAIN_ERROR("can not support frequency Change ...");
m_needProcess = false;
}
if (m_input.getMap() == m_output.getMap()) {
// nothing to process...
m_needProcess = false;
DRAIN_INFO(" no need to convert ... " << m_input.getMap() << " ==> " << m_output.getMap());
return;
}
}
2015-04-13 21:49:48 +02:00
bool audio::drain::ChannelReorder::process(audio::Time& _time,
2015-04-10 23:00:13 +02:00
void* _input,
size_t _inputNbChunk,
void*& _output,
size_t& _outputNbChunk) {
audio::drain::AutoLogInOut tmpLog("ChannelReorder");
_outputNbChunk = _inputNbChunk;
2015-01-27 21:26:03 +01:00
// check if we need to process:
if (m_needProcess == false) {
_output = _input;
return true;
}
2018-06-19 22:13:48 +02:00
if (_input == null) {
_output = &(m_outputData[0]);
_outputNbChunk = 0;
DRAIN_ERROR("null pointer input ... ");
return false;
}
m_outputData.resize(_outputNbChunk*m_output.getMap().size()*m_formatSize);
_output = &(m_outputData[0]);
// real process: (only depend of data size):
switch (m_output.getFormat()) {
case audio::format_int8:
{
DRAIN_VERBOSE("convert " << m_input.getMap() << " ==> " << m_output.getMap() << " format=" << int32_t(m_formatSize));
int8_t* in = static_cast<int8_t*>(_input);
int8_t* out = static_cast<int8_t*>(_output);
for (size_t kkk=0; kkk<m_output.getMap().size(); ++kkk) {
int32_t convertId = -1;
if ( m_input.getMap().size() == 1
&& m_input.getMap()[0] == audio::channel_frontCenter) {
convertId = 0;
} else {
for (size_t jjj=0; jjj<m_input.getMap().size(); ++jjj) {
if (m_output.getMap()[kkk] == m_input.getMap()[jjj]) {
convertId = jjj;
break;
}
}
}
DRAIN_VERBOSE(" " << convertId << " ==> " << kkk);
if (convertId == -1) {
for (size_t iii=0; iii<_outputNbChunk; ++iii) {
out[iii*m_output.getMap().size()+kkk] = 0;
}
} else {
for (size_t iii=0; iii<_outputNbChunk; ++iii) {
out[iii*m_output.getMap().size()+kkk] = in[iii*m_input.getMap().size()+convertId];
}
}
}
}
break;
default:
case audio::format_int16:
2015-02-19 22:00:21 +01:00
if (m_output.getMap().size() == 1) {
DRAIN_VERBOSE("convert " << m_input.getMap() << " ==> " << m_output.getMap() << " format=" << int32_t(m_formatSize));
int16_t* in = static_cast<int16_t*>(_input);
int16_t* out = static_cast<int16_t*>(_output);
for (size_t iii=0; iii<_outputNbChunk; ++iii) {
int32_t val = 0;
for (size_t jjj=0; jjj<m_input.getMap().size(); ++jjj) {
val += in[iii*m_input.getMap().size()+jjj];
}
out[iii] = val/m_input.getMap().size();
}
} else {
DRAIN_VERBOSE("convert " << m_input.getMap() << " ==> " << m_output.getMap() << " format=" << int32_t(m_formatSize));
int16_t* in = static_cast<int16_t*>(_input);
int16_t* out = static_cast<int16_t*>(_output);
for (size_t kkk=0; kkk<m_output.getMap().size(); ++kkk) {
int32_t convertId = -1;
if ( m_input.getMap().size() == 1
&& m_input.getMap()[0] == audio::channel_frontCenter) {
convertId = 0;
} else {
for (size_t jjj=0; jjj<m_input.getMap().size(); ++jjj) {
if (m_output.getMap()[kkk] == m_input.getMap()[jjj]) {
convertId = jjj;
break;
}
}
}
DRAIN_VERBOSE(" " << convertId << " ==> " << kkk);
if (convertId == -1) {
for (size_t iii=0; iii<_outputNbChunk; ++iii) {
out[iii*m_output.getMap().size()+kkk] = 0;
}
} else {
for (size_t iii=0; iii<_outputNbChunk; ++iii) {
out[iii*m_output.getMap().size()+kkk] = in[iii*m_input.getMap().size()+convertId];
}
}
}
}
break;
case audio::format_int16_on_int32:
case audio::format_int24:
case audio::format_int32:
case audio::format_float:
{
DRAIN_VERBOSE("convert (2) " << m_input.getMap() << " ==> " << m_output.getMap());
uint32_t* in = static_cast<uint32_t*>(_input);
uint32_t* out = static_cast<uint32_t*>(_output);
for (size_t kkk=0; kkk<m_output.getMap().size(); ++kkk) {
int32_t convertId = -1;
if ( m_input.getMap().size() == 1
&& m_input.getMap()[0] == audio::channel_frontCenter) {
convertId = 0;
} else {
for (size_t jjj=0; jjj<m_input.getMap().size(); ++jjj) {
if (m_output.getMap()[kkk] == m_input.getMap()[jjj]) {
convertId = jjj;
break;
}
}
}
if (convertId == -1) {
for (size_t iii=0; iii<_outputNbChunk; ++iii) {
out[iii*m_output.getMap().size()+kkk] = 0;
}
} else {
for (size_t iii=0; iii<_outputNbChunk; ++iii) {
out[iii*m_output.getMap().size()+kkk] = in[iii*m_input.getMap().size()+convertId];
}
}
}
}
break;
case audio::format_double:
{
DRAIN_VERBOSE("convert (2) " << m_input.getMap() << " ==> " << m_output.getMap());
uint64_t* in = static_cast<uint64_t*>(_input);
uint64_t* out = static_cast<uint64_t*>(_output);
for (size_t kkk=0; kkk<m_output.getMap().size(); ++kkk) {
int32_t convertId = -1;
if ( m_input.getMap().size() == 1
&& m_input.getMap()[0] == audio::channel_frontCenter) {
convertId = 0;
} else {
for (size_t jjj=0; jjj<m_input.getMap().size(); ++jjj) {
if (m_output.getMap()[kkk] == m_input.getMap()[jjj]) {
convertId = jjj;
break;
}
}
}
if (convertId == -1) {
for (size_t iii=0; iii<_outputNbChunk; ++iii) {
out[iii*m_output.getMap().size()+kkk] = 0;
}
} else {
for (size_t iii=0; iii<_outputNbChunk; ++iii) {
out[iii*m_output.getMap().size()+kkk] = in[iii*m_input.getMap().size()+convertId];
}
}
}
}
break;
}
return true;
}