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