c@73: "use strict"; c@73: c@73: var exampleModule = ExampleModule(); c@73: c@73: // It is possible to declare both parameters and return values as c@73: // "string", in which case Emscripten will take care of c@73: // conversions. But it's not clear how one would manage memory for c@73: // newly-constructed returned C strings -- the returned pointer from c@73: // vampipeRequestJson would appear (?) to be thrown away by the c@73: // Emscripten string converter if we declare it as returning a string, c@73: // so we have no opportunity to pass it to vampipeFreeJson, which c@73: // suggests this would leak memory if the string isn't static. Not c@73: // wholly sure though. Anyway, passing and returning pointers (as c@73: // numbers) means we can manage the Emscripten heap memory however we c@73: // want in our request wrapper function below. c@73: c@73: var vampipeRequestJson = exampleModule.cwrap( c@73: 'vampipeRequestJson', 'number', ['number'] c@73: ); c@73: c@73: var vampipeFreeJson = exampleModule.cwrap( c@73: 'vampipeFreeJson', 'void', ['number'] c@73: ); c@73: c@73: function request(jsonStr) { c@73: var m = exampleModule; c@73: // Inspection reveals that intArrayFromString converts the string c@73: // from utf16 to utf8, which is what we want (though the docs c@73: // don't mention this). Note the *Cstr values are Emscripten heap c@73: // pointers c@73: var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL); c@73: var outCstr = vampipeRequestJson(inCstr); c@73: m._free(inCstr); c@73: var result = m.Pointer_stringify(outCstr); c@73: vampipeFreeJson(outCstr); c@73: return result; c@73: } c@73: c@73: function note(blah) { c@73: document.getElementById("test-result").innerHTML += blah + "
"; c@73: } c@73: c@73: function test() { c@73: c@73: note("Querying plugin list..."); c@73: var result = request('{"type": "list"}'); c@73: note("Result JSON = " + result); c@73: c@73: note("
Loading zero crossings plugin..."); c@73: result = request('{"type":"load","content": {"pluginKey":"vamp-example-plugins:zerocrossing","inputSampleRate":16,"adapterFlags":["AdaptAllSafe"]}}'); c@73: note("Result JSON = " + result); c@73: c@73: note("
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..."); c@73: result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}'); c@73: note("Result JSON = " + result); c@73: c@73: note("
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."); c@73: result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}'); c@73: note("Result JSON = " + result); c@73: c@73: note("
Now processing a couple of blocks of data, on the same assumptions..."); c@73: result = request('{"type":"process","content":{"pluginHandle":1,"processInput":{"timestamp":{"s":0,"n":0},"inputBuffers":[{"values":[0,1,-1,0,1,-1,0,1]}]}}}'); c@73: note("Result JSON = " + result); c@73: result = request('{"type":"process","content":{"pluginHandle":1,"processInput":{"timestamp":{"s":0,"n":500000000},"inputBuffers":[{"values":[0,1,-1,0,1,-1,0,1]}]}}}'); c@73: note("Result JSON = " + result); c@73: c@73: note("
Cleaning up the plugin and getting any remaining features..."); c@73: result = request('{"type":"finish","content":{"pluginHandle":1}}'); c@73: note("Result JSON = " + result); c@73: c@73: note("
A process call should now fail, as the plugin has been cleaned up."); c@73: result = request('{"type":"process","content":{"pluginHandle":1,"processInput":{"timestamp":{"s":0,"n":1000000000},"inputBuffers":[{"values":[0,1,-1,0,1,-1,0,1]}]}}}'); c@73: note("Result JSON = " + result); c@73: } c@73: c@73: window.onload = function() { c@73: test(); c@73: }