Skip to content

Commit

Permalink
ggwave : add option to query the generated tones
Browse files Browse the repository at this point in the history
Calling the getWaveformTones() method after calling encode() gives a
list of the tones participating in the generated sound
  • Loading branch information
ggerganov committed Apr 3, 2021
1 parent 7d88f42 commit ba87a65
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
21 changes: 18 additions & 3 deletions examples/ggwave-cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ int main(int argc, char** argv) {
printf(" -pN - select playback device N\n");
printf(" -tN - transmission protocol\n");
printf(" -lN - fixed payload length of size N, N in [1, %d]\n", GGWave::kMaxLengthFixed);
printf(" -v - print generated tones on resend\n");
printf("\n");

auto argm = parseCmdArguments(argc, argv);
int captureId = argm["c"].empty() ? 0 : std::stoi(argm["c"]);
int playbackId = argm["p"].empty() ? 0 : std::stoi(argm["p"]);
int txProtocol = argm["t"].empty() ? 1 : std::stoi(argm["t"]);
int payloadLength = argm["l"].empty() ? -1 : std::stoi(argm["l"]);
bool printTones = argm.find("v") == argm.end() ? false : true;

if (GGWave_init(playbackId, captureId, payloadLength) == false) {
fprintf(stderr, "Failed to initialize GGWave\n");
Expand All @@ -51,13 +53,26 @@ int main(int argc, char** argv) {
std::string inputOld = "";
while (true) {
std::string input;
std::cout << "Enter text: ";
printf("Enter text: ");
fflush(stdout);
getline(std::cin, input);
if (input.empty()) {
std::cout << "Re-sending ... " << std::endl;
printf("Re-sending ...\n");
input = inputOld;

if (printTones) {
printf("Printing generated waveform tones (Hz):\n");
auto waveformTones = ggWave->getWaveformTones();
for (int i = 0; i < (int) waveformTones.size(); ++i) {
printf(" - frame %3d: ", i);
for (int j = 0; j < (int) waveformTones[i].size(); ++j) {
printf("%8.2f ", waveformTones[i][j].freq_hz);
}
printf("\n");
}
}
} else {
std::cout << "Sending ... " << std::endl;
printf("Sending ...\n");
}
{
std::lock_guard<std::mutex> lock(mutex);
Expand Down
15 changes: 15 additions & 0 deletions include/ggwave/ggwave.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ class GGWave {
return kTxProtocols;
}

struct ToneData {
double freq_hz;
double duration_ms;
};

using Tones = std::vector<ToneData>;
using WaveformTones = std::vector<Tones>;

using AmplitudeData = std::vector<float>;
using AmplitudeDataI16 = std::vector<int16_t>;
using SpectrumData = std::vector<float>;
Expand Down Expand Up @@ -355,6 +363,12 @@ class GGWave {
static const TxProtocol & getTxProtocol(int id) { return getTxProtocols().at(TxProtocolId(id)); }
static const TxProtocol & getTxProtocol(TxProtocolId id) { return getTxProtocols().at(id); }

// get a list of the tones generated for the last waveform
//
// Call this method after calling encode() to get a list of the tones participating in the generated waveform
//
const WaveformTones & getWaveformTones() { return m_waveformTones; }

bool takeTxAmplitudeI16(AmplitudeDataI16 & dst);

// Rx
Expand Down Expand Up @@ -473,6 +487,7 @@ class GGWave {
TxRxData m_outputBlockTmp;
AmplitudeDataI16 m_outputBlockI16;
AmplitudeDataI16 m_txAmplitudeDataI16;
WaveformTones m_waveformTones;

// Impl
// todo : move all members inside Impl
Expand Down
16 changes: 16 additions & 0 deletions src/ggwave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,18 +546,26 @@ bool GGWave::encode(const CBWaveformOut & cbWaveformOut) {
float factor = kBaseSampleRate/m_sampleRateOut;
uint32_t offset = 0;

m_waveformTones.clear();

while (m_hasNewTxData) {
std::fill(m_outputBlock.begin(), m_outputBlock.end(), 0.0f);

std::uint16_t nFreq = 0;
m_waveformTones.push_back({});

if (frameId < m_nMarkerFrames) {
nFreq = m_nBitsInMarker;

for (int i = 0; i < m_nBitsInMarker; ++i) {
m_waveformTones.back().push_back({});
m_waveformTones.back().back().duration_ms = (1000.0*m_samplesPerFrame)/kBaseSampleRate;
if (i%2 == 0) {
::addAmplitudeSmooth(bit1Amplitude[i], m_outputBlock, m_sendVolume, 0, m_samplesPerFrame, frameId, m_nMarkerFrames);
m_waveformTones.back().back().freq_hz = bitFreq(m_txProtocol, i);
} else {
::addAmplitudeSmooth(bit0Amplitude[i], m_outputBlock, m_sendVolume, 0, m_samplesPerFrame, frameId, m_nMarkerFrames);
m_waveformTones.back().back().freq_hz = bitFreq(m_txProtocol, i) + m_hzPerSample;
}
}
} else if (frameId < m_nMarkerFrames + totalDataFrames) {
Expand All @@ -583,21 +591,29 @@ bool GGWave::encode(const CBWaveformOut & cbWaveformOut) {
if (dataBits[k] == 0) continue;

++nFreq;
m_waveformTones.back().push_back({});
m_waveformTones.back().back().duration_ms = (1000.0*m_samplesPerFrame)/kBaseSampleRate;
if (k%2) {
::addAmplitudeSmooth(bit0Amplitude[k/2], m_outputBlock, m_sendVolume, 0, m_samplesPerFrame, cycleModMain, m_txProtocol.framesPerTx);
m_waveformTones.back().back().freq_hz = bitFreq(m_txProtocol, k/2) + m_hzPerSample;
} else {
::addAmplitudeSmooth(bit1Amplitude[k/2], m_outputBlock, m_sendVolume, 0, m_samplesPerFrame, cycleModMain, m_txProtocol.framesPerTx);
m_waveformTones.back().back().freq_hz = bitFreq(m_txProtocol, k/2);
}
}
} else if (frameId < m_nMarkerFrames + totalDataFrames + m_nMarkerFrames) {
nFreq = m_nBitsInMarker;

int fId = frameId - (m_nMarkerFrames + totalDataFrames);
for (int i = 0; i < m_nBitsInMarker; ++i) {
m_waveformTones.back().push_back({});
m_waveformTones.back().back().duration_ms = (1000.0*m_samplesPerFrame)/kBaseSampleRate;
if (i%2 == 0) {
addAmplitudeSmooth(bit0Amplitude[i], m_outputBlock, m_sendVolume, 0, m_samplesPerFrame, fId, m_nMarkerFrames);
m_waveformTones.back().back().freq_hz = bitFreq(m_txProtocol, i) + m_hzPerSample;
} else {
addAmplitudeSmooth(bit1Amplitude[i], m_outputBlock, m_sendVolume, 0, m_samplesPerFrame, fId, m_nMarkerFrames);
m_waveformTones.back().back().freq_hz = bitFreq(m_txProtocol, i);
}
}
} else {
Expand Down

0 comments on commit ba87a65

Please sign in to comment.