mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-02-06 16:47:59 +08:00
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:
1
bindings/javascript/.gitignore
vendored
Normal file
1
bindings/javascript/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
publish.log
|
||||
24
bindings/javascript/CMakeLists.txt
Normal file
24
bindings/javascript/CMakeLists.txt
Normal 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
|
||||
)
|
||||
65
bindings/javascript/emscripten.cpp
Normal file
65
bindings/javascript/emscripten.cpp
Normal 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();
|
||||
}));
|
||||
}
|
||||
21
bindings/javascript/ggwave.js
Normal file
21
bindings/javascript/ggwave.js
Normal file
File diff suppressed because one or more lines are too long
26
bindings/javascript/package-tmpl.json
Normal file
26
bindings/javascript/package-tmpl.json
Normal 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"
|
||||
}
|
||||
26
bindings/javascript/package.json
Normal file
26
bindings/javascript/package.json
Normal 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"
|
||||
}
|
||||
Reference in New Issue
Block a user