comparison test/perf-test-node.js @ 56:49a9c10bd980

Renames
author Chris Cannam
date Thu, 10 Nov 2016 13:56:56 +0000
parents ff0dcd339c9d
children
comparison
equal deleted inserted replaced
55:62a41dccac51 56:49a9c10bd980
1 "use strict"; 1 "use strict";
2 2
3 var VampExamplePlugins = require("../examples/VampExamplePlugins"); 3 var extractorName = "VampExamplePlugins";
4 var pluginKey = "vamp-example-plugins:zerocrossing";
5 var extractor = require("../examples/" + extractorName);
4 var base64 = require("./base64"); 6 var base64 = require("./base64");
5 var exampleModule = VampExamplePlugins(); 7 var extractorModule = extractor();
6 8
7 // It is possible to declare both parameters and return values as 9 // It is possible to declare both parameters and return values as
8 // "string", in which case Emscripten will take care of 10 // "string", in which case Emscripten will take care of
9 // conversions. But it's not clear how one would manage memory for 11 // conversions. But it's not clear how one would manage memory for
10 // newly-constructed returned C strings -- the returned pointer from 12 // newly-constructed returned C strings -- the returned pointer from
14 // suggests this would leak memory if the string isn't static. Not 16 // suggests this would leak memory if the string isn't static. Not
15 // wholly sure though. Anyway, passing and returning pointers (as 17 // wholly sure though. Anyway, passing and returning pointers (as
16 // numbers) means we can manage the Emscripten heap memory however we 18 // numbers) means we can manage the Emscripten heap memory however we
17 // want in our request wrapper function below. 19 // want in our request wrapper function below.
18 20
19 var piperRequestJson = exampleModule.cwrap( 21 var piperRequestJson = extractorModule.cwrap(
20 'piperRequestJson', 'number', ['number'] 22 'piperRequestJson', 'number', ['number']
21 ); 23 );
22 24
23 var piperProcessRaw = exampleModule.cwrap( 25 var piperProcessRaw = extractorModule.cwrap(
24 "piperProcessRaw", "number", ["number", "number", "number", "number"] 26 "piperProcessRaw", "number", ["number", "number", "number", "number"]
25 ); 27 );
26 28
27 var piperFreeJson = exampleModule.cwrap( 29 var piperFreeJson = extractorModule.cwrap(
28 'piperFreeJson', 'void', ['number'] 30 'piperFreeJson', 'void', ['number']
29 ); 31 );
30 32
31 function note(blah) { 33 function note(blah) {
32 console.log(blah); 34 console.log(blah);
39 function processRaw(request) { 41 function processRaw(request) {
40 42
41 const nChannels = request.processInput.inputBuffers.length; 43 const nChannels = request.processInput.inputBuffers.length;
42 const nFrames = request.processInput.inputBuffers[0].length; 44 const nFrames = request.processInput.inputBuffers[0].length;
43 45
44 const buffersPtr = exampleModule._malloc(nChannels * 4); 46 const buffersPtr = extractorModule._malloc(nChannels * 4);
45 const buffers = new Uint32Array( 47 const buffers = new Uint32Array(
46 exampleModule.HEAPU8.buffer, buffersPtr, nChannels); 48 extractorModule.HEAPU8.buffer, buffersPtr, nChannels);
47 49
48 for (let i = 0; i < nChannels; ++i) { 50 for (let i = 0; i < nChannels; ++i) {
49 const framesPtr = exampleModule._malloc(nFrames * 4); 51 const framesPtr = extractorModule._malloc(nFrames * 4);
50 const frames = new Float32Array( 52 const frames = new Float32Array(
51 exampleModule.HEAPU8.buffer, framesPtr, nFrames); 53 extractorModule.HEAPU8.buffer, framesPtr, nFrames);
52 frames.set(request.processInput.inputBuffers[i]); 54 frames.set(request.processInput.inputBuffers[i]);
53 buffers[i] = framesPtr; 55 buffers[i] = framesPtr;
54 } 56 }
55 57
56 const responseJson = piperProcessRaw( 58 const responseJson = piperProcessRaw(
58 buffersPtr, 60 buffersPtr,
59 request.processInput.timestamp.s, 61 request.processInput.timestamp.s,
60 request.processInput.timestamp.n); 62 request.processInput.timestamp.n);
61 63
62 for (let i = 0; i < nChannels; ++i) { 64 for (let i = 0; i < nChannels; ++i) {
63 exampleModule._free(buffers[i]); 65 extractorModule._free(buffers[i]);
64 } 66 }
65 exampleModule._free(buffersPtr); 67 extractorModule._free(buffersPtr);
66 68
67 const responseJstr = exampleModule.Pointer_stringify(responseJson); 69 const responseJstr = extractorModule.Pointer_stringify(responseJson);
68 const response = JSON.parse(responseJstr); 70 const response = JSON.parse(responseJstr);
69 71
70 piperFreeJson(responseJson); 72 piperFreeJson(responseJson);
71 73
72 return response; 74 return response;
88 return makeTimestamp(frame / rate); 90 return makeTimestamp(frame / rate);
89 } 91 }
90 92
91 function request(jsonStr) { 93 function request(jsonStr) {
92 note("Request JSON = " + jsonStr); 94 note("Request JSON = " + jsonStr);
93 var m = exampleModule; 95 var m = extractorModule;
94 // Inspection reveals that intArrayFromString converts the string 96 // Inspection reveals that intArrayFromString converts the string
95 // from utf16 to utf8, which is what we want (though the docs 97 // from utf16 to utf8, which is what we want (though the docs
96 // don't mention this). Note the *Cstr values are Emscripten heap 98 // don't mention this). Note the *Cstr values are Emscripten heap
97 // pointers 99 // pointers
98 var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL); 100 var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL);
149 function test() { 151 function test() {
150 152
151 const rate = 44100; 153 const rate = 44100;
152 154
153 comment("Loading zero crossings plugin..."); 155 comment("Loading zero crossings plugin...");
154 let result = request('{"method":"load","params": {"key":"vamp-example-plugins:zerocrossing","inputSampleRate":' + rate + ',"adapterFlags":["AdaptAllSafe"]}}'); 156 let result = request('{"method":"load","params": {"key":"' + pluginKey + '","inputSampleRate":' + rate + ',"adapterFlags":["AdaptAllSafe"]}}');
155 157
156 const blockSize = 1024; 158 const blockSize = 1024;
157 159
158 result = request('{"method":"configure","params":{"handle":1,"configuration":{"blockSize": ' + blockSize + ', "channelCount": 1, "stepSize": ' + blockSize + '}}}'); 160 result = request('{"method":"configure","params":{"handle":1,"configuration":{"blockSize": ' + blockSize + ', "channelCount": 1, "stepSize": ' + blockSize + '}}}');
159 161