Commit 854bac4a authored by Rafaël Carré's avatar Rafaël Carré
Browse files

* #6629 : use number of samples as arguments for audio filters

remove unused functions
parent f8206833
...@@ -141,14 +141,16 @@ int AudioRtpRecordHandler::processDataEncode (void) ...@@ -141,14 +141,16 @@ int AudioRtpRecordHandler::processDataEncode (void)
return 0; return 0;
} }
fadeIn (micData, bytesToGet / sizeof(SFLDataFormat), &_audioRtpRecord._micAmplFactor); int samples = bytesToGet / sizeof(SFLDataFormat);
fadeIn (micData, samples, &_audioRtpRecord._micAmplFactor);
if(Manager::instance().getEchoCancelState() == "enabled") if(Manager::instance().getEchoCancelState() == "enabled")
echoCanceller.getData(micData); echoCanceller.getData(micData);
_audioRtpRecord.audioProcessMutex.enter(); _audioRtpRecord.audioProcessMutex.enter();
if (Manager::instance().audioPreference.getNoiseReduce()) if (Manager::instance().audioPreference.getNoiseReduce())
_audioRtpRecord._noiseSuppress->process(micData, bytesToGet); _audioRtpRecord._noiseSuppress->process(micData, samples);
_audioRtpRecord.audioProcessMutex.leave(); _audioRtpRecord.audioProcessMutex.leave();
#ifdef DUMP_PROCESS_DATA_ENCODE #ifdef DUMP_PROCESS_DATA_ENCODE
...@@ -203,7 +205,7 @@ void AudioRtpRecordHandler::processDataDecode (unsigned char *spkrData, unsigned ...@@ -203,7 +205,7 @@ void AudioRtpRecordHandler::processDataDecode (unsigned char *spkrData, unsigned
} }
if(Manager::instance().getEchoCancelState() == "enabled") if(Manager::instance().getEchoCancelState() == "enabled")
echoCanceller.putData(out, outSamples * sizeof (SFLDataFormat)); echoCanceller.putData(out, outSamples);
Manager::instance().getMainBuffer()->putData (out, outSamples * sizeof (SFLDataFormat), id_); Manager::instance().getMainBuffer()->putData (out, outSamples * sizeof (SFLDataFormat), id_);
} }
......
...@@ -113,34 +113,12 @@ DelayDetection::DelayDetection() : _internalState (WaitForSpeaker), _decimationF ...@@ -113,34 +113,12 @@ DelayDetection::DelayDetection() : _internalState (WaitForSpeaker), _decimationF
} }
void DelayDetection::reset() void DelayDetection::putData (SFLDataFormat *inputData, int nbSamples)
{ {
_nbMicSampleStored = 0;
_nbSpkrSampleStored = 0;
_decimationFilter.reset();
_bandpassFilter.reset();
memset (_spkrReference, 0, sizeof (float) *WINDOW_SIZE*2);
memset (_capturedData, 0, sizeof (float) *DELAY_BUFF_SIZE*2);
memset (_spkrReferenceDown, 0, sizeof (float) *WINDOW_SIZE*2);
memset (_captureDataDown, 0, sizeof (float) *DELAY_BUFF_SIZE*2);
memset (_spkrReferenceFilter, 0, sizeof (float) *WINDOW_SIZE*2);
memset (_captureDataFilter, 0, sizeof (float) *DELAY_BUFF_SIZE*2);
memset (_correlationResult, 0, sizeof (float) *DELAY_BUFF_SIZE*2);
_internalState = WaitForSpeaker;
}
void DelayDetection::putData (SFLDataFormat *inputData, int nbBytes)
{
// Machine may already got a spkr and is waiting for mic or computing correlation // Machine may already got a spkr and is waiting for mic or computing correlation
if (_nbSpkrSampleStored == WINDOW_SIZE) if (_nbSpkrSampleStored == WINDOW_SIZE)
return; return;
int nbSamples = nbBytes/sizeof (SFLDataFormat);
if ( (_nbSpkrSampleStored + nbSamples) > WINDOW_SIZE) if ( (_nbSpkrSampleStored + nbSamples) > WINDOW_SIZE)
nbSamples = WINDOW_SIZE - _nbSpkrSampleStored; nbSamples = WINDOW_SIZE - _nbSpkrSampleStored;
...@@ -166,14 +144,12 @@ void DelayDetection::putData (SFLDataFormat *inputData, int nbBytes) ...@@ -166,14 +144,12 @@ void DelayDetection::putData (SFLDataFormat *inputData, int nbBytes)
} }
void DelayDetection::process (SFLDataFormat *inputData, int nbBytes) void DelayDetection::process (SFLDataFormat *inputData, int nbSamples)
{ {
if (_internalState != WaitForMic) if (_internalState != WaitForMic)
return; return;
int nbSamples = nbBytes/sizeof (SFLDataFormat);
if ( (_nbMicSampleStored + nbSamples) > DELAY_BUFF_SIZE) if ( (_nbMicSampleStored + nbSamples) > DELAY_BUFF_SIZE)
nbSamples = DELAY_BUFF_SIZE - _nbMicSampleStored; nbSamples = DELAY_BUFF_SIZE - _nbMicSampleStored;
......
...@@ -105,11 +105,9 @@ class DelayDetection ...@@ -105,11 +105,9 @@ class DelayDetection
~DelayDetection(); ~DelayDetection();
void reset (void); void putData (SFLDataFormat *inputData, int samples);
void putData (SFLDataFormat *inputData, int nbBytes);
void process (SFLDataFormat *inputData, int nbBytes); void process (SFLDataFormat *inputData, int samples);
private: private:
......
...@@ -211,10 +211,10 @@ void EchoCancel::reset() ...@@ -211,10 +211,10 @@ void EchoCancel::reset()
_processedByte = 0; _processedByte = 0;
} }
void EchoCancel::putData (SFLDataFormat *inputData, int nbBytes) void EchoCancel::putData (SFLDataFormat *inputData, int samples)
{ {
_delayDetector.putData (inputData, nbBytes); _delayDetector.putData (inputData, samples * sizeof(SFLDataFormat));
if (_spkrStoped) { if (_spkrStoped) {
_debug ("EchoCancel: Flush data"); _debug ("EchoCancel: Flush data");
...@@ -224,7 +224,7 @@ void EchoCancel::putData (SFLDataFormat *inputData, int nbBytes) ...@@ -224,7 +224,7 @@ void EchoCancel::putData (SFLDataFormat *inputData, int nbBytes)
} }
// Put data in speaker ring buffer // Put data in speaker ring buffer
_spkrData->Put (inputData, nbBytes); _spkrData->Put (inputData, samples * sizeof(SFLDataFormat));
} }
...@@ -241,25 +241,25 @@ int EchoCancel::getData (SFLDataFormat *outputData) ...@@ -241,25 +241,25 @@ int EchoCancel::getData (SFLDataFormat *outputData)
return copied; return copied;
} }
int EchoCancel::process (SFLDataFormat *inputData, SFLDataFormat *outputData, int nbBytes) int EchoCancel::process (SFLDataFormat *inputData, SFLDataFormat *outputData, int samples)
{ {
_delayDetector.process (inputData, nbBytes); _delayDetector.process (inputData, samples);
if (_spkrStoped) { if (_spkrStoped) {
memcpy(outputData, inputData, nbBytes); memcpy(outputData, inputData, samples * sizeof(SFLDataFormat));
return nbBytes; return samples;
} }
int byteSize = _smplPerFrame*sizeof (SFLDataFormat); int byteSize = _smplPerFrame*sizeof (SFLDataFormat);
// init temporary buffers // init temporary buffers
memset (_tmpSpkr, 0, BUFF_SIZE*sizeof (SFLDataFormat)); memset (_tmpSpkr, 0, sizeof(_tmpSpkr));
memset (_tmpMic, 0, BUFF_SIZE*sizeof (SFLDataFormat)); memset (_tmpMic, 0, sizeof (_tmpMic));
memset (_tmpOut, 0, BUFF_SIZE*sizeof (SFLDataFormat)); memset (_tmpOut, 0, sizeof (_tmpOut));
// Put mic data in ringbuffer // Put mic data in ringbuffer
_micData->Put (inputData, nbBytes); _micData->Put (inputData, samples * sizeof(SFLDataFormat));
// Store data for synchronization // Store data for synchronization
int spkrAvail = _spkrData->AvailForGet(); int spkrAvail = _spkrData->AvailForGet();
......
...@@ -86,7 +86,7 @@ class EchoCancel ...@@ -86,7 +86,7 @@ class EchoCancel
* Add speaker data into internal buffer * Add speaker data into internal buffer
* \param inputData containing far-end voice data to be sent to speakers * \param inputData containing far-end voice data to be sent to speakers
*/ */
void putData (SFLDataFormat *inputData, int nbBytes); void putData (SFLDataFormat *inputData, int samples);
/** /**
* Get data ready to be played by speakers * Get data ready to be played by speakers
...@@ -98,7 +98,7 @@ class EchoCancel ...@@ -98,7 +98,7 @@ class EchoCancel
* \param inputData containing mixed echo and voice data * \param inputData containing mixed echo and voice data
* \param outputData containing * \param outputData containing
*/ */
int process (SFLDataFormat *inputData, SFLDataFormat *outputData, int nbBytes); int process (SFLDataFormat *inputData, SFLDataFormat *outputData, int samples);
/** /**
* Set echo canceller internal sampling rate, reset if sampling rate changed * Set echo canceller internal sampling rate, reset if sampling rate changed
......
...@@ -5,12 +5,15 @@ ...@@ -5,12 +5,15 @@
* Author: asavard * Author: asavard
*/ */
#include <cassert>
#include <stdexcept>
#include "echosuppress.h" #include "echosuppress.h"
#include "pj/pool.h" #include "pj/pool.h"
#include "pj/os.h" #include "pj/os.h"
#include <stdexcept>
#define ECHO_CANCEL_MEM_SIZE 1000 #define ECHO_CANCEL_MEM_SIZE 1000
#define SAMPLES_PER_FRAME 160
EchoSuppress::EchoSuppress(pj_pool_t *pool) EchoSuppress::EchoSuppress(pj_pool_t *pool)
{ {
...@@ -23,7 +26,7 @@ EchoSuppress::EchoSuppress(pj_pool_t *pool) ...@@ -23,7 +26,7 @@ EchoSuppress::EchoSuppress(pj_pool_t *pool)
if (!pj_thread_is_registered()) if (!pj_thread_is_registered())
_warn("EchoCancel: Thread not registered..."); _warn("EchoCancel: Thread not registered...");
if (pjmedia_echo_create(pool, 8000, 160, 250, 0, PJMEDIA_ECHO_SIMPLE, &echoState) != PJ_SUCCESS) if (pjmedia_echo_create(pool, 8000, SAMPLES_PER_FRAME, 250, 0, PJMEDIA_ECHO_SIMPLE, &echoState) != PJ_SUCCESS)
throw std::runtime_error("EchoCancel: Error: Could not create echo canceller"); throw std::runtime_error("EchoCancel: Error: Could not create echo canceller");
} }
...@@ -31,16 +34,15 @@ EchoSuppress::~EchoSuppress() ...@@ -31,16 +34,15 @@ EchoSuppress::~EchoSuppress()
{ {
} }
void EchoSuppress::putData (SFLDataFormat *inputData, int nbBytes) void EchoSuppress::putData (SFLDataFormat *inputData, int samples)
{ {
assert(samples = SAMPLES_PER_FRAME);
if (pjmedia_echo_playback(echoState, reinterpret_cast<pj_int16_t *>(inputData)) != PJ_SUCCESS) if (pjmedia_echo_playback(echoState, reinterpret_cast<pj_int16_t *>(inputData)) != PJ_SUCCESS)
_warn("EchoCancel: Warning: Problem while putting input data"); _warn("EchoCancel: Warning: Problem while putting input data");
} }
int EchoSuppress::getData(SFLDataFormat *outputData) void EchoSuppress::getData(SFLDataFormat *outputData)
{ {
if (pjmedia_echo_capture(echoState, reinterpret_cast<pj_int16_t *>(outputData), 0) != PJ_SUCCESS) if (pjmedia_echo_capture(echoState, reinterpret_cast<pj_int16_t *>(outputData), 0) != PJ_SUCCESS)
_warn("EchoCancel: Warning: Problem while getting output data"); _warn("EchoCancel: Warning: Problem while getting output data");
return 0;
} }
...@@ -23,7 +23,7 @@ class EchoSuppress { ...@@ -23,7 +23,7 @@ class EchoSuppress {
*/ */
void putData (SFLDataFormat *, int); void putData (SFLDataFormat *, int);
int getData(SFLDataFormat *); void getData(SFLDataFormat *);
private: private:
......
...@@ -42,12 +42,12 @@ std::fstream tmpIn("gaintestin.raw", std::fstream::out); ...@@ -42,12 +42,12 @@ std::fstream tmpIn("gaintestin.raw", std::fstream::out);
std::fstream tmpOut("gaintestout.raw", std::fstream::out); std::fstream tmpOut("gaintestout.raw", std::fstream::out);
#endif #endif
void GainControl::process(SFLDataFormat *buf, int bufLength) void GainControl::process(SFLDataFormat *buf, int samples)
{ {
double rms, rmsAvgLevel, in, out, diffRms, maxRms; double rms, rmsAvgLevel, in, out, diffRms, maxRms;
maxRms = 0.0; maxRms = 0.0;
for(int i = 0; i < bufLength; i++) { for(int i = 0; i < samples; i++) {
// linear conversion // linear conversion
in = (double)buf[i] / (double)SHRT_MAX; in = (double)buf[i] / (double)SHRT_MAX;
......
...@@ -23,9 +23,9 @@ public: ...@@ -23,9 +23,9 @@ public:
/** /**
* Apply addaptive gain factor on input signal * Apply addaptive gain factor on input signal
* /param Input audio buffer * /param Input audio buffer
* /param Input buffer length * /param Input samples
*/ */
void process(SFLDataFormat *, int); void process(SFLDataFormat *, int samples);
private: private:
......
...@@ -31,34 +31,10 @@ ...@@ -31,34 +31,10 @@
#include <cassert> #include <cassert>
#include "noisesuppress.h" #include "noisesuppress.h"
NoiseSuppress::NoiseSuppress (int smplPerFrame, int samplingRate) : _noiseState (NULL) NoiseSuppress::NoiseSuppress (int smplPerFrame, int samplingRate)
, _smplPerFrame (smplPerFrame) : _smplPerFrame (smplPerFrame)
, _samplingRate (samplingRate)
{ {
initNewNoiseSuppressor (_smplPerFrame, _samplingRate); _noiseState = speex_preprocess_state_init (_smplPerFrame, samplingRate);
}
NoiseSuppress::~NoiseSuppress()
{
speex_preprocess_state_destroy (_noiseState);
}
void NoiseSuppress::reset (void)
{
speex_preprocess_state_destroy (_noiseState);
initNewNoiseSuppressor (_smplPerFrame, _samplingRate);
}
void NoiseSuppress::process (SFLDataFormat *data, int nBytes)
{
assert(_smplPerFrame == nBytes / sizeof(SFLDataFormat));
speex_preprocess_run (_noiseState, data);
}
void NoiseSuppress::initNewNoiseSuppressor (int smplPerFrame, int samplingRate)
{
_noiseState = speex_preprocess_state_init (smplPerFrame, samplingRate);
int i=1; int i=1;
speex_preprocess_ctl (_noiseState, SPEEX_PREPROCESS_SET_DENOISE, &i); speex_preprocess_ctl (_noiseState, SPEEX_PREPROCESS_SET_DENOISE, &i);
i=-20; i=-20;
...@@ -78,3 +54,15 @@ void NoiseSuppress::initNewNoiseSuppressor (int smplPerFrame, int samplingRate) ...@@ -78,3 +54,15 @@ void NoiseSuppress::initNewNoiseSuppressor (int smplPerFrame, int samplingRate)
i = 0; i = 0;
speex_preprocess_ctl (_noiseState, SPEEX_PREPROCESS_SET_VAD, &i); speex_preprocess_ctl (_noiseState, SPEEX_PREPROCESS_SET_VAD, &i);
} }
NoiseSuppress::~NoiseSuppress()
{
speex_preprocess_state_destroy (_noiseState);
}
void NoiseSuppress::process (SFLDataFormat *data, int samples)
{
assert(_smplPerFrame == samples);
speex_preprocess_run (_noiseState, data);
}
...@@ -36,33 +36,19 @@ ...@@ -36,33 +36,19 @@
class NoiseSuppress class NoiseSuppress
{ {
public: public:
NoiseSuppress (int smplPerFrame, int samplingRate); NoiseSuppress (int smplPerFrame, int samplingRate);
~NoiseSuppress (void); ~NoiseSuppress (void);
/** void process (SFLDataFormat *data, int samples);
* Reset noise suppressor internal state at runtime. Usefull when making a new call
*/
void reset (void);
void process (SFLDataFormat *data, int nbBytes);
private: private:
void initNewNoiseSuppressor (int _smplPerFrame, int samplingRate);
/**
* Noise reduction processing state
*/
SpeexPreprocessState *_noiseState; SpeexPreprocessState *_noiseState;
int _smplPerFrame; int _smplPerFrame;
int _samplingRate;
}; };
#endif #endif
...@@ -84,7 +84,7 @@ SpeexEchoCancel::~SpeexEchoCancel() ...@@ -84,7 +84,7 @@ SpeexEchoCancel::~SpeexEchoCancel()
} }
void SpeexEchoCancel::putData (SFLDataFormat *inputData, int nbBytes) void SpeexEchoCancel::putData (SFLDataFormat *inputData, int samples)
{ {
if (_spkrStopped) { if (_spkrStopped) {
_micData->flushAll(); _micData->flushAll();
...@@ -93,13 +93,13 @@ void SpeexEchoCancel::putData (SFLDataFormat *inputData, int nbBytes) ...@@ -93,13 +93,13 @@ void SpeexEchoCancel::putData (SFLDataFormat *inputData, int nbBytes)
} }
#ifdef DUMP_ECHOCANCEL_INTERNAL_DATA #ifdef DUMP_ECHOCANCEL_INTERNAL_DATA
spkrFile->write(reinterpret_cast<char *>(inputData), nbBytes); spkrFile->write(reinterpret_cast<char *>(inputData), samples * sizeof(SFLDataFormat));
#endif #endif
_spkrData->Put (inputData, nbBytes); _spkrData->Put (inputData, samples * sizeof(SFLDataFormat));
} }
int SpeexEchoCancel::process (SFLDataFormat *inputData, SFLDataFormat *outputData, int nbBytes) int SpeexEchoCancel::process (SFLDataFormat *inputData, SFLDataFormat *outputData, int samples)
{ {
if (_spkrStopped) if (_spkrStopped)
return 0; return 0;
...@@ -116,66 +116,50 @@ int SpeexEchoCancel::process (SFLDataFormat *inputData, SFLDataFormat *outputDat ...@@ -116,66 +116,50 @@ int SpeexEchoCancel::process (SFLDataFormat *inputData, SFLDataFormat *outputDat
#endif #endif
// Put mic data in ringbuffer // Put mic data in ringbuffer
_micData->Put (inputData, nbBytes); _micData->Put (inputData, samples * sizeof(SFLDataFormat));
// Store data for synchronization // Store data for synchronization
int spkrAvail = _spkrData->AvailForGet(); int spkrAvail = _spkrData->AvailForGet();
int micAvail = _micData->AvailForGet(); int micAvail = _micData->AvailForGet();
// Init number of frame processed if (spkrAvail < (_echoDelay+byteSize) || micAvail < byteSize) {
int nbFrame = 0; _micData->Discard(byteSize);
return 0;
// Get data from mic and speaker }
// if ((spkrAvail >= (byteSize * 6)) && (micAvail >= byteSize)) {
if ((spkrAvail >= (_echoDelay+byteSize)) && (micAvail >= byteSize)) {
int nbSamples = byteSize / sizeof(SFLDataFormat);
// get synchronized data _spkrData->Get (_tmpSpkr, byteSize);
_spkrData->Get (_tmpSpkr, byteSize); _micData->Get (_tmpMic, byteSize);
_micData->Get (_tmpMic, byteSize);
#ifdef DUMP_ECHOCANCEL_INTERNAL_DATA #ifdef DUMP_ECHOCANCEL_INTERNAL_DATA
micProcessFile->write(reinterpret_cast<char *>(_tmpMic), byteSize); micProcessFile->write(reinterpret_cast<char *>(_tmpMic), byteSize);
spkrProcessFile->write(reinterpret_cast<char *>(_tmpSpkr), byteSize); spkrProcessFile->write(reinterpret_cast<char *>(_tmpSpkr), byteSize);
#endif #endif
int32_t tmp; for(int i = 0; i < EC_FRAME_SIZE; i++) {
for(int i = 0; i < nbSamples; i++) { int32_t tmp = _tmpSpkr[i] * 3;
tmp = _tmpSpkr[i] * 3; if (tmp > SHRT_MAX)
if(tmp > SHRT_MAX) { tmp = SHRT_MAX;
tmp = SHRT_MAX; _tmpSpkr[i] = (int16_t)tmp;
}
_tmpSpkr[i] = (int16_t)tmp;
_tmpMic[i] /= 3; _tmpMic[i] /= 3;
} }
// Processed echo cancellation speex_echo_cancellation (_echoState, _tmpMic, _tmpSpkr, _tmpOut);
speex_echo_cancellation (_echoState, _tmpMic, _tmpSpkr, _tmpOut); speex_preprocess_run(_preState, reinterpret_cast<short *>(_tmpOut));
speex_preprocess_run(_preState, reinterpret_cast<short *>(_tmpOut));
#ifdef DUMP_ECHOCANCEL_INTERNAL_DATA #ifdef DUMP_ECHOCANCEL_INTERNAL_DATA
echoFile->write(reinterpret_cast<char *>(_tmpOut), byteSize); echoFile->write(reinterpret_cast<char *>(_tmpOut), byteSize);
#endif #endif
for(int i = 0; i < nbSamples; i++) { for(int i = 0; i < EC_FRAME_SIZE; i++) {
_tmpOut[i] *= 3; _tmpOut[i] *= 3;
} }
memcpy (outputData, _tmpOut, byteSize); memcpy (outputData, _tmpOut, byteSize);
spkrAvail = _spkrData->AvailForGet(); spkrAvail = _spkrData->AvailForGet();
micAvail = _micData->AvailForGet(); micAvail = _micData->AvailForGet();