annotate test/quick-test.js @ 176:eaf46e7647a0 tip master

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