Chris@4
|
1 "use strict";
|
Chris@4
|
2
|
Chris@4
|
3 var exampleModule = ExampleModule();
|
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@4
|
9 // vampipeRequestJson would appear (?) to be thrown away by the
|
Chris@4
|
10 // Emscripten string converter if we declare it as returning a string,
|
Chris@4
|
11 // so we have no opportunity to pass it to vampipeFreeJson, 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@4
|
17 var vampipeRequestJson = exampleModule.cwrap(
|
Chris@4
|
18 'vampipeRequestJson', 'number', ['number']
|
Chris@4
|
19 );
|
Chris@4
|
20
|
Chris@4
|
21 var vampipeFreeJson = exampleModule.cwrap(
|
Chris@4
|
22 'vampipeFreeJson', 'void', ['number']
|
Chris@4
|
23 );
|
Chris@4
|
24
|
Chris@4
|
25 function request(jsonStr) {
|
Chris@4
|
26 var m = exampleModule;
|
Chris@4
|
27 // Inspection reveals that intArrayFromString converts the string
|
Chris@4
|
28 // from utf16 to utf8, which is what we want (though the docs
|
Chris@4
|
29 // don't mention this). Note the *Cstr values are Emscripten heap
|
Chris@4
|
30 // pointers
|
Chris@4
|
31 var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL);
|
Chris@4
|
32 var outCstr = vampipeRequestJson(inCstr);
|
Chris@4
|
33 m._free(inCstr);
|
Chris@4
|
34 var result = m.Pointer_stringify(outCstr);
|
Chris@4
|
35 vampipeFreeJson(outCstr);
|
Chris@4
|
36 return result;
|
Chris@4
|
37 }
|
Chris@4
|
38
|
Chris@4
|
39 function note(blah) {
|
Chris@4
|
40 document.getElementById("test-result").innerHTML += blah + "<br>";
|
Chris@4
|
41 }
|
Chris@4
|
42
|
Chris@4
|
43 function test() {
|
Chris@4
|
44
|
Chris@4
|
45 note("Querying plugin list...");
|
Chris@4
|
46 var result = request('{"type": "list"}');
|
Chris@4
|
47 note("Result JSON = " + result);
|
Chris@4
|
48
|
Chris@4
|
49 note("<br>Loading zero crossings plugin...");
|
Chris@4
|
50 result = request('{"type":"load","content": {"pluginKey":"vamp-example-plugins:zerocrossing","inputSampleRate":16,"adapterFlags":["AdaptAllSafe"]}}');
|
Chris@4
|
51 note("Result JSON = " + result);
|
Chris@4
|
52
|
Chris@4
|
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...");
|
Chris@4
|
54 result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}');
|
Chris@4
|
55 note("Result JSON = " + result);
|
Chris@4
|
56
|
Chris@4
|
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.");
|
Chris@4
|
58 result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": 8, "channelCount": 1, "stepSize": 8}}}');
|
Chris@4
|
59 note("Result JSON = " + result);
|
Chris@4
|
60
|
Chris@4
|
61 note("<br>Now processing a couple of blocks of data, on the same assumptions...");
|
Chris@4
|
62 result = request('{"type":"process","content":{"pluginHandle":1,"processInput":{"timestamp":{"s":0,"n":0},"inputBuffers":[{"values":[0,1,-1,0,1,-1,0,1]}]}}}');
|
Chris@4
|
63 note("Result JSON = " + result);
|
Chris@4
|
64 result = request('{"type":"process","content":{"pluginHandle":1,"processInput":{"timestamp":{"s":0,"n":500000000},"inputBuffers":[{"values":[0,1,-1,0,1,-1,0,1]}]}}}');
|
Chris@4
|
65 note("Result JSON = " + result);
|
Chris@4
|
66
|
Chris@4
|
67 note("<br>Cleaning up the plugin and getting any remaining features...");
|
Chris@4
|
68 result = request('{"type":"finish","content":{"pluginHandle":1}}');
|
Chris@4
|
69 note("Result JSON = " + result);
|
Chris@4
|
70
|
Chris@4
|
71 note("<br>A process call should now fail, as the plugin has been cleaned up.");
|
Chris@4
|
72 result = request('{"type":"process","content":{"pluginHandle":1,"processInput":{"timestamp":{"s":0,"n":1000000000},"inputBuffers":[{"values":[0,1,-1,0,1,-1,0,1]}]}}}');
|
Chris@4
|
73 note("Result JSON = " + result);
|
Chris@4
|
74 }
|
Chris@4
|
75
|
Chris@4
|
76 window.onload = function() {
|
Chris@4
|
77 test();
|
Chris@4
|
78 }
|