c@103: "use strict"; c@103: c@103: var VampExamplePlugins = require("./VampExamplePlugins"); c@104: var base64 = require("./base64"); c@103: var exampleModule = VampExamplePlugins(); c@103: c@103: // It is possible to declare both parameters and return values as c@103: // "string", in which case Emscripten will take care of c@103: // conversions. But it's not clear how one would manage memory for c@103: // newly-constructed returned C strings -- the returned pointer from c@103: // vampipeRequestJson would appear (?) to be thrown away by the c@103: // Emscripten string converter if we declare it as returning a string, c@103: // so we have no opportunity to pass it to vampipeFreeJson, which c@103: // suggests this would leak memory if the string isn't static. Not c@103: // wholly sure though. Anyway, passing and returning pointers (as c@103: // numbers) means we can manage the Emscripten heap memory however we c@103: // want in our request wrapper function below. c@103: c@103: var vampipeRequestJson = exampleModule.cwrap( c@103: 'vampipeRequestJson', 'number', ['number'] c@103: ); c@103: c@103: var vampipeProcessRaw = exampleModule.cwrap( c@103: "vampipeProcessRaw", "number", ["number", "number", "number", "number"] c@103: ); c@103: c@103: var vampipeFreeJson = exampleModule.cwrap( c@103: 'vampipeFreeJson', 'void', ['number'] c@103: ); c@103: c@103: function note(blah) { c@103: console.log(blah); c@103: } c@103: c@103: function comment(blah) { c@103: console.log(blah); c@103: } c@103: c@103: function processRaw(request) { c@103: c@103: const nChannels = request.processInput.inputBuffers.length; c@105: const nFrames = request.processInput.inputBuffers[0].length; c@103: c@103: const buffersPtr = exampleModule._malloc(nChannels * 4); c@103: const buffers = new Uint32Array( c@103: exampleModule.HEAPU8.buffer, buffersPtr, nChannels); c@103: c@103: for (let i = 0; i < nChannels; ++i) { c@103: const framesPtr = exampleModule._malloc(nFrames * 4); c@103: const frames = new Float32Array( c@103: exampleModule.HEAPU8.buffer, framesPtr, nFrames); c@105: frames.set(request.processInput.inputBuffers[i]); c@103: buffers[i] = framesPtr; c@103: } c@103: c@103: const responseJson = vampipeProcessRaw( c@109: request.handle, c@103: buffersPtr, c@103: request.processInput.timestamp.s, c@103: request.processInput.timestamp.n); c@103: c@103: for (let i = 0; i < nChannels; ++i) { c@103: exampleModule._free(buffers[i]); c@103: } c@103: exampleModule._free(buffersPtr); c@103: c@105: const responseJstr = exampleModule.Pointer_stringify(responseJson); c@105: const response = JSON.parse(responseJstr); c@103: c@103: vampipeFreeJson(responseJson); c@103: c@103: return response; c@103: } c@103: c@104: function makeTimestamp(seconds) { c@104: if (seconds >= 0.0) { c@104: return { c@104: s: Math.floor(seconds), c@104: n: Math.floor((seconds - Math.floor(seconds)) * 1e9 + 0.5) c@104: }; c@104: } else { c@104: const { s, n } = makeTimestamp(-seconds); c@104: return { s: -s, n: -n }; c@104: } c@104: } c@104: c@104: function frame2timestamp(frame, rate) { c@104: return makeTimestamp(frame / rate); c@104: } c@104: c@103: function request(jsonStr) { c@103: note("Request JSON = " + jsonStr); c@103: var m = exampleModule; c@103: // Inspection reveals that intArrayFromString converts the string c@103: // from utf16 to utf8, which is what we want (though the docs c@103: // don't mention this). Note the *Cstr values are Emscripten heap c@103: // pointers c@103: var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL); c@103: var outCstr = vampipeRequestJson(inCstr); c@103: m._free(inCstr); c@103: var result = m.Pointer_stringify(outCstr); c@103: vampipeFreeJson(outCstr); c@103: note("Returned JSON = " + result); c@103: return result; c@103: } c@103: c@104: function myFromBase64(b64) { c@104: while (b64.length % 4 > 0) { b64 += "="; } c@104: let conv = new Float32Array(base64.toByteArray(b64).buffer); c@104: return conv; c@104: } c@104: c@104: function convertWireFeature(wfeature) { c@104: let out = {}; c@104: if (wfeature.timestamp != null) { c@104: out.timestamp = wfeature.timestamp; c@104: } c@104: if (wfeature.duration != null) { c@104: out.duration = wfeature.duration; c@104: } c@104: if (wfeature.label != null) { c@104: out.label = wfeature.label; c@104: } c@105: const vv = wfeature.featureValues; c@105: if (vv != null) { c@105: if (typeof vv === "string") { c@105: out.featureValues = myFromBase64(vv); c@105: } else { c@105: out.featureValues = new Float32Array(vv); c@105: } c@104: } c@104: return out; c@104: } c@104: c@104: function convertWireFeatureList(wfeatures) { c@104: return wfeatures.map(convertWireFeature); c@104: } c@104: c@104: function responseToFeatureSet(response) { c@104: const features = new Map(); c@109: const processResponse = response.result; c@104: const wireFeatures = processResponse.features; c@104: Object.keys(wireFeatures).forEach(key => { c@104: return features.set(key, convertWireFeatureList(wireFeatures[key])); c@104: }); c@104: return features; c@104: } c@104: c@103: function test() { c@103: c@104: const rate = 44100; c@104: c@103: comment("Loading zero crossings plugin..."); c@109: let result = request('{"method":"load","params": {"key":"vamp-example-plugins:zerocrossing","inputSampleRate":' + rate + ',"adapterFlags":["AdaptAllSafe"]}}'); c@103: c@103: const blockSize = 1024; c@103: c@109: result = request('{"method":"configure","params":{"handle":1,"configuration":{"blockSize": ' + blockSize + ', "channelCount": 1, "stepSize": ' + blockSize + '}}}'); c@103: c@103: const nblocks = 1000; c@103: c@104: const makeBlock = (n => { c@104: return { c@104: timestamp : frame2timestamp(n * blockSize, rate), c@104: inputBuffers : [ c@105: new Float32Array(Array.from(Array(blockSize).keys(), c@105: n => n / blockSize)) c@104: ], c@104: } c@104: }); c@104: c@104: const blocks = Array.from(Array(nblocks).keys(), makeBlock); c@103: c@103: comment("Now processing " + nblocks + " blocks of 1024 samples each..."); c@103: c@104: let total = 0; c@104: c@103: let start = (new Date()).getTime(); c@103: comment("Start at " + start); c@103: c@103: for (let i = 0; i < nblocks; ++i) { c@103: result = processRaw({ c@109: "handle": 1, c@104: "processInput": blocks[i] c@103: }); c@104: let features = responseToFeatureSet(result); c@105: let count = features.get("counts")[0].featureValues[0]; c@104: total += count; c@103: } c@103: c@103: let finish = (new Date()).getTime(); c@103: comment("Finish at " + finish + " for a time of " + (finish - start) + " ms"); c@103: c@104: comment("Total = " + total); c@104: c@103: comment("Again..."); c@104: c@104: total = 0; c@103: c@103: start = (new Date()).getTime(); c@103: comment("Start at " + start); c@103: c@103: for (let i = 0; i < nblocks; ++i) { c@103: result = processRaw({ c@109: "handle": 1, c@104: "processInput": blocks[i] c@103: }); c@104: let features = responseToFeatureSet(result); c@105: let count = features.get("counts")[0].featureValues[0]; c@104: total += count; c@103: } c@103: c@103: finish = (new Date()).getTime(); c@103: comment("Finish at " + finish + " for a time of " + (finish - start) + " ms"); c@103: c@104: comment("Total = " + total); c@104: c@103: comment("Cleaning up the plugin and getting any remaining features..."); c@109: result = request('{"method":"finish","params":{"handle":1}}'); c@103: } c@103: c@103: test(); c@103: