[
  {
    "path": ".gitignore",
    "content": "/node_modules\n/dist\n/package-lock.json\n"
  },
  {
    "path": "CONTRIBUTING.md",
    "content": "# How to Contribute\n\nWe'd love to accept your patches and contributions to this project. There are\njust a few small guidelines you need to follow.\n\n## Contributor License Agreement\n\nContributions to this project must be accompanied by a Contributor License\nAgreement. You (or your employer) retain the copyright to your contribution,\nthis simply gives us permission to use and redistribute your contributions as\npart of the project. Head over to <https://cla.developers.google.com/> to see\nyour current agreements on file or to sign a new one.\n\nYou generally only need to submit a CLA once, so if you've already submitted one\n(even if it was for a different project), you probably don't need to do it\nagain.\n\n## Code reviews\n\nAll submissions, including submissions by project members, require review. We\nuse GitHub pull requests for this purpose. Consult\n[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more\ninformation on using pull requests.\n\n## Community Guidelines\n\nThis project follows [Google's Open Source Community\nGuidelines](https://opensource.google.com/conduct/).\n"
  },
  {
    "path": "LICENSE",
    "content": "\n                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"[]\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n   Copyright 2018 Google Inc.\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License."
  },
  {
    "path": "README.md",
    "content": "<p align=\"center\">\n  <img src=\"https://i.imgur.com/RqW6b4o.png\" width=\"200\" height=\"200\" alt=\"AudioWorklet Polyfill\">\n  <h1 align=\"center\">\n  \tAudioWorklet Polyfill\n\t<a href=\"https://www.npmjs.org/package/audioworklet-polyfill\"><img src=\"https://img.shields.io/npm/v/audioworklet-polyfill.svg?style=flat\" alt=\"npm\"></a>\n  </h1>\n  <h3 align=\"center\">1kB polyfill for <a href=\"https://developers.google.com/web/updates/2017/12/audio-worklet\">AudioWorklet</a>.</h3>\n</p>\n\n`audioworklet-polyfill` is a tiny JavaScript library that brings AudioWorklet support to all major browsers: Chrome, Firefox, Safari and Edge. It uses ScriptProcessorNode under the hood, and runs your Worklet code in an isolated scope on the main thread ([read why](#why-are-worklets-emulated-on-the-main-thread)).\n\n| [Basic Demo](https://googlechromelabs.github.io/audioworklet-polyfill/) | [DSP Playground Demo](https://audio-dsp-playground-polyfilled.surge.sh) |\n|-|-|\n\n> **New to AudioWorklet?** Check out this great [Introduction and Demos](https://developers.google.com/web/updates/2017/12/audio-worklet) or the [AudioWorklet Examples].\n\n## Usage\n\n```html\n<script src=\"audioworklet-polyfill.js\"></script>\n<!-- or: -->\n<script src=\"https://unpkg.com/audioworklet-polyfill/dist/audioworklet-polyfill.js\"></script>\n```\n\nOr with a bundler:\n\n```js\nimport 'audioworklet-polyfill';\n```\n\n... or with ES Modules on the web:\n\n```js\nimport('https://unpkg.com/audioworklet-polyfill/dist/audioworklet-polyfill.js');\n```\n\n## Roadmap\n\n- Improve support for custom parameters\n\n## Why are Worklets emulated on the main thread?\n\nThis polyfill is intended to be a bridging solution until AudioWorklet is implemented across all browsers. It's an improvement over ScriptProcessorNode even though that's what it uses under the hood, because code written using this polyfill is forwards-compatible: as native support improves, your application improves. This polyfill offers a simple, future-proof alternative to ScriptProcessorNode without introducing Workers or relying on shared memory.\n\n## Similar Libraries\n\n[@jariseon](https://github.com/jariseon) has implemented [a similar polyfill](https://github.com/jariseon/audioworklet-polyfill) that uses Web Workers for audio processing.\n\n## License\n\n[Apache 2.0](LICENSE)\n\n[AudioWorklet]: https://webaudio.github.io/web-audio-api/#AudioWorklet\n[AudioWorklet Examples]: https://googlechromelabs.github.io/web-audio-samples/audio-worklet/\n"
  },
  {
    "path": "demo/demo-worklet.js",
    "content": "/**\n * Copyright 2018 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n * use this file except in compliance with the License. You may obtain a copy of\n * the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\n\nclass DemoProcessor extends AudioWorkletProcessor {\n  static get parameterDescriptors () {\n    return [\n      { name: 'gain', defaultValue: 1 }\n    ];\n  }\n\n  process (inputs, outputs, parameters) {\n    const input = inputs[0];\n    const output = outputs[0];\n    const gain = parameters.gain;\n    for (let channel = 0; channel < input.length; channel++) {\n      const inputChannel = input[channel];\n      const outputChannel = output[channel];\n      for (let i = 0; i < inputChannel.length; i++) {\n        outputChannel[i] = inputChannel[i] * gain[i];\n      }\n    }\n  }\n}\n\nregisterProcessor('demo', DemoProcessor);\n"
  },
  {
    "path": "demo/demo.js",
    "content": "/**\n * Copyright 2018 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n * use this file except in compliance with the License. You may obtain a copy of\n * the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\n\n/* eslint-disable */\n\n// Copied verbatim from houdini-samples:\n// https://developers.google.com/web/updates/2017/12/audio-worklet#background_scriptprocessornode\n\nwindow.AudioContext = window.AudioContext || window.webkitAudioContext;\n\ndocument.getElementById('play').onclick = function() {\n\tvar context = new AudioContext();\n\n\t// Inject a tiny tranpiler to make IE11+ behave nicely:\n\ttry {\n\t\teval('class F {}');\n\t} catch (e) {\n\t\tcontext.audioWorklet.transpile = window.transpilerLite;\n\t}\n\n\tcontext.audioWorklet.addModule('noise-generator-worklet.js').then(() => {\n\t\tlet modulator = context.createOscillator();\n\t\tlet modGain = context.createGain();\n\t\tlet noiseGenerator = new AudioWorkletNode(context, 'noise-generator');\n\t\tnoiseGenerator.connect(context.destination);\n\t\tlet paramAmp = noiseGenerator.parameters.get('amplitude');\n\t\tmodulator.connect(modGain).connect(paramAmp);\n\t\tmodulator.frequency.value = 0.5;\n\t\tmodGain.gain.value = 0.75;\n\t\tmodulator.start();\n\t});\n\t\n\t/*\n\t// Loads module script via AudioWorklet.\n\tcontext.audioWorklet.addModule('demo-worklet.js').then(() => {\n\t\tvar oscillator = context.createOscillator();\n\n\t\t// After the resolution of module loading, an AudioWorkletNode can be constructed.\n\t\tvar demoNode = new AudioWorkletNode(context, 'demo');\n\n\t\t// AudioWorkletNode can be interoperable with other native AudioNodes.\n\t\toscillator.connect(demoNode).connect(context.destination);\n\t\toscillator.start();\n\t});\n\t*/\n}\n"
  },
  {
    "path": "demo/index.html",
    "content": "<!DOCTYPE html>\n<html>\n\t<head>\n\t\t<link rel=\"stylesheet\" href=\"style.css\">\n\t</head>\n\t<body>\n\t\t<p>\n\t\t\tThis is a demonstration of the AudioWorklet Polyfill.\n\t\t</p>\n\n\t\t<p style=\"font-style: italic;\">\n\t\t\t<strong>Note:</strong> This polyfill runs AudioWorklets in an isolated context on the main thread.\n\t\t</p>\n\n\t\t<div>\n\t\t\t<strong>Source:</strong>\n\t\t\t<pre><a href=\"./demo.js\">demo.js</a></pre>\n\t\t\t<pre><a href=\"./noise-generator-worklet.js\">noise-generator-worklet.js</a></pre>\n\t\t\t<pre><a href=\"./demo-worklet.js\">demo-worklet.js</a></pre>\n\t\t</div>\n\n\t\t<button id=\"play\">Click me!</button>\n\n\t\t<script src=\"https://cdn.rawgit.com/developit/781ef9620da8a30228b9f0c6e21fa7f6/raw/b228b3fadb2284957d040057e6701a49f522198f/transpiler-lite.umd.js\"></script>\n\t\t<script src=\"../dist/audioworklet-polyfill.js\"></script>\n\n\t\t<script src=\"demo.js\"></script>\n\t</body>\n</html>\n"
  },
  {
    "path": "demo/noise-generator-worklet.js",
    "content": "// Copyright (c) 2017 The Chromium Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style license that can be\n// found in the LICENSE file.\n\n/**\n * A noise generator example with a gain AudioParam.\n *\n * @class NoiseGenerator\n * @extends AudioWorkletProcessor\n */\nclass NoiseGenerator extends AudioWorkletProcessor {\n  static get parameterDescriptors () {\n    return [{ name: 'amplitude', defaultValue: 0.25, minValue: 0, maxValue: 1 }];\n  }\n\n  // constructor (options) {\n  //   super();\n  //   console.log('options: ', options);\n  //   console.log(this.port);\n  // }\n\n  process (inputs, outputs, parameters) {\n    const output = outputs[0];\n    const amplitude = parameters.amplitude;\n    for (let channel = 0; channel < output.length; ++channel) {\n      const outputChannel = output[channel];\n      for (let i = 0; i < outputChannel.length; ++i) {\n        outputChannel[i] = 2 * (Math.random() - 0.5) * amplitude[i];\n      }\n    }\n\n    return true;\n  }\n}\n\nregisterProcessor('noise-generator', NoiseGenerator);\n"
  },
  {
    "path": "demo/style.css",
    "content": "/**\n * Copyright 2018 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n * use this file except in compliance with the License. You may obtain a copy of\n * the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\n\nbody {\n\tfont: 16px/1.3 -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\";\n}\n\npre {\n\tmargin: 0;\n}"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"audioworklet-polyfill\",\n  \"version\": \"1.1.2\",\n  \"description\": \"AudioWorklet polyfill using the legacy ScriptProcessor API.\",\n  \"source\": \"src/index.js\",\n  \"main\": \"dist/audioworklet-polyfill.js\",\n  \"license\": \"Apache-2.0\",\n  \"author\": \"The Chromium Authors\",\n  \"contributors\": [\n    {\n      \"name\": \"Jason Miller\",\n      \"email\": \"developit@google.com\"\n    }\n  ],\n  \"repository\": \"GoogleChromeLabs/audioworklet-polyfill\",\n  \"scripts\": {\n    \"build\": \"microbundle -f iife\",\n    \"start\": \"concurrently serve \\\"microbundle watch -f iife\\\"\",\n    \"test\": \"eslint src\",\n    \"deploy\": \"cp -rf demo build && cp -r dist build/ && sed -i '' 's/\\\\.\\\\.\\\\/dist/dist/' build/index.html && gh-pages -d build && rm -rf build\"\n  },\n  \"eslintConfig\": {\n    \"extends\": \"standard\",\n    \"env\": {\n      \"browser\": true,\n      \"node\": false\n    },\n    \"rules\": {\n      \"indent\": [\n        2,\n        2\n      ],\n      \"semi\": [\n        2,\n        \"always\"\n      ],\n      \"prefer-const\": 1\n    },\n    \"globals\": {\n      \"registerProcessor\": 1,\n      \"AudioWorkletProcessor\": 1\n    }\n  },\n  \"files\": [\n    \"src\",\n    \"dist\"\n  ],\n  \"keywords\": [\n    \"audio\",\n    \"worklet\",\n    \"polyfill\",\n    \"audio worklet\",\n    \"scriptprocessornode\",\n    \"web audio\"\n  ],\n  \"devDependencies\": {\n    \"concurrently\": \"^3.5.1\",\n    \"eslint\": \"^4.19.1\",\n    \"eslint-config-standard\": \"^11.0.0\",\n    \"eslint-plugin-import\": \"^2.12.0\",\n    \"eslint-plugin-node\": \"^6.0.1\",\n    \"eslint-plugin-promise\": \"^3.7.0\",\n    \"eslint-plugin-standard\": \"^3.0.1\",\n    \"gh-pages\": \"^1.1.0\",\n    \"microbundle\": \"^0.4.4\",\n    \"serve\": \"^6.5.7\"\n  }\n}\n"
  },
  {
    "path": "src/index.js",
    "content": "/**\n * Copyright 2018 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n * use this file except in compliance with the License. You may obtain a copy of\n * the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\n\nimport { Realm } from './realm';\n\nconst PARAMS = [];\nlet nextPort;\n\nif (typeof AudioWorkletNode !== 'function' || !(\"audioWorklet\" in AudioContext.prototype)) {\n  self.AudioWorkletNode = function AudioWorkletNode (context, name, options) {\n    const processor = getProcessorsForContext(context)[name];\n    const outputChannels = options && options.outputChannelCount ? options.outputChannelCount[0] : 2;\n    const scriptProcessor = context.createScriptProcessor(undefined, 2, outputChannels);\n\n    scriptProcessor.parameters = new Map();\n    if (processor.properties) {\n      for (let i = 0; i < processor.properties.length; i++) {\n        const prop = processor.properties[i];\n        const node = context.createGain().gain;\n        node.value = prop.defaultValue;\n        // @TODO there's no good way to construct the proxy AudioParam here\n        scriptProcessor.parameters.set(prop.name, node);\n      }\n    }\n\n    const mc = new MessageChannel();\n    nextPort = mc.port2;\n    const inst = new processor.Processor(options || {});\n    nextPort = null;\n\n    scriptProcessor.port = mc.port1;\n    scriptProcessor.processor = processor;\n    scriptProcessor.instance = inst;\n    scriptProcessor.onaudioprocess = onAudioProcess;\n    return scriptProcessor;\n  };\n\n  Object.defineProperty((self.AudioContext || self.webkitAudioContext).prototype, 'audioWorklet', {\n    get () {\n      return this.$$audioWorklet || (this.$$audioWorklet = new self.AudioWorklet(this));\n    }\n  });\n\n  self.AudioWorklet = class AudioWorklet {\n    constructor (audioContext) {\n      this.$$context = audioContext;\n    }\n\n    addModule (url, options) {\n      return fetch(url).then(r => {\n        if (!r.ok) throw Error(r.status);\n        return r.text();\n      }).then(code => {\n        const context = {\n          sampleRate: this.$$context.sampleRate,\n          currentTime: this.$$context.currentTime,\n          AudioWorkletProcessor () {\n            this.port = nextPort;\n          },\n          registerProcessor: (name, Processor) => {\n            const processors = getProcessorsForContext(this.$$context);\n            processors[name] = {\n              realm,\n              context,\n              Processor,\n              properties: Processor.parameterDescriptors || []\n            };\n          }\n        };\n\n        context.self = context;\n        const realm = new Realm(context, document.documentElement);\n        realm.exec(((options && options.transpile) || String)(code));\n        return null;\n      });\n    }\n  };\n}\n\nfunction onAudioProcess (e) {\n  const parameters = {};\n  let index = -1;\n  this.parameters.forEach((value, key) => {\n    const arr = PARAMS[++index] || (PARAMS[index] = new Float32Array(this.bufferSize));\n    // @TODO proper values here if possible\n    arr.fill(value.value);\n    parameters[key] = arr;\n  });\n  this.processor.realm.exec(\n    'self.sampleRate=sampleRate=' + this.context.sampleRate + ';' +\n    'self.currentTime=currentTime=' + this.context.currentTime\n  );\n  const inputs = channelToArray(e.inputBuffer);\n  const outputs = channelToArray(e.outputBuffer);\n  this.instance.process([inputs], [outputs], parameters);\n\n  // @todo - keepalive\n  // let ret = this.instance.process([inputs], [outputs], parameters);\n  // if (ret === true) { }\n}\n\nfunction channelToArray (ch) {\n  const out = [];\n  for (let i = 0; i < ch.numberOfChannels; i++) {\n    out[i] = ch.getChannelData(i);\n  }\n  return out;\n}\n\nfunction getProcessorsForContext (audioContext) {\n  return audioContext.$$processors || (audioContext.$$processors = {});\n}\n"
  },
  {
    "path": "src/realm.js",
    "content": "/**\n * Copyright 2018 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n * use this file except in compliance with the License. You may obtain a copy of\n * the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\n\nexport function Realm (scope, parentElement) {\n  const frame = document.createElement('iframe');\n  frame.style.cssText = 'position:absolute;left:0;top:-999px;width:1px;height:1px;';\n  parentElement.appendChild(frame);\n  const win = frame.contentWindow;\n  const doc = win.document;\n  let vars = 'var window,$hook';\n  for (const i in win) {\n    if (!(i in scope) && i !== 'eval') {\n      vars += ',';\n      vars += i;\n    }\n  }\n  for (const i in scope) {\n    vars += ',';\n    vars += i;\n    vars += '=self.';\n    vars += i;\n  }\n  const script = doc.createElement('script');\n  script.appendChild(doc.createTextNode(\n    `function $hook(self,console) {\"use strict\";\n        ${vars};return function() {return eval(arguments[0])}}`\n  ));\n  doc.body.appendChild(script);\n  this.exec = win.$hook.call(scope, scope, console);\n}\n"
  }
]