Path: blob/master/platform/web/js/libs/library_godot_webmidi.js
10279 views
/**************************************************************************/1/* library_godot_webmidi.js */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930const GodotWebMidi = {3132$GodotWebMidi__deps: ['$GodotRuntime'],33$GodotWebMidi: {34abortControllers: [],35isListening: false,36},3738godot_js_webmidi_open_midi_inputs__deps: ['$GodotWebMidi'],39godot_js_webmidi_open_midi_inputs__proxy: 'sync',40godot_js_webmidi_open_midi_inputs__sig: 'iiii',41godot_js_webmidi_open_midi_inputs: function (pSetInputNamesCb, pOnMidiMessageCb, pDataBuffer, dataBufferLen) {42if (GodotWebMidi.is_listening) {43return 0; // OK44}45if (!navigator.requestMIDIAccess) {46return 2; // ERR_UNAVAILABLE47}48const setInputNamesCb = GodotRuntime.get_func(pSetInputNamesCb);49const onMidiMessageCb = GodotRuntime.get_func(pOnMidiMessageCb);5051GodotWebMidi.isListening = true;52navigator.requestMIDIAccess().then((midi) => {53const inputs = [...midi.inputs.values()];54const inputNames = inputs.map((input) => input.name);5556const c_ptr = GodotRuntime.allocStringArray(inputNames);57setInputNamesCb(inputNames.length, c_ptr);58GodotRuntime.freeStringArray(c_ptr, inputNames.length);5960inputs.forEach((input, i) => {61const abortController = new AbortController();62GodotWebMidi.abortControllers.push(abortController);63input.addEventListener('midimessage', (event) => {64const status = event.data[0];65const data = event.data.slice(1);66const size = data.length;6768if (size > dataBufferLen) {69throw new Error(`data too big ${size} > ${dataBufferLen}`);70}71HEAPU8.set(data, pDataBuffer);7273onMidiMessageCb(i, status, pDataBuffer, data.length);74}, { signal: abortController.signal });75});76});7778return 0; // OK79},8081godot_js_webmidi_close_midi_inputs__deps: ['$GodotWebMidi'],82godot_js_webmidi_close_midi_inputs__proxy: 'sync',83godot_js_webmidi_close_midi_inputs__sig: 'v',84godot_js_webmidi_close_midi_inputs: function () {85for (const abortController of GodotWebMidi.abortControllers) {86abortController.abort();87}88GodotWebMidi.abortControllers = [];89GodotWebMidi.isListening = false;90},91};9293mergeInto(LibraryManager.library, GodotWebMidi);949596