Javascript bindings (#14)

* Initial version ready

- bindings are in `bindings/emscripten.cpp`
- minimal Javascript example is in `examples/ggwave-js`

* add npm package + add test-ggwave.js

* js : rename export name to "ggwave_factory"

* update to v0.1.5

* Update README.md

* npm : add npm-publish target
This commit is contained in:
Georgi Gerganov
2021-01-24 17:25:58 +02:00
committed by GitHub
parent 105e0fd48d
commit 19f353018d
19 changed files with 485 additions and 57 deletions

1
bindings/javascript/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
publish.log

View File

@@ -0,0 +1,24 @@
set(TARGET libggwave)
add_executable(${TARGET}
emscripten.cpp
)
target_link_libraries(${TARGET} PRIVATE
ggwave
)
set_target_properties(${TARGET} PROPERTIES LINK_FLAGS " \
--bind \
-s MODULARIZE=1 \
-s SINGLE_FILE=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s EXPORT_NAME=\"'ggwave_factory'\" \
")
add_custom_command(
TARGET libggwave POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_BINARY_DIR}/bin/libggwave.js
${CMAKE_CURRENT_SOURCE_DIR}/ggwave.js
)

View File

@@ -0,0 +1,65 @@
#include "ggwave/ggwave.h"
#include <emscripten.h>
#include <emscripten/bind.h>
EMSCRIPTEN_BINDINGS(ggwave) {
emscripten::enum_<ggwave_SampleFormat>("SampleFormat")
.value("GGWAVE_SAMPLE_FORMAT_UNDEFINED", GGWAVE_SAMPLE_FORMAT_UNDEFINED)
.value("GGWAVE_SAMPLE_FORMAT_U8", GGWAVE_SAMPLE_FORMAT_U8)
.value("GGWAVE_SAMPLE_FORMAT_I8", GGWAVE_SAMPLE_FORMAT_I8)
.value("GGWAVE_SAMPLE_FORMAT_U16", GGWAVE_SAMPLE_FORMAT_U16)
.value("GGWAVE_SAMPLE_FORMAT_I16", GGWAVE_SAMPLE_FORMAT_I16)
.value("GGWAVE_SAMPLE_FORMAT_F32", GGWAVE_SAMPLE_FORMAT_F32)
;
emscripten::enum_<ggwave_TxProtocolId>("TxProtocolId")
.value("GGWAVE_TX_PROTOCOL_AUDIBLE_NORMAL", GGWAVE_TX_PROTOCOL_AUDIBLE_NORMAL)
.value("GGWAVE_TX_PROTOCOL_AUDIBLE_FAST", GGWAVE_TX_PROTOCOL_AUDIBLE_FAST)
.value("GGWAVE_TX_PROTOCOL_AUDIBLE_FASTEST", GGWAVE_TX_PROTOCOL_AUDIBLE_FASTEST)
.value("GGWAVE_TX_PROTOCOL_ULTRASOUND_NORMAL", GGWAVE_TX_PROTOCOL_ULTRASOUND_NORMAL)
.value("GGWAVE_TX_PROTOCOL_ULTRASOUND_FAST", GGWAVE_TX_PROTOCOL_ULTRASOUND_FAST)
.value("GGWAVE_TX_PROTOCOL_ULTRASOUND_FASTEST", GGWAVE_TX_PROTOCOL_ULTRASOUND_FASTEST)
;
emscripten::class_<ggwave_Parameters>("Parameters")
.constructor<>()
.property("sampleRateInp", &ggwave_Parameters::sampleRateInp)
.property("sampleRateOut", &ggwave_Parameters::sampleRateOut)
.property("samplesPerFrame", &ggwave_Parameters::samplesPerFrame)
.property("sampleFormatInp", &ggwave_Parameters::sampleFormatInp)
.property("sampleFormatOut", &ggwave_Parameters::sampleFormatOut)
;
emscripten::function("getDefaultParameters", &ggwave_getDefaultParameters);
emscripten::function("init", &ggwave_init);
emscripten::function("free", &ggwave_free);
emscripten::function("encode", emscripten::optional_override(
[](ggwave_Instance instance,
const std::string & data,
ggwave_TxProtocolId txProtocolId,
int volume) {
auto n = ggwave_encode(instance, data.data(), data.size(), txProtocolId, volume, nullptr, 1);
std::vector<char> result(n);
result.resize(n);
ggwave_encode(instance, data.data(), data.size(), txProtocolId, volume, result.data(), 0);
return emscripten::val(
emscripten::typed_memory_view(result.size(),
result.data()));
}));
emscripten::function("decode", emscripten::optional_override(
[](ggwave_Instance instance,
const std::string & data) {
char output[256];
auto n = ggwave_decode(instance, data.data(), data.size(), output);
if (n > 0) {
return std::string(output, n);
}
return std::string();
}));
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,26 @@
{
"name": "ggwave",
"version": "@PROJECT_VERSION@",
"description": "Tiny data-over-sound library",
"main": "ggwave.js",
"scripts": {
"test": "echo \"todo: add tests\" && exit 0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ggerganov/ggwave.git"
},
"keywords": [
"data-over-sound",
"fsk",
"sound-library",
"ultrasound",
"ecc"
],
"author": "Georgi Gerganov",
"license": "MIT",
"bugs": {
"url": "https://github.com/ggerganov/ggwave/issues"
},
"homepage": "https://github.com/ggerganov/ggwave#readme"
}

View File

@@ -0,0 +1,26 @@
{
"name": "ggwave",
"version": "0.1.5",
"description": "Tiny data-over-sound library",
"main": "ggwave.js",
"scripts": {
"test": "echo \"todo: add tests\" && exit 0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ggerganov/ggwave.git"
},
"keywords": [
"data-over-sound",
"fsk",
"sound-library",
"ultrasound",
"ecc"
],
"author": "Georgi Gerganov",
"license": "MIT",
"bugs": {
"url": "https://github.com/ggerganov/ggwave/issues"
},
"homepage": "https://github.com/ggerganov/ggwave#readme"
}