ggwave : big refactoring / renaming

This commit is contained in:
Georgi Gerganov
2022-06-05 11:05:34 +03:00
parent 3f52b0f1b0
commit 0cf3d0e36b
26 changed files with 1097 additions and 834 deletions

View File

@@ -44,41 +44,39 @@ void mainUpdate(void *) {
extern "C" {
EMSCRIPTEN_KEEPALIVE
int sendData(int textLength, const char * text, int protocolId, int volume) {
g_ggWave->init(textLength, text, g_ggWave->getTxProtocol(protocolId), volume);
g_ggWave->init(textLength, text, GGWave::TxProtocolId(protocolId), volume);
return 0;
}
EMSCRIPTEN_KEEPALIVE
int getText(char * text) {
std::copy(g_ggWave->getRxData().begin(), g_ggWave->getRxData().end(), text);
std::copy(g_ggWave->rxData().begin(), g_ggWave->rxData().end(), text);
return 0;
}
EMSCRIPTEN_KEEPALIVE
float getSampleRate() { return g_ggWave->getSampleRateInp(); }
float sampleRate() { return g_ggWave->sampleRateInp(); }
EMSCRIPTEN_KEEPALIVE
int getFramesToRecord() { return g_ggWave->getFramesToRecord(); }
int framesToRecord() { return g_ggWave->rxFramesToRecord(); }
EMSCRIPTEN_KEEPALIVE
int getFramesLeftToRecord() { return g_ggWave->getFramesLeftToRecord(); }
int framesLeftToRecord() { return g_ggWave->rxFramesLeftToRecord(); }
EMSCRIPTEN_KEEPALIVE
int getFramesToAnalyze() { return g_ggWave->getFramesToAnalyze(); }
int framesToAnalyze() { return g_ggWave->rxFramesToAnalyze(); }
EMSCRIPTEN_KEEPALIVE
int getFramesLeftToAnalyze() { return g_ggWave->getFramesLeftToAnalyze(); }
int framesLeftToAnalyze() { return g_ggWave->rxFramesLeftToAnalyze(); }
EMSCRIPTEN_KEEPALIVE
int hasDeviceOutput() { return g_devIdOut; }
int hasDeviceOutput() { return g_devIdOut; }
EMSCRIPTEN_KEEPALIVE
int hasDeviceCapture() { return g_devIdInp; }
int hasDeviceCapture() { return g_devIdInp; }
EMSCRIPTEN_KEEPALIVE
int doInit() {
return g_doInit();
}
int doInit() { return g_doInit(); }
}
void GGWave_setDefaultCaptureDeviceName(std::string name) {
@@ -89,7 +87,8 @@ bool GGWave_init(
const int playbackId,
const int captureId,
const int payloadLength,
const float sampleRateOffset) {
const float sampleRateOffset,
const bool useDSS) {
if (g_devIdInp && g_devIdOut) {
return false;
@@ -225,6 +224,9 @@ bool GGWave_init(
if (reinit) {
if (g_ggWave) delete g_ggWave;
ggwave_OperatingMode mode = GGWAVE_OPERATING_MODE_RX;
if (useDSS) mode = ggwave_OperatingMode(mode | GGWAVE_OPERATING_MODE_USE_DSS);
g_ggWave = new GGWave({
payloadLength,
(float) g_obtainedSpecInp.freq,
@@ -234,7 +236,7 @@ bool GGWave_init(
GGWave::kDefaultSoundMarkerThreshold,
sampleFormatInp,
sampleFormatOut,
GGWAVE_OPERATING_MODE_RX,
mode,
});
}
@@ -248,16 +250,16 @@ bool GGWave_mainLoop() {
return false;
}
if (g_ggWave->hasTxData() == false) {
if (g_ggWave->txHasData() == 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()) {
if ((int) SDL_GetQueuedAudioSize(g_devIdOut) < g_ggWave->samplesPerFrame()*g_ggWave->sampleSizeOut()) {
SDL_PauseAudioDevice(g_devIdInp, SDL_FALSE);
const int nHave = (int) SDL_GetQueuedAudioSize(g_devIdInp);
const int nNeed = g_ggWave->getSamplesPerFrame()*g_ggWave->getSampleSizeBytesInp();
const int nNeed = g_ggWave->samplesPerFrame()*g_ggWave->sampleSizeInp();
if (::getTime_ms(tLastNoData, tNow) > 500.0f && nHave >= nNeed) {
static std::vector<uint8_t> dataInp(nNeed);
SDL_DequeueAudio(g_devIdInp, dataInp.data(), nNeed);
@@ -318,10 +320,12 @@ int main(int argc, char** argv) {
printf("Usage: %s [-cN] [-lN]\n", argv[0]);
printf(" -cN - select capture device N\n");
printf(" -lN - fixed payload length of size N, N in [1, %d]\n", GGWave::kMaxLengthFixed);
printf(" -s - use Direct Sequence Spread (DSS)\n");
printf("\n");
#endif
const GGWave::TxProtocols protocols = {
auto & protocols = GGWave::Protocols::rx();
protocols = {
{ "[R2T2] Normal", 64, 9, 1, 2, true, },
{ "[R2T2] Fast", 64, 6, 1, 2, true, },
{ "[R2T2] Fastest", 64, 3, 1, 2, true, },
@@ -330,20 +334,19 @@ int main(int argc, char** argv) {
{ "[R2T2] Low Fastest", 16, 3, 1, 2, true, },
};
const auto argm = parseCmdArguments(argc, argv);
const int captureId = argm.count("c") == 0 ? 0 : std::stoi(argm.at("c"));
const auto argm = parseCmdArguments(argc, argv);
const int captureId = argm.count("c") == 0 ? 0 : std::stoi(argm.at("c"));
const int payloadLength = argm.count("l") == 0 ? 16 : std::stoi(argm.at("l"));
const bool useDSS = argm.count("s") > 0;
bool isInitialized = false;
g_doInit = [&]() {
if (GGWave_init(0, captureId, payloadLength, 0) == false) {
if (GGWave_init(0, captureId, payloadLength, 0, useDSS) == false) {
fprintf(stderr, "Failed to initialize GGWave\n");
return false;
}
g_ggWave->setRxProtocols(protocols);
isInitialized = true;
printf("Listening for payload with length = %d bytes ..\n", payloadLength);