annotate quick-test.js @ 100:708bd44f8019

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