Code Monkey home page Code Monkey logo

web-dsp's Introduction

webDSP Logo

A client-side DSP library utilizing the power of WebAssembly (.wasm)

WebDSP is a collection of highly performant algorithms, which are designed to be building blocks for web applications that aim to operate on media data. The methods are written in C++ and compiled to WASM, and exposed as simple vanilla Javascript functions developers can run on the client side.

WebAssembly is very young, and this is the first .wasm based library designed to be dropped in to existing production level JS code bases. With that in mind, there was an explicit goal to optimize and simplify how JS developers can load and use .wasm functionality. Just as important, was our goal to lay the groundwork for future open source WebAssembly module developers.

Demo & Starter Kit

Check out the demo video editor and corresponding repo.

To quickly start working with WebAssembly and to build your own modules, please see our started WebAssembly work environment you can npm install and launch wasm-init.

Install

Clone this repo and drop only the 'lib' folder into your project. Simply load our library file in a script tag. You can also get the module via npm install web-dsp, which comes with a built-in npm executable (get-dsp), which will copy the lib folder into your project directory.

<script src = '/lib/webdsp.js' type = 'text/javascript'>

Loading the WebAssembly Module

Use loadWASM() to fetch the WebAssembly module as a promise object. Use jsFallback() in the catch block to handle browsers that don't support .wasm

var webdsp = {};
loadWASM().then(module => {
  webdsp = module;
  // things to execute on page load only after module is loaded
});

Note WebAssembly modules need to be loaded with an HTTP request (fetch). Chrome does not support local file access via HTTP, so the files must be loaded using a server. In Firefox, it is possible to load the module without a server as a plain HTML file.
After loading, a WebAssembly method can be called with plain JS:

//get image data from canvas
pixels = context.getImageData(0,0,width,height);
button.addEventListener('click', () => {
  pixels.data.set(webdsp.invert(pixels.data));
});

Video and Image Filter Methods

These modular filters can execute on an array of RGBA pixel data:

webdsp.grayScale(pixelData)
webdsp.brighten(pixelData)
webdsp.invert(pixelData)
webdsp.noise(pixelData)
webdsp.sobelFilter(pixelData, width, height, invert=false)
webdsp.convFilter(pixelData, width, height, kernel, divisor, bias=0, count=1)
webdsp.multiFilter(pixelData, width, filterType, mag, multiplier, adjacent)

Filter templates:

webdsp.sunset(pixelData, width)
webdsp.analogTV(pixelData, width)
webdsp.emboss(pixelData, width)
webdsp.blur(pixelData, width, height)
webdsp.sharpen(pixelData, width, height))
webdsp.strongSharpen(pixelData, width, height)
webdsp.clarity(pixelData, width, height)
webdsp.goodMorning(pixelData, width, height)
webdsp.acid(pixelData, width, height)
webdsp.urple(pixelData, width)
webdsp.forest(pixelData, width)
webdsp.romance(pixelData, width)
webdsp.hippo(pixelData, width)
webdsp.longhorn(pixelData, width)
webdsp.underground(pixelData, width)
webdsp.rooster(pixelData, width)
webdsp.mist(pixelData, width)
webdsp.tingle(pixelData, width)
webdsp.bacteria(pixelData, width)
webdsp.dewdrops(pixelData, width, height)
webdsp.destruction(pixelData, width, height)
webdsp.hulk(pixelData, width)
webdsp.ghost(pixelData, width)
webdsp.twisted(pixelData, width)
webdsp.security(pixelData, width)

For production level environments, it's important to note that all available methods have JavaScript fallback functions that are automatically exported with the module so older browsers can still run your code. However, note that the more intensive convolution and edge detection filters will run very slowly or hang the browser completely without WebAssembly support.

TODO:

The following filter fallback implementations need to be properly matched with their C++ counterparts: underground, rooster, mist, kaleidoscope, bacteria, hulk edge, ghost, twisted.
Cache .wasm module on client

web-dsp's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

web-dsp's Issues

what does glue code do?

The repo is great, but there some code inside I don't understand.

There is a global variable -- Module. However, the glue code is like a black box, how do i know what .wasm exposed. Yes, I could get some exposed fn using WebAssembly.Module.exports/imports, but in this glue code, I should need to do this by Module.ccall.

so, how can get information from glue code? like:

wam['grayScale'] = function (pixelData) {
              const len = pixelData.length
              const mem = _malloc(len); // get the memory of wasm
              HEAPU8.set(pixelData, mem); // transfer the pixelData
              _grayScale(mem, len); // trigger the grayScale fun
              const filtered = HEAPU8.subarray(mem, mem + len); // get the resulted data
              _free(mem); // release memory
              return filtered; // return buffer
            };

where could I find some explanation of the above code, like HEAPU8, or _free.

I feel a little confused.

Examples

Would be great to add some simple examples to the repo. Feel free to close if this is not something you're interested in.

webassembly is slower than javascript

I have seen a nice video which was talking about performance of web-assembly, but running your example
the javascript is playing with similar speed, but on the graph it has been shown that the wasm is -97% is faster than javascript.

https://youtu.be/6v4E6oksar0?t=1796

my green line is showing 2.3, and my blue line is showing 400-500

No error, but still not working...

I have this code, can you explain why this doesn't show any result?

    var c = document.getElementById("c2");
    var ctx = c.getContext("2d");

    // Create gradient
    var grd = ctx.createLinearGradient(0, 0, 200, 0);
    grd.addColorStop(0, "red");
    grd.addColorStop(1, "white");

    // Fill with gradient
    ctx.fillStyle = grd;
    ctx.fillRect(10, 10, 150, 80);
         /*webassembly*/
        let wam;
        loadWASM()
          .then(module => {
            wam = module;
        }).catch((err) => {
          console.log('Error in fetching module: ', err);
        }).then(() => {
            window.onload = (() => {
                var pixels = ctx.getImageData(0, 0, 400, 300);
                //console.log(pixels);
                var xx = pixels.data.set(wam.goodMorning(pixels.data, 400, 300));
                //var xx = wam.goodMorning(pixels.data, 400, 300);
                //console.log(xx);
                pixels.data.set(xx);
            })();
        });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.