ggwave : add "soundMarkerThreshold" parameter

Can be used to control the threshold used for distinguishing odd from even
frequencies in the sound markers.
This commit is contained in:
Georgi Gerganov
2021-02-21 00:13:28 +02:00
parent 6011c0cef2
commit f4fb02d5d4
5 changed files with 14 additions and 5 deletions

View File

@@ -220,6 +220,7 @@ bool GGWave_init(
g_obtainedSpecInp.freq,
g_obtainedSpecOut.freq,
GGWave::kDefaultSamplesPerFrame,
GGWave::kDefaultSoundMarkerThreshold,
sampleFormatInp,
sampleFormatOut});
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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--;
}
}

View File

@@ -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";