c@73: "use strict"; c@73: c@97: var exampleModule = VampExamplePluginsModule(); 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@75: function note(blah) { c@75: document.getElementById("test-result").innerHTML += blah + "
"; c@75: } c@75: c@75: function comment(blah) { c@75: note("
" + blah + ""); c@75: } c@75: c@73: function request(jsonStr) { c@75: note("Request JSON = " + 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@75: note("Returned JSON = " + result); c@73: return result; c@73: } c@73: c@73: function test() { c@73: c@75: comment("Querying plugin list..."); c@109: var result = request('{"method": "list"}'); c@73: c@75: comment("Loading zero crossings plugin..."); c@109: result = request('{"method":"load","params": {"key":"vamp-example-plugins:powerspectrum","inputSampleRate":16,"adapterFlags":["AdaptAllSafe"]}}'); c@73: c@109: comment("I'm now assuming that the load succeeded and the returned handle 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@109: result = request('{"method":"configure","params":{"handle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}'); c@73: c@75: 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."); c@109: result = request('{"method":"configure","params":{"handle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}'); c@73: c@75: comment("Now processing a couple of blocks of data, on the same assumptions..."); c@109: result = request('{"method":"process","params":{"handle":1,"processInput":{"timestamp":{"s":0,"n":0},"inputBuffers":[[0,1,-1,0,1,-1,0,1]]}}}'); c@109: result = request('{"method":"process","params":{"handle":1,"processInput":{"timestamp":{"s":0,"n":500000000},"inputBuffers":[[0,1,-1,0,1,-1,0,1]]}}}'); c@73: c@75: comment("Cleaning up the plugin and getting any remaining features..."); c@109: result = request('{"method":"finish","params":{"handle":1}}'); c@73: c@75: comment("A process call should now fail, as the plugin has been cleaned up."); c@109: result = request('{"method":"process","params":{"handle":1,"processInput":{"timestamp":{"s":0,"n":1000000000},"inputBuffers":[{"values":[0,1,-1,0,1,-1,0,1]}]}}}'); c@73: } c@73: c@73: window.onload = function() { c@73: test(); c@73: }