Skip to content

Commit

Permalink
ggwave : add "soundMarkerThreshold" parameter
Browse files Browse the repository at this point in the history
Can be used to control the threshold used for distinguishing odd from even
frequencies in the sound markers.
  • Loading branch information
ggerganov committed Feb 20, 2021
1 parent 6011c0c commit f4fb02d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/ggwave-common-sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ bool GGWave_init(
g_obtainedSpecInp.freq,
g_obtainedSpecOut.freq,
GGWave::kDefaultSamplesPerFrame,
GGWave::kDefaultSoundMarkerThreshold,
sampleFormatInp,
sampleFormatOut});
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ggwave-to-file/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> bufferPCM;
Expand Down
4 changes: 4 additions & 0 deletions include/ggwave/ggwave.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -225,6 +226,7 @@ class GGWave {
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;
Expand Down Expand Up @@ -378,6 +380,8 @@ class GGWave {
const int m_nMarkerFrames;
const int m_encodedDataOffset;

const float m_soundMarkerThreshold;

// common

bool m_isFixedPayloadLength;
Expand Down
11 changes: 7 additions & 4 deletions src/ggwave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ggwave_Instance ggwave_init(const ggwave_Parameters parameters) {
parameters.sampleRateInp,
parameters.sampleRateOut,
parameters.samplesPerFrame,
parameters.soundMarkerThreshold,
parameters.sampleFormatInp,
parameters.sampleFormatOut});

Expand Down Expand Up @@ -275,6 +276,7 @@ const GGWave::Parameters & GGWave::getDefaultParameters() {
kBaseSampleRate,
kBaseSampleRate,
kDefaultSamplesPerFrame,
kDefaultSoundMarkerThreshold,
GGWAVE_SAMPLE_FORMAT_F32,
GGWAVE_SAMPLE_FORMAT_F32,
};
Expand All @@ -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),
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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--;
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/test-ggwave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down

0 comments on commit f4fb02d

Please sign in to comment.