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