| 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 |
const GodotWebMidi = { |
| 32 |
|
| 33 |
$GodotWebMidi__deps: ['$GodotRuntime'], |
| 34 |
$GodotWebMidi: { |
| 35 |
abortControllers: [], |
| 36 |
isListening: false, |
| 37 |
}, |
| 38 |
|
| 39 |
godot_js_webmidi_open_midi_inputs__deps: ['$GodotWebMidi'], |
| 40 |
godot_js_webmidi_open_midi_inputs__proxy: 'sync', |
| 41 |
godot_js_webmidi_open_midi_inputs__sig: 'iiii', |
| 42 |
godot_js_webmidi_open_midi_inputs: function (pSetInputNamesCb, pOnMidiMessageCb, pDataBuffer, dataBufferLen) { |
| 43 |
if (GodotWebMidi.is_listening) { |
| 44 |
return 0; |
| 45 |
} |
| 46 |
if (!navigator.requestMIDIAccess) { |
| 47 |
return 2; |
| 48 |
} |
| 49 |
const setInputNamesCb = GodotRuntime.get_func(pSetInputNamesCb); |
| 50 |
const onMidiMessageCb = GodotRuntime.get_func(pOnMidiMessageCb); |
| 51 |
|
| 52 |
GodotWebMidi.isListening = true; |
| 53 |
navigator.requestMIDIAccess().then((midi) => { |
| 54 |
const inputs = [...midi.inputs.values()]; |
| 55 |
const inputNames = inputs.map((input) => input.name); |
| 56 |
|
| 57 |
const c_ptr = GodotRuntime.allocStringArray(inputNames); |
| 58 |
setInputNamesCb(inputNames.length, c_ptr); |
| 59 |
GodotRuntime.freeStringArray(c_ptr, inputNames.length); |
| 60 |
|
| 61 |
inputs.forEach((input, i) => { |
| 62 |
const abortController = new AbortController(); |
| 63 |
GodotWebMidi.abortControllers.push(abortController); |
| 64 |
input.addEventListener('midimessage', (event) => { |
| 65 |
const status = event.data[0]; |
| 66 |
const data = event.data.slice(1); |
| 67 |
const size = data.length; |
| 68 |
|
| 69 |
if (size > dataBufferLen) { |
| 70 |
throw new Error(`data too big ${size} > ${dataBufferLen}`); |
| 71 |
} |
| 72 |
HEAPU8.set(data, pDataBuffer); |
| 73 |
|
| 74 |
onMidiMessageCb(i, status, pDataBuffer, data.length); |
| 75 |
}, { signal: abortController.signal }); |
| 76 |
}); |
| 77 |
}); |
| 78 |
|
| 79 |
return 0; |
| 80 |
}, |
| 81 |
|
| 82 |
godot_js_webmidi_close_midi_inputs__deps: ['$GodotWebMidi'], |
| 83 |
godot_js_webmidi_close_midi_inputs__proxy: 'sync', |
| 84 |
godot_js_webmidi_close_midi_inputs__sig: 'v', |
| 85 |
godot_js_webmidi_close_midi_inputs: function () { |
| 86 |
for (const abortController of GodotWebMidi.abortControllers) { |
| 87 |
abortController.abort(); |
| 88 |
} |
| 89 |
GodotWebMidi.abortControllers = []; |
| 90 |
GodotWebMidi.isListening = false; |
| 91 |
}, |
| 92 |
}; |
| 93 |
|
| 94 |
mergeInto(LibraryManager.library, GodotWebMidi); |