midi_driver_winmidi.cpp 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // For MIM_DATA: dwParam1 = wMidiMessage, dwParam2 = dwTimestamp.
  36. // Windows implementation has already unpacked running status and dropped any SysEx,
  37. // so we can just forward straight to the event.
  38. const uint8_t *midi_msg = (uint8_t *)&dwParam1;
  39. send_event((int)dwInstance, midi_msg[0], &midi_msg[1], 2);
  40. }
  41. }
  42. Error MIDIDriverWinMidi::open() {
  43. int device_index = 0;
  44. for (UINT i = 0; i < midiInGetNumDevs(); i++) {
  45. HMIDIIN midi_in;
  46. MIDIINCAPS caps;
  47. MMRESULT open_res = midiInOpen(&midi_in, i, (DWORD_PTR)read,
  48. (DWORD_PTR)device_index, CALLBACK_FUNCTION);
  49. MMRESULT caps_res = midiInGetDevCaps(i, &caps, sizeof(MIDIINCAPS));
  50. if (open_res == MMSYSERR_NOERROR) {
  51. midiInStart(midi_in);
  52. connected_sources.push_back(midi_in);
  53. if (caps_res == MMSYSERR_NOERROR) {
  54. connected_input_names.push_back(caps.szPname);
  55. } else {
  56. // Should push something even if we don't get a name,
  57. // so that the IDs line up correctly on the script side.
  58. connected_input_names.push_back("ERROR");
  59. }
  60. // Only increment device index for successfully connected devices.
  61. device_index++;
  62. } else {
  63. char err[256];
  64. midiInGetErrorText(open_res, err, 256);
  65. ERR_PRINT("midiInOpen error: " + String(err));
  66. if (caps_res == MMSYSERR_NOERROR) {
  67. ERR_PRINT("Can't open MIDI device \"" + String(caps.szPname) + "\", is it being used by another application?");
  68. }
  69. }
  70. }
  71. return OK;
  72. }
  73. void MIDIDriverWinMidi::close() {
  74. for (int i = 0; i < connected_sources.size(); i++) {
  75. HMIDIIN midi_in = connected_sources[i];
  76. midiInStop(midi_in);
  77. midiInClose(midi_in);
  78. }
  79. connected_sources.clear();
  80. connected_input_names.clear();
  81. }
  82. MIDIDriverWinMidi::~MIDIDriverWinMidi() {
  83. close();
  84. }
  85. #endif // WINMIDI_ENABLED