annotate quick-test.js @ 6:9bba59806014

Include the requested JSON in the html as well as the returned stuff
author Chris Cannam
date Wed, 24 Aug 2016 18:00:28 +0100
parents 3a5a6535d50d
children 2a4685f230ac
rev   line source
Chris@4 1 "use strict";
Chris@4 2
Chris@4 3 var exampleModule = ExampleModule();
Chris@4 4
Chris@4 5 // It is possible to declare both parameters and return values as
Chris@4 6 // "string", in which case Emscripten will take care of
Chris@4 7 // conversions. But it's not clear how one would manage memory for
Chris@4 8 // newly-constructed returned C strings -- the returned pointer from
Chris@4 9 // vampipeRequestJson would appear (?) to be thrown away by the
Chris@4 10 // Emscripten string converter if we declare it as returning a string,
Chris@4 11 // so we have no opportunity to pass it to vampipeFreeJson, which
Chris@4 12 // suggests this would leak memory if the string isn't static. Not
Chris@4 13 // wholly sure though. Anyway, passing and returning pointers (as
Chris@4 14 // numbers) means we can manage the Emscripten heap memory however we
Chris@4 15 // want in our request wrapper function below.
Chris@4 16
Chris@4 17 var vampipeRequestJson = exampleModule.cwrap(
Chris@4 18 'vampipeRequestJson', 'number', ['number']
Chris@4 19 );
Chris@4 20
Chris@4 21 var vampipeFreeJson = exampleModule.cwrap(
Chris@4 22 'vampipeFreeJson', 'void', ['number']
Chris@4 23 );
Chris@4 24
Chris@6 25 function note(blah) {
Chris@6 26 document.getElementById("test-result").innerHTML += blah + "<br>";
Chris@6 27 }
Chris@6 28
Chris@6 29 function comment(blah) {
Chris@6 30 note("<br><i>" + blah + "</i>");
Chris@6 31 }
Chris@6 32
Chris@4 33 function request(jsonStr) {
Chris@6 34 note("Request JSON = " + jsonStr);
Chris@4 35 var m = exampleModule;
Chris@4 36 // Inspection reveals that intArrayFromString converts the string
Chris@4 37 // from utf16 to utf8, which is what we want (though the docs
Chris@4 38 // don't mention this). Note the *Cstr values are Emscripten heap
Chris@4 39 // pointers
Chris@4 40 var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL);
Chris@4 41 var outCstr = vampipeRequestJson(inCstr);
Chris@4 42 m._free(inCstr);
Chris@4 43 var result = m.Pointer_stringify(outCstr);
Chris@4 44 vampipeFreeJson(outCstr);
Chris@6 45 note("Returned JSON = " + result);
Chris@4 46 return result;
Chris@4 47 }
Chris@4 48
Chris@4 49 function test() {
Chris@4 50
Chris@6 51 comment("Querying plugin list...");
Chris@4 52 var result = request('{"type": "list"}');
Chris@4 53
Chris@6 54 comment("Loading zero crossings plugin...");
Chris@4 55 result = request('{"type":"load","content": {"pluginKey":"vamp-example-plugins:zerocrossing","inputSampleRate":16,"adapterFlags":["AdaptAllSafe"]}}');
Chris@4 56
Chris@6 57 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 58 result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}');
Chris@4 59
Chris@6 60 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 61 result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}');
Chris@4 62
Chris@6 63 comment("Now processing a couple of blocks of data, on the same assumptions...");
Chris@4 64 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 65 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 66
Chris@6 67 comment("Cleaning up the plugin and getting any remaining features...");
Chris@4 68 result = request('{"type":"finish","content":{"pluginHandle":1}}');
Chris@4 69
Chris@6 70 comment("A process call should now fail, as the plugin has been cleaned up.");
Chris@4 71 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 72 }
Chris@4 73
Chris@4 74 window.onload = function() {
Chris@4 75 test();
Chris@4 76 }