midi_driver_alsamidi.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**************************************************************************/
  2. /* midi_driver_alsamidi.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. #ifdef ALSAMIDI_ENABLED
  31. #include "midi_driver_alsamidi.h"
  32. #include "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. #include <errno.h>
  35. MIDIDriverALSAMidi::InputConnection::InputConnection(int p_device_index,
  36. snd_rawmidi_t *p_rawmidi) :
  37. parser(p_device_index), rawmidi_ptr(p_rawmidi) {}
  38. void MIDIDriverALSAMidi::InputConnection::read() {
  39. int read_count;
  40. do {
  41. uint8_t buffer[32];
  42. read_count = snd_rawmidi_read(rawmidi_ptr, buffer, sizeof(buffer));
  43. if (read_count < 0) {
  44. if (read_count != -EAGAIN) {
  45. ERR_PRINT("snd_rawmidi_read error: " + String(snd_strerror(read_count)));
  46. }
  47. } else {
  48. for (int i = 0; i < read_count; i++) {
  49. parser.parse_fragment(buffer[i]);
  50. }
  51. }
  52. } while (read_count > 0);
  53. }
  54. void MIDIDriverALSAMidi::thread_func(void *p_udata) {
  55. MIDIDriverALSAMidi *md = static_cast<MIDIDriverALSAMidi *>(p_udata);
  56. while (!md->exit_thread.is_set()) {
  57. md->lock();
  58. for (InputConnection &conn : md->connected_inputs) {
  59. conn.read();
  60. }
  61. md->unlock();
  62. OS::get_singleton()->delay_usec(1000);
  63. }
  64. }
  65. Error MIDIDriverALSAMidi::open() {
  66. void **hints;
  67. if (snd_device_name_hint(-1, "rawmidi", &hints) < 0) {
  68. return ERR_CANT_OPEN;
  69. }
  70. lock();
  71. int device_index = 0;
  72. for (void **h = hints; *h != nullptr; h++) {
  73. char *name = snd_device_name_get_hint(*h, "NAME");
  74. if (name != nullptr) {
  75. snd_rawmidi_t *midi_in;
  76. int ret = snd_rawmidi_open(&midi_in, nullptr, name, SND_RAWMIDI_NONBLOCK);
  77. if (ret >= 0) {
  78. // Get display name.
  79. snd_rawmidi_info_t *info;
  80. snd_rawmidi_info_malloc(&info);
  81. snd_rawmidi_info(midi_in, info);
  82. connected_input_names.push_back(snd_rawmidi_info_get_name(info));
  83. snd_rawmidi_info_free(info);
  84. connected_inputs.push_back(InputConnection(device_index, midi_in));
  85. // Only increment device_index for successfully connected devices.
  86. device_index++;
  87. }
  88. }
  89. if (name != nullptr) {
  90. free(name);
  91. }
  92. }
  93. snd_device_name_free_hint(hints);
  94. unlock();
  95. exit_thread.clear();
  96. thread.start(MIDIDriverALSAMidi::thread_func, this);
  97. return OK;
  98. }
  99. void MIDIDriverALSAMidi::close() {
  100. exit_thread.set();
  101. if (thread.is_started()) {
  102. thread.wait_to_finish();
  103. }
  104. for (const InputConnection &conn : connected_inputs) {
  105. snd_rawmidi_close(conn.rawmidi_ptr);
  106. }
  107. connected_inputs.clear();
  108. connected_input_names.clear();
  109. }
  110. void MIDIDriverALSAMidi::lock() const {
  111. mutex.lock();
  112. }
  113. void MIDIDriverALSAMidi::unlock() const {
  114. mutex.unlock();
  115. }
  116. MIDIDriverALSAMidi::MIDIDriverALSAMidi() {
  117. exit_thread.clear();
  118. }
  119. MIDIDriverALSAMidi::~MIDIDriverALSAMidi() {
  120. close();
  121. }
  122. #endif // ALSAMIDI_ENABLED