ggwave : refactoring + comments

This commit is contained in:
Georgi Gerganov
2021-01-17 21:19:33 +02:00
parent e1ba135da7
commit cd332b5919
10 changed files with 143 additions and 75 deletions

View File

@@ -30,8 +30,9 @@ int main(int argc, char** argv) {
auto ggWave = GGWave_instance();
printf("Available Tx protocols:\n");
for (int i = 0; i < (int) ggWave->getTxProtocols().size(); ++i) {
printf(" -t%d : %s\n", i, ggWave->getTxProtocols()[i].name);
const auto & protocols = GGWave::getTxProtocols();
for (const auto & protocol : protocols) {
printf(" -t%d : %s\n", protocol.first, protocol.second.name);
}
if (txProtocol < 0 || txProtocol > (int) ggWave->getTxProtocols().size()) {
@@ -56,7 +57,7 @@ int main(int argc, char** argv) {
}
{
std::lock_guard<std::mutex> lock(mutex);
ggWave->init(input.size(), input.data(), ggWave->getTxProtocols()[txProtocol], 10);
ggWave->init(input.size(), input.data(), ggWave->getTxProtocol(txProtocol), 10);
}
inputOld = input;
}

View File

@@ -34,7 +34,7 @@ GGWave *g_ggWave = nullptr;
extern "C" {
EMSCRIPTEN_KEEPALIVE
int sendData(int textLength, const char * text, int protocolId, int volume) {
g_ggWave->init(textLength, text, g_ggWave->getTxProtocols()[protocolId], volume);
g_ggWave->init(textLength, text, g_ggWave->getTxProtocol(protocolId), volume);
return 0;
}
@@ -242,7 +242,7 @@ bool GGWave_mainLoop() {
return false;
}
static GGWave::CBQueueAudio cbQueueAudio = [&](const void * data, uint32_t nBytes) {
static GGWave::CBEnqueueAudio cbQueueAudio = [&](const void * data, uint32_t nBytes) {
SDL_QueueAudio(g_devIdOut, data, nBytes);
};
@@ -259,7 +259,7 @@ bool GGWave_mainLoop() {
if ((int) SDL_GetQueuedAudioSize(g_devIdOut) < g_ggWave->getSamplesPerFrame()*g_ggWave->getSampleSizeBytesOut()) {
SDL_PauseAudioDevice(g_devIdIn, SDL_FALSE);
if (::getTime_ms(tLastNoData, tNow) > 500.0f) {
g_ggWave->receive(CBDequeueAudio);
g_ggWave->decode(CBDequeueAudio);
if ((int) SDL_GetQueuedAudioSize(g_devIdIn) > 32*g_ggWave->getSamplesPerFrame()*g_ggWave->getSampleSizeBytesIn()) {
SDL_ClearQueuedAudio(g_devIdIn);
}
@@ -273,7 +273,7 @@ bool GGWave_mainLoop() {
SDL_PauseAudioDevice(g_devIdOut, SDL_TRUE);
SDL_PauseAudioDevice(g_devIdIn, SDL_TRUE);
g_ggWave->send(cbQueueAudio);
g_ggWave->encode(cbQueueAudio);
}
return true;

View File

@@ -5,7 +5,7 @@ import numpy as np
p = pyaudio.PyAudio()
# generate audio waveform for string "hello python"
waveform = ggwave.encode("hello python", txProtocol = 1, volume = 20)
waveform = ggwave.encode("hello python", txProtocolId = 1, volume = 20)
print("Transmitting text 'hello python' ...")
stream = p.open(format=pyaudio.paInt16, channels=1, rate=48000, output=True, frames_per_buffer=4096)

View File

@@ -18,8 +18,8 @@ int main(int argc, char** argv) {
fprintf(stderr, " Available protocols:\n");
const auto & protocols = GGWave::getTxProtocols();
for (int i = 0; i < (int) protocols.size(); ++i) {
fprintf(stderr, " %d - %s\n", i, protocols[i].name);
for (const auto & protocol : protocols) {
fprintf(stderr, " %d - %s\n", protocol.first, protocol.second.name);
}
fprintf(stderr, "\n");
@@ -71,15 +71,15 @@ int main(int argc, char** argv) {
fprintf(stderr, "Generating waveform for message '%s' ...\n", message.c_str());
GGWave ggWave(GGWave::kBaseSampleRate, sampleRateOut, 1024, 4, 2);
ggWave.init(message.size(), message.data(), ggWave.getTxProtocols()[protocolId], volume);
ggWave.init(message.size(), message.data(), ggWave.getTxProtocol(protocolId), volume);
std::vector<char> bufferPCM;
GGWave::CBQueueAudio cbQueueAudio = [&](const void * data, uint32_t nBytes) {
GGWave::CBEnqueueAudio cbEnqueueAudio = [&](const void * data, uint32_t nBytes) {
bufferPCM.resize(nBytes);
std::memcpy(bufferPCM.data(), data, nBytes);
};
if (ggWave.send(cbQueueAudio) == false) {
if (ggWave.encode(cbEnqueueAudio) == false) {
fprintf(stderr, "Failed to generate waveform!\n");
return -4;
}

View File

@@ -523,7 +523,7 @@ void updateCore() {
g_ggWave->init(
(int) inputCurrent.message.data.size(),
inputCurrent.message.data.data(),
g_ggWave->getTxProtocols()[inputCurrent.message.protocolId],
g_ggWave->getTxProtocol(inputCurrent.message.protocolId),
100*inputCurrent.message.volume);
inputCurrent.update = false;
@@ -892,10 +892,10 @@ void renderMain() {
ImGui::Text("Tx Protocol: ");
ImGui::SetCursorScreenPos({ posSave.x + kLabelWidth, posSave.y });
}
if (ImGui::BeginCombo("##protocol", g_ggWave->getTxProtocols()[settings.protocolId].name)) {
if (ImGui::BeginCombo("##protocol", g_ggWave->getTxProtocol(settings.protocolId).name)) {
for (int i = 0; i < (int) g_ggWave->getTxProtocols().size(); ++i) {
const bool isSelected = (settings.protocolId == i);
if (ImGui::Selectable(g_ggWave->getTxProtocols()[i].name, isSelected)) {
if (ImGui::Selectable(g_ggWave->getTxProtocol(i).name, isSelected)) {
settings.protocolId = i;
}
@@ -966,7 +966,7 @@ void renderMain() {
const auto msgStatus = message.received ? "Recv" : "Send";
const auto msgColor = message.received ? ImVec4 { 0.0f, 1.0f, 0.0f, interp } : ImVec4 { 1.0f, 1.0f, 0.0f, interp };
ImGui::TextColored(msgColor, "[%s] %s (%s):", ::toTimeString(message.timestamp), msgStatus, g_ggWave->getTxProtocols()[message.protocolId].name);
ImGui::TextColored(msgColor, "[%s] %s (%s):", ::toTimeString(message.timestamp), msgStatus, g_ggWave->getTxProtocol(message.protocolId).name);
{
auto p0 = ImGui::GetCursorScreenPos();