GithubHelp home page GithubHelp logo

haywirez / ringbuf.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from padenot/ringbuf.js

0.0 1.0 0.0 49 KB

Wait-free thread-safe single-consumer single-producer ring buffer using SharedArrayBuffer

Home Page: https://paul.cx/ringbuf.js/

License: Mozilla Public License 2.0

HTML 6.04% JavaScript 93.96%

ringbuf.js's Introduction

ringbuf.js

A thread-safe wait-free single-consumer single-producer ring buffer for the web, and some utilities.

The main files of this library:

  • js/ringbuf.js: base data structure, implementing the ring-buffer. This is intentionally heavily commented.
  • js/audioqueue.js: wrapper for audio data streaming, without using postMessage.
  • js/param.js: wrapper for parameter changes, allowing to send pairs of index and value without using postMessage.

This library contains an example, explained below, with the following files:

  • example/app.js: example usage, main thread side (send audio and param changes to the real-time thread)
  • example/processor.js: example usage, real-time thread side (receive audio and parameter changes from the main thread)
  • example/utils.js: helper to load multiple files in an AudioWorkletGlobalScope
  • example/index.js: vendored and built version of the library to have the example easily online.

Demo and use-cases

A sine wave is generated on the main thread, sent to the audio thread, played out. The frequency of this sine wave is controllable from the main thread. The amplitude of this sine wave is also controllable: the amplitude parameter is communicated lock-free to the real-time thread.

Yes this is a contrived example, people should not use the main thread or any other non-real-time thread this if they can. However sending audio from a non-real-time thread to a real-time thread is sometimes useful:

  • Decoding a audio codecs that browsers don't support natively in a web worker, sending the PCM to an AudioWorklet (no need to fiddle with AudioBufferSourceNode, etc.)
  • Implementing emulators for (e.g.) old consoles that only had one execution thread and did everything on the same CPU
  • Porting code that is using a push-style audio API (SDL_QueueAudio) without having to refactor everything.

The opposite (recording the input of an AudioWorklet) is very useful:

  • Implement off-main-thread off-real-time-thread audio analysis (streaming the real-time audio data to a web worker, visualizing it using an OffscreenCanvas, shielding the audio processing and visualization from main thread load)
  • Implement off-main-thread off-real-time thread encoding of audio data in a codec not supported by MediaRecorder, or maybe with more flexibility.

Run locally

cd example; node server.js

This is a simple web server that sets the right headers to use SharedArrayBuffer (see Planned changes to shared memory on MDN), and uses https (however this requires a bit of manual setup, mkcert can be useful, read the source, it's not particularly complicated).

Contribute

Please do (just open an issue or send a PR).

npm run-script build

allows running the build step and copying the file to allow the example to work.

License

Mozilla Public License 2.0

ringbuf.js's People

Contributors

padenot avatar chrisguttandin avatar fippo avatar

Watchers

 avatar

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.