Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/winmidi/midi_driver_winmidi.cpp
9973 views
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
31
#ifdef WINMIDI_ENABLED
32
33
#include "midi_driver_winmidi.h"
34
35
#include "core/string/print_string.h"
36
37
void MIDIDriverWinMidi::read(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) {
38
if (wMsg == MIM_DATA) {
39
// For MIM_DATA: dwParam1 = wMidiMessage, dwParam2 = dwTimestamp.
40
// Windows implementation has already unpacked running status and dropped any SysEx,
41
// so we can just forward straight to the event.
42
const uint8_t *midi_msg = (uint8_t *)&dwParam1;
43
send_event((int)dwInstance, midi_msg[0], &midi_msg[1], 2);
44
}
45
}
46
47
Error MIDIDriverWinMidi::open() {
48
int device_index = 0;
49
for (UINT i = 0; i < midiInGetNumDevs(); i++) {
50
HMIDIIN midi_in;
51
MIDIINCAPS caps;
52
53
MMRESULT open_res = midiInOpen(&midi_in, i, (DWORD_PTR)read,
54
(DWORD_PTR)device_index, CALLBACK_FUNCTION);
55
MMRESULT caps_res = midiInGetDevCaps(i, &caps, sizeof(MIDIINCAPS));
56
57
if (open_res == MMSYSERR_NOERROR) {
58
midiInStart(midi_in);
59
connected_sources.push_back(midi_in);
60
if (caps_res == MMSYSERR_NOERROR) {
61
connected_input_names.push_back(caps.szPname);
62
} else {
63
// Should push something even if we don't get a name,
64
// so that the IDs line up correctly on the script side.
65
connected_input_names.push_back("ERROR");
66
}
67
// Only increment device index for successfully connected devices.
68
device_index++;
69
} else {
70
char err[256];
71
midiInGetErrorText(open_res, err, 256);
72
ERR_PRINT("midiInOpen error: " + String(err));
73
74
if (caps_res == MMSYSERR_NOERROR) {
75
ERR_PRINT("Can't open MIDI device \"" + String(caps.szPname) + "\", is it being used by another application?");
76
}
77
}
78
}
79
80
return OK;
81
}
82
83
void MIDIDriverWinMidi::close() {
84
for (int i = 0; i < connected_sources.size(); i++) {
85
HMIDIIN midi_in = connected_sources[i];
86
midiInStop(midi_in);
87
midiInClose(midi_in);
88
}
89
connected_sources.clear();
90
connected_input_names.clear();
91
}
92
93
MIDIDriverWinMidi::~MIDIDriverWinMidi() {
94
close();
95
}
96
97
#endif // WINMIDI_ENABLED
98
99