From 174b6872e886f32eaa45dcadbbea5350de7372de Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Sat, 19 Dec 2020 11:26:36 +0200 Subject: [PATCH] examples : add "simple-rx" example + minor clean-up --- examples/CMakeLists.txt | 1 + examples/ggwave-common-sdl2.cpp | 12 ++++++------ examples/ggwave-common-sdl2.h | 2 -- examples/ggwave-common.h | 2 ++ examples/ggwave-gui/main.cpp | 22 ++++++++++------------ examples/ggwave-wasm/main.cpp | 3 +-- examples/simple-rx/CMakeLists.txt | 13 +++++++++++++ examples/simple-rx/main.cpp | 30 ++++++++++++++++++++++++++++++ include/ggwave/ggwave.h | 1 + 9 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 examples/simple-rx/CMakeLists.txt create mode 100644 examples/simple-rx/main.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e9f8a79..f4d20d5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -73,5 +73,6 @@ if (GGWAVE_SUPPORT_SDL2) add_subdirectory(ggwave-cli) add_subdirectory(ggwave-gui) + add_subdirectory(simple-rx) endif() endif() diff --git a/examples/ggwave-common-sdl2.cpp b/examples/ggwave-common-sdl2.cpp index 6089dc9..088a2b6 100644 --- a/examples/ggwave-common-sdl2.cpp +++ b/examples/ggwave-common-sdl2.cpp @@ -1,9 +1,10 @@ #include "ggwave-common-sdl2.h" -#include "ggwave/ggwave.h" - #include "ggwave-common.h" +#include "ggwave/ggwave.h" + +#include #include #include @@ -14,8 +15,6 @@ #define EMSCRIPTEN_KEEPALIVE #endif -constexpr double kBaseSampleRate = 48000.0; - namespace { std::string g_defaultCaptureDeviceName = ""; @@ -31,6 +30,7 @@ GGWave *g_ggWave = nullptr; } // JS interface + extern "C" { EMSCRIPTEN_KEEPALIVE int sendData(int textLength, const char * text, int protocolId, int volume) { @@ -117,7 +117,7 @@ bool GGWave_init( SDL_AudioSpec playbackSpec; SDL_zero(playbackSpec); - playbackSpec.freq = ::kBaseSampleRate; + playbackSpec.freq = GGWave::kBaseSampleRate; playbackSpec.format = AUDIO_S16SYS; playbackSpec.channels = 1; playbackSpec.samples = 16*1024; @@ -160,7 +160,7 @@ bool GGWave_init( if (g_devIdIn == 0) { SDL_AudioSpec captureSpec; captureSpec = g_obtainedSpecOut; - captureSpec.freq = ::kBaseSampleRate; + captureSpec.freq = GGWave::kBaseSampleRate; captureSpec.format = AUDIO_F32SYS; captureSpec.samples = 4096; diff --git a/examples/ggwave-common-sdl2.h b/examples/ggwave-common-sdl2.h index 2232d9d..a411bd2 100644 --- a/examples/ggwave-common-sdl2.h +++ b/examples/ggwave-common-sdl2.h @@ -1,7 +1,5 @@ #pragma once -#include - #include class GGWave; diff --git a/examples/ggwave-common.h b/examples/ggwave-common.h index 2b87e39..5258b18 100644 --- a/examples/ggwave-common.h +++ b/examples/ggwave-common.h @@ -4,6 +4,8 @@ #include #include +// some basic helper methods for the examples + template float getTime_ms(const T & tStart, const T & tEnd) { return ((float)(std::chrono::duration_cast(tEnd - tStart).count()))/1000.0; diff --git a/examples/ggwave-gui/main.cpp b/examples/ggwave-gui/main.cpp index e6d7bac..e1264ef 100644 --- a/examples/ggwave-gui/main.cpp +++ b/examples/ggwave-gui/main.cpp @@ -4,6 +4,7 @@ #include +#include #include // ImGui helpers @@ -107,9 +108,6 @@ bool ImGui_SetStyle() { return true; } -static SDL_Window * g_window; -static void * g_gl_context; - int main(int argc, char** argv) { auto argm = parseCmdArguments(argc, argv); int captureId = argm["c"].empty() ? 0 : std::stoi(argm["c"]); @@ -130,28 +128,28 @@ int main(int argc, char** argv) { const char * windowTitle = "ggwave-gui"; SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); - g_window = SDL_CreateWindow(windowTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowX, windowY, window_flags); + SDL_Window * window = SDL_CreateWindow(windowTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowX, windowY, window_flags); - g_gl_context = SDL_GL_CreateContext(g_window); - SDL_GL_MakeCurrent(g_window, g_gl_context); + void * gl_context = SDL_GL_CreateContext(window); + SDL_GL_MakeCurrent(window, gl_context); SDL_GL_SetSwapInterval(1); // Enable vsync - ImGui_Init(g_window, g_gl_context); + ImGui_Init(window, gl_context); ImGui_SetStyle(); - ImGui_NewFrame(g_window); + ImGui_NewFrame(window); ImGui::Render(); auto worker = initMain(); while (true) { - if (ImGui_BeginFrame(g_window) == false) { + if (ImGui_BeginFrame(window) == false) { break; } renderMain(); - ImGui_EndFrame(g_window); + ImGui_EndFrame(window); } deinitMain(worker); @@ -161,8 +159,8 @@ int main(int argc, char** argv) { ImGui_Shutdown(); ImGui::DestroyContext(); - SDL_GL_DeleteContext(g_gl_context); - SDL_DestroyWindow(g_window); + SDL_GL_DeleteContext(gl_context); + SDL_DestroyWindow(window); SDL_Quit(); return 0; diff --git a/examples/ggwave-wasm/main.cpp b/examples/ggwave-wasm/main.cpp index 86eb406..40852f5 100644 --- a/examples/ggwave-wasm/main.cpp +++ b/examples/ggwave-wasm/main.cpp @@ -1,8 +1,7 @@ -#include "ggwave/ggwave.h" +#include "build_timestamp.h" #include "ggwave-common-sdl2.h" -#include "build_timestamp.h" #include "emscripten/emscripten.h" void update() { diff --git a/examples/simple-rx/CMakeLists.txt b/examples/simple-rx/CMakeLists.txt new file mode 100644 index 0000000..48d7a64 --- /dev/null +++ b/examples/simple-rx/CMakeLists.txt @@ -0,0 +1,13 @@ +add_executable(simple-rx main.cpp) + +target_include_directories(simple-rx PRIVATE + .. + ${SDL2_INCLUDE_DIRS} + ) + +target_link_libraries(simple-rx PRIVATE + ggwave + ggwave-common + ggwave-common-sdl2 + ${CMAKE_THREAD_LIBS_INIT} + ) diff --git a/examples/simple-rx/main.cpp b/examples/simple-rx/main.cpp new file mode 100644 index 0000000..c73623b --- /dev/null +++ b/examples/simple-rx/main.cpp @@ -0,0 +1,30 @@ +#include "ggwave/ggwave.h" + +#include "ggwave-common.h" +#include "ggwave-common-sdl2.h" + +#include +#include + +int main(int argc, char** argv) { + printf("Usage: %s [-cN]\n", argv[0]); + printf(" -cN - select capture device N\n"); + printf("\n"); + + auto argm = parseCmdArguments(argc, argv); + int captureId = argm["c"].empty() ? 0 : std::stoi(argm["c"]); + + if (GGWave_init(0, captureId) == false) { + fprintf(stderr, "Failed to initialize GGWave\n"); + return -1; + } + + while (true) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + GGWave_mainLoop(); + } + + GGWave_deinit(); + + return 0; +} diff --git a/include/ggwave/ggwave.h b/include/ggwave/ggwave.h index 119af09..a8a9fdf 100644 --- a/include/ggwave/ggwave.h +++ b/include/ggwave/ggwave.h @@ -6,6 +6,7 @@ class GGWave { public: + static constexpr auto kBaseSampleRate = 48000.0; static constexpr auto kMaxSamplesPerFrame = 1024; static constexpr auto kMaxDataBits = 256; static constexpr auto kMaxDataSize = 256;