From f4fb02d5d4cfd6c1021d73b55a0e52ac9d3dbdfa Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Sun, 21 Feb 2021 00:13:28 +0200 Subject: [PATCH] ggwave : add "soundMarkerThreshold" parameter Can be used to control the threshold used for distinguishing odd from even frequencies in the sound markers. --- examples/ggwave-common-sdl2.cpp | 1 + examples/ggwave-to-file/main.cpp | 2 +- include/ggwave/ggwave.h | 4 ++++ src/ggwave.cpp | 11 +++++++---- tests/test-ggwave.cpp | 1 + 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/examples/ggwave-common-sdl2.cpp b/examples/ggwave-common-sdl2.cpp index c5d56ca..4a4df5c 100644 --- a/examples/ggwave-common-sdl2.cpp +++ b/examples/ggwave-common-sdl2.cpp @@ -220,6 +220,7 @@ bool GGWave_init( g_obtainedSpecInp.freq, g_obtainedSpecOut.freq, GGWave::kDefaultSamplesPerFrame, + GGWave::kDefaultSoundMarkerThreshold, sampleFormatInp, sampleFormatOut}); } diff --git a/examples/ggwave-to-file/main.cpp b/examples/ggwave-to-file/main.cpp index 10c90a7..137c23f 100644 --- a/examples/ggwave-to-file/main.cpp +++ b/examples/ggwave-to-file/main.cpp @@ -69,7 +69,7 @@ int main(int argc, char** argv) { fprintf(stderr, "Generating waveform for message '%s' ...\n", message.c_str()); - GGWave ggWave({ -1, GGWave::kBaseSampleRate, sampleRateOut, 1024, GGWAVE_SAMPLE_FORMAT_F32, GGWAVE_SAMPLE_FORMAT_I16 }); + GGWave ggWave({ -1, GGWave::kBaseSampleRate, sampleRateOut, 1024, GGWave::kDefaultSamplesPerFrame, GGWAVE_SAMPLE_FORMAT_F32, GGWAVE_SAMPLE_FORMAT_I16 }); ggWave.init(message.size(), message.data(), ggWave.getTxProtocol(protocolId), volume); std::vector bufferPCM; diff --git a/include/ggwave/ggwave.h b/include/ggwave/ggwave.h index 94d85c1..76dfbfa 100644 --- a/include/ggwave/ggwave.h +++ b/include/ggwave/ggwave.h @@ -69,6 +69,7 @@ extern "C" { int sampleRateInp; // capture sample rate int sampleRateOut; // playback sample rate int samplesPerFrame; // number of samples per audio frame + float soundMarkerThreshold; // sound marker detection threshold ggwave_SampleFormat sampleFormatInp; // format of the captured audio samples ggwave_SampleFormat sampleFormatOut; // format of the playback audio samples } ggwave_Parameters; @@ -225,6 +226,7 @@ public: static constexpr auto kBaseSampleRate = 48000; static constexpr auto kDefaultSamplesPerFrame = 1024; static constexpr auto kDefaultVolume = 10; + static constexpr auto kDefaultSoundMarkerThreshold = 3.0f; static constexpr auto kMaxSamplesPerFrame = 2048; static constexpr auto kMaxDataBits = 256; static constexpr auto kMaxDataSize = 256; @@ -378,6 +380,8 @@ private: const int m_nMarkerFrames; const int m_encodedDataOffset; + const float m_soundMarkerThreshold; + // common bool m_isFixedPayloadLength; diff --git a/src/ggwave.cpp b/src/ggwave.cpp index 33a7705..a5306cc 100644 --- a/src/ggwave.cpp +++ b/src/ggwave.cpp @@ -33,6 +33,7 @@ ggwave_Instance ggwave_init(const ggwave_Parameters parameters) { parameters.sampleRateInp, parameters.sampleRateOut, parameters.samplesPerFrame, + parameters.soundMarkerThreshold, parameters.sampleFormatInp, parameters.sampleFormatOut}); @@ -275,6 +276,7 @@ const GGWave::Parameters & GGWave::getDefaultParameters() { kBaseSampleRate, kBaseSampleRate, kDefaultSamplesPerFrame, + kDefaultSoundMarkerThreshold, GGWAVE_SAMPLE_FORMAT_F32, GGWAVE_SAMPLE_FORMAT_F32, }; @@ -298,6 +300,7 @@ GGWave::GGWave(const Parameters & parameters) : m_nBitsInMarker(16), m_nMarkerFrames(parameters.payloadLength > 0 ? 0 : 16), m_encodedDataOffset(parameters.payloadLength > 0 ? 0 : 3), + m_soundMarkerThreshold(parameters.soundMarkerThreshold), // common m_isFixedPayloadLength(parameters.payloadLength > 0), m_payloadLength(parameters.payloadLength), @@ -1047,9 +1050,9 @@ void GGWave::decode_variable() { int bin = std::round(freq*m_ihzPerSample); if (i%2 == 0) { - if (m_sampleSpectrum[bin] <= 3.0f*m_sampleSpectrum[bin + m_freqDelta_bin]) --nDetectedMarkerBits; + if (m_sampleSpectrum[bin] <= m_soundMarkerThreshold*m_sampleSpectrum[bin + m_freqDelta_bin]) --nDetectedMarkerBits; } else { - if (m_sampleSpectrum[bin] >= 3.0f*m_sampleSpectrum[bin + m_freqDelta_bin]) --nDetectedMarkerBits; + if (m_sampleSpectrum[bin] >= m_soundMarkerThreshold*m_sampleSpectrum[bin + m_freqDelta_bin]) --nDetectedMarkerBits; } } @@ -1096,9 +1099,9 @@ void GGWave::decode_variable() { int bin = std::round(freq*m_ihzPerSample); if (i%2 == 0) { - if (m_sampleSpectrum[bin] >= 3.0f*m_sampleSpectrum[bin + m_freqDelta_bin]) nDetectedMarkerBits--; + if (m_sampleSpectrum[bin] >= m_soundMarkerThreshold*m_sampleSpectrum[bin + m_freqDelta_bin]) nDetectedMarkerBits--; } else { - if (m_sampleSpectrum[bin] <= 3.0f*m_sampleSpectrum[bin + m_freqDelta_bin]) nDetectedMarkerBits--; + if (m_sampleSpectrum[bin] <= m_soundMarkerThreshold*m_sampleSpectrum[bin + m_freqDelta_bin]) nDetectedMarkerBits--; } } diff --git a/tests/test-ggwave.cpp b/tests/test-ggwave.cpp index d82412c..b663714 100644 --- a/tests/test-ggwave.cpp +++ b/tests/test-ggwave.cpp @@ -192,6 +192,7 @@ int main(int argc, char ** argv) { // playback / capture at different sample rates for (int srInp = GGWave::kBaseSampleRate/3; srInp <= 2*GGWave::kBaseSampleRate; srInp += 1100) { auto parameters = GGWave::getDefaultParameters(); + parameters.soundMarkerThreshold = 1.1f; std::string payload = "hello123";