From 2440d0aa3f0ca38f27fc739e56e55a754fe411ae Mon Sep 17 00:00:00 2001 From: Felix The Cat Date: Wed, 6 Jan 2021 11:28:33 +0300 Subject: [PATCH] Improved handling of starting and ending silence --- Measurer.cpp | 22 ++++++++++++++++++++-- Measurer.h | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Measurer.cpp b/Measurer.cpp index 3e00e23..a6d7db7 100644 --- a/Measurer.cpp +++ b/Measurer.cpp @@ -83,11 +83,19 @@ float Measurer::ProcessData(QByteArray data) } cerr << "Samples read: " << oss.str() << "\n"; } + // determine silence threshold (will consider signal less than SILENCE of maximum as silence) + float max_sample = abs(samples[0]); + for (int i = 1; i < (int)samples.size(); i++) + { + float q = abs(samples[i]); + if (q > max_sample) max_sample = q; + } + float silence_level = SILENCE_LEVEL * max_sample; // cut off starting and ending silence int left_pos = 0; - while (left_pos < (int)samples.size() && abs(samples[left_pos]) < 1e-4) left_pos++; + while (left_pos < (int)samples.size() && abs(samples[left_pos]) < silence_level) left_pos++; int right_pos = (int)samples.size() - 1; - while (right_pos >= 0 && abs(samples[right_pos]) < 1e-4) right_pos--; + while (right_pos >= 0 && abs(samples[right_pos]) < silence_level) right_pos--; int len = right_pos - left_pos + 1; int pow = 1, m = 0; while (2 * pow <= len) @@ -101,6 +109,16 @@ float Measurer::ProcessData(QByteArray data) d[i] = Complex(samples[left_pos + i], 0); } FFT(d, pow, false); + if (debug) + { + ostringstream oss; + for (int i = 0; i < pow; i++) + { + float amp = (float)sqrt(d[i].real() * d[i].real() + d[i].imag() * d[i].imag()); + oss << amp << " "; + } + cerr << "Spectrum: " << oss.str() << "\n"; + } float max = -1; int argmax = 0; for (int i = 0; i < pow; i++) diff --git a/Measurer.h b/Measurer.h index 461e152..37aa04a 100644 --- a/Measurer.h +++ b/Measurer.h @@ -13,6 +13,7 @@ class Measurer { const int TONE_FREQUENCY = 6000; const int SAMPLE_RATE = 44100; + const float SILENCE_LEVEL = 0.3; MeasureChannel channel; QAudioDeviceInfo input, output;