annotate test/quick-test.js @ 154:6f01107166d5

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