mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-02-08 01:58:02 +08:00
refactor example + adding imgui and imtui submodules
This commit is contained in:
@@ -1,94 +1,15 @@
|
||||
/*! \file main.cpp
|
||||
* \brief Send/Receive data through sound in the terminal
|
||||
* \author Georgi Gerganov
|
||||
*/
|
||||
|
||||
#include "ggwave/ggwave.h"
|
||||
|
||||
#include "ggwave-common.h"
|
||||
#include "ggwave-common-sdl2.h"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_audio.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
|
||||
static char *g_defaultCaptureDeviceName = nullptr;
|
||||
|
||||
static int g_captureId = -1;
|
||||
static int g_playbackId = -1;
|
||||
|
||||
static bool g_isInitialized = false;
|
||||
|
||||
static SDL_AudioDeviceID g_devIdIn = 0;
|
||||
static SDL_AudioDeviceID g_devIdOut = 0;
|
||||
|
||||
static GGWave *g_ggWave = nullptr;
|
||||
|
||||
// main loop
|
||||
void update() {
|
||||
if (g_isInitialized == false) return;
|
||||
|
||||
SDL_Event e;
|
||||
SDL_bool shouldTerminate = SDL_FALSE;
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_QUIT) {
|
||||
shouldTerminate = SDL_TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
static GGWave::CBQueueAudio cbQueueAudio = [&](const void * data, uint32_t nBytes) {
|
||||
SDL_QueueAudio(g_devIdOut, data, nBytes);
|
||||
};
|
||||
|
||||
static GGWave::CBDequeueAudio CBDequeueAudio = [&](void * data, uint32_t nMaxBytes) {
|
||||
return SDL_DequeueAudio(g_devIdIn, data, nMaxBytes);
|
||||
};
|
||||
|
||||
if (g_ggWave->getHasData() == false) {
|
||||
SDL_PauseAudioDevice(g_devIdOut, SDL_FALSE);
|
||||
|
||||
static auto tLastNoData = std::chrono::high_resolution_clock::now();
|
||||
auto tNow = std::chrono::high_resolution_clock::now();
|
||||
|
||||
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);
|
||||
if ((int) SDL_GetQueuedAudioSize(g_devIdIn) > 32*g_ggWave->getSamplesPerFrame()*g_ggWave->getSampleSizeBytesIn()) {
|
||||
SDL_ClearQueuedAudio(g_devIdIn);
|
||||
}
|
||||
} else {
|
||||
SDL_ClearQueuedAudio(g_devIdIn);
|
||||
}
|
||||
} else {
|
||||
tLastNoData = tNow;
|
||||
//SDL_ClearQueuedAudio(g_devIdIn);
|
||||
//SDL_Delay(10);
|
||||
}
|
||||
} else {
|
||||
SDL_PauseAudioDevice(g_devIdOut, SDL_TRUE);
|
||||
SDL_PauseAudioDevice(g_devIdIn, SDL_TRUE);
|
||||
|
||||
g_ggWave->send(cbQueueAudio);
|
||||
}
|
||||
|
||||
if (shouldTerminate) {
|
||||
SDL_PauseAudioDevice(g_devIdIn, 1);
|
||||
SDL_CloseAudioDevice(g_devIdIn);
|
||||
SDL_PauseAudioDevice(g_devIdOut, 1);
|
||||
SDL_CloseAudioDevice(g_devIdOut);
|
||||
SDL_CloseAudio();
|
||||
SDL_Quit();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
printf("Usage: %s [-cN] [-pN] [-tN]\n", argv[0]);
|
||||
printf(" -cN - select capture device N\n");
|
||||
@@ -100,61 +21,58 @@ int main(int argc, char** argv) {
|
||||
printf(" -t3 : Ultrasonic\n");
|
||||
printf("\n");
|
||||
|
||||
g_defaultCaptureDeviceName = nullptr;
|
||||
|
||||
auto argm = parseCmdArguments(argc, argv);
|
||||
g_captureId = argm["c"].empty() ? 0 : std::stoi(argm["c"]);
|
||||
g_playbackId = argm["p"].empty() ? 0 : std::stoi(argm["p"]);
|
||||
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"]);
|
||||
|
||||
initSDL2ForGGWave(
|
||||
g_isInitialized,
|
||||
g_playbackId,
|
||||
g_devIdIn,
|
||||
g_captureId,
|
||||
g_devIdOut,
|
||||
g_ggWave);
|
||||
if (GGWave_init(playbackId, captureId) == false) {
|
||||
fprintf(stderr, "Failed to initialize GGWave\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_ggWave->setTxMode(GGWave::TxMode::VariableLength);
|
||||
auto ggWave = GGWave_instance();
|
||||
|
||||
ggWave->setTxMode(GGWave::TxMode::VariableLength);
|
||||
|
||||
printf("Selecting Tx protocol %d\n", txProtocol);
|
||||
switch (txProtocol) {
|
||||
case 0:
|
||||
{
|
||||
printf("Using 'Normal' Tx Protocol\n");
|
||||
g_ggWave->setParameters(1, 40, 9, 3, 50);
|
||||
ggWave->setParameters(1, 40, 9, 3, 50);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
{
|
||||
printf("Using 'Fast' Tx Protocol\n");
|
||||
g_ggWave->setParameters(1, 40, 6, 3, 50);
|
||||
ggWave->setParameters(1, 40, 6, 3, 50);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
printf("Using 'Fastest' Tx Protocol\n");
|
||||
g_ggWave->setParameters(1, 40, 3, 3, 50);
|
||||
ggWave->setParameters(1, 40, 3, 3, 50);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
printf("Using 'Ultrasonic' Tx Protocol\n");
|
||||
g_ggWave->setParameters(1, 320, 9, 3, 50);
|
||||
ggWave->setParameters(1, 320, 9, 3, 50);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
printf("Using 'Fast' Tx Protocol\n");
|
||||
g_ggWave->setParameters(1, 40, 6, 3, 50);
|
||||
ggWave->setParameters(1, 40, 6, 3, 50);
|
||||
}
|
||||
};
|
||||
printf("\n");
|
||||
|
||||
g_ggWave->init(0, "");
|
||||
ggWave->init(0, "");
|
||||
|
||||
std::mutex mutex;
|
||||
std::thread inputThread([&mutex]() {
|
||||
std::thread inputThread([&]() {
|
||||
std::string inputOld = "";
|
||||
while (true) {
|
||||
std::string input;
|
||||
@@ -168,30 +86,23 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
g_ggWave->init(input.size(), input.data());
|
||||
ggWave->init(input.size(), input.data());
|
||||
}
|
||||
inputOld = input;
|
||||
}
|
||||
});
|
||||
|
||||
while (true) {
|
||||
SDL_Delay(1);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
update();
|
||||
GGWave_mainLoop();
|
||||
}
|
||||
}
|
||||
|
||||
inputThread.join();
|
||||
|
||||
delete g_ggWave;
|
||||
|
||||
SDL_PauseAudioDevice(g_devIdIn, 1);
|
||||
SDL_CloseAudioDevice(g_devIdIn);
|
||||
SDL_PauseAudioDevice(g_devIdOut, 1);
|
||||
SDL_CloseAudioDevice(g_devIdOut);
|
||||
SDL_CloseAudio();
|
||||
SDL_Quit();
|
||||
GGWave_deinit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user