diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..24ca935 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/node_modules +/dist +/package-lock.json diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..bb70059 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,28 @@ +# How to Contribute + +We'd love to accept your patches and contributions to this project. There are +just a few small guidelines you need to follow. + +## Contributor License Agreement + +Contributions to this project must be accompanied by a Contributor License +Agreement. You (or your employer) retain the copyright to your contribution, +this simply gives us permission to use and redistribute your contributions as +part of the project. Head over to to see +your current agreements on file or to sign a new one. + +You generally only need to submit a CLA once, so if you've already submitted one +(even if it was for a different project), you probably don't need to do it +again. + +## Code reviews + +All submissions, including submissions by project members, require review. We +use GitHub pull requests for this purpose. Consult +[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more +information on using pull requests. + +## Community Guidelines + +This project follows [Google's Open Source Community +Guidelines](https://opensource.google.com/conduct/). diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f107611 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2018 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..89d97d8 --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# AudioWorklet Polyfill + +Polyfills [AudioWorklet] in browsers that don't support them, using ScriptProcessorNodes under the hood. + +Worklets are invoked on the main thread within an isolated scope emulating that of a Worklet. + +| [Demo #1](https://googlechromelabs.github.io/audioworklet-polyfill/) | [Demo #2](https://audio-dsp-playground-polyfilled.surge.sh) | +|-|-| + +## Usage + +```html + + + +``` + +Or with a bundler: + +```js +import 'audioworklet-polyfill'; +``` + +... or with ES Modules on the web: + +```js +import('https://unpkg.com/audioworklet-polyfill/dist/audioworklet-polyfill.js'); +``` + +## Roadmap + +- Improve support for custom parameters + +## Similar Libraries + +[@jariseon](https://github.com/jariseon) has implemented [a similar polyfill](https://github.com/jariseon/audioworklet-polyfill) that uses Web Workers for audio processing. + +## License + +[Apache 2.0](LICENSE) + +[AudioWorklets]: https://googlechromelabs.github.io/web-audio-samples/audio-worklet/ diff --git a/demo/demo-worklet.js b/demo/demo-worklet.js new file mode 100644 index 0000000..5d751a4 --- /dev/null +++ b/demo/demo-worklet.js @@ -0,0 +1,38 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +class DemoProcessor extends AudioWorkletProcessor { + static get parameterDescriptors () { + return [ + { name: 'gain', defaultValue: 1 } + ]; + } + + process (inputs, outputs, parameters) { + const input = inputs[0]; + const output = outputs[0]; + const gain = parameters.gain; + for (let channel = 0; channel < input.length; channel++) { + const inputChannel = input[channel]; + const outputChannel = output[channel]; + for (let i = 0; i < inputChannel.length; i++) { + outputChannel[i] = inputChannel[i] * gain[i]; + } + } + } +} + +registerProcessor('demo', DemoProcessor); diff --git a/demo/demo.js b/demo/demo.js new file mode 100644 index 0000000..48596ab --- /dev/null +++ b/demo/demo.js @@ -0,0 +1,60 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +/* eslint-disable */ + +// Copied verbatim from houdini-samples: +// https://developers.google.com/web/updates/2017/12/audio-worklet#background_scriptprocessornode + +document.getElementById('play').onclick = function() { + var context = new AudioContext(); + + // Inject a tiny tranpiler to make IE11+ behave nicely: + try { + eval('class F {}'); + } catch (e) { + context.audioWorklet.transpile = window.transpilerLite; + } + + context.audioWorklet.addModule('noise-generator-worklet.js').then(() => { + let modulator = new OscillatorNode(context); + let modGain = new GainNode(context); + let noiseGenerator = new AudioWorkletNode(context, 'noise-generator'); + noiseGenerator.connect(context.destination); + // Connect the oscillator to 'amplitude' AudioParam. + let paramAmp = noiseGenerator.parameters.get('amplitude'); + console.log(paramAmp); + setInterval(() => console.log(paramAmp.value), 250); + modulator.connect(modGain).connect(paramAmp); + modulator.frequency.value = 0.5; + modGain.gain.value = 0.75; + modulator.start(); + }); + + /* + // Loads module script via AudioWorklet. + context.audioWorklet.addModule('dmeo-worklet.js').then(() => { + var oscillator = new OscillatorNode(context); + + // After the resolution of module loading, an AudioWorkletNode can be constructed. + var demoNode = new AudioWorkletNode(context, 'demo'); + + // AudioWorkletNode can be interoperable with other native AudioNodes. + oscillator.connect(demoNode).connect(context.destination); + oscillator.start(); + }); + */ +} \ No newline at end of file diff --git a/demo/index.html b/demo/index.html new file mode 100644 index 0000000..c435736 --- /dev/null +++ b/demo/index.html @@ -0,0 +1,29 @@ + + + + + + +

+ This is a demonstration of the AudioWorklet Polyfill. +

+ +

+ Note: This polyfill runs AudioWorklets in an isolated context on the main thread. +

+ +
+ Source: +
demo.js
+
noise-generator-worklet.js
+
demo-worklet.js
+
+ + + + + + + + + diff --git a/demo/noise-generator-worklet.js b/demo/noise-generator-worklet.js new file mode 100644 index 0000000..7ee3737 --- /dev/null +++ b/demo/noise-generator-worklet.js @@ -0,0 +1,31 @@ +// Copyright (c) 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/** + * A noise generator example with a gain AudioParam. + * + * @class NoiseGenerator + * @extends AudioWorkletProcessor + */ +class NoiseGenerator extends AudioWorkletProcessor { + static get parameterDescriptors () { + return [{ name: 'amplitude', defaultValue: 0.25, minValue: 0, maxValue: 1 }]; + } + + process (inputs, outputs, parameters) { + const output = outputs[0]; + const amplitude = parameters.amplitude; + // console.log(amplitude); + for (let channel = 0; channel < output.length; ++channel) { + const outputChannel = output[channel]; + for (let i = 0; i < outputChannel.length; ++i) { + outputChannel[i] = 2 * (Math.random() - 0.5) * amplitude[i]; + } + } + + return true; + } +} + +registerProcessor('noise-generator', NoiseGenerator); diff --git a/demo/style.css b/demo/style.css new file mode 100644 index 0000000..8ff683f --- /dev/null +++ b/demo/style.css @@ -0,0 +1,23 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +body { + font: 16px/1.3 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; +} + +pre { + margin: 0; +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..e450e22 --- /dev/null +++ b/package.json @@ -0,0 +1,68 @@ +{ + "name": "audioworklet-polyfill", + "version": "1.0.0", + "description": "AudioWorklet polyfill using the legacy ScriptProcessor API.", + "source": "src/index.js", + "main": "dist/audioworklet-polyfill.js", + "license": "Apache-2.0", + "author": "The Chromium Authors", + "contributors": [ + { + "name": "Jason Miller", + "email": "developit@google.com" + } + ], + "repository": "GoogleChromeLabs/audioworklet-polyfill", + "scripts": { + "build": "microbundle -f iife && cp -r demo dist/", + "start": "concurrently serve \"microbundle watch -f iife\"", + "test": "eslint src", + "deploy": "cp -rf demo build && cp -r dist build/ && sed -i '' 's/\\.\\.\\/dist/dist/' build/index.html && gh-pages -d build && rm -rf build" + }, + "eslintConfig": { + "extends": "standard", + "env": { + "browser": true, + "node": false + }, + "rules": { + "indent": [ + 2, + 2 + ], + "semi": [ + 2, + "always" + ], + "prefer-const": 1 + }, + "globals": { + "registerProcessor": 1, + "AudioWorkletProcessor": 1 + } + }, + "files": [ + "src", + "dist" + ], + "keywords": [ + "audio", + "worklet", + "polyfill", + "audio worklet", + "scriptprocessornode", + "web audio" + ], + "devDependencies": { + "concurrently": "^3.5.1", + "eslint": "^4.19.1", + "eslint-config-standard": "^11.0.0", + "eslint-plugin-import": "^2.12.0", + "eslint-plugin-node": "^6.0.1", + "eslint-plugin-promise": "^3.7.0", + "eslint-plugin-standard": "^3.0.1", + "gh-pages": "^1.1.0", + "microbundle": "^0.4.4", + "serve": "^6.5.7" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..5c8df09 --- /dev/null +++ b/src/index.js @@ -0,0 +1,140 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +import { Realm } from './realm'; + +const BUFFER_SIZE = 256; + +if (typeof AudioWorkletNode !== 'function') { + window.AudioWorkletNode = function AudioWorkletNode (context, name) { + const processor = getProcessorsForContext(context)[name]; + + this.parameters = new Map(); + if (processor.properties) { + for (let i = 0; i < processor.properties.length; i++) { + const prop = processor.properties[i]; + const node = context.createGain().gain; + node.value = prop.defaultValue; + // @TODO there's no good way to construct the proxy AudioParam here + this.parameters.set(prop.name, node); + } + } + + const inst = new processor.Processor({}); + + this.port = processor.port; + const scriptProcessor = context.createScriptProcessor(BUFFER_SIZE); + scriptProcessor.node = this; + scriptProcessor.processor = processor; + scriptProcessor.instance = inst; + scriptProcessor.onaudioprocess = onAudioProcess; + Object.defineProperty(this, '$$node', { value: scriptProcessor }); + }; + + Object.defineProperties(window.AudioWorkletNode.prototype = Object.create(AudioNode.prototype), { + bufferSize: { + get () { + return this.$$node.bufferSize; + } + }, + connect: { value (to) { + return this.$$node.connect(to); + } }, + disconnect: { value () { + return this.$$node.disconnect(); + } } + }); + + Object.defineProperty(AudioContext.prototype, 'audioWorklet', { + get () { + return this.$$audioWorklet || (this.$$audioWorklet = new window.AudioWorklet(this)); + } + }); + + window.AudioWorklet = class AudioWorklet { + constructor (audioContext) { + this.$$context = audioContext; + } + + addModule (url, options) { + return fetch(url).then(r => { + if (!r.ok) throw Error(r.status); + return r.text(); + }).then(code => { + const mc = new MessageChannel(); + const context = { + sampleRate: 0, + currentTime: 0, + AudioWorkletProcessor () { }, + registerProcessor: (name, Processor) => { + const processors = getProcessorsForContext(this.$$context); + processors[name] = { + port: mc.port1, + context, + Processor, + properties: Processor.parameterDescriptors || [] + }; + }, + postMessage (e) { mc.port2.postMessage(e); }, + addEventListener (type, fn) { mc.port2.addEventListener(type, fn); }, + removeEventListener (type, fn) { mc.port2.addEventListener(type, fn); } + }; + Object.defineProperty(context, 'onmessage', { + get () { return mc.port2.onmessage; }, + set (f) { mc.port2.onmessage = f; } + }); + context.self = context; + const realm = new Realm(context, document.documentElement); + realm.exec(((options && options.transpile) || String)(code)); + return null; + }); + } + }; +} + +const PARAMS = []; + +function onAudioProcess (e) { + const parameters = {}; + let index = -1; + this.node.parameters.forEach((value, key) => { + const arr = PARAMS[++index] || (PARAMS[index] = new Float32Array(BUFFER_SIZE)); + // @TODO proper values here if possible + arr.fill(value.value); + parameters[key] = arr; + }); + this.processor.context.sampleRate = this.context.sampleRate; + this.processor.context.currentTime = this.context.currentTime; + const inputs = channelToArray(e.inputBuffer); + const outputs = channelToArray(e.outputBuffer); + this.instance.process([inputs], [outputs], parameters); + + // @todo - keepalive + // let ret = this.instance.process([inputs], [outputs], parameters); + // if (ret === true) { } +} + +function channelToArray (ch) { + const out = []; + for (let i = 0; i < ch.numberOfChannels; i++) { + out[i] = ch.getChannelData(i); + } + return out; +} + +function getProcessorsForContext (audioContext) { + return audioContext.$$processors || (audioContext.$$processors = {}); +} diff --git a/src/realm.js b/src/realm.js new file mode 100644 index 0000000..47c0628 --- /dev/null +++ b/src/realm.js @@ -0,0 +1,43 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +export function Realm (scope, parentElement) { + const frame = document.createElement('iframe'); + frame.style.cssText = 'position:absolute;left:0;top:-999px;width:1px;height:1px;'; + parentElement.appendChild(frame); + const win = frame.contentWindow; + const doc = win.document; + let vars = 'var window,$hook'; + for (const i in win) { + if (!(i in scope) && i !== 'eval') { + vars += ','; + vars += i; + } + } + for (const i in scope) { + vars += ','; + vars += i; + vars += '=self.'; + vars += i; + } + const script = doc.createElement('script'); + script.appendChild(doc.createTextNode( + `function $hook(self,console) {"use strict"; + ${vars};return function() {return eval(arguments[0])}}` + )); + doc.body.appendChild(script); + this.exec = win.$hook(scope, console); +}