examples : add "simple-rx" example + minor clean-up

This commit is contained in:
Georgi Gerganov
2020-12-19 11:26:36 +02:00
parent 9edc4d8239
commit 174b6872e8
9 changed files with 64 additions and 22 deletions

View File

@@ -73,5 +73,6 @@ if (GGWAVE_SUPPORT_SDL2)
add_subdirectory(ggwave-cli)
add_subdirectory(ggwave-gui)
add_subdirectory(simple-rx)
endif()
endif()

View File

@@ -1,9 +1,10 @@
#include "ggwave-common-sdl2.h"
#include "ggwave/ggwave.h"
#include "ggwave-common.h"
#include "ggwave/ggwave.h"
#include <SDL.h>
#include <SDL_opengl.h>
#include <chrono>
@@ -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;

View File

@@ -1,7 +1,5 @@
#pragma once
#include <SDL.h>
#include <string>
class GGWave;

View File

@@ -4,6 +4,8 @@
#include <string>
#include <map>
// some basic helper methods for the examples
template <class T>
float getTime_ms(const T & tStart, const T & tEnd) {
return ((float)(std::chrono::duration_cast<std::chrono::microseconds>(tEnd - tStart).count()))/1000.0;

View File

@@ -4,6 +4,7 @@
#include <imgui-extra/imgui_impl.h>
#include <SDL.h>
#include <SDL_opengl.h>
// 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;

View File

@@ -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() {

View File

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

View File

@@ -0,0 +1,30 @@
#include "ggwave/ggwave.h"
#include "ggwave-common.h"
#include "ggwave-common-sdl2.h"
#include <cstdio>
#include <thread>
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;
}

View File

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