webmidi_driver.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "webmidi_driver.h"
  31. #ifdef PROXY_TO_PTHREAD_ENABLED
  32. #include "core/object/callable_method_pointer.h"
  33. #endif
  34. MIDIDriverWebMidi *MIDIDriverWebMidi::get_singleton() {
  35. return static_cast<MIDIDriverWebMidi *>(MIDIDriver::get_singleton());
  36. }
  37. Error MIDIDriverWebMidi::open() {
  38. 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);
  39. if (error == ERR_UNAVAILABLE) {
  40. ERR_PRINT("Web MIDI is not supported on this browser.");
  41. }
  42. return error;
  43. }
  44. void MIDIDriverWebMidi::close() {
  45. get_singleton()->connected_input_names.clear();
  46. godot_js_webmidi_close_midi_inputs();
  47. }
  48. MIDIDriverWebMidi::~MIDIDriverWebMidi() {
  49. close();
  50. }
  51. void MIDIDriverWebMidi::set_input_names_callback(int p_size, const char **p_input_names) {
  52. Vector<String> input_names;
  53. for (int i = 0; i < p_size; i++) {
  54. input_names.append(String::utf8(p_input_names[i]));
  55. }
  56. #ifdef PROXY_TO_PTHREAD_ENABLED
  57. if (!Thread::is_main_thread()) {
  58. callable_mp_static(MIDIDriverWebMidi::_set_input_names_callback).call_deferred(input_names);
  59. return;
  60. }
  61. #endif
  62. _set_input_names_callback(input_names);
  63. }
  64. void MIDIDriverWebMidi::_set_input_names_callback(const Vector<String> &p_input_names) {
  65. get_singleton()->connected_input_names.clear();
  66. for (const String &input_name : p_input_names) {
  67. get_singleton()->connected_input_names.push_back(input_name);
  68. }
  69. }
  70. void MIDIDriverWebMidi::on_midi_message(int p_device_index, int p_status, const uint8_t *p_data, int p_data_len) {
  71. PackedByteArray data;
  72. data.resize(p_data_len);
  73. uint8_t *data_ptr = data.ptrw();
  74. for (int i = 0; i < p_data_len; i++) {
  75. data_ptr[i] = p_data[i];
  76. }
  77. #ifdef PROXY_TO_PTHREAD_ENABLED
  78. if (!Thread::is_main_thread()) {
  79. callable_mp_static(MIDIDriverWebMidi::_on_midi_message).call_deferred(p_device_index, p_status, data, p_data_len);
  80. return;
  81. }
  82. #endif
  83. _on_midi_message(p_device_index, p_status, data, p_data_len);
  84. }
  85. void MIDIDriverWebMidi::_on_midi_message(int p_device_index, int p_status, const PackedByteArray &p_data, int p_data_len) {
  86. MIDIDriver::send_event(p_device_index, p_status, p_data.ptr(), p_data_len);
  87. }