rescript-web-audio
v0.4.0
Published
Zero-cost bindings for the Web Audio API
Downloads
87
Readme
rescript-web-audio
Zero-cost ReScript bindings for the Web Audio API
Installation
pnpm install rescript-web-audio
Then add rescript-web-audio
to bs-dependencies
in rescript.json
.
{
"name": "my-project",
"bs-dependencies": ["@rescript/core", "rescript-web-audio"]
}
Usage
This library is heavily inspired by the design of rescript-webapi and ocaml-ffmpeg, which both use phantom types and implementation inheritance to translate an Object Oriented API into the functional paradigm of OCaml. Here is an example of what it looks like in the wild:
open WebAudio
// Constructors become ModuleName.make().
let ctx = AudioContext.make()
let osc = OscillatorNode.make(
ctx,
~options={
// Strings with a finite number of possible values become polymorphic variants.
type_: #sine,
frequency: 440.0,
},
)
let gain = GainNode.make(
ctx,
~options={
gain: 0.3,
},
)
// Methods become functions.
let _ = OscillatorNode.connectNode(osc, gain)
// Fields become getters (and setters when appropriate).
let _ = GainNode.connectNode(gain, AudioContext.getDestination(ctx))
OscillatorNode.start(osc)