Created
May 24, 2017 02:07
-
-
Save bassjacob/e9d59522c4e6b6977db622553ae933b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Containers; | |
type destination; | |
type pitch = C | Cs | D | Eb | E | F | Fs | G | Gs | A | Bb | B; | |
module Pitches = Map.Make({ type t = pitch; let compare = compare; }); | |
let pitches = Pitches.fromList | |
[ | |
(C, 16.35), | |
(Cs, 17.32), | |
(D, 18.35), | |
(Eb, 19.45), | |
(E, 20.60), | |
(F, 21.83), | |
(Fs, 23.12), | |
(G, 24.50), | |
(Gs, 25.96), | |
(A, 27.50), | |
(Bb, 29.14), | |
(B, 30.87), | |
]; | |
let rec pow b e => { | |
e == 0.0 ? 1.0 : b *. pow b (e -. 1.0); | |
}; | |
let getPitch pitch octave => { | |
(pow 2.0 octave) *. Pitches.find pitch pitches; | |
}; | |
type oscillator = Js.t { | |
. | |
frequency: Js.t { | |
. | |
value [@bs.set] [@bs.get]: float | |
}, | |
connect [@bs.meth] : destination => unit, | |
_type [@bs.set] [@bs.get] : string, | |
start [@bs.meth] : float => unit, | |
stop [@bs.meth] : float => unit | |
}; | |
type audioContext = Js.t { | |
. | |
destination: destination, | |
createOscillator [@bs.meth] : unit => oscillator | |
}; | |
external audioContext : unit => audioContext = "window.AudioContext" [@@bs.new]; | |
external createOscillator : audioContext => unit => oscillator = "" [@@bs.send]; | |
let c = audioContext (); | |
let play note => { | |
let o = c##createOscillator (); | |
o##_type #= "square"; | |
o##frequency##value #= (getPitch note 4.0); | |
o##connect (c##destination); | |
o##start (0.25); | |
o; | |
}; | |
let o = play B; | |
Js_global.setTimeout (fun _ => {o##frequency##value #= (getPitch C 4.0)}) 1000; | |
Js_global.setTimeout (fun _ => {o##stop 0.0}) 10000; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment