Skip to content

Instantly share code, notes, and snippets.

@danyx23
Last active August 14, 2016 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danyx23/a1f88da8913299bdb153a83979234878 to your computer and use it in GitHub Desktop.
Save danyx23/a1f88da8913299bdb153a83979234878 to your computer and use it in GitHub Desktop.
-- this is early brainstorm stage. Writing to arraybuffers is a mutation and we don't have those in Elm atm
-- aside from via Tasks/Cmds. Can we model mutations as something you build up and then "perform" all at once?
-- I'm not sure we could keep all of Elms guarantees (debugging, replay of actions, ...), but at least this API
-- would try to make the mutations happen in a single place? Or maybe performMutations will have to be a Cmd
-- even though it is all native JS code to satisfy the self imposed Elm constraints?
-- new library functions:
createArrayBuffer : Int -> ArrayBufferMutation
toFloat32Array : ArrayBufferMutation -> Float32ArrayMutation -- creates a typed float32 array
float32map : (Float -> Int -> Float) -> Float32ArrayMutation -> Float32ArrayMutation
fromFloat32Array : Float32ArrayMutation -> ArrayBufferMutation -- noop in JS, exists to satisfy the type checker
performMutations : ArrayBufferMutation -> ArrayBuffer
type alias Frequency = Float
type alias Duration = Float
type alias SampleRate = Int
createPureTone : SampleRate -> Frequency -> Duration -> ArrayBuffer
createPureTone sampleRate frequency duration =
let
mutations =
createArrayBuffer (sampleRate * duration)
|> toFloat32Array
|> float32map (\value index -> sin <| index / sampleRate / (2 * pi) * frequency) -- I'm tired, I hope this makes sense
|> fromFloat32Array
in
performMutations mutations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment