midi_driver_winmidi.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**************************************************************************/
  2. /* midi_driver_winmidi.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 WINMIDI_ENABLED
  31. #include "midi_driver_winmidi.h"
  32. #include "core/string/print_string.h"
  33. void MIDIDriverWinMidi::read(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) {
  34. if (wMsg == MIM_DATA) {
  35. receive_input_packet((uint64_t)dwParam2, (uint8_t *)&dwParam1, 3);
  36. }
  37. }
  38. Error MIDIDriverWinMidi::open() {
  39. for (UINT i = 0; i < midiInGetNumDevs(); i++) {
  40. HMIDIIN midi_in;
  41. MMRESULT res = midiInOpen(&midi_in, i, (DWORD_PTR)read, (DWORD_PTR)this, CALLBACK_FUNCTION);
  42. if (res == MMSYSERR_NOERROR) {
  43. midiInStart(midi_in);
  44. connected_sources.insert(i, midi_in);
  45. } else {
  46. char err[256];
  47. midiInGetErrorText(res, err, 256);
  48. ERR_PRINT("midiInOpen error: " + String(err));
  49. MIDIINCAPS caps;
  50. res = midiInGetDevCaps(i, &caps, sizeof(MIDIINCAPS));
  51. if (res == MMSYSERR_NOERROR) {
  52. ERR_PRINT("Can't open MIDI device \"" + String(caps.szPname) + "\", is it being used by another application?");
  53. }
  54. }
  55. }
  56. return OK;
  57. }
  58. PackedStringArray MIDIDriverWinMidi::get_connected_inputs() {
  59. PackedStringArray list;
  60. for (int i = 0; i < connected_sources.size(); i++) {
  61. HMIDIIN midi_in = connected_sources[i];
  62. UINT id = 0;
  63. MMRESULT res = midiInGetID(midi_in, &id);
  64. if (res == MMSYSERR_NOERROR) {
  65. MIDIINCAPS caps;
  66. res = midiInGetDevCaps(i, &caps, sizeof(MIDIINCAPS));
  67. if (res == MMSYSERR_NOERROR) {
  68. list.push_back(caps.szPname);
  69. }
  70. }
  71. }
  72. return list;
  73. }
  74. void MIDIDriverWinMidi::close() {
  75. for (int i = 0; i < connected_sources.size(); i++) {
  76. HMIDIIN midi_in = connected_sources[i];
  77. midiInStop(midi_in);
  78. midiInClose(midi_in);
  79. }
  80. connected_sources.clear();
  81. }
  82. MIDIDriverWinMidi::MIDIDriverWinMidi() {
  83. }
  84. MIDIDriverWinMidi::~MIDIDriverWinMidi() {
  85. close();
  86. }
  87. #endif // WINMIDI_ENABLED