Skip to content

Instantly share code, notes, and snippets.

@magicmonty
Last active December 1, 2017 10:39
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 magicmonty/51ae5e5c9393fbca154180995e8649bb to your computer and use it in GitHub Desktop.
Save magicmonty/51ae5e5c9393fbca154180995e8649bb to your computer and use it in GitHub Desktop.
Fable MIDI test
open Fable.Core
open Fable.Core.JsInterop
open Fable.Import
[<StringEnum>]
type MIDIPortType =
| Input
| Output
[<StringEnum>]
type MIDIPortDeviceState =
| Disconnected
| Connected
[<StringEnum>]
type MIDIPortConnectionState =
| Open
| Closed
| Pending
type MIDIPort =
[<Emit("$0.id")>]
abstract Id: string with get
[<Emit("$0.manufacturer")>]
abstract Manufacturer: string option with get
[<Emit("$0.name")>]
abstract Name: string option with get
[<Emit("$0.type")>]
abstract Type: MIDIPortType with get
[<Emit("$0.version")>]
abstract Version: string option with get
[<Emit("$0.state")>]
abstract State: MIDIPortDeviceState with get
[<Emit("$0.connection")>]
abstract Connection: MIDIPortConnectionState with get
[<Emit("$0.onstatechange")>]
abstract OnStateChange : obj with get, set
[<Emit("$0.open()")>]
abstract Open : unit -> JS.Promise<MIDIPort>
[<Emit("$0.close()")>]
abstract Close : unit -> JS.Promise<MIDIPort>
type MIDIInput =
inherit MIDIPort
[<Emit("$0.onmidimessage")>]
abstract OnMIDIMessage : obj with get, set
type MIDIOutput =
inherit MIDIPort
[<Emit("$0.send($1, $2)")>]
abstract Send: (byte list*double) -> unit
[<Emit("$0.clear()")>]
abstract Clear: unit -> unit
type MIDIInputMap = (string*MIDIInput) list
type MIDIOutputMap = (string*MIDIOutput) list
type MIDIAccess =
[<Emit("$0.inputs")>]
abstract Inputs : MIDIInputMap with get
[<Emit("$0.outputs")>]
abstract Outputs : MIDIOutputMap with get
[<Emit("$0.onstatechange")>]
abstract OnStateChange : obj with get, set
[<Emit("$0.sysexEnabled")>]
abstract SysexEnabled : bool with get, set
type MIDIOption =
| Sysex of bool
| Software of bool
module Intern =
[<Emit("navigator.requestMIDIAccess($0)")>]
let requestMIDIAccess obj : JS.Promise<MIDIAccess> = jsNative
module MIDI =
let requestAccess (options: MIDIOption list) =
Intern.requestMIDIAccess (keyValueList CaseRules.LowerFirst options)
let onMIDISuccess (midiAccess: MIDIAccess) =
let key, port = midiAccess.Outputs.[0]
printfn "%A" port.Name
let onMIDIError (ex: obj) =
printfn "Error: %s" (unbox ex?message)
let promise f = System.Func<_, _> f
let run f = (f >> U2.Case1) |> promise
(MIDI.requestAccess []).``then``(onfulfilled=run onMIDISuccess, onrejected=run onMIDIError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment