Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/web/webmidi_driver.cpp
10277 views
1
/**************************************************************************/
2
/* webmidi_driver.cpp */
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
#include "webmidi_driver.h"
32
33
#ifdef PROXY_TO_PTHREAD_ENABLED
34
#include "core/object/callable_method_pointer.h"
35
#endif
36
37
MIDIDriverWebMidi *MIDIDriverWebMidi::get_singleton() {
38
return static_cast<MIDIDriverWebMidi *>(MIDIDriver::get_singleton());
39
}
40
41
Error MIDIDriverWebMidi::open() {
42
Error error = (Error)godot_js_webmidi_open_midi_inputs(&MIDIDriverWebMidi::set_input_names_callback, &MIDIDriverWebMidi::on_midi_message, _event_buffer, MIDIDriverWebMidi::MAX_EVENT_BUFFER_LENGTH);
43
if (error == ERR_UNAVAILABLE) {
44
ERR_PRINT("Web MIDI is not supported on this browser.");
45
}
46
return error;
47
}
48
49
void MIDIDriverWebMidi::close() {
50
get_singleton()->connected_input_names.clear();
51
godot_js_webmidi_close_midi_inputs();
52
}
53
54
MIDIDriverWebMidi::~MIDIDriverWebMidi() {
55
close();
56
}
57
58
void MIDIDriverWebMidi::set_input_names_callback(int p_size, const char **p_input_names) {
59
Vector<String> input_names;
60
for (int i = 0; i < p_size; i++) {
61
input_names.append(String::utf8(p_input_names[i]));
62
}
63
#ifdef PROXY_TO_PTHREAD_ENABLED
64
if (!Thread::is_main_thread()) {
65
callable_mp_static(MIDIDriverWebMidi::_set_input_names_callback).call_deferred(input_names);
66
return;
67
}
68
#endif
69
70
_set_input_names_callback(input_names);
71
}
72
73
void MIDIDriverWebMidi::_set_input_names_callback(const Vector<String> &p_input_names) {
74
get_singleton()->connected_input_names.clear();
75
for (const String &input_name : p_input_names) {
76
get_singleton()->connected_input_names.push_back(input_name);
77
}
78
}
79
80
void MIDIDriverWebMidi::on_midi_message(int p_device_index, int p_status, const uint8_t *p_data, int p_data_len) {
81
PackedByteArray data;
82
data.resize(p_data_len);
83
uint8_t *data_ptr = data.ptrw();
84
for (int i = 0; i < p_data_len; i++) {
85
data_ptr[i] = p_data[i];
86
}
87
#ifdef PROXY_TO_PTHREAD_ENABLED
88
if (!Thread::is_main_thread()) {
89
callable_mp_static(MIDIDriverWebMidi::_on_midi_message).call_deferred(p_device_index, p_status, data, p_data_len);
90
return;
91
}
92
#endif
93
_on_midi_message(p_device_index, p_status, data, p_data_len);
94
}
95
96
void MIDIDriverWebMidi::_on_midi_message(int p_device_index, int p_status, const PackedByteArray &p_data, int p_data_len) {
97
MIDIDriver::send_event(p_device_index, p_status, p_data.ptr(), p_data_len);
98
}
99
100