Mercurial > hg > piper-vamp-js
comparison perf-test-node.js @ 104:2b20e610c4c2
Fix object structure for process input in perf test
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Mon, 26 Sep 2016 16:18:44 +0100 |
parents | f7b53c0faa53 |
children | 34480328bf5c |
comparison
equal
deleted
inserted
replaced
103:f7b53c0faa53 | 104:2b20e610c4c2 |
---|---|
1 "use strict"; | 1 "use strict"; |
2 | 2 |
3 var VampExamplePlugins = require("./VampExamplePlugins"); | 3 var VampExamplePlugins = require("./VampExamplePlugins"); |
4 var base64 = require("./base64"); | |
4 var exampleModule = VampExamplePlugins(); | 5 var exampleModule = VampExamplePlugins(); |
5 | 6 |
6 // It is possible to declare both parameters and return values as | 7 // It is possible to declare both parameters and return values as |
7 // "string", in which case Emscripten will take care of | 8 // "string", in which case Emscripten will take care of |
8 // conversions. But it's not clear how one would manage memory for | 9 // conversions. But it's not clear how one would manage memory for |
67 exampleModule.Pointer_stringify(responseJson)); | 68 exampleModule.Pointer_stringify(responseJson)); |
68 | 69 |
69 vampipeFreeJson(responseJson); | 70 vampipeFreeJson(responseJson); |
70 | 71 |
71 return response; | 72 return response; |
73 } | |
74 | |
75 function makeTimestamp(seconds) { | |
76 if (seconds >= 0.0) { | |
77 return { | |
78 s: Math.floor(seconds), | |
79 n: Math.floor((seconds - Math.floor(seconds)) * 1e9 + 0.5) | |
80 }; | |
81 } else { | |
82 const { s, n } = makeTimestamp(-seconds); | |
83 return { s: -s, n: -n }; | |
84 } | |
85 } | |
86 | |
87 function frame2timestamp(frame, rate) { | |
88 return makeTimestamp(frame / rate); | |
72 } | 89 } |
73 | 90 |
74 function request(jsonStr) { | 91 function request(jsonStr) { |
75 note("Request JSON = " + jsonStr); | 92 note("Request JSON = " + jsonStr); |
76 var m = exampleModule; | 93 var m = exampleModule; |
85 vampipeFreeJson(outCstr); | 102 vampipeFreeJson(outCstr); |
86 note("Returned JSON = " + result); | 103 note("Returned JSON = " + result); |
87 return result; | 104 return result; |
88 } | 105 } |
89 | 106 |
107 function myFromBase64(b64) { | |
108 while (b64.length % 4 > 0) { b64 += "="; } | |
109 let conv = new Float32Array(base64.toByteArray(b64).buffer); | |
110 return conv; | |
111 } | |
112 | |
113 function convertWireFeature(wfeature) { | |
114 let out = {}; | |
115 if (wfeature.timestamp != null) { | |
116 out.timestamp = wfeature.timestamp; | |
117 } | |
118 if (wfeature.duration != null) { | |
119 out.duration = wfeature.duration; | |
120 } | |
121 if (wfeature.label != null) { | |
122 out.label = wfeature.label; | |
123 } | |
124 if (wfeature.b64values != null && wfeature.b64values !== "") { | |
125 out.values = myFromBase64(wfeature.b64values); | |
126 } else if (wfeature.values != null) { | |
127 out.values = new Float32Array(wfeature.values); | |
128 } | |
129 return out; | |
130 } | |
131 | |
132 function convertWireFeatureList(wfeatures) { | |
133 return wfeatures.map(convertWireFeature); | |
134 } | |
135 | |
136 function responseToFeatureSet(response) { | |
137 const features = new Map(); | |
138 const processResponse = response.content; | |
139 const wireFeatures = processResponse.features; | |
140 Object.keys(wireFeatures).forEach(key => { | |
141 return features.set(key, convertWireFeatureList(wireFeatures[key])); | |
142 }); | |
143 return features; | |
144 } | |
145 | |
90 function test() { | 146 function test() { |
91 | 147 |
148 const rate = 44100; | |
149 | |
92 comment("Loading zero crossings plugin..."); | 150 comment("Loading zero crossings plugin..."); |
93 let result = request('{"type":"load","content": {"pluginKey":"vamp-example-plugins:zerocrossing","inputSampleRate":44100,"adapterFlags":["AdaptAllSafe"]}}'); | 151 let result = request('{"type":"load","content": {"pluginKey":"vamp-example-plugins:zerocrossing","inputSampleRate":' + rate + ',"adapterFlags":["AdaptAllSafe"]}}'); |
94 | 152 |
95 const blockSize = 1024; | 153 const blockSize = 1024; |
96 | 154 |
97 result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": ' + blockSize + ', "channelCount": 1, "stepSize": ' + blockSize + '}}}'); | 155 result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": ' + blockSize + ', "channelCount": 1, "stepSize": ' + blockSize + '}}}'); |
98 | 156 |
99 const nblocks = 1000; | 157 const nblocks = 1000; |
100 | 158 |
101 let processInputBuffers = [new Float32Array( | 159 const makeBlock = (n => { |
102 Array.from(Array(blockSize).keys(), n => n / blockSize)) | 160 return { |
103 ]; | 161 timestamp : frame2timestamp(n * blockSize, rate), |
162 inputBuffers : [ | |
163 { values : new Float32Array( | |
164 Array.from(Array(blockSize).keys(), | |
165 n => n / blockSize)) } | |
166 ], | |
167 } | |
168 }); | |
169 | |
170 const blocks = Array.from(Array(nblocks).keys(), makeBlock); | |
104 | 171 |
105 comment("Now processing " + nblocks + " blocks of 1024 samples each..."); | 172 comment("Now processing " + nblocks + " blocks of 1024 samples each..."); |
106 | 173 |
174 let total = 0; | |
175 | |
107 let start = (new Date()).getTime(); | 176 let start = (new Date()).getTime(); |
108 comment("Start at " + start); | 177 comment("Start at " + start); |
109 | 178 |
110 for (let i = 0; i < nblocks; ++i) { | 179 for (let i = 0; i < nblocks; ++i) { |
111 let ts = { "s": i, "n": 0 }; // wholly bogus, but ZC plugin doesn't use it | |
112 result = processRaw({ | 180 result = processRaw({ |
113 "pluginHandle": 1, | 181 "pluginHandle": 1, |
114 "processInput": { | 182 "processInput": blocks[i] |
115 "timestamp": ts, | |
116 "inputBuffers": processInputBuffers | |
117 } | |
118 }); | 183 }); |
184 let features = responseToFeatureSet(result); | |
185 let count = features.get("counts")[0].values[0]; | |
186 total += count; | |
119 } | 187 } |
120 | 188 |
121 let finish = (new Date()).getTime(); | 189 let finish = (new Date()).getTime(); |
122 comment("Finish at " + finish + " for a time of " + (finish - start) + " ms"); | 190 comment("Finish at " + finish + " for a time of " + (finish - start) + " ms"); |
123 | 191 |
192 comment("Total = " + total); | |
193 | |
124 comment("Again..."); | 194 comment("Again..."); |
195 | |
196 total = 0; | |
125 | 197 |
126 start = (new Date()).getTime(); | 198 start = (new Date()).getTime(); |
127 comment("Start at " + start); | 199 comment("Start at " + start); |
128 | 200 |
129 for (let i = 0; i < nblocks; ++i) { | 201 for (let i = 0; i < nblocks; ++i) { |
130 let ts = { "s": i, "n": 0 }; // wholly bogus, but ZC plugin doesn't use it | |
131 result = processRaw({ | 202 result = processRaw({ |
132 "pluginHandle": 1, | 203 "pluginHandle": 1, |
133 "processInput": { | 204 "processInput": blocks[i] |
134 "timestamp": ts, | |
135 "inputBuffers": processInputBuffers | |
136 } | |
137 }); | 205 }); |
206 let features = responseToFeatureSet(result); | |
207 let count = features.get("counts")[0].values[0]; | |
208 total += count; | |
138 } | 209 } |
139 | 210 |
140 finish = (new Date()).getTime(); | 211 finish = (new Date()).getTime(); |
141 comment("Finish at " + finish + " for a time of " + (finish - start) + " ms"); | 212 comment("Finish at " + finish + " for a time of " + (finish - start) + " ms"); |
142 | 213 |
214 comment("Total = " + total); | |
215 | |
143 comment("Cleaning up the plugin and getting any remaining features..."); | 216 comment("Cleaning up the plugin and getting any remaining features..."); |
144 result = request('{"type":"finish","content":{"pluginHandle":1}}'); | 217 result = request('{"type":"finish","content":{"pluginHandle":1}}'); |
145 } | 218 } |
146 | 219 |
147 test(); | 220 test(); |