midi_driver_alsamidi.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**************************************************************************/
  2. /* midi_driver_alsamidi.h */
  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. #ifndef MIDI_DRIVER_ALSAMIDI_H
  31. #define MIDI_DRIVER_ALSAMIDI_H
  32. #ifdef ALSAMIDI_ENABLED
  33. #include "core/os/midi_driver.h"
  34. #include "core/os/mutex.h"
  35. #include "core/os/thread.h"
  36. #include "core/templates/safe_refcount.h"
  37. #include "core/templates/vector.h"
  38. #ifdef SOWRAP_ENABLED
  39. #include "../alsa/asound-so_wrap.h"
  40. #else
  41. #include <alsa/asoundlib.h>
  42. #endif
  43. #include <stdio.h>
  44. class MIDIDriverALSAMidi : public MIDIDriver {
  45. Thread thread;
  46. Mutex mutex;
  47. class InputConnection {
  48. public:
  49. InputConnection() = default;
  50. InputConnection(snd_rawmidi_t *midi_in) :
  51. rawmidi_ptr{ midi_in } {}
  52. // Read in and parse available data, forwarding any complete messages through the driver.
  53. int read_in(MIDIDriverALSAMidi &driver, uint64_t timestamp);
  54. snd_rawmidi_t *rawmidi_ptr = nullptr;
  55. private:
  56. static const size_t MSG_BUFFER_SIZE = 3;
  57. uint8_t buffer[MSG_BUFFER_SIZE] = { 0 };
  58. size_t expected_data = 0;
  59. size_t received_data = 0;
  60. bool skipping_sys_ex = false;
  61. void parse_byte(uint8_t byte, MIDIDriverALSAMidi &driver, uint64_t timestamp);
  62. };
  63. Vector<InputConnection> connected_inputs;
  64. SafeFlag exit_thread;
  65. static void thread_func(void *p_udata);
  66. enum class MessageCategory {
  67. Data,
  68. Voice,
  69. SysExBegin,
  70. SystemCommon, // excluding System Exclusive Begin/End
  71. SysExEnd,
  72. RealTime,
  73. };
  74. // If the passed byte is a status byte, return the associated message category,
  75. // else return MessageCategory::Data.
  76. static MessageCategory msg_category(uint8_t msg_part);
  77. // Return the number of data bytes expected for the provided status byte.
  78. static size_t msg_expected_data(uint8_t status_byte);
  79. void lock() const;
  80. void unlock() const;
  81. public:
  82. virtual Error open();
  83. virtual void close();
  84. virtual PackedStringArray get_connected_inputs();
  85. MIDIDriverALSAMidi();
  86. virtual ~MIDIDriverALSAMidi();
  87. };
  88. #endif // ALSAMIDI_ENABLED
  89. #endif // MIDI_DRIVER_ALSAMIDI_H