├── .travis.yml ├── lib ├── webdsp_c.wasm ├── webdsp_polyfill.js ├── webdsp.js └── webdsp_c.js ├── images └── webdsplogo.png ├── .gitignore ├── bin └── get-dsp.js ├── cpp ├── compileWASM.bat ├── compileWASM.sh └── webdsp.cpp ├── test ├── test.js └── test.cpp ├── compile.js ├── package.json ├── LICENSE └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js -------------------------------------------------------------------------------- /lib/webdsp_c.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shamadee/web-dsp/HEAD/lib/webdsp_c.wasm -------------------------------------------------------------------------------- /images/webdsplogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shamadee/web-dsp/HEAD/images/webdsplogo.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | media/ 3 | demo.js 4 | gulpfile.js 5 | index.html 6 | server.js 7 | smoothie.js 8 | style.css 9 | .DS_Store 10 | cpp/dft.cpp 11 | settings.json 12 | .vscode/ 13 | test/testCpp -------------------------------------------------------------------------------- /bin/get-dsp.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const path = require('path'); 4 | const fs = require('fs-extra'); 5 | 6 | const projectDir = process.cwd(); 7 | const libDir = path.join(projectDir, 'node_modules/web-dsp', './lib'); 8 | 9 | fs.copy(libDir, `${projectDir}/lib`, (err) => { 10 | if (err) process.stdout.write(err); 11 | }); 12 | -------------------------------------------------------------------------------- /cpp/compileWASM.bat: -------------------------------------------------------------------------------- 1 | set CPP_FUNCS=[^ 2 | '_grayScale', ^ 3 | '_brighten', ^ 4 | '_invert', ^ 5 | '_noise', ^ 6 | '_multiFilter', ^ 7 | '_sobelFilter', ^ 8 | '_convFilter',] 9 | 10 | emcc -o ./lib/webdsp_c.js ./cpp/webdsp.cpp -lm -O3^ 11 | -s WASM=1^ 12 | -s BINARYEN_IMPRECISE=1^ 13 | -s EXPORTED_FUNCTIONS="%CPP_FUNCS%"^ 14 | -s ALLOW_MEMORY_GROWTH=1 -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | const expect = require('chai').expect; 2 | const assert = require('assert'); 3 | const Browser = require('zombie'); 4 | 5 | Browser.localhost('example.com', 3000); 6 | 7 | describe('Should get to the page and', function () { 8 | let app, browser; 9 | before(function() { 10 | app = require('./../server'); 11 | browser = new Browser(); 12 | return browser.visit('/'); 13 | }); 14 | it('should have a title element with text "WASM Example"', function () { 15 | browser.assert.text('title', 'WASM Example'); 16 | }); 17 | it('should have the loadWASM function', function () { 18 | assert(browser.window.document._global.loadWASM); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /compile.js: -------------------------------------------------------------------------------- 1 | const exec = require('child_process').exec; 2 | const fs = require('fs'); 3 | 4 | const compileCommand = getCompileCommand(); 5 | exec(compileCommand, (err, stdout, stderr) => { 6 | 7 | if (isWin()) { 8 | const data = fs.readFileSync('lib/webdsp_c.js', 'utf8'); 9 | const newData = data.replace(/else\{doRun\(\)\}/g, '$&script.dispatchEvent(doneEvent);'); 10 | fs.writeFileSync('lib/webdsp_c.js', newData); 11 | } 12 | 13 | console.log(stdout); 14 | console.log(stderr); 15 | }); 16 | 17 | function getCompileCommand() { 18 | if (isWin()) { 19 | return 'call cpp/compileWASM.bat'; 20 | } else { 21 | return '. ./cpp/compileWASM.sh'; 22 | } 23 | } 24 | 25 | function isWin() { 26 | return /^win/.test(process.platform); 27 | } -------------------------------------------------------------------------------- /cpp/compileWASM.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # use path to emsdk folder, relative to this directory 4 | if [[ :$PATH: != *:"/emsdk":* ]] 5 | then 6 | BASEDIR="./../emsdk" 7 | BASEDIRSHA="./../../../Documents/emsdk" 8 | EMSDK_ENV=$(find "$BASEDIR" -type f -name "emsdk_env.sh") 9 | source "$EMSDK_ENV" 10 | fi 11 | # add exported C/C++ functions here 12 | CPP_FUNCS="[ 13 | '_grayScale', 14 | '_brighten', 15 | '_invert', 16 | '_noise', 17 | '_multiFilter', 18 | '_sobelFilter', 19 | '_convFilter', 20 | ]" 21 | 22 | echo "compiling C++ to WASM ..." 23 | emcc -o ./lib/webdsp_c.js ./cpp/webdsp.cpp -lm -O3 -s WASM=1 \ 24 | -s BINARYEN_IMPRECISE=1 \ 25 | -s EXPORTED_FUNCTIONS="$CPP_FUNCS" \ 26 | -s ALLOW_MEMORY_GROWTH=1 \ 27 | 28 | sed -i .bak 's/else{doRun()}/&script.dispatchEvent(doneEvent);/' lib/webdsp_c.js 29 | 30 | rm lib/*.bak 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web-dsp", 3 | "version": "0.0.10", 4 | "description": "A dsp library for the web, written with WebAssembly", 5 | "main": "test.js", 6 | "scripts": { 7 | "test": "npm run testCPP", 8 | "testCPP": "g++ ./test/test.cpp -o ./test/testCpp && ./test/testCpp", 9 | "testJS": "mocha || true", 10 | "compile": "echo \"compiling C/C++ to WASM\" && node compile.js" 11 | }, 12 | "bin": { 13 | "get-dsp": "./bin/get-dsp.js" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/shamadee/web-dsp.git" 18 | }, 19 | "author": "shamadee", 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://shamadee/web-dsp/issues" 23 | }, 24 | "homepage": "https://github.com/shamadee/web-dsp#readme", 25 | "devDependencies": { 26 | "browser-sync": "^2.18.8", 27 | "chai": "^3.5.0", 28 | "mocha": "^3.2.0", 29 | "zombie": "^5.0.5" 30 | }, 31 | "dependencies": { 32 | "fs-extra": "^2.1.2", 33 | "path": "^0.12.7" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test/test.cpp: -------------------------------------------------------------------------------- 1 | #define CATCH_CONFIG_MAIN 2 | #include 3 | #include "./lib/catch.hpp" 4 | #include "../cpp/webdsp.cpp" 5 | 6 | TEST_CASE("Testing grayScale function", "[grayScale]") { 7 | int len = 4; 8 | unsigned char data[] = { 12, 15, 199, 124 }; 9 | unsigned char out[] = { 12, 12, 12, 124 }; 10 | grayScale(data, len); 11 | SECTION("R G and B should be equal") { 12 | for (int i = 0; i < len; i++) { 13 | REQUIRE( data[i] == out[i] ); 14 | } 15 | } 16 | } 17 | 18 | TEST_CASE("Testing brighten function", "[brighten]") { 19 | int len = 4; 20 | int brightness = 15; 21 | unsigned char data[] = { 12, 15, 255, 255 }; 22 | unsigned char out[] = { 12 + brightness, 15 + brightness, 255, 255 }; 23 | brighten(data, len, brightness); 24 | SECTION("R and G should be increased by brightness") { 25 | for (int i = 0; i < len; i++) { 26 | REQUIRE( data[i] == out[i] ); 27 | } 28 | } 29 | 30 | unsigned char data2[] = { 255, 234, 121, 255 }; 31 | brighten(data2, len, 100); 32 | SECTION("No value should be greater than 255") { 33 | for (int i = 0; i < len; i++) { 34 | REQUIRE( data2[i] <= 255 ); 35 | } 36 | } 37 | } 38 | 39 | TEST_CASE("Testing invert function", "[invert]") { 40 | int len = 4; 41 | int max = 255; 42 | unsigned char data[] = {1, 2, 3, 255 }; 43 | unsigned char out[] = {254, 253, 252, 255}; 44 | invert(data, len); 45 | SECTION("R G B should be inverted") { 46 | for (int i = 0; i < len; i++) { 47 | REQUIRE( data[i] == out[i] ); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![webDSP Logo](/images/webdsplogo.png) 2 |
3 | ## A client-side DSP library utilizing the power of WebAssembly (.wasm) 4 | 5 | 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.
6 |
7 | 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. 8 | 9 | ### Demo & Starter Kit 10 | 11 | Check out the [demo video editor](http://tiny.cc/webdsp) and [corresponding repo](https://github.com/shamadee/web-dsp-demo).
12 | 13 | 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](https://www.npmjs.com/package/wasm-init). 14 | 15 | ### Install 16 | 17 | 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. 18 | ```html 19 |