0.2.0-dev Download ZIP
94 lines ·4.2 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* library_godot_webmidi.js */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 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; // OK |
| 45 | } |
| 46 | if (!navigator.requestMIDIAccess) { |
| 47 | return 2; // ERR_UNAVAILABLE |
| 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; // OK |
| 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); |