All Projects → padenot → ringbuf.js

padenot / ringbuf.js

Licence: MPL-2.0 license
Wait-free thread-safe single-consumer single-producer ring buffer using SharedArrayBuffer

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects
Makefile
30231 projects

Projects that are alternatives of or similar to ringbuf.js

RingBuffer
A RingBuffer library for Arduino
Stars: ✭ 34 (-70.18%)
Mutual labels:  ring-buffer
kprobe-template
kprobes template
Stars: ✭ 30 (-73.68%)
Mutual labels:  ring-buffer
hatrack
Fast, multi-reader, multi-writer, lockless data structures for parallel programming
Stars: ✭ 55 (-51.75%)
Mutual labels:  ring-buffer
RingBuffer
Classic ringbuffer with optional Stream interface
Stars: ✭ 53 (-53.51%)
Mutual labels:  ring-buffer
fifo-rs
A first-in-first-out for bytes, like kfifo in Linux.
Stars: ✭ 18 (-84.21%)
Mutual labels:  ring-buffer
RingBuffer
模仿 kfifo 实现的环形缓冲区
Stars: ✭ 64 (-43.86%)
Mutual labels:  ring-buffer
go-ringbuf
Lock-free MPMC Ring Buffer (Generic) for SMP, in golang. Some posts in chinese:
Stars: ✭ 43 (-62.28%)
Mutual labels:  ring-buffer
stm32-hal-libraries
Useful libraries for STM32 HAL
Stars: ✭ 29 (-74.56%)
Mutual labels:  ring-buffer
RingBuffer
基于C语言开发的轻量级环形缓冲区
Stars: ✭ 32 (-71.93%)
Mutual labels:  ring-buffer
ring-channel
Bounded MPMC channel abstraction on top of a ring buffer
Stars: ✭ 24 (-78.95%)
Mutual labels:  ring-buffer

ringbuf.js

test npm

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.

Examples and use-cases

https://ringbuf-js.netlify.app/ is a deployment of the examples in this repository with a web server that answers with the right headers for this directory, and allows the example to work. More details available at Planned changes to shared memory .

Those examples work in browsers that support both the AudioWorklet, and SharedArrayBuffer.

While most real-time audio work should happen on a real-time thread (which means inside the AudioWorkletGlobaleScope on the Web), sending (resp. receiving) audio to (from) a non-real-time thread is 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.)
  • Conversely, recording the output of an AudioContext using an AudioWorkletNode with a very high degree of reliability and extreme flexibility, possibly using Web Codecs or a WASM based solution for the encoding, and then sending the result to the network or storing it locally.
  • 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.
  • 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)

Run locally

cd public; 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).

Contribute

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

make build

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

make doc

allows rebuilding the documentation.

Compatibility

This needs the SharedArrayBuffer, so a couple of HTTP headers might need to be set on the web server serving the page.

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

As of 2021-11-09, the following browsers are compatible:

  • Firefox Desktop all current versions including current ESR
  • Firefox for Android all current versions
  • Chrome Desktop and (usually) Chromium-based browsers (for a long time)
  • Chrome for Android version 88 and later and browsers based on Chrome version 88 and later
  • Safari run in the following way: __XPC_JSC_useSharedArrayBuffer=1 open -a "Safari"
  • Safari Tech Preview's current version

License

Mozilla Public License 2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].