Skip to content
Snippets Groups Projects
Commit 5b7c167b authored by Tristan Matthews's avatar Tristan Matthews
Browse files

* #7097: cleanup

parent cce5909c
Branches
Tags
No related merge requests found
...@@ -46,14 +46,14 @@ ...@@ -46,14 +46,14 @@
RawFile::RawFile(const std::string& name, sfl::AudioCodec* codec, unsigned int sampleRate) RawFile::RawFile(const std::string& name, sfl::AudioCodec* codec, unsigned int sampleRate)
: audioCodec(codec) : audioCodec(codec)
{ {
filepath = name; filepath_ = name;
if (filepath.empty()) if (filepath_.empty())
throw AudioFileException("Unable to open audio file: filename is empty"); throw AudioFileException("Unable to open audio file: filename is empty");
std::fstream file; std::fstream file;
file.open(filepath.c_str(), std::fstream::in); file.open(filepath_.c_str(), std::fstream::in);
if (!file.is_open()) if (!file.is_open())
throw AudioFileException("Unable to open audio file"); throw AudioFileException("Unable to open audio file");
...@@ -122,7 +122,7 @@ WaveFile::WaveFile(const std::string& fileName, unsigned int audioSamplingRate) ...@@ -122,7 +122,7 @@ WaveFile::WaveFile(const std::string& fileName, unsigned int audioSamplingRate)
if (!fs) if (!fs)
throw AudioFileException("File " + fileName + " doesn't exist"); throw AudioFileException("File " + fileName + " doesn't exist");
filepath = fileName; filepath_ = fileName;
std::fstream fileStream; std::fstream fileStream;
fileStream.open(fileName.c_str(), std::ios::in | std::ios::binary); fileStream.open(fileName.c_str(), std::ios::in | std::ios::binary);
......
...@@ -54,13 +54,13 @@ class AudioFileException : public std::runtime_error { ...@@ -54,13 +54,13 @@ class AudioFileException : public std::runtime_error {
*/ */
class AudioFile : public AudioLoop { class AudioFile : public AudioLoop {
public: public:
const std::string &getFilePath(void) const { std::string getFilePath(void) const {
return filepath; return filepath_;
} }
protected: protected:
/** The absolute path to the sound file */ /** The absolute path to the sound file */
std::string filepath; std::string filepath_;
}; };
...@@ -82,7 +82,7 @@ class RawFile : public AudioFile { ...@@ -82,7 +82,7 @@ class RawFile : public AudioFile {
RawFile(const RawFile& rh); RawFile(const RawFile& rh);
// Assignment Operator // Assignment Operator
RawFile& operator= (const RawFile& rh); const RawFile& operator= (const RawFile& rh);
/** Your preferred codec */ /** Your preferred codec */
sfl::AudioCodec* audioCodec; sfl::AudioCodec* audioCodec;
...@@ -99,4 +99,4 @@ class WaveFile : public AudioFile { ...@@ -99,4 +99,4 @@ class WaveFile : public AudioFile {
WaveFile(const std::string&, unsigned int); WaveFile(const std::string&, unsigned int);
}; };
#endif #endif // __AUDIOFILE_H__
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
*/ */
#include <fstream> #include <fstream>
#include <limits.h> #include <climits>
#include "speexechocancel.h" #include "speexechocancel.h"
#include <speex/speex_echo.h> #include <speex/speex_echo.h>
...@@ -38,130 +38,95 @@ SpeexEchoCancel::SpeexEchoCancel() ...@@ -38,130 +38,95 @@ SpeexEchoCancel::SpeexEchoCancel()
int echoDelayMs = Manager::instance().getEchoCancelDelay(); int echoDelayMs = Manager::instance().getEchoCancelDelay();
int echoTailLengthMs = Manager::instance().getEchoCancelTailLength(); int echoTailLengthMs = Manager::instance().getEchoCancelTailLength();
_echoDelay = echoDelayMs * samplingRate / 1000; echoDelay_ = echoDelayMs * samplingRate / 1000;
_echoTailLength = echoTailLengthMs * samplingRate / 1000; echoTailLength_ = echoTailLengthMs * samplingRate / 1000;
// _echoState = speex_echo_state_init (EC_FRAME_SIZE, EC_FILTER_LENGTH); // echoState_ = speex_echo_state_init (EC_FRAME_SIZE, EC_FILTER_LENGTH);
_echoState = speex_echo_state_init(EC_FRAME_SIZE, _echoTailLength); echoState_ = speex_echo_state_init(EC_FRAME_SIZE, echoTailLength_);
_preState = speex_preprocess_state_init(EC_FRAME_SIZE, samplingRate); preState_ = speex_preprocess_state_init(EC_FRAME_SIZE, samplingRate);
_debug("EchoCancel: Initializing echo canceller with delay: %d, filter length: %d, frame size: %d and samplerate %d", _debug("EchoCancel: Initializing echo canceller with delay: %d, filter length: %d, frame size: %d and samplerate %d",
_echoDelay, _echoTailLength, EC_FRAME_SIZE, samplingRate); echoDelay_, echoTailLength_, EC_FRAME_SIZE, samplingRate);
speex_echo_ctl(_echoState, SPEEX_ECHO_SET_SAMPLING_RATE, &samplingRate); speex_echo_ctl(echoState_, SPEEX_ECHO_SET_SAMPLING_RATE, &samplingRate);
speex_preprocess_ctl(_preState, SPEEX_PREPROCESS_SET_ECHO_STATE, _echoState); speex_preprocess_ctl(preState_, SPEEX_PREPROCESS_SET_ECHO_STATE, echoState_);
_micData = new RingBuffer(100000); micData_ = new RingBuffer(100000);
_spkrData = new RingBuffer(100000); spkrData_ = new RingBuffer(100000);
_micData->createReadPointer(); micData_->createReadPointer();
_spkrData->createReadPointer(); spkrData_->createReadPointer();
#ifdef DUMP_ECHOCANCEL_INTERNAL_DATA spkrStopped_ = true;
micFile = new ofstream("test_mic_data.raw");
spkrFile = new ofstream("test_spkr_data.raw");
micProcessFile = new ofstream("test_mic_data_process.raw", std::ofstream::out);
spkrProcessFile = new ofstream("test_spkr_data_process.raw", std::ofstream::out);
echoFile = new ofstream("test_echo_data.raw");
#endif
_spkrStopped = true;
} }
SpeexEchoCancel::~SpeexEchoCancel() SpeexEchoCancel::~SpeexEchoCancel()
{ {
speex_echo_state_destroy(_echoState); speex_echo_state_destroy(echoState_);
speex_preprocess_state_destroy(_preState); speex_preprocess_state_destroy(preState_);
delete _micData; delete micData_;
delete _spkrData; delete spkrData_;
#ifdef DUMP_ECHOCANCEL_INTERNAL_DATA
delete micFile;
delete spkrFile;
delete micProcessFile;
delete spkrProcessFile;
delete echoFile;
#endif
} }
void SpeexEchoCancel::putData(SFLDataFormat *inputData, int samples) void SpeexEchoCancel::putData(SFLDataFormat *inputData, int samples)
{ {
if (_spkrStopped) { if (spkrStopped_) {
_micData->flushAll(); micData_->flushAll();
_spkrData->flushAll(); spkrData_->flushAll();
_spkrStopped = false; spkrStopped_ = false;
} }
#ifdef DUMP_ECHOCANCEL_INTERNAL_DATA spkrData_->Put(inputData, samples * sizeof(SFLDataFormat));
spkrFile->write(reinterpret_cast<char *>(inputData), samples * sizeof(SFLDataFormat));
#endif
_spkrData->Put(inputData, samples * sizeof(SFLDataFormat));
} }
int SpeexEchoCancel::process(SFLDataFormat *inputData, SFLDataFormat *outputData, int samples) int SpeexEchoCancel::process(SFLDataFormat *inputData, SFLDataFormat *outputData, int samples)
{ {
if (_spkrStopped) if (spkrStopped_)
return 0; return 0;
const int byteSize = EC_FRAME_SIZE * sizeof(SFLDataFormat); const int byteSize = EC_FRAME_SIZE * sizeof(SFLDataFormat);
// init temporary buffers // init temporary buffers
memset(_tmpSpkr, 0, sizeof(_tmpSpkr)); memset(tmpSpkr_, 0, sizeof(tmpSpkr_));
memset(_tmpMic, 0, sizeof(_tmpMic)); memset(tmpMic_, 0, sizeof(tmpMic_));
memset(_tmpOut, 0, sizeof(_tmpOut)); memset(tmpOut_, 0, sizeof(tmpOut_));
#ifdef DUMP_ECHOCANCEL_INTERNAL_DATA
micFile->write(reinterpret_cast<char *>(inputData), nbBytes);
#endif
// Put mic data in ringbuffer // Put mic data in ringbuffer
_micData->Put(inputData, samples * sizeof(SFLDataFormat)); 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();
if (spkrAvail < (_echoDelay+byteSize) || micAvail < byteSize) { if (spkrAvail < (echoDelay_+byteSize) || micAvail < byteSize) {
_micData->Discard(byteSize); micData_->Discard(byteSize);
return 0; return 0;
} }
_spkrData->Get(_tmpSpkr, byteSize); spkrData_->Get(tmpSpkr_, byteSize);
_micData->Get(_tmpMic, byteSize); micData_->Get(tmpMic_, byteSize);
#ifdef DUMP_ECHOCANCEL_INTERNAL_DATA
micProcessFile->write(reinterpret_cast<char *>(_tmpMic), byteSize);
spkrProcessFile->write(reinterpret_cast<char *>(_tmpSpkr), byteSize);
#endif
for (int i = 0; i < EC_FRAME_SIZE; i++) { for (int i = 0; i < EC_FRAME_SIZE; i++) {
int32_t tmp = _tmpSpkr[i] * 3; int32_t 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;
} }
speex_echo_cancellation(echoState_, tmpMic_, tmpSpkr_, tmpOut_);
speex_preprocess_run(preState_, reinterpret_cast<short *>(tmpOut_));
speex_echo_cancellation(_echoState, _tmpMic, _tmpSpkr, _tmpOut); for (int i = 0; i < EC_FRAME_SIZE; i++)
speex_preprocess_run(_preState, reinterpret_cast<short *>(_tmpOut)); tmpOut_[i] *= 3;
#ifdef DUMP_ECHOCANCEL_INTERNAL_DATA
echoFile->write(reinterpret_cast<char *>(_tmpOut), byteSize);
#endif
for (int i = 0; i < EC_FRAME_SIZE; i++) {
_tmpOut[i] *= 3;
}
memcpy(outputData, _tmpOut, byteSize); memcpy(outputData, tmpOut_, byteSize);
spkrAvail = _spkrData->AvailForGet(); spkrAvail = spkrData_->AvailForGet();
micAvail = _micData->AvailForGet(); micAvail = micData_->AvailForGet();
return EC_FRAME_SIZE; return EC_FRAME_SIZE;
} }
...@@ -49,29 +49,21 @@ class SpeexEchoCancel { ...@@ -49,29 +49,21 @@ class SpeexEchoCancel {
private: private:
SpeexEchoState *_echoState; SpeexEchoState *echoState_;
SpeexPreprocessState *_preState; SpeexPreprocessState *preState_;
RingBuffer *_micData; RingBuffer *micData_;
RingBuffer *_spkrData; RingBuffer *spkrData_;
int _echoDelay; int echoDelay_;
int _echoTailLength; int echoTailLength_;
bool _spkrStopped; bool spkrStopped_;
SFLDataFormat _tmpSpkr[5000]; SFLDataFormat tmpSpkr_[5000];
SFLDataFormat _tmpMic[5000]; SFLDataFormat tmpMic_[5000];
SFLDataFormat _tmpOut[5000]; SFLDataFormat tmpOut_[5000];
#ifdef DUMP_ECHOCANCEL_INTERNAL_DATA
ofstream *micFile;
ofstream *spkrFile;
ofstream *micProcessFile;
ofstream *spkrProcessFile;
ofstream *echoFile;
#endif
}; };
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment