mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-03-17 07:56:00 +08:00
Initial commit
This commit is contained in:
61
examples/CMakeLists.txt
Normal file
61
examples/CMakeLists.txt
Normal file
@@ -0,0 +1,61 @@
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
add_library(ggwave-common ${GGWAVE_LIBRARY_TYPE}
|
||||
ggwave-common.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(ggwave-common PRIVATE
|
||||
)
|
||||
|
||||
if (GGWAVE_SUPPORT_SDL2)
|
||||
# SDL2
|
||||
|
||||
if (EMSCRIPTEN)
|
||||
set (CMAKE_CXX_FLAGS "-s ALLOW_MEMORY_GROWTH=1 -s USE_SDL=2 -s ASSERTIONS=1 -s DISABLE_EXCEPTION_CATCHING=0 -s 'EXTRA_EXPORTED_RUNTIME_METHODS=[\"writeArrayToMemory\"]'")
|
||||
endif()
|
||||
|
||||
add_library(ggwave-common-sdl2 ${GGWAVE_LIBRARY_TYPE}
|
||||
ggwave-common-sdl2.cpp
|
||||
)
|
||||
|
||||
if (NOT EMSCRIPTEN)
|
||||
find_package(SDL2)
|
||||
|
||||
if (NOT USE_FINDSDL2 AND NOT SDL2_FOUND)
|
||||
message(WARNING "Unable to find SDL2 library. It is either not installed or CMake cannot find it."
|
||||
" In the latter case, setting the USE_FINDSDL2 variable might help:\n"
|
||||
" $ cmake -D USE_FINDSDL2 .."
|
||||
)
|
||||
|
||||
message(FATAL_ERROR "Aborting")
|
||||
endif()
|
||||
|
||||
string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
|
||||
endif()
|
||||
|
||||
message(STATUS "SDL2_INCLUDE_DIRS = ${SDL2_INCLUDE_DIRS}")
|
||||
message(STATUS "SDL2_LIBRARIES = ${SDL2_LIBRARIES}")
|
||||
|
||||
# ggwave-common-sdl2
|
||||
|
||||
target_include_directories(ggwave-common-sdl2 PUBLIC
|
||||
${SDL2_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_link_libraries(ggwave-common-sdl2 PRIVATE
|
||||
ggwave
|
||||
${SDL2_LIBRARIES}
|
||||
)
|
||||
endif()
|
||||
|
||||
if (GGWAVE_SUPPORT_SDL2)
|
||||
if (EMSCRIPTEN)
|
||||
# emscripten sdl2 examples
|
||||
|
||||
add_subdirectory(ggwave-wasm)
|
||||
else()
|
||||
# non-emscripten sdl2 examples
|
||||
|
||||
add_subdirectory(ggwave-cli)
|
||||
endif()
|
||||
endif()
|
||||
13
examples/ggwave-cli/CMakeLists.txt
Normal file
13
examples/ggwave-cli/CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
add_executable(ggwave-cli main.cpp)
|
||||
|
||||
target_include_directories(ggwave-cli PRIVATE
|
||||
..
|
||||
${SDL2_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_link_libraries(ggwave-cli PRIVATE
|
||||
ggwave
|
||||
ggwave-common
|
||||
ggwave-common-sdl2
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
)
|
||||
197
examples/ggwave-cli/main.cpp
Normal file
197
examples/ggwave-cli/main.cpp
Normal file
@@ -0,0 +1,197 @@
|
||||
/*! \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");
|
||||
printf(" -pN - select playback device N\n");
|
||||
printf(" -tN - transmission protocol:\n");
|
||||
printf(" -t0 : Normal\n");
|
||||
printf(" -t1 : Fast (default)\n");
|
||||
printf(" -t2 : Fastest\n");
|
||||
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 txProtocol = argm["t"].empty() ? 1 : std::stoi(argm["t"]);
|
||||
|
||||
initSDL2ForGGWave(
|
||||
g_isInitialized,
|
||||
g_playbackId,
|
||||
g_devIdIn,
|
||||
g_captureId,
|
||||
g_devIdOut,
|
||||
g_ggWave);
|
||||
|
||||
g_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);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
{
|
||||
printf("Using 'Fast' Tx Protocol\n");
|
||||
g_ggWave->setParameters(1, 40, 6, 3, 50);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
printf("Using 'Fastest' Tx Protocol\n");
|
||||
g_ggWave->setParameters(1, 40, 3, 3, 50);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
printf("Using 'Ultrasonic' Tx Protocol\n");
|
||||
g_ggWave->setParameters(1, 320, 9, 3, 50);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
printf("Using 'Fast' Tx Protocol\n");
|
||||
g_ggWave->setParameters(1, 40, 6, 3, 50);
|
||||
}
|
||||
};
|
||||
printf("\n");
|
||||
|
||||
g_ggWave->init(0, "");
|
||||
|
||||
std::mutex mutex;
|
||||
std::thread inputThread([&mutex]() {
|
||||
std::string inputOld = "";
|
||||
while (true) {
|
||||
std::string input;
|
||||
std::cout << "Enter text: ";
|
||||
getline(std::cin, input);
|
||||
if (input.empty()) {
|
||||
std::cout << "Re-sending ... " << std::endl;
|
||||
input = inputOld;
|
||||
} else {
|
||||
std::cout << "Sending ... " << std::endl;
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
g_ggWave->init(input.size(), input.data());
|
||||
}
|
||||
inputOld = input;
|
||||
}
|
||||
});
|
||||
|
||||
while (true) {
|
||||
SDL_Delay(1);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
return 0;
|
||||
}
|
||||
152
examples/ggwave-common-sdl2.cpp
Normal file
152
examples/ggwave-common-sdl2.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
#include "ggwave-common-sdl2.h"
|
||||
|
||||
#include "ggwave/ggwave.h"
|
||||
|
||||
constexpr double kBaseSampleRate = 48000.0;
|
||||
|
||||
bool initSDL2ForGGWave(
|
||||
bool & isInitialized,
|
||||
const int playbackId,
|
||||
SDL_AudioDeviceID & devIdIn,
|
||||
const int captureId,
|
||||
SDL_AudioDeviceID & devIdOut,
|
||||
GGWave *& ggWave,
|
||||
const char * defaultCaptureDeviceName) {
|
||||
if (isInitialized) return 0;
|
||||
|
||||
printf("Initializing ...\n");
|
||||
|
||||
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
||||
|
||||
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
SDL_SetHintWithPriority(SDL_HINT_AUDIO_RESAMPLING_MODE, "medium", SDL_HINT_OVERRIDE);
|
||||
|
||||
{
|
||||
int nDevices = SDL_GetNumAudioDevices(SDL_FALSE);
|
||||
printf("Found %d playback devices:\n", nDevices);
|
||||
for (int i = 0; i < nDevices; i++) {
|
||||
printf(" - Playback device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_FALSE));
|
||||
}
|
||||
}
|
||||
{
|
||||
int nDevices = SDL_GetNumAudioDevices(SDL_TRUE);
|
||||
printf("Found %d capture devices:\n", nDevices);
|
||||
for (int i = 0; i < nDevices; i++) {
|
||||
printf(" - Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
|
||||
}
|
||||
}
|
||||
|
||||
SDL_AudioSpec playbackSpec;
|
||||
SDL_zero(playbackSpec);
|
||||
|
||||
playbackSpec.freq = ::kBaseSampleRate;
|
||||
playbackSpec.format = AUDIO_S16SYS;
|
||||
playbackSpec.channels = 1;
|
||||
playbackSpec.samples = 16*1024;
|
||||
playbackSpec.callback = NULL;
|
||||
|
||||
SDL_AudioSpec obtainedSpecIn;
|
||||
SDL_AudioSpec obtainedSpecOut;
|
||||
|
||||
int sampleSizeBytesIn = 4;
|
||||
int sampleSizeBytesOut = 2;
|
||||
|
||||
SDL_zero(obtainedSpecIn);
|
||||
SDL_zero(obtainedSpecOut);
|
||||
|
||||
if (playbackId >= 0) {
|
||||
printf("Attempt to open playback device %d : '%s' ...\n", playbackId, SDL_GetAudioDeviceName(playbackId, SDL_FALSE));
|
||||
devIdOut = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(playbackId, SDL_FALSE), SDL_FALSE, &playbackSpec, &obtainedSpecOut, 0);
|
||||
} else {
|
||||
printf("Attempt to open default playback device ...\n");
|
||||
devIdOut = SDL_OpenAudioDevice(NULL, SDL_FALSE, &playbackSpec, &obtainedSpecOut, 0);
|
||||
}
|
||||
|
||||
if (!devIdOut) {
|
||||
printf("Couldn't open an audio device for playback: %s!\n", SDL_GetError());
|
||||
devIdOut = 0;
|
||||
} else {
|
||||
printf("Obtained spec for output device (SDL Id = %d):\n", devIdOut);
|
||||
printf(" - Sample rate: %d (required: %d)\n", obtainedSpecOut.freq, playbackSpec.freq);
|
||||
printf(" - Format: %d (required: %d)\n", obtainedSpecOut.format, playbackSpec.format);
|
||||
printf(" - Channels: %d (required: %d)\n", obtainedSpecOut.channels, playbackSpec.channels);
|
||||
printf(" - Samples per frame: %d (required: %d)\n", obtainedSpecOut.samples, playbackSpec.samples);
|
||||
|
||||
if (obtainedSpecOut.format != playbackSpec.format ||
|
||||
obtainedSpecOut.channels != playbackSpec.channels ||
|
||||
obtainedSpecOut.samples != playbackSpec.samples) {
|
||||
SDL_CloseAudio();
|
||||
fprintf(stderr, "Failed to initialize playback SDL_OpenAudio!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
switch (obtainedSpecOut.format) {
|
||||
case AUDIO_U8:
|
||||
case AUDIO_S8:
|
||||
sampleSizeBytesOut = 1;
|
||||
break;
|
||||
case AUDIO_U16SYS:
|
||||
case AUDIO_S16SYS:
|
||||
sampleSizeBytesOut = 2;
|
||||
break;
|
||||
case AUDIO_S32SYS:
|
||||
case AUDIO_F32SYS:
|
||||
sampleSizeBytesOut = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_AudioSpec captureSpec;
|
||||
captureSpec = obtainedSpecOut;
|
||||
captureSpec.freq = ::kBaseSampleRate;
|
||||
captureSpec.format = AUDIO_F32SYS;
|
||||
captureSpec.samples = 4096;
|
||||
|
||||
if (captureId >= 0) {
|
||||
printf("Attempt to open capture device %d : '%s' ...\n", captureId, SDL_GetAudioDeviceName(captureId, SDL_FALSE));
|
||||
devIdIn = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(captureId, SDL_TRUE), SDL_TRUE, &captureSpec, &obtainedSpecIn, 0);
|
||||
} else {
|
||||
printf("Attempt to open default capture device ...\n");
|
||||
devIdIn = SDL_OpenAudioDevice(defaultCaptureDeviceName, SDL_TRUE, &captureSpec, &obtainedSpecIn, 0);
|
||||
}
|
||||
if (!devIdIn) {
|
||||
printf("Couldn't open an audio device for capture: %s!\n", SDL_GetError());
|
||||
devIdIn = 0;
|
||||
} else {
|
||||
printf("Obtained spec for input device (SDL Id = %d):\n", devIdIn);
|
||||
printf(" - Sample rate: %d\n", obtainedSpecIn.freq);
|
||||
printf(" - Format: %d (required: %d)\n", obtainedSpecIn.format, captureSpec.format);
|
||||
printf(" - Channels: %d (required: %d)\n", obtainedSpecIn.channels, captureSpec.channels);
|
||||
printf(" - Samples per frame: %d\n", obtainedSpecIn.samples);
|
||||
}
|
||||
|
||||
switch (obtainedSpecIn.format) {
|
||||
case AUDIO_U8:
|
||||
case AUDIO_S8:
|
||||
sampleSizeBytesIn = 1;
|
||||
break;
|
||||
case AUDIO_U16SYS:
|
||||
case AUDIO_S16SYS:
|
||||
sampleSizeBytesIn = 2;
|
||||
break;
|
||||
case AUDIO_S32SYS:
|
||||
case AUDIO_F32SYS:
|
||||
sampleSizeBytesIn = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
ggWave = new GGWave(
|
||||
obtainedSpecIn.freq,
|
||||
obtainedSpecOut.freq,
|
||||
1024,
|
||||
sampleSizeBytesIn,
|
||||
sampleSizeBytesOut);
|
||||
|
||||
isInitialized = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
14
examples/ggwave-common-sdl2.h
Normal file
14
examples/ggwave-common-sdl2.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
class GGWave;
|
||||
|
||||
bool initSDL2ForGGWave(
|
||||
bool & isInitialized,
|
||||
const int playbackId,
|
||||
SDL_AudioDeviceID & devIdIn,
|
||||
const int captureId,
|
||||
SDL_AudioDeviceID & devIdOut,
|
||||
GGWave *& ggWave,
|
||||
const char * defaultCaptureDeviceName = nullptr);
|
||||
17
examples/ggwave-common.cpp
Normal file
17
examples/ggwave-common.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "ggwave-common.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
std::map<std::string, std::string> parseCmdArguments(int argc, char ** argv) {
|
||||
int last = argc;
|
||||
std::map<std::string, std::string> res;
|
||||
for (int i = 1; i < last; ++i) {
|
||||
if (argv[i][0] == '-') {
|
||||
if (strlen(argv[i]) > 1) {
|
||||
res[std::string(1, argv[i][1])] = strlen(argv[i]) > 2 ? argv[i] + 2 : "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
12
examples/ggwave-common.h
Normal file
12
examples/ggwave-common.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> parseCmdArguments(int argc, char ** argv);
|
||||
18
examples/ggwave-wasm/CMakeLists.txt
Normal file
18
examples/ggwave-wasm/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
set(TARGET ggwave-wasm)
|
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/build_timestamp.h.tmpl ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/build_timestamp.h @ONLY)
|
||||
|
||||
add_executable(${TARGET}
|
||||
main.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${TARGET} PRIVATE
|
||||
..
|
||||
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/
|
||||
)
|
||||
|
||||
target_link_libraries(${TARGET} PRIVATE
|
||||
ggwave
|
||||
ggwave-common
|
||||
ggwave-common-sdl2
|
||||
)
|
||||
1
examples/ggwave-wasm/build_timestamp.h.tmpl
Normal file
1
examples/ggwave-wasm/build_timestamp.h.tmpl
Normal file
@@ -0,0 +1 @@
|
||||
static const char * BUILD_TIMESTAMP="@GIT_DATE@ (@GIT_SHA1@)";
|
||||
181
examples/ggwave-wasm/main.cpp
Normal file
181
examples/ggwave-wasm/main.cpp
Normal file
@@ -0,0 +1,181 @@
|
||||
/*! \file main.cpp
|
||||
* \brief Send/Receive - WebAssembly port
|
||||
* \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 "build_timestamp.h"
|
||||
#include "emscripten/emscripten.h"
|
||||
|
||||
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;
|
||||
|
||||
// JS interface
|
||||
extern "C" {
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int setText(int textLength, const char * text) {
|
||||
g_ggWave->init(textLength, text);
|
||||
return 0;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int getText(char * text) {
|
||||
std::copy(g_ggWave->getRxData().begin(), g_ggWave->getRxData().end(), text);
|
||||
return 0;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int getSampleRate() { return g_ggWave->getSampleRateIn(); }
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
float getAverageRxTime_ms() { return g_ggWave->getAverageRxTime_ms(); }
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int getFramesToRecord() { return g_ggWave->getFramesToRecord(); }
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int getFramesLeftToRecord() { return g_ggWave->getFramesLeftToRecord(); }
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int getFramesToAnalyze() { return g_ggWave->getFramesToAnalyze(); }
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int getFramesLeftToAnalyze() { return g_ggWave->getFramesLeftToAnalyze(); }
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int hasDeviceOutput() { return g_devIdOut; }
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int hasDeviceCapture() { return (g_ggWave->getTotalBytesCaptured() > 0) ? g_devIdIn : 0; }
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int doInit() {
|
||||
return initSDL2ForGGWave(
|
||||
g_isInitialized,
|
||||
g_playbackId,
|
||||
g_devIdIn,
|
||||
g_captureId,
|
||||
g_devIdOut,
|
||||
g_ggWave,
|
||||
g_defaultCaptureDeviceName);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int setTxMode(int txMode) {
|
||||
g_ggWave->setTxMode((GGWave::TxMode)(txMode));
|
||||
g_ggWave->init(0, "");
|
||||
return 0;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
void setParameters(
|
||||
int paramFreqDelta,
|
||||
int paramFreqStart,
|
||||
int paramFramesPerTx,
|
||||
int paramBytesPerTx,
|
||||
int /*paramECCBytesPerTx*/,
|
||||
int paramVolume) {
|
||||
if (g_ggWave == nullptr) return;
|
||||
|
||||
g_ggWave->setParameters(
|
||||
paramFreqDelta,
|
||||
paramFreqStart,
|
||||
paramFramesPerTx,
|
||||
paramBytesPerTx,
|
||||
paramVolume);
|
||||
|
||||
g_ggWave->init(0, "");
|
||||
}
|
||||
}
|
||||
|
||||
// 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();
|
||||
#ifdef __EMSCRIPTEN__
|
||||
emscripten_cancel_main_loop();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
int main(int , char** argv) {
|
||||
printf("Build time: %s\n", BUILD_TIMESTAMP);
|
||||
printf("Press the Init button to start\n");
|
||||
|
||||
g_defaultCaptureDeviceName = argv[1];
|
||||
|
||||
emscripten_set_main_loop(update, 60, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user