audio-river/test/testMuxer.cpp

186 lines
6.0 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
*/
2016-10-02 22:19:26 +02:00
#include <test-debug/debug.hpp>
2017-11-07 13:38:04 +01:00
#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-04 22:48:04 +01:00
2015-03-03 23:17:43 +01:00
namespace river_test_muxer {
2015-03-04 22:48:04 +01:00
class TestClass {
private:
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<audio::river::Manager> m_manager;
ememory::SharedPtr<audio::river::Interface> m_interfaceIn;
ememory::SharedPtr<audio::river::Interface> m_interfaceOut;
2015-03-04 22:59:04 +01:00
double m_phase;
2015-03-04 22:48:04 +01:00
public:
2016-07-19 21:43:58 +02:00
TestClass(ememory::SharedPtr<audio::river::Manager> _manager) :
2015-03-04 22:59:04 +01:00
m_manager(_manager),
m_phase(0) {
2017-08-28 00:08:50 +02:00
etk::Vector<audio::channel> channelMap;
channelMap.pushBack(audio::channel_frontLeft);
channelMap.pushBack(audio::channel_frontRight);
2015-03-04 22:59:04 +01:00
m_interfaceOut = m_manager->createOutput(48000,
channelMap,
audio::format_int16,
2015-03-13 23:22:17 +01:00
"speaker");
2018-06-19 22:13:48 +02:00
if(m_interfaceOut == null) {
TEST_ERROR("null interface");
return;
}
2015-03-04 22:59:04 +01:00
// set callback mode ...
2017-11-07 13:38:04 +01:00
m_interfaceOut->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-04 22:59:04 +01:00
m_interfaceOut->addVolumeGroup("FLOW");
2015-03-12 22:28:15 +01:00
//m_interfaceOut->setParameter("volume", "FLOW", "-6dB");
2015-03-04 22:59:04 +01:00
2015-03-04 22:48:04 +01:00
//Set stereo output:
m_interfaceIn = m_manager->createInput(48000,
2017-08-28 00:08:50 +02:00
etk::Vector<audio::channel>(),
2015-03-04 22:48:04 +01:00
audio::format_int16,
2015-03-13 23:22:17 +01:00
"microphone-muxed");
2018-06-19 22:13:48 +02:00
if(m_interfaceIn == null) {
TEST_ERROR("null interface");
return;
}
2015-03-04 22:48:04 +01:00
// set callback mode ...
2017-09-26 15:57:44 +02:00
m_interfaceIn->setInputCallback([=](const void* _data,
const audio::Time& _time,
size_t _nbChunk,
enum audio::format _format,
uint32_t _frequency,
const etk::Vector<audio::channel>& _map) {
onDataReceived(_data, _time, _nbChunk, _format, _frequency, _map);
});
2015-03-04 22:48:04 +01:00
m_manager->generateDotAll("activeProcess.dot");
}
2015-03-04 22:59:04 +01:00
void onDataNeeded(void* _data,
2015-04-13 21:49:48 +02:00
const audio::Time& _time,
2015-03-04 22:59:04 +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-03-04 22:59:04 +01:00
int16_t* data = static_cast<int16_t*>(_data);
double baseCycle = 2.0*M_PI/(double)48000 * 440;
for (int32_t iii=0; iii<_nbChunk; iii++) {
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
2015-03-12 22:28:15 +01:00
data[_map.size()*iii+jjj] = sin(m_phase) * 7000;
2015-03-04 22:59:04 +01:00
}
m_phase += baseCycle;
if (m_phase >= 2*M_PI) {
m_phase -= 2*M_PI;
}
}
}
2015-03-04 22:48:04 +01:00
void onDataReceived(const void* _data,
2015-04-13 21:49:48 +02:00
const audio::Time& _time,
2015-03-04 22:48:04 +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-03-04 22:48:04 +01:00
if (_format != audio::format_int16) {
2015-10-14 21:21:03 +02:00
TEST_ERROR("call wrong type ... (need int16_t)");
2015-03-04 22:48:04 +01:00
}
2018-10-23 22:19:32 +02:00
//TEST_SAVE_FILE_MACRO(int16_t, "REC_MicrophoneMuxed.raw", _data, _nbChunk*_map.size());
2015-10-14 21:21:03 +02:00
TEST_ERROR("Receive data ... " << _nbChunk << " map=" << _map);
2015-03-04 22:48:04 +01:00
}
void run() {
2018-06-19 22:13:48 +02:00
if(m_interfaceIn == null) {
TEST_ERROR("null interface");
return;
}
2018-06-19 22:13:48 +02:00
if(m_interfaceOut == null) {
TEST_ERROR("null interface");
return;
}
2015-03-04 22:59:04 +01:00
m_interfaceOut->start();
2015-03-04 22:48:04 +01:00
m_interfaceIn->start();
2017-09-26 15:57:44 +02:00
ethread::sleepMilliSeconds(1000*(10));
2015-03-04 22:48:04 +01:00
m_interfaceIn->stop();
2015-03-04 22:59:04 +01:00
m_interfaceOut->stop();
2015-03-04 22:48:04 +01:00
}
};
2015-03-03 23:17:43 +01:00
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"
" timestamp-mode:'trigered',\n"
" },\n"
" group:'groupSynchro',\n"
" frequency:0,\n"
" channel-map:['front-left', 'front-right'],\n"
" type:'auto',\n"
" nb-chunk:1024,\n"
" },\n"
" microphone:{\n"
" io:'input',\n"
" map-on:{\n"
" interface:'auto',\n"
" name:'default',\n"
" timestamp-mode:'trigered',\n"
" },\n"
" group:'groupSynchro',\n"
" frequency:0,\n"
" channel-map:['front-left', 'front-right'],\n"
" type:'auto',\n"
" nb-chunk:1024\n"
" },\n"
" microphone-muxed:{\n"
" io:'muxer',\n"
" # connect in input mode\n"
" map-on-input-1:{\n"
" # generic virtual definition\n"
" io:'input',\n"
" map-on:'microphone',\n"
" },\n"
" # connect in feedback mode\n"
" map-on-input-2:{\n"
" io:'feedback',\n"
" map-on:'speaker',\n"
" },\n"
" input-2-remap:['rear-left', 'rear-right'],\n"
" #classical format configuration:\n"
" frequency:48000,\n"
" channel-map:[\n"
" 'front-left', 'front-right', 'rear-left', 'rear-right'\n"
" ],\n"
" type:'int16',\n"
" mux-demux-type:'int16',\n"
" }\n"
"}\n";
2015-03-12 23:17:14 +01:00
2015-03-04 22:48:04 +01:00
TEST(TestMuxer, testMuxing) {
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<TestClass> process = ememory::makeShared<TestClass>(manager);
2015-03-04 22:48:04 +01:00
process->run();
process.reset();
2017-09-14 00:59:21 +02:00
ethread::sleepMilliSeconds((500));
2015-04-11 09:38:30 +02:00
audio::river::unInit();
2015-03-04 22:48:04 +01:00
}
2015-03-03 23:17:43 +01:00
};