annotate quick-test.js @ 43:90bf9d9f9c95

Rearrange and rename VamPipe -> Piper as appropriate
author Chris Cannam
date Mon, 10 Oct 2016 17:05:37 +0100
parents a734a7e976fa
children
rev   line source
Chris@4 1 "use strict";
Chris@4 2
Chris@28 3 var exampleModule = VampExamplePluginsModule();
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@43 9 // piperRequestJson would appear (?) to be thrown away by the
Chris@4 10 // Emscripten string converter if we declare it as returning a string,
Chris@43 11 // so we have no opportunity to pass it to piperFreeJson, 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@43 17 var piperRequestJson = exampleModule.cwrap(
Chris@43 18 'piperRequestJson', 'number', ['number']
Chris@4 19 );
Chris@4 20
Chris@43 21 var piperFreeJson = exampleModule.cwrap(
Chris@43 22 'piperFreeJson', '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@43 41 var outCstr = piperRequestJson(inCstr);
Chris@4 42 m._free(inCstr);
Chris@4 43 var result = m.Pointer_stringify(outCstr);
Chris@43 44 piperFreeJson(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@40 52 var result = request('{"method": "list"}');
Chris@4 53
Chris@6 54 comment("Loading zero crossings plugin...");
Chris@40 55 result = request('{"method":"load","params": {"key":"vamp-example-plugins:powerspectrum","inputSampleRate":16,"adapterFlags":["AdaptAllSafe"]}}');
Chris@4 56
Chris@40 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...");
Chris@40 58 result = request('{"method":"configure","params":{"handle":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@40 61 result = request('{"method":"configure","params":{"handle":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@40 64 result = request('{"method":"process","params":{"handle":1,"processInput":{"timestamp":{"s":0,"n":0},"inputBuffers":[[0,1,-1,0,1,-1,0,1]]}}}');
Chris@40 65 result = request('{"method":"process","params":{"handle":1,"processInput":{"timestamp":{"s":0,"n":500000000},"inputBuffers":[[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@40 68 result = request('{"method":"finish","params":{"handle":1}}');
Chris@4 69
Chris@6 70 comment("A process call should now fail, as the plugin has been cleaned up.");
Chris@40 71 result = request('{"method":"process","params":{"handle":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 }