Path: blob/master/drivers/winmidi/midi_driver_winmidi.cpp
9973 views
/**************************************************************************/1/* midi_driver_winmidi.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#ifdef WINMIDI_ENABLED3132#include "midi_driver_winmidi.h"3334#include "core/string/print_string.h"3536void MIDIDriverWinMidi::read(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) {37if (wMsg == MIM_DATA) {38// For MIM_DATA: dwParam1 = wMidiMessage, dwParam2 = dwTimestamp.39// Windows implementation has already unpacked running status and dropped any SysEx,40// so we can just forward straight to the event.41const uint8_t *midi_msg = (uint8_t *)&dwParam1;42send_event((int)dwInstance, midi_msg[0], &midi_msg[1], 2);43}44}4546Error MIDIDriverWinMidi::open() {47int device_index = 0;48for (UINT i = 0; i < midiInGetNumDevs(); i++) {49HMIDIIN midi_in;50MIDIINCAPS caps;5152MMRESULT open_res = midiInOpen(&midi_in, i, (DWORD_PTR)read,53(DWORD_PTR)device_index, CALLBACK_FUNCTION);54MMRESULT caps_res = midiInGetDevCaps(i, &caps, sizeof(MIDIINCAPS));5556if (open_res == MMSYSERR_NOERROR) {57midiInStart(midi_in);58connected_sources.push_back(midi_in);59if (caps_res == MMSYSERR_NOERROR) {60connected_input_names.push_back(caps.szPname);61} else {62// Should push something even if we don't get a name,63// so that the IDs line up correctly on the script side.64connected_input_names.push_back("ERROR");65}66// Only increment device index for successfully connected devices.67device_index++;68} else {69char err[256];70midiInGetErrorText(open_res, err, 256);71ERR_PRINT("midiInOpen error: " + String(err));7273if (caps_res == MMSYSERR_NOERROR) {74ERR_PRINT("Can't open MIDI device \"" + String(caps.szPname) + "\", is it being used by another application?");75}76}77}7879return OK;80}8182void MIDIDriverWinMidi::close() {83for (int i = 0; i < connected_sources.size(); i++) {84HMIDIIN midi_in = connected_sources[i];85midiInStop(midi_in);86midiInClose(midi_in);87}88connected_sources.clear();89connected_input_names.clear();90}9192MIDIDriverWinMidi::~MIDIDriverWinMidi() {93close();94}9596#endif // WINMIDI_ENABLED979899