audio-river/test/testEchoDelay.hpp

427 lines
16 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
2016-10-02 22:19:26 +02:00
#include <test-debug/debug.hpp>
2015-03-04 22:15:35 +01:00
2015-03-03 23:17:43 +01:00
namespace river_test_echo_delay {
class TestClass {
private:
2016-07-19 21:43:58 +02:00
ememory::SharedPtr<audio::river::Manager> m_manager;
ememory::SharedPtr<audio::river::Interface> m_interfaceOut;
ememory::SharedPtr<audio::river::Interface> m_interfaceIn;
ememory::SharedPtr<audio::river::Interface> m_interfaceFB;
2015-03-03 23:17:43 +01:00
double m_phase;
2015-03-04 22:15:35 +01:00
double m_freq;
int32_t m_nextSampleCount;
2017-09-26 15:57:44 +02:00
echrono::milliseconds m_delayBetweenEvent;
2015-04-13 21:49:48 +02:00
audio::Time m_nextTick;
audio::Time m_currentTick;
2015-03-04 22:15:35 +01:00
int32_t m_stateFB;
int32_t m_stateMic;
2017-08-28 00:08:50 +02:00
etk::Vector<uint64_t> m_delayListMic;
2015-03-04 22:25:22 +01:00
bool m_estimateVolumeInput;
int16_t m_volumeInputMax;
int16_t m_volumeInputMin;
float m_gain;
2015-03-03 23:17:43 +01:00
public:
2016-07-19 21:43:58 +02:00
TestClass(ememory::SharedPtr<audio::river::Manager> _manager) :
2015-03-03 23:17:43 +01:00
m_manager(_manager),
m_phase(0),
2015-03-04 22:15:35 +01:00
m_freq(400),
m_nextSampleCount(0),
m_delayBetweenEvent(400),
m_stateFB(3),
2015-03-04 22:25:22 +01:00
m_stateMic(0),
m_estimateVolumeInput(true),
m_gain(-40) {
2015-03-03 23:17:43 +01:00
//Set stereo output:
2017-08-28 00:08:50 +02:00
etk::Vector<audio::channel> channelMap;
2015-03-03 23:17:43 +01:00
m_interfaceOut = m_manager->createOutput(48000,
channelMap,
audio::format_int16,
2015-03-13 23:22:17 +01:00
"speaker");
if(m_interfaceOut == 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 ...
2017-09-26 15:57:44 +02:00
m_interfaceOut->setOutputCallback([=](const void* _data,
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
m_interfaceOut->addVolumeGroup("FLOW");
2017-08-28 00:08:50 +02:00
m_interfaceOut->setParameter("volume", "FLOW", etk::toString(m_gain) + "dB");
2015-03-04 22:15:35 +01:00
2015-03-03 23:17:43 +01:00
m_interfaceIn = m_manager->createInput(48000,
channelMap,
audio::format_int16,
2015-03-13 23:22:17 +01:00
"microphone");
if(m_interfaceIn == 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 ...
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:15:35 +01:00
2015-03-13 23:22:17 +01:00
m_interfaceFB = m_manager->createFeedback(48000,
channelMap,
audio::format_int16,
"speaker");
if(m_interfaceFB == nullptr) {
2015-10-14 21:21:03 +02:00
TEST_ERROR("nullptr interface");
return;
}
2015-03-04 22:15:35 +01:00
// set callback mode ...
2017-09-26 15:57:44 +02:00
m_interfaceFB->setInputCallback([=](const void* _data,
const audio::Time& _time,
size_t _nbChunk,
enum audio::format _format,
uint32_t _frequency,
const etk::Vector<audio::channel>& _map) {
onDataReceivedFeedBack(_data, _time, _nbChunk, _format, _frequency, _map);
});
2015-03-04 22:15:35 +01:00
m_manager->generateDotAll("activeProcess.dot");
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-03-04 22:15:35 +01:00
int16_t* data = static_cast<int16_t*>(_data);
double baseCycle = 2.0*M_PI/(double)48000 * m_freq;
2015-03-04 22:25:22 +01:00
if (m_estimateVolumeInput == true) {
2015-03-04 22:15:35 +01:00
for (int32_t iii=0; iii<_nbChunk; iii++) {
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
2015-03-04 22:25:22 +01:00
data[_map.size()*iii+jjj] = sin(m_phase) * 30000;
}
m_phase += baseCycle;
if (m_phase >= 2*M_PI) {
m_phase -= 2*M_PI;
2015-03-04 22:15:35 +01:00
}
}
2015-03-04 22:25:22 +01:00
} else {
2015-04-13 21:49:48 +02:00
if (_time == audio::Time()) {
2015-03-04 22:25:22 +01:00
for (int32_t iii=0; iii<_nbChunk; iii++) {
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
data[_map.size()*iii+jjj] = 0;
}
}
return;
}
2015-04-13 21:49:48 +02:00
if (m_nextTick == audio::Time()) {
2015-03-04 22:25:22 +01:00
m_nextTick = _time + m_delayBetweenEvent;
2015-03-04 22:15:35 +01:00
m_nextSampleCount = m_delayBetweenEvent.count()*int64_t(_frequency)/1000;
2015-03-04 22:25:22 +01:00
m_phase = -1;
2015-03-04 22:15:35 +01:00
}
2015-10-14 21:21:03 +02:00
//TEST_INFO("sample : " << m_nextSampleCount);
2015-03-04 22:25:22 +01:00
for (int32_t iii=0; iii<_nbChunk; iii++) {
if (m_nextSampleCount > 0) {
m_nextSampleCount--;
} else {
m_phase = 0;
m_nextSampleCount = m_delayBetweenEvent.count()*int64_t(_frequency)/1000;
m_currentTick = m_nextTick;
m_nextTick += m_delayBetweenEvent;
2015-03-03 23:17:43 +01:00
}
2015-03-04 22:25:22 +01:00
if (m_phase >= 0) {
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
data[_map.size()*iii+jjj] = sin(m_phase) * 30000;
}
double newPhase = m_phase+baseCycle;
if ( m_phase < M_PI
&& newPhase >= M_PI) {
// the zero crossing position :
m_currentTick = getInterpolateTime(_time, iii, sin(m_phase) * 30000, sin(newPhase) * 30000, _frequency);
// start detection ...
m_stateFB = 0;
m_stateMic = 0;
2015-10-14 21:21:03 +02:00
TEST_WARNING("Time Pulse zero crossing: " << m_currentTick << " id=" << iii);
2015-03-04 22:25:22 +01:00
}
m_phase = newPhase;
if (m_phase >= 2*M_PI) {
m_phase = -1;
//m_freq += 50.0;
if (m_freq>20000.0) {
m_freq = 400.0;
}
}
} else {
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
data[_map.size()*iii+jjj] = 0;
2015-03-04 22:15:35 +01:00
}
}
}
}
}
2015-04-13 21:49:48 +02:00
audio::Time getInterpolateTime(audio::Time _time, int32_t _pos, int16_t _val1, int16_t _val2, uint32_t _frequency) {
2015-03-04 22:15:35 +01:00
if (_val1 == 0) {
2015-04-13 21:49:48 +02:00
return _time + audio::Duration(0, int64_t(_pos)*1000000000LL/int64_t(_frequency));
2015-03-04 22:15:35 +01:00
} else if (_val2 == 0) {
2015-04-13 21:49:48 +02:00
return _time + audio::Duration(0, int64_t(_pos+1)*1000000000LL/int64_t(_frequency));
2015-03-04 22:15:35 +01:00
}
double xxx = double(-_val1) / double(_val2 - _val1);
2015-10-14 21:21:03 +02:00
TEST_VERBOSE("deltaPos:" << xxx);
2015-04-13 21:49:48 +02:00
return _time + audio::Duration(0, int64_t((double(_pos)+xxx)*1000000000.0)/int64_t(_frequency));
2015-03-04 22:15:35 +01:00
}
void onDataReceivedFeedBack(const void* _data,
2015-04-13 21:49:48 +02:00
const audio::Time& _time,
2015-03-04 22:15:35 +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:15:35 +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:15:35 +01:00
}
2015-10-14 21:21:03 +02:00
TEST_SAVE_FILE_MACRO(int16_t, "REC_FeedBack.raw", _data, _nbChunk*_map.size());
2015-03-04 22:25:22 +01:00
if (m_estimateVolumeInput == true) {
// nothing to do ...
} else {
const int16_t* data = static_cast<const int16_t*>(_data);
// Detect Zero crossing after a max/min ...
for (size_t iii=0; iii<_nbChunk; ++iii) {
//for (size_t jjj=0; jjj<_map.size(); ++jjj) {
size_t jjj=0; {
if (m_stateFB == 0) {
if (data[iii*_map.size() + jjj] > INT16_MAX/5) {
m_stateFB = 1;
2015-10-14 21:21:03 +02:00
TEST_VERBOSE("FB: detect Normal " << iii);
2015-03-04 22:25:22 +01:00
} else if (data[iii*_map.size() + jjj] < -INT16_MAX/5) {
m_stateFB = 2;
2015-10-14 21:21:03 +02:00
TEST_VERBOSE("FB: detect inverse " << iii);
2015-03-04 22:25:22 +01:00
}
} else if (m_stateFB == 1) {
// normale phase
if (data[iii*_map.size() + jjj] <= 0) {
// detect inversion of signe ...
m_stateFB = 3;
2015-04-13 21:49:48 +02:00
audio::Time time = getInterpolateTime(_time, iii-1, data[(iii-1)*_map.size() + jjj], data[iii*_map.size() + jjj], _frequency);
2015-10-14 21:21:03 +02:00
TEST_VERBOSE("FB: 1 position -1: " << iii-1 << " " << data[(iii-1)*_map.size() + jjj]);
TEST_VERBOSE("FB: 1 position 0: " << iii << " " << data[iii*_map.size() + jjj]);
2015-03-04 22:25:22 +01:00
2015-10-14 21:21:03 +02:00
TEST_WARNING("FB: 1 time detected: " << time << " delay = " << float((time-m_currentTick).count())/1000.0f << "µs");
2015-03-04 22:25:22 +01:00
}
} else if (m_stateFB == 2) {
// inverse phase
if (data[iii*_map.size() + jjj] >= 0) {
// detect inversion of signe ...
m_stateFB = 3;
2015-04-13 21:49:48 +02:00
audio::Time time = getInterpolateTime(_time, iii-1, data[(iii-1)*_map.size() + jjj], data[iii*_map.size() + jjj], _frequency);
2015-10-14 21:21:03 +02:00
TEST_VERBOSE("FB: 2 position -1: " << iii-1 << " " << data[(iii-1)*_map.size() + jjj]);
TEST_VERBOSE("FB: 2 position 0: " << iii << " " << data[iii*_map.size() + jjj]);
TEST_WARNING("FB: 2 time detected: " << time << " delay = " << float((time-m_currentTick).count())/1000.0f << "µs");
2015-03-04 22:25:22 +01:00
}
} else if (m_stateFB == 3) {
// TODO : Detect the pic ...
// do nothing ...
2015-03-04 22:15:35 +01:00
}
2015-03-03 23:17:43 +01:00
}
}
}
}
void onDataReceived(const 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-03-03 23:17:43 +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-03 23:17:43 +01:00
}
2015-10-14 21:21:03 +02:00
TEST_SAVE_FILE_MACRO(int16_t, "REC_Microphone.raw", _data, _nbChunk*_map.size());
2015-03-04 22:15:35 +01:00
const int16_t* data = static_cast<const int16_t*>(_data);
2015-03-04 22:25:22 +01:00
if (m_estimateVolumeInput == true) {
m_stateMic ++;
const int16_t* data = static_cast<const int16_t*>(_data);
if (m_stateMic <= 40) {
for (size_t iii=0; iii<_nbChunk*_map.size(); ++iii) {
2015-10-14 21:21:03 +02:00
//TEST_INFO("value=" << data[iii]);
2017-08-28 00:08:50 +02:00
m_volumeInputMax = etk::max(int16_t(data[iii]), m_volumeInputMax);
m_volumeInputMin = etk::min(int16_t(data[iii]), m_volumeInputMin);
2015-03-04 22:25:22 +01:00
}
if (m_stateMic == 40) {
m_volumeInputMax *= 2;
m_volumeInputMin *= 2;
}
} else if (m_stateMic <= 10000) {
int16_t valueMax = 0;
int16_t valueMin = 0;
for (size_t iii=0; iii<_nbChunk*_map.size(); ++iii) {
2015-10-14 21:21:03 +02:00
//TEST_INFO("value=" << data[iii]);
2017-08-28 00:08:50 +02:00
valueMax = etk::max(int16_t(data[iii]), valueMax);
valueMin = etk::min(int16_t(data[iii]), valueMin);
2015-03-04 22:25:22 +01:00
}
if ( valueMax > m_volumeInputMax
&& valueMin < m_volumeInputMin
&& ( m_gain == 0.0
2015-03-04 22:48:04 +01:00
|| ( valueMax > INT16_MAX*2/3
&& valueMin < INT16_MIN*2/3
2015-03-04 22:25:22 +01:00
)
)
) {
m_gain += 3.0f;
2017-08-28 00:08:50 +02:00
m_gain = etk::min(m_gain, 0.0f);
m_interfaceOut->setParameter("volume", "FLOW", etk::toString(m_gain) + "dB");
2015-10-14 21:21:03 +02:00
TEST_INFO("Set detection volume : " << m_gain << " m_stateMic=" << m_stateMic);
2015-03-04 22:25:22 +01:00
m_stateMic = 3;
m_phase = -1;
m_estimateVolumeInput = false;
return;
} else {
if (m_stateMic%2 == 0) {
if (m_gain == 0.0f) {
2015-10-14 21:21:03 +02:00
TEST_CRITICAL("Can not find the basicVolume ...");
2015-03-04 22:25:22 +01:00
}
// just update volume
m_gain += 1.0f;
2017-08-28 00:08:50 +02:00
m_gain = etk::min(m_gain, 0.0f);
m_interfaceOut->setParameter("volume", "FLOW", etk::toString(m_gain) + "dB");
2015-03-04 22:15:35 +01:00
}
2015-03-04 22:25:22 +01:00
}
}
} else {
// Detect Zero crossing after a max/min ...
for (size_t iii=0; iii<_nbChunk; ++iii) {
//for (size_t jjj=0; jjj<_map.size(); ++jjj) {
size_t jjj=0; {
if (m_stateMic == 0) {
2015-03-04 22:29:33 +01:00
if (data[iii*_map.size() + jjj] > m_volumeInputMax) {
2015-03-04 22:25:22 +01:00
m_stateMic = 1;
2015-10-14 21:21:03 +02:00
TEST_VERBOSE("Mic: detect Normal " << iii);
2015-03-04 22:29:33 +01:00
} else if (data[iii*_map.size() + jjj] < m_volumeInputMin) {
2015-03-04 22:25:22 +01:00
m_stateMic = 2;
2015-10-14 21:21:03 +02:00
TEST_VERBOSE("Mic: detect inverse " << iii);
2015-03-04 22:25:22 +01:00
}
} else if (m_stateMic == 1) {
// normale phase
if (data[iii*_map.size() + jjj] <= 0) {
// detect inversion of signe ...
m_stateMic = 3;
2015-04-13 21:49:48 +02:00
audio::Time time = getInterpolateTime(_time, iii-1, data[(iii-1)*_map.size() + jjj], data[iii*_map.size() + jjj], _frequency);
2015-10-14 21:21:03 +02:00
TEST_VERBOSE("MIC: 1 position -1: " << iii-1 << " " << data[(iii-1)*_map.size() + jjj]);
TEST_VERBOSE("MIC: 1 position 0: " << iii << " " << data[iii*_map.size() + jjj]);
2015-04-13 21:49:48 +02:00
audio::Duration delay = time-m_currentTick;
2015-03-04 22:25:22 +01:00
int32_t sampleDalay = (delay.count()*_frequency)/1000000000LL;
2015-10-14 21:21:03 +02:00
TEST_WARNING("MIC: 1 time detected: " << time << " delay = " << float(delay.count())/1000.0f << "µs samples=" << sampleDalay);
2017-08-28 00:08:50 +02:00
m_delayListMic.pushBack(delay.count());
2015-03-04 22:25:22 +01:00
}
} else if (m_stateMic == 2) {
// inverse phase
if (data[iii*_map.size() + jjj] >= 0) {
// detect inversion of signe ...
m_stateMic = 3;
2015-04-13 21:49:48 +02:00
audio::Time time = getInterpolateTime(_time, iii-1, data[(iii-1)*_map.size() + jjj], data[iii*_map.size() + jjj], _frequency);
2015-10-14 21:21:03 +02:00
TEST_VERBOSE("MIC: 2 position -1: " << iii-1 << " " << data[(iii-1)*_map.size() + jjj]);
TEST_VERBOSE("MIC: 2 position 0: " << iii << " " << data[iii*_map.size() + jjj]);
2015-04-13 21:49:48 +02:00
audio::Duration delay = time-m_currentTick;
2015-03-04 22:25:22 +01:00
int32_t sampleDalay = (delay.count()*_frequency)/1000000000LL;
2015-10-14 21:21:03 +02:00
TEST_WARNING("MIC: 2 time detected: " << time << " delay = " << float(delay.count())/1000.0f << "µs samples=" << sampleDalay);
2017-08-28 00:08:50 +02:00
m_delayListMic.pushBack(delay.count());
2015-03-04 22:25:22 +01:00
}
} else if (m_stateMic == 3) {
// TODO : Detect the pic ...
// do nothing ...
2015-03-04 22:15:35 +01:00
}
}
}
}
2015-03-03 23:17:43 +01:00
}
void run() {
if(m_interfaceIn == nullptr) {
2015-10-14 21:21:03 +02:00
TEST_ERROR("nullptr interface");
return;
}
if(m_interfaceOut == nullptr) {
2015-10-14 21:21:03 +02:00
TEST_ERROR("nullptr interface");
return;
}
if(m_interfaceFB == nullptr) {
2015-10-14 21:21:03 +02:00
TEST_ERROR("nullptr interface");
return;
}
2015-03-03 23:17:43 +01:00
m_interfaceOut->start();
m_interfaceIn->start();
2015-03-04 22:25:22 +01:00
//m_interfaceFB->start();
2015-03-12 22:28:15 +01:00
while (m_estimateVolumeInput == true) {
2017-09-14 00:59:21 +02:00
ethread::sleepMilliSeconds((10));
2015-03-12 22:28:15 +01:00
}
2017-09-26 15:57:44 +02:00
ethread::sleepMilliSeconds(1000*(10));
2015-03-04 22:25:22 +01:00
//m_interfaceFB->stop();
2015-03-03 23:17:43 +01:00
m_interfaceIn->stop();
m_interfaceOut->stop();
2015-03-04 22:25:22 +01:00
int64_t delayAverage = 0;
if (m_delayListMic.size() > 0) {
for (size_t iii=0; iii<m_delayListMic.size(); ++iii) {
delayAverage += m_delayListMic[iii];
}
delayAverage /= m_delayListMic.size();
}
int32_t sampleDalay = (delayAverage*48000)/1000000000LL;
2015-10-14 21:21:03 +02:00
TEST_ERROR("Average delay in ns : " << delayAverage << " nbSample=" << sampleDalay);
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"
//" name:'hw:0,0',\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"
//" name:'hw:0,0',\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"
"}\n";
2015-03-12 23:17:14 +01:00
2015-03-03 23:17:43 +01:00
TEST(TestTime, testDelay) {
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-03 23:17:43 +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-03 23:17:43 +01:00
}
};