Skip to content

Instantly share code, notes, and snippets.

@mlms13
Created April 8, 2020 21:27
Show Gist options
  • Save mlms13/2c6afc7f70cfda896896b7c0ab96fd61 to your computer and use it in GitHub Desktop.
Save mlms13/2c6afc7f70cfda896896b7c0ab96fd61 to your computer and use it in GitHub Desktop.
Reason Web Audio
module AudioDestinationNode = {
type t;
};
module OscillatorNode = {
type t;
[@bs.send]
external connect: (t, AudioDestinationNode.t) => unit = "connect";
};
module AudioContext = {
type t;
[@bs.scope "window"] [@bs.new]
external unsafeMakeAudioContext: unit => t = "AudioContext";
[@bs.scope "window"] [@bs.new]
external unsafeMakeWebkitAudioContext: unit => t = "webkitAudioContext";
[@bs.get]
external getDestination: t => AudioDestinationNode.t = "destination";
[@bs.send]
external createOscillator: t => OscillatorNode.t = "createOscillator";
let make: unit => option(t) =
() =>
try (Some(unsafeMakeAudioContext())) {
| _ =>
try (Some(unsafeMakeWebkitAudioContext())) {
| _ => None
}
};
};
switch (AudioContext.make()) {
| None => Js.log("Uh oh, no audio context")
| Some(ctx) =>
open AudioContext;
open OscillatorNode;
let oscillatorNode = ctx->createOscillator;
let destination = ctx->getDestination;
oscillatorNode->connect(destination);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment