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