annotate quick-test.js @ 73:8db927152497

Simple test script (and associated fixes) for the JS code
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 24 Aug 2016 10:50:40 +0100
parents
children 9bba59806014
rev   line source
c@73 1 "use strict";
c@73 2
c@73 3 var exampleModule = ExampleModule();
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@73 25 function request(jsonStr) {
c@73 26 var m = exampleModule;
c@73 27 // Inspection reveals that intArrayFromString converts the string
c@73 28 // from utf16 to utf8, which is what we want (though the docs
c@73 29 // don't mention this). Note the *Cstr values are Emscripten heap
c@73 30 // pointers
c@73 31 var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL);
c@73 32 var outCstr = vampipeRequestJson(inCstr);
c@73 33 m._free(inCstr);
c@73 34 var result = m.Pointer_stringify(outCstr);
c@73 35 vampipeFreeJson(outCstr);
c@73 36 return result;
c@73 37 }
c@73 38
c@73 39 function note(blah) {
c@73 40 document.getElementById("test-result").innerHTML += blah + "<br>";
c@73 41 }
c@73 42
c@73 43 function test() {
c@73 44
c@73 45 note("Querying plugin list...");
c@73 46 var result = request('{"type": "list"}');
c@73 47 note("Result JSON = " + result);
c@73 48
c@73 49 note("<br>Loading zero crossings plugin...");
c@73 50 result = request('{"type":"load","content": {"pluginKey":"vamp-example-plugins:zerocrossing","inputSampleRate":16,"adapterFlags":["AdaptAllSafe"]}}');
c@73 51 note("Result JSON = " + result);
c@73 52
c@73 53 note("<br>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 54 result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}');
c@73 55 note("Result JSON = " + result);
c@73 56
c@73 57 note("<br>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 58 result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}');
c@73 59 note("Result JSON = " + result);
c@73 60
c@73 61 note("<br>Now processing a couple of blocks of data, on the same assumptions...");
c@73 62 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 63 note("Result JSON = " + result);
c@73 64 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 65 note("Result JSON = " + result);
c@73 66
c@73 67 note("<br>Cleaning up the plugin and getting any remaining features...");
c@73 68 result = request('{"type":"finish","content":{"pluginHandle":1}}');
c@73 69 note("Result JSON = " + result);
c@73 70
c@73 71 note("<br>A process call should now fail, as the plugin has been cleaned up.");
c@73 72 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 73 note("Result JSON = " + result);
c@73 74 }
c@73 75
c@73 76 window.onload = function() {
c@73 77 test();
c@73 78 }