Skip to content

Commit

Permalink
added agc; added normalization to captured samples; some bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
YYY committed Jun 3, 2019
1 parent 72f9b2e commit 33175f7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
22 changes: 17 additions & 5 deletions Source/Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ Processor::~Processor()
{
}

double Processor::getAgcGained(double sample)
{
double gained = sample * agcGain;
double _agcSpeed = agcSpeed / frameSize;
if (std::abs(gained) > 1) agcGain -= agcSpeed;
else agcGain += _agcSpeed;
return gained;
}

void Processor::addSamples(std::vector<double> samples)
{
for (int i = 0; i < samples.size(); ++i)
Expand Down Expand Up @@ -197,6 +206,8 @@ void Processor::loadState(std::string jsonText)

threshold = jsonObj["threshold"].get<double>();

reload();

}

double Processor::getThreshold()
Expand All @@ -221,12 +232,13 @@ bool Processor::process()
for (int i = 0; i < frameSize; ++i)
{
uint16_t ptr = bufferPtr - frameSize + i;
frame[i] = buffer[ptr];
frame[i] = getAgcGained(buffer[ptr]);
}

if (capturingSample)
{
SoundDetector::getSpectrum(frame);
SoundDetector::normalize(frame);
sampleSpectrum[capturingSampleId] = frame;
capturingSample = false;
reload();
Expand All @@ -239,13 +251,13 @@ bool Processor::process()
if (sampleEnabled[i])
{
double distance = detectors[i].process(frame);
mfccScore = std::max((1 - distance - 0.35) * 3, mfccScore);
mfccScore = std::max((1 - distance - 0.48) * 10, mfccScore);
}
}
currentMfccScore = std::min(mfccScore, 1.);
currentMfccScore = std::max(std::min(mfccScore, 1.), 0.);

//OutputDebugString(std::to_string(currentMfccScore).c_str());
//OutputDebugString("\n");
OutputDebugString(std::to_string(currentMfccScore).c_str());
OutputDebugString("\n");

bool result = currentMfccScore > threshold;

Expand Down
4 changes: 3 additions & 1 deletion Source/Processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ class Processor
bool capturingSample = false;
int capturingSampleId;
Processor *getMuteTimeFrom;
double getAgcGained(double sample);

double mfccScoreOffset;
double mfccScoreScale;
double threshold = 0.5;

double agcSpeed = 0.2;
double agcGain = 1;

};

21 changes: 19 additions & 2 deletions Source/SoundDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ void SoundDetector::getSpectrum(std::vector<double>& frame)
}
}

void SoundDetector::normalize(std::vector<double>& frame)
{
double sum = 0;
for (int i = 0; i < frame.size(); ++i)
{
sum += frame[i];
}
if (sum != 0)
{
sum /= frame.size();
for (int i = 0; i < frame.size(); ++i)
{
frame[i] /= sum;
}
}
}

void SoundDetector::_process(std::vector<double> frame)
{
getSpectrum(frame);
Expand All @@ -122,7 +139,7 @@ void SoundDetector::_process(std::vector<double> frame)

int scaled_size = std::min((int)(ratio * 300), (int)frame.size() / 2);

if (ratio > 2 || ratio < 0.5) scaled_size = 0;
if (ratio > 2 || ratio < 0.5 || !std::isfinite(ratio)) scaled_size = 0;

std::vector<double> scaled_sample_spectrum(scaled_size);

Expand All @@ -144,7 +161,7 @@ void SoundDetector::_process(std::vector<double> frame)
sum_ += pow(frame[i], 2) * pow(scaled_sample_spectrum[i], 2) / std::pow(scaled_size, 4);
}

calculatedDistance = distance - sum_ / 5;
calculatedDistance = distance - sum_ / 3;
}

double SoundDetector::process(std::vector<double> frame)
Expand Down
1 change: 1 addition & 0 deletions Source/SoundDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SoundDetector
~SoundDetector();

static void getSpectrum(std::vector<double>& frame);
static void normalize(std::vector<double>& frame);

private:
void _process(std::vector<double> frame);
Expand Down

0 comments on commit 33175f7

Please sign in to comment.