Skip to content

Instantly share code, notes, and snippets.

@msabwat
Created June 3, 2020 18:32
Show Gist options
  • Save msabwat/47b32000d4686c25b060050db30954a2 to your computer and use it in GitHub Desktop.
Save msabwat/47b32000d4686c25b060050db30954a2 to your computer and use it in GitHub Desktop.
// command : em++ --bind main.cpp -o test.html -g
#include <emscripten.h>
#include <emscripten/emscripten.h>
#include <emscripten/val.h>
#include <emscripten/bind.h>
#include <strings.h>
using namespace emscripten;
void awn_cb (int channels) {
val console = val::global("console");
console.call<void>("log", channels);
}
class AudioWorkletWrapper {
public:
AudioWorkletWrapper(int sample_rate, int channels, val sab_view) {
_channels = channels;
_sample_rate = sample_rate;
_sab_view = sab_view;
val audio_ctx_options = val::object();
audio_ctx_options.set("sampleRate", _sample_rate);
_context = val::global("AudioContext").new_(audio_ctx_options);
_context.call<void>("suspend");
};
void init_worklet_node() {
val aw = _context["audioWorklet"];
// bind awn_cb
val awn_cb_args = val::module_property("awn_cb").call<val>("bind", val::null(), 42);
// val console = val::global("console");
// console.call<void>("log", awn_cb_args);
// awn_cb_args();
aw.call<val>("addModule", std::string("worklet-processor.js")).call<val>("then", awn_cb_args);
}
val getContext() const { return _context; }
void setContext(val _) { _context = _; }
private:
val _context = val::undefined();
int _channels = 0;
int _sample_rate = 0;
val _sab_view = val::null();
};
EMSCRIPTEN_BINDINGS(AWWSCOPE) {
function("awn_cb", &awn_cb);
class_<AudioWorkletWrapper>("AudioWorkletWrapper")
.constructor<int, int, val>()
.function("initWorkletNode", &AudioWorkletWrapper::init_worklet_node)
.property("context", &AudioWorkletWrapper::getContext, &AudioWorkletWrapper::setContext);
};
int main()
{
val awInst = val::module_property("AudioWorkletWrapper").new_(44100, 2, val::array());
awInst.call<val>("initWorkletNode");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment