Commit 5b7c167b authored by Tristan Matthews's avatar Tristan Matthews
Browse files

* #7097: cleanup

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