audio-river/test/testFormat.cpp

220 lines
7.5 KiB
C++
Raw Permalink 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
*/
2017-11-07 13:38:04 +01:00
#include <test-debug/debug.hpp>
#include <audio/river/river.hpp>
#include <audio/river/Manager.hpp>
#include <audio/river/Interface.hpp>
#include <etest/etest.hpp>
#include <etk/etk.hpp>
extern "C" {
#include <math.h>
}
#include <ethread/Thread.hpp>
#include <ethread/tools.hpp>
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");
2018-06-19 22:13:48 +02:00
if(m_interface == null) {
TEST_ERROR("null interface");
return;
}
2015-03-03 23:17:43 +01:00
// set callback mode ...
2017-11-07 13:38:04 +01:00
m_interface->setOutputCallback([=](void* _data,
2017-09-26 15:57:44 +02:00
const audio::Time& _time,
size_t _nbChunk,
enum audio::format _format,
uint32_t _frequency,
const etk::Vector<audio::channel>& _map) {
onDataNeeded(_data, _time, _nbChunk, _format, _frequency, _map);
});
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() {
2018-06-19 22:13:48 +02:00
if(m_interface == null) {
TEST_ERROR("null interface");
return;
2015-03-03 23:17:43 +01:00
}
m_interface->start();
// wait 2 second ...
2017-09-26 15:57:44 +02:00
ethread::sleepMilliSeconds(1000*(1));
m_interface->stop();
2017-09-14 00:59:21 +02:00
ethread::sleepMilliSeconds((100));
2015-03-03 23:17:43 +01:00
}
};
2017-11-07 13:38:04 +01:00
etk::Vector<float> listFreq = {4000, 8000, 16000, 32000, 48000, 48001, 64000, 96000, 11250, 2250, 44100, 88200};
etk::Vector<int32_t> listChannel = {1, 2, 4};
etk::Vector<audio::format> listFormat = {audio::format_int16, audio::format_int16_on_int32, audio::format_int32, audio::format_float};
2015-03-03 23:17:43 +01:00
2017-11-07 13:38:04 +01:00
TEST(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");
2017-11-07 13:38:04 +01:00
for (auto itFrequency: listFreq) {
ememory::SharedPtr<testOutCallbackType> process = ememory::makeShared<testOutCallbackType>(manager, itFrequency, 2, audio::format_int16);
process->run();
process.reset();
ethread::sleepMilliSeconds((500));
}
2015-04-11 09:38:30 +02:00
audio::river::unInit();
2015-03-03 23:17:43 +01:00
}
2017-11-07 13:38:04 +01:00
TEST(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");
2017-11-07 13:38:04 +01:00
for (auto itFormat: listFormat) {
ememory::SharedPtr<testOutCallbackType> process = ememory::makeShared<testOutCallbackType>(manager, 48000, 2, itFormat);
process->run();
process.reset();
ethread::sleepMilliSeconds((500));
}
2015-04-11 09:38:30 +02:00
audio::river::unInit();
2015-03-03 23:17:43 +01:00
}
2017-11-07 13:38:04 +01:00
TEST(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");
2017-11-07 13:38:04 +01:00
for (auto itChannel: listChannel) {
ememory::SharedPtr<testOutCallbackType> process = ememory::makeShared<testOutCallbackType>(manager, 48000, itChannel, audio::format_int16);
process->run();
process.reset();
ethread::sleepMilliSeconds((500));
}
2015-04-11 09:38:30 +02:00
audio::river::unInit();
2015-03-03 23:17:43 +01:00
}
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-11-07 13:38:04 +01:00
for (auto itFrequency: listFreq) {
for (auto itChannel: listChannel) {
for (auto itFormat: listFormat) {
TEST_INFO("freq=" << itFrequency << " channel=" << listChannel << " format=" << audio::getFormatString(itFormat));
ememory::SharedPtr<testOutCallbackType> process = ememory::makeShared<testOutCallbackType>(manager, itFrequency, itChannel, itFormat);
2015-03-03 23:17:43 +01:00
process->run();
process.reset();
2017-09-14 00:59:21 +02:00
ethread::sleepMilliSeconds((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
}
2017-11-07 13:38:04 +01:00
}
2015-03-03 23:17:43 +01:00