Chris@4: "use strict";
Chris@4:
Chris@4: var exampleModule = ExampleModule();
Chris@4:
Chris@4: // It is possible to declare both parameters and return values as
Chris@4: // "string", in which case Emscripten will take care of
Chris@4: // conversions. But it's not clear how one would manage memory for
Chris@4: // newly-constructed returned C strings -- the returned pointer from
Chris@4: // vampipeRequestJson would appear (?) to be thrown away by the
Chris@4: // Emscripten string converter if we declare it as returning a string,
Chris@4: // so we have no opportunity to pass it to vampipeFreeJson, which
Chris@4: // suggests this would leak memory if the string isn't static. Not
Chris@4: // wholly sure though. Anyway, passing and returning pointers (as
Chris@4: // numbers) means we can manage the Emscripten heap memory however we
Chris@4: // want in our request wrapper function below.
Chris@4:
Chris@4: var vampipeRequestJson = exampleModule.cwrap(
Chris@4: 'vampipeRequestJson', 'number', ['number']
Chris@4: );
Chris@4:
Chris@4: var vampipeFreeJson = exampleModule.cwrap(
Chris@4: 'vampipeFreeJson', 'void', ['number']
Chris@4: );
Chris@4:
Chris@6: function note(blah) {
Chris@6: document.getElementById("test-result").innerHTML += blah + "
";
Chris@6: }
Chris@6:
Chris@6: function comment(blah) {
Chris@6: note("
" + blah + "");
Chris@6: }
Chris@6:
Chris@4: function request(jsonStr) {
Chris@6: note("Request JSON = " + jsonStr);
Chris@4: var m = exampleModule;
Chris@4: // Inspection reveals that intArrayFromString converts the string
Chris@4: // from utf16 to utf8, which is what we want (though the docs
Chris@4: // don't mention this). Note the *Cstr values are Emscripten heap
Chris@4: // pointers
Chris@4: var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL);
Chris@4: var outCstr = vampipeRequestJson(inCstr);
Chris@4: m._free(inCstr);
Chris@4: var result = m.Pointer_stringify(outCstr);
Chris@4: vampipeFreeJson(outCstr);
Chris@6: note("Returned JSON = " + result);
Chris@4: return result;
Chris@4: }
Chris@4:
Chris@4: function test() {
Chris@4:
Chris@6: comment("Querying plugin list...");
Chris@4: var result = request('{"type": "list"}');
Chris@4:
Chris@6: comment("Loading zero crossings plugin...");
Chris@8: result = request('{"type":"load","content": {"pluginKey":"vamp-example-plugins:powerspectrum","inputSampleRate":16,"adapterFlags":["AdaptAllSafe"]}}');
Chris@4:
Chris@6: comment("I'm now assuming that the load succeeded and the returned pluginHandle was 1. I haven't bothered to parse the JSON. If those assumptions are wrong, this obviously isn't going to work. Configuring the plugin...");
Chris@4: result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}');
Chris@4:
Chris@6: comment("If I try to configure it again, it should fail because it's already configured... but this doesn't change anything, and subsequent processing should work fine. Just an example of a failure call. NB this only works if Emscripten has exception catching enabled -- it's off by default in opt builds, which would just end the script here. Wonder what the performance penalty is like.");
Chris@4: result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}');
Chris@4:
Chris@6: comment("Now processing a couple of blocks of data, on the same assumptions...");
Chris@4: result = request('{"type":"process","content":{"pluginHandle":1,"processInput":{"timestamp":{"s":0,"n":0},"inputBuffers":[{"values":[0,1,-1,0,1,-1,0,1]}]}}}');
Chris@4: result = request('{"type":"process","content":{"pluginHandle":1,"processInput":{"timestamp":{"s":0,"n":500000000},"inputBuffers":[{"values":[0,1,-1,0,1,-1,0,1]}]}}}');
Chris@4:
Chris@6: comment("Cleaning up the plugin and getting any remaining features...");
Chris@4: result = request('{"type":"finish","content":{"pluginHandle":1}}');
Chris@4:
Chris@6: comment("A process call should now fail, as the plugin has been cleaned up.");
Chris@4: result = request('{"type":"process","content":{"pluginHandle":1,"processInput":{"timestamp":{"s":0,"n":1000000000},"inputBuffers":[{"values":[0,1,-1,0,1,-1,0,1]}]}}}');
Chris@4: }
Chris@4:
Chris@4: window.onload = function() {
Chris@4: test();
Chris@4: }