audio-river/test/testFormat.hpp

238 lines
8.5 KiB
C++
Raw Normal View History

2015-03-03 23:17:43 +01:00
/** @file
* @author Edouard DUPIN
* @copyright 2015, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
2015-03-03 23:17:43 +01:00
*/
#pragma once
2015-03-03 23:17:43 +01:00
namespace river_test_format {
2017-08-28 00:08:50 +02:00
static const etk::String configurationRiver =
2015-03-13 23:22:17 +01:00
"{\n"
" speaker:{\n"
" io:'output',\n"
" map-on:{\n"
" interface:'auto',\n"
" name:'default',\n"
" },\n"
" frequency:0,\n"
" channel-map:['front-left', 'front-right'],\n"
" type:'auto',\n"
" nb-chunk:1024,\n"
" volume-name:'MASTER'\n"
" }\n"
"}\n";
2015-03-03 23:17:43 +01:00
class testOutCallbackType {
private:
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<audio::river::Manager> m_manager;
ememory::SharedPtr<audio::river::Interface> m_interface;
2015-03-03 23:17:43 +01:00
double m_phase;
float m_freq;
int32_t m_nbChannels;
float m_generateFreq;
public:
2016-07-19 21:43:58 +02:00
testOutCallbackType(const ememory::SharedPtr<audio::river::Manager>& _manager,
2015-03-03 23:17:43 +01:00
float _freq=48000.0f,
int32_t _nbChannels=2,
audio::format _format=audio::format_int16) :
m_manager(_manager),
m_phase(0),
m_freq(_freq),
m_nbChannels(_nbChannels),
m_generateFreq(550.0f) {
//Set stereo output:
2017-08-28 00:08:50 +02:00
etk::Vector<audio::channel> channelMap;
2015-03-03 23:17:43 +01:00
if (m_nbChannels == 1) {
2017-08-28 00:08:50 +02:00
channelMap.pushBack(audio::channel_frontCenter);
2015-03-03 23:17:43 +01:00
} else if (m_nbChannels == 2) {
2017-08-28 00:08:50 +02:00
channelMap.pushBack(audio::channel_frontLeft);
channelMap.pushBack(audio::channel_frontRight);
2015-03-03 23:17:43 +01:00
} else if (m_nbChannels == 4) {
2017-08-28 00:08:50 +02:00
channelMap.pushBack(audio::channel_frontLeft);
channelMap.pushBack(audio::channel_frontRight);
channelMap.pushBack(audio::channel_rearLeft);
channelMap.pushBack(audio::channel_rearRight);
2015-03-03 23:17:43 +01:00
} else {
2015-10-14 21:21:03 +02:00
TEST_ERROR("Can not generate with channel != 1,2,4");
2015-03-03 23:17:43 +01:00
return;
}
m_interface = m_manager->createOutput(m_freq,
channelMap,
_format,
"speaker",
"WriteModeCallbackType");
if(m_interface == nullptr) {
2015-10-14 21:21:03 +02:00
TEST_ERROR("nullptr interface");
return;
}
2015-03-03 23:17:43 +01:00
// set callback mode ...
m_interface->setOutputCallback(std::bind(&testOutCallbackType::onDataNeeded,
2015-03-03 23:17:43 +01:00
this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3,
std::placeholders::_4,
std::placeholders::_5,
std::placeholders::_6));
2015-03-03 23:17:43 +01:00
}
void onDataNeeded(void* _data,
2015-04-13 21:49:48 +02:00
const audio::Time& _time,
2015-03-03 23:17:43 +01:00
size_t _nbChunk,
enum audio::format _format,
uint32_t _frequency,
2017-08-28 00:08:50 +02:00
const etk::Vector<audio::channel>& _map) {
2015-10-14 21:21:03 +02:00
//TEST_DEBUG("Get data ... " << _format << " map=" << _map << " chunk=" << _nbChunk);
2015-03-03 23:17:43 +01:00
double baseCycle = 2.0*M_PI/double(m_freq) * double(m_generateFreq);
if (_format == audio::format_int16) {
int16_t* data = static_cast<int16_t*>(_data);
for (int32_t iii=0; iii<_nbChunk; iii++) {
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
data[_map.size()*iii+jjj] = cos(m_phase) * double(INT16_MAX);
}
m_phase += baseCycle;
if (m_phase >= 2*M_PI) {
m_phase -= 2*M_PI;
}
}
} else if (_format == audio::format_int16_on_int32) {
int32_t* data = static_cast<int32_t*>(_data);
for (int32_t iii=0; iii<_nbChunk; iii++) {
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
data[_map.size()*iii+jjj] = cos(m_phase) * double(INT16_MAX);
}
m_phase += baseCycle;
if (m_phase >= 2*M_PI) {
m_phase -= 2*M_PI;
}
}
} else if (_format == audio::format_int32) {
int32_t* data = static_cast<int32_t*>(_data);
for (int32_t iii=0; iii<_nbChunk; iii++) {
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
data[_map.size()*iii+jjj] = cos(m_phase) * double(INT32_MAX);
}
m_phase += baseCycle;
if (m_phase >= 2*M_PI) {
m_phase -= 2*M_PI;
}
}
} else if (_format == audio::format_float) {
float* data = static_cast<float*>(_data);
for (int32_t iii=0; iii<_nbChunk; iii++) {
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
data[_map.size()*iii+jjj] = cos(m_phase);
}
m_phase += baseCycle;
if (m_phase >= 2*M_PI) {
m_phase -= 2*M_PI;
}
}
}
}
void run() {
if(m_interface == nullptr) {
2015-10-14 21:21:03 +02:00
TEST_ERROR("nullptr interface");
return;
2015-03-03 23:17:43 +01:00
}
m_interface->start();
// wait 2 second ...
2016-10-13 21:29:18 +02:00
std::this_thread::sleep_for(std::chrono::seconds(1));
m_interface->stop();
2016-10-13 21:29:18 +02:00
std::this_thread::sleep_for(std::chrono::milliseconds(100));
2015-03-03 23:17:43 +01:00
}
};
class testResampling : public ::testing::TestWithParam<float> {};
TEST_P(testResampling, base) {
2015-04-11 09:38:30 +02:00
audio::river::initString(configurationRiver);
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<audio::river::Manager> manager;
2015-04-11 09:38:30 +02:00
manager = audio::river::Manager::create("testApplication");
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<testOutCallbackType> process = ememory::makeShared<testOutCallbackType>(manager, GetParam(), 2, audio::format_int16);
2015-03-03 23:17:43 +01:00
process->run();
process.reset();
2016-10-13 21:29:18 +02:00
std::this_thread::sleep_for(std::chrono::milliseconds(500));
2015-04-11 09:38:30 +02:00
audio::river::unInit();
2015-03-03 23:17:43 +01:00
}
INSTANTIATE_TEST_CASE_P(InstantiationName,
testResampling,
::testing::Values(4000, 8000, 16000, 32000, 48000, 48001, 64000, 96000, 11250, 2250, 44100, 88200));
class testFormat : public ::testing::TestWithParam<audio::format> {};
TEST_P(testFormat, base) {
2015-04-11 09:38:30 +02:00
audio::river::initString(configurationRiver);
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<audio::river::Manager> manager;
2015-04-11 09:38:30 +02:00
manager = audio::river::Manager::create("testApplication");
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<testOutCallbackType> process = ememory::makeShared<testOutCallbackType>(manager, 48000, 2, GetParam());
2015-03-03 23:17:43 +01:00
process->run();
process.reset();
2016-10-13 21:29:18 +02:00
std::this_thread::sleep_for(std::chrono::milliseconds(500));
2015-04-11 09:38:30 +02:00
audio::river::unInit();
2015-03-03 23:17:43 +01:00
}
INSTANTIATE_TEST_CASE_P(InstantiationName,
testFormat,
::testing::Values(audio::format_int16, audio::format_int16_on_int32, audio::format_int32, audio::format_float));
class testChannels : public ::testing::TestWithParam<int32_t> {};
TEST_P(testChannels, base) {
2015-04-11 09:38:30 +02:00
audio::river::initString(configurationRiver);
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<audio::river::Manager> manager;
2015-04-11 09:38:30 +02:00
manager = audio::river::Manager::create("testApplication");
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<testOutCallbackType> process = ememory::makeShared<testOutCallbackType>(manager, 48000, GetParam(), audio::format_int16);
2015-03-03 23:17:43 +01:00
process->run();
process.reset();
2016-10-13 21:29:18 +02:00
std::this_thread::sleep_for(std::chrono::milliseconds(500));
2015-04-11 09:38:30 +02:00
audio::river::unInit();
2015-03-03 23:17:43 +01:00
}
INSTANTIATE_TEST_CASE_P(InstantiationName,
testChannels,
::testing::Values(1,2,4));
TEST(TestALL, testChannelsFormatResampling) {
2015-04-11 09:38:30 +02:00
audio::river::initString(configurationRiver);
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<audio::river::Manager> manager;
2015-04-11 09:38:30 +02:00
manager = audio::river::Manager::create("testApplication");
2015-10-14 21:21:03 +02:00
TEST_INFO("test convert flaot to output (callback mode)");
2017-08-28 00:08:50 +02:00
etk::Vector<float> listFreq;
listFreq.pushBack(4000);
listFreq.pushBack(8000);
listFreq.pushBack(16000);
listFreq.pushBack(32000);
listFreq.pushBack(48000);
listFreq.pushBack(48001);
listFreq.pushBack(64000);
listFreq.pushBack(96000);
listFreq.pushBack(11250);
listFreq.pushBack(2250);
listFreq.pushBack(44100);
listFreq.pushBack(88200);
etk::Vector<int32_t> listChannel;
listChannel.pushBack(1);
listChannel.pushBack(2);
listChannel.pushBack(4);
etk::Vector<audio::format> listFormat;
listFormat.pushBack(audio::format_int16);
listFormat.pushBack(audio::format_int16_on_int32);
listFormat.pushBack(audio::format_int32);
listFormat.pushBack(audio::format_float);
2015-03-03 23:17:43 +01:00
for (size_t fff=0; fff<listFreq.size(); ++fff) {
for (size_t ccc=0; ccc<listChannel.size(); ++ccc) {
for (size_t iii=0; iii<listFormat.size(); ++iii) {
2015-10-14 21:21:03 +02:00
TEST_INFO("freq=" << listFreq[fff] << " channel=" << listChannel[ccc] << " format=" << getFormatString(listFormat[iii]));
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<testOutCallbackType> process = ememory::makeShared<testOutCallbackType>(manager, listFreq[fff], listChannel[ccc], listFormat[iii]);
2015-03-03 23:17:43 +01:00
process->run();
process.reset();
2016-10-13 21:29:18 +02:00
std::this_thread::sleep_for(std::chrono::milliseconds(500));
2015-03-03 23:17:43 +01:00
}
}
}
2015-04-11 09:38:30 +02:00
audio::river::unInit();
2015-03-03 23:17:43 +01:00
}
};