1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use js_sys::Array;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;
use web_sys::MidiOptions;

pub struct MidiAccess {
    access: web_sys::MidiAccess,
}

impl MidiAccess {
    pub async fn get_access() -> Self {
        let window = web_sys::window().expect("no global `window` exists");

        let access: web_sys::MidiAccess = JsFuture::from(
            window
                .navigator()
                .request_midi_access_with_options(MidiOptions::new().sysex(true).software(true))
                .unwrap(),
        )
        .await
        .unwrap()
        .into();

        Self { access }
    }

    pub fn inputs(&self) -> Vec<MidiInput> {
        let mut result: Vec<MidiInput> = Vec::new();

        for entry in js_sys::try_iter(&self.access.inputs()).unwrap().unwrap() {
            let array: Array = entry.unwrap().into();
            result.push(MidiInput {
                input: array.get(1).into(),
            });
        }

        result
    }

    pub fn outputs(&self) -> Vec<MidiOutput> {
        let mut result: Vec<MidiOutput> = Vec::new();

        for entry in js_sys::try_iter(&self.access.inputs()).unwrap().unwrap() {
            let array: Array = entry.unwrap().into();
            result.push(MidiOutput {
                output: array.get(1).into(),
            });
        }

        result
    }
}

pub struct MidiInput {
    input: web_sys::MidiInput,
}

impl MidiInput {
    // pub fn onmidimessage(&self) -> ... {
    //     todo!()
    // }

    // pub fn set_onmidimessage(&self) -> ... {
    //     todo!()
    // }

    pub fn id(&self) -> String {
        self.input.id()
    }

    pub fn manufacturer(&self) -> Option<String> {
        self.input.manufacturer()
    }

    pub fn name(&self) -> Option<String> {
        self.input.name()
    }

    pub fn version(&self) -> Option<String> {
        self.input.version()
    }

    // pub fn state(&self) -> MidiPortDeviceState {
    //     self.input.state()
    // }

    // pub fn connection(&self) -> MidiPortConnectionState {
    //     self.input.connection()
    // }

    // pub fn onstatechange(&self) -> ... {
    //     todo!()
    // }

    // pub fn set_onstatechange(&self) -> ... {
    //     todo!()
    // }

    pub async fn open(&self) -> &Self {
        JsFuture::from(self.input.open()).await.unwrap();
        self
    }

    pub async fn close(&self) -> &Self {
        JsFuture::from(self.input.close()).await.unwrap();
        self
    }
}

pub struct MidiOutput {
    output: web_sys::MidiOutput,
}

impl MidiOutput {
    pub fn clear(&self) {
        self.output.clear()
    }

    // TODO: fix input and output types to be something sensible
    pub fn send(&self, data: &JsValue) -> Result<(), JsValue> {
        self.output.send(data)
    }

    // TODO: fix input and output types to be something sensible
    pub fn send_with_timestamp(&self, data: &JsValue, timestamp: f64) -> Result<(), JsValue> {
        self.output.send_with_timestamp(data, timestamp)
    }

    pub fn id(&self) -> String {
        self.output.id()
    }

    pub fn manufacturer(&self) -> Option<String> {
        self.output.manufacturer()
    }

    pub fn name(&self) -> Option<String> {
        self.output.name()
    }

    pub fn version(&self) -> Option<String> {
        self.output.version()
    }

    // pub fn state(&self) -> MidiPortDeviceState {
    //     self.output.state()
    // }

    // pub fn connection(&self) -> MidiPortConnectionState {
    //     self.output.connection()
    // }

    // pub fn onstatechange(&self) -> ... {
    //     todo!()
    // }

    // pub fn set_onstatechange(&self) -> ... {
    //     todo!()
    // }

    pub async fn open(&self) -> &Self {
        JsFuture::from(self.output.open()).await.unwrap();
        self
    }

    pub async fn close(&self) -> &Self {
        JsFuture::from(self.output.close()).await.unwrap();
        self
    }
}