[DEV] auto update volume
This commit is contained in:
parent
8a59126beb
commit
034c95f286
@ -124,7 +124,7 @@ void river::io::Group::stop() {
|
|||||||
RIVER_ERROR(" have " << count << " interfaces ...");
|
RIVER_ERROR(" have " << count << " interfaces ...");
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
RIVER_ERROR("GROUP :::::::::::: STOP() [START]");
|
RIVER_ERROR("GROUP :::::::::::: STOP() [START]");
|
||||||
for (size_t iii=0; iii<m_list.size(); ++iii) {
|
for (int32_t iii=m_list.size()-1; iii>=0; --iii) {
|
||||||
if (m_list[iii] != nullptr) {
|
if (m_list[iii] != nullptr) {
|
||||||
m_list[iii]->stop();
|
m_list[iii]->stop();
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,11 @@ namespace river_test_echo_delay {
|
|||||||
std11::chrono::system_clock::time_point m_currentTick;
|
std11::chrono::system_clock::time_point m_currentTick;
|
||||||
int32_t m_stateFB;
|
int32_t m_stateFB;
|
||||||
int32_t m_stateMic;
|
int32_t m_stateMic;
|
||||||
|
std::vector<uint64_t> m_delayListMic;
|
||||||
|
bool m_estimateVolumeInput;
|
||||||
|
int16_t m_volumeInputMax;
|
||||||
|
int16_t m_volumeInputMin;
|
||||||
|
float m_gain;
|
||||||
public:
|
public:
|
||||||
TestClass(std11::shared_ptr<river::Manager> _manager) :
|
TestClass(std11::shared_ptr<river::Manager> _manager) :
|
||||||
m_manager(_manager),
|
m_manager(_manager),
|
||||||
@ -35,7 +40,9 @@ namespace river_test_echo_delay {
|
|||||||
m_nextSampleCount(0),
|
m_nextSampleCount(0),
|
||||||
m_delayBetweenEvent(400),
|
m_delayBetweenEvent(400),
|
||||||
m_stateFB(3),
|
m_stateFB(3),
|
||||||
m_stateMic(3) {
|
m_stateMic(0),
|
||||||
|
m_estimateVolumeInput(true),
|
||||||
|
m_gain(-40) {
|
||||||
//Set stereo output:
|
//Set stereo output:
|
||||||
std::vector<audio::channel> channelMap;
|
std::vector<audio::channel> channelMap;
|
||||||
channelMap.push_back(audio::channel_frontLeft);
|
channelMap.push_back(audio::channel_frontLeft);
|
||||||
@ -55,6 +62,7 @@ namespace river_test_echo_delay {
|
|||||||
std11::placeholders::_5,
|
std11::placeholders::_5,
|
||||||
std11::placeholders::_6));
|
std11::placeholders::_6));
|
||||||
m_interfaceOut->addVolumeGroup("FLOW");
|
m_interfaceOut->addVolumeGroup("FLOW");
|
||||||
|
m_interfaceOut->setParameter("volume", "FLOW", etk::to_string(m_gain) + "dB");
|
||||||
|
|
||||||
m_interfaceIn = m_manager->createInput(48000,
|
m_interfaceIn = m_manager->createInput(48000,
|
||||||
channelMap,
|
channelMap,
|
||||||
@ -96,6 +104,17 @@ namespace river_test_echo_delay {
|
|||||||
const std::vector<audio::channel>& _map) {
|
const std::vector<audio::channel>& _map) {
|
||||||
int16_t* data = static_cast<int16_t*>(_data);
|
int16_t* data = static_cast<int16_t*>(_data);
|
||||||
double baseCycle = 2.0*M_PI/(double)48000 * m_freq;
|
double baseCycle = 2.0*M_PI/(double)48000 * m_freq;
|
||||||
|
if (m_estimateVolumeInput == true) {
|
||||||
|
for (int32_t iii=0; iii<_nbChunk; iii++) {
|
||||||
|
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
|
||||||
|
data[_map.size()*iii+jjj] = sin(m_phase) * 30000;
|
||||||
|
}
|
||||||
|
m_phase += baseCycle;
|
||||||
|
if (m_phase >= 2*M_PI) {
|
||||||
|
m_phase -= 2*M_PI;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if (_time == std11::chrono::system_clock::time_point()) {
|
if (_time == std11::chrono::system_clock::time_point()) {
|
||||||
for (int32_t iii=0; iii<_nbChunk; iii++) {
|
for (int32_t iii=0; iii<_nbChunk; iii++) {
|
||||||
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
|
for (int32_t jjj=0; jjj<_map.size(); jjj++) {
|
||||||
@ -148,6 +167,7 @@ namespace river_test_echo_delay {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
std11::chrono::system_clock::time_point getInterpolateTime(std11::chrono::system_clock::time_point _time, int32_t _pos, int16_t _val1, int16_t _val2, uint32_t _frequency) {
|
std11::chrono::system_clock::time_point getInterpolateTime(std11::chrono::system_clock::time_point _time, int32_t _pos, int16_t _val1, int16_t _val2, uint32_t _frequency) {
|
||||||
if (_val1 == 0) {
|
if (_val1 == 0) {
|
||||||
return _time + std11::chrono::nanoseconds(int64_t(_pos)*1000000000LL/int64_t(_frequency));
|
return _time + std11::chrono::nanoseconds(int64_t(_pos)*1000000000LL/int64_t(_frequency));
|
||||||
@ -169,6 +189,9 @@ namespace river_test_echo_delay {
|
|||||||
APPL_ERROR("call wrong type ... (need int16_t)");
|
APPL_ERROR("call wrong type ... (need int16_t)");
|
||||||
}
|
}
|
||||||
RIVER_SAVE_FILE_MACRO(int16_t, "REC_FeedBack.raw", _data, _nbChunk*_map.size());
|
RIVER_SAVE_FILE_MACRO(int16_t, "REC_FeedBack.raw", _data, _nbChunk*_map.size());
|
||||||
|
if (m_estimateVolumeInput == true) {
|
||||||
|
// nothing to do ...
|
||||||
|
} else {
|
||||||
const int16_t* data = static_cast<const int16_t*>(_data);
|
const int16_t* data = static_cast<const int16_t*>(_data);
|
||||||
// Detect Zero crossing after a max/min ...
|
// Detect Zero crossing after a max/min ...
|
||||||
for (size_t iii=0; iii<_nbChunk; ++iii) {
|
for (size_t iii=0; iii<_nbChunk; ++iii) {
|
||||||
@ -190,6 +213,7 @@ namespace river_test_echo_delay {
|
|||||||
std11::chrono::system_clock::time_point time = getInterpolateTime(_time, iii-1, data[(iii-1)*_map.size() + jjj], data[iii*_map.size() + jjj], _frequency);
|
std11::chrono::system_clock::time_point time = getInterpolateTime(_time, iii-1, data[(iii-1)*_map.size() + jjj], data[iii*_map.size() + jjj], _frequency);
|
||||||
APPL_VERBOSE("FB: 1 position -1: " << iii-1 << " " << data[(iii-1)*_map.size() + jjj]);
|
APPL_VERBOSE("FB: 1 position -1: " << iii-1 << " " << data[(iii-1)*_map.size() + jjj]);
|
||||||
APPL_VERBOSE("FB: 1 position 0: " << iii << " " << data[iii*_map.size() + jjj]);
|
APPL_VERBOSE("FB: 1 position 0: " << iii << " " << data[iii*_map.size() + jjj]);
|
||||||
|
|
||||||
APPL_WARNING("FB: 1 time detected: " << time << " delay = " << float((time-m_currentTick).count())/1000.0f << "µs");
|
APPL_WARNING("FB: 1 time detected: " << time << " delay = " << float((time-m_currentTick).count())/1000.0f << "µs");
|
||||||
}
|
}
|
||||||
} else if (m_stateFB == 2) {
|
} else if (m_stateFB == 2) {
|
||||||
@ -209,6 +233,7 @@ namespace river_test_echo_delay {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
void onDataReceived(const void* _data,
|
void onDataReceived(const void* _data,
|
||||||
const std11::chrono::system_clock::time_point& _time,
|
const std11::chrono::system_clock::time_point& _time,
|
||||||
size_t _nbChunk,
|
size_t _nbChunk,
|
||||||
@ -220,6 +245,56 @@ namespace river_test_echo_delay {
|
|||||||
}
|
}
|
||||||
RIVER_SAVE_FILE_MACRO(int16_t, "REC_Microphone.raw", _data, _nbChunk*_map.size());
|
RIVER_SAVE_FILE_MACRO(int16_t, "REC_Microphone.raw", _data, _nbChunk*_map.size());
|
||||||
const int16_t* data = static_cast<const int16_t*>(_data);
|
const int16_t* data = static_cast<const int16_t*>(_data);
|
||||||
|
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) {
|
||||||
|
//APPL_INFO("value=" << data[iii]);
|
||||||
|
m_volumeInputMax = std::max(int16_t(data[iii]), m_volumeInputMax);
|
||||||
|
m_volumeInputMin = std::min(int16_t(data[iii]), m_volumeInputMin);
|
||||||
|
}
|
||||||
|
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) {
|
||||||
|
//APPL_INFO("value=" << data[iii]);
|
||||||
|
valueMax = std::max(int16_t(data[iii]), valueMax);
|
||||||
|
valueMin = std::min(int16_t(data[iii]), valueMin);
|
||||||
|
}
|
||||||
|
if ( valueMax > m_volumeInputMax
|
||||||
|
&& valueMin < m_volumeInputMin
|
||||||
|
&& ( m_gain == 0.0
|
||||||
|
|| ( valueMax > INT16_MAX/2
|
||||||
|
&& valueMin < INT16_MIN/2
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
m_gain += 3.0f;
|
||||||
|
m_gain = std::min(m_gain, 0.0f);
|
||||||
|
m_interfaceOut->setParameter("volume", "FLOW", etk::to_string(m_gain) + "dB");
|
||||||
|
APPL_INFO("Set detection volume : " << m_gain << " m_stateMic=" << m_stateMic);
|
||||||
|
m_stateMic = 3;
|
||||||
|
m_phase = -1;
|
||||||
|
m_estimateVolumeInput = false;
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
if (m_stateMic%2 == 0) {
|
||||||
|
if (m_gain == 0.0f) {
|
||||||
|
APPL_CRITICAL("Can not find the basicVolume ...");
|
||||||
|
}
|
||||||
|
// just update volume
|
||||||
|
m_gain += 1.0f;
|
||||||
|
m_gain = std::min(m_gain, 0.0f);
|
||||||
|
m_interfaceOut->setParameter("volume", "FLOW", etk::to_string(m_gain) + "dB");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// Detect Zero crossing after a max/min ...
|
// Detect Zero crossing after a max/min ...
|
||||||
for (size_t iii=0; iii<_nbChunk; ++iii) {
|
for (size_t iii=0; iii<_nbChunk; ++iii) {
|
||||||
//for (size_t jjj=0; jjj<_map.size(); ++jjj) {
|
//for (size_t jjj=0; jjj<_map.size(); ++jjj) {
|
||||||
@ -236,11 +311,14 @@ namespace river_test_echo_delay {
|
|||||||
// normale phase
|
// normale phase
|
||||||
if (data[iii*_map.size() + jjj] <= 0) {
|
if (data[iii*_map.size() + jjj] <= 0) {
|
||||||
// detect inversion of signe ...
|
// detect inversion of signe ...
|
||||||
m_stateFB = 3;
|
m_stateMic = 3;
|
||||||
std11::chrono::system_clock::time_point time = getInterpolateTime(_time, iii-1, data[(iii-1)*_map.size() + jjj], data[iii*_map.size() + jjj], _frequency);
|
std11::chrono::system_clock::time_point time = getInterpolateTime(_time, iii-1, data[(iii-1)*_map.size() + jjj], data[iii*_map.size() + jjj], _frequency);
|
||||||
APPL_VERBOSE("MIC: 1 position -1: " << iii-1 << " " << data[(iii-1)*_map.size() + jjj]);
|
APPL_VERBOSE("MIC: 1 position -1: " << iii-1 << " " << data[(iii-1)*_map.size() + jjj]);
|
||||||
APPL_VERBOSE("MIC: 1 position 0: " << iii << " " << data[iii*_map.size() + jjj]);
|
APPL_VERBOSE("MIC: 1 position 0: " << iii << " " << data[iii*_map.size() + jjj]);
|
||||||
APPL_WARNING("MIC: 1 time detected: " << time << " delay = " << float((time-m_currentTick).count())/1000.0f << "µs");
|
std11::chrono::nanoseconds delay = time-m_currentTick;
|
||||||
|
int32_t sampleDalay = (delay.count()*_frequency)/1000000000LL;
|
||||||
|
APPL_WARNING("MIC: 1 time detected: " << time << " delay = " << float(delay.count())/1000.0f << "µs samples=" << sampleDalay);
|
||||||
|
m_delayListMic.push_back(delay.count());
|
||||||
}
|
}
|
||||||
} else if (m_stateMic == 2) {
|
} else if (m_stateMic == 2) {
|
||||||
// inverse phase
|
// inverse phase
|
||||||
@ -250,7 +328,10 @@ namespace river_test_echo_delay {
|
|||||||
std11::chrono::system_clock::time_point time = getInterpolateTime(_time, iii-1, data[(iii-1)*_map.size() + jjj], data[iii*_map.size() + jjj], _frequency);
|
std11::chrono::system_clock::time_point time = getInterpolateTime(_time, iii-1, data[(iii-1)*_map.size() + jjj], data[iii*_map.size() + jjj], _frequency);
|
||||||
APPL_VERBOSE("MIC: 2 position -1: " << iii-1 << " " << data[(iii-1)*_map.size() + jjj]);
|
APPL_VERBOSE("MIC: 2 position -1: " << iii-1 << " " << data[(iii-1)*_map.size() + jjj]);
|
||||||
APPL_VERBOSE("MIC: 2 position 0: " << iii << " " << data[iii*_map.size() + jjj]);
|
APPL_VERBOSE("MIC: 2 position 0: " << iii << " " << data[iii*_map.size() + jjj]);
|
||||||
APPL_WARNING("MIC: 2 time detected: " << time << " delay = " << float((time-m_currentTick).count())/1000.0f << "µs");
|
std11::chrono::nanoseconds delay = time-m_currentTick;
|
||||||
|
int32_t sampleDalay = (delay.count()*_frequency)/1000000000LL;
|
||||||
|
APPL_WARNING("MIC: 2 time detected: " << time << " delay = " << float(delay.count())/1000.0f << "µs samples=" << sampleDalay);
|
||||||
|
m_delayListMic.push_back(delay.count());
|
||||||
}
|
}
|
||||||
} else if (m_stateMic == 3) {
|
} else if (m_stateMic == 3) {
|
||||||
// TODO : Detect the pic ...
|
// TODO : Detect the pic ...
|
||||||
@ -258,24 +339,26 @@ namespace river_test_echo_delay {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
const int16_t* data = static_cast<const int16_t*>(_data);
|
|
||||||
int16_t value = 0;
|
|
||||||
for (size_t iii=0; iii<_nbChunk*_map.size(); ++iii) {
|
|
||||||
//APPL_INFO("value=" << data[iii]);
|
|
||||||
value = std::max(int16_t(std::abs(data[iii])), value);
|
|
||||||
}
|
}
|
||||||
APPL_INFO("Get data ... max=" << value);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
void run() {
|
void run() {
|
||||||
m_interfaceOut->start();
|
m_interfaceOut->start();
|
||||||
m_interfaceIn->start();
|
m_interfaceIn->start();
|
||||||
m_interfaceFB->start();
|
//m_interfaceFB->start();
|
||||||
usleep(30000000);
|
usleep(10000000);
|
||||||
m_interfaceFB->stop();
|
//m_interfaceFB->stop();
|
||||||
m_interfaceIn->stop();
|
m_interfaceIn->stop();
|
||||||
m_interfaceOut->stop();
|
m_interfaceOut->stop();
|
||||||
|
|
||||||
|
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;
|
||||||
|
APPL_ERROR("Average delay in ns : " << delayAverage << " nbSample=" << sampleDalay);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user