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