1 /*
2  * This file is part of EasyRPG Player.
3  *
4  * EasyRPG Player is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * EasyRPG Player is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifdef _WIN32
19 #include "midiout_device_win32.h"
20 #include "output.h"
21 
get_error_str(MMRESULT res)22 static std::string get_error_str(MMRESULT res) {
23 	char errMsg[120];
24 	midiOutGetErrorTextA(res, errMsg, 120);
25 	return std::string(errMsg);
26 }
27 
Win32MidiOutDevice()28 Win32MidiOutDevice::Win32MidiOutDevice() {
29 	// TODO: Windows MIDI Mapper was removed in Windows 8.
30 	// This means it's impossible to change the default ("0") MIDI device
31 	// without third party software. We should allow specifying the MIDI device
32 	// ID in a config file.
33 	unsigned int device_id = 0;
34 
35 	MMRESULT err = midiOutOpen(&midi_out, device_id, 0, 0, CALLBACK_NULL);
36 	if (err != MMSYSERR_NOERROR) {
37 		Output::Debug("Win32 midiOutOpen {} failed: ({}) {}", 0, err, get_error_str(err));
38 		midi_out = nullptr;
39 		return;
40 	}
41 }
42 
~Win32MidiOutDevice()43 Win32MidiOutDevice::~Win32MidiOutDevice() {
44 	if (midi_out) {
45 		midiOutClose(midi_out);
46 		midi_out = NULL;
47 	}
48 }
49 
SendMidiMessage(uint32_t message)50 void Win32MidiOutDevice::SendMidiMessage(uint32_t message) {
51 	midiOutShortMsg(midi_out, message);
52 }
53 
SendSysExMessage(const uint8_t * data,size_t size)54 void Win32MidiOutDevice::SendSysExMessage(const uint8_t* data, size_t size) {
55 	MIDIHDR hdr;
56 	MMRESULT res;
57 	hdr.dwBufferLength = size;
58 	hdr.dwBytesRecorded = size;
59 	hdr.dwFlags = 0;
60 	hdr.lpData = (LPSTR) data;
61 	res = midiOutPrepareHeader(midi_out, &hdr, sizeof(hdr));
62 	if (res != MMSYSERR_NOERROR) {
63 		Output::Debug("Win32 midiOutPrepareHeader failed: ({}) {}", res, get_error_str(res));
64 		return;
65 	}
66 	res = midiOutLongMsg(midi_out, &hdr, sizeof(hdr));
67 	if (res != MMSYSERR_NOERROR) {
68 		Output::Debug("Win32 midiOutLongMsg failed: ({}) {}", res, get_error_str(res));
69 	}
70 	res = midiOutUnprepareHeader(midi_out, &hdr, sizeof(hdr));
71 	if (res != MMSYSERR_NOERROR) {
72 		Output::Debug("Win32 midiOutUnprepareHeader failed: ({}) {}", res, get_error_str(res));
73 	}
74 }
75 
SendMidiReset()76 void Win32MidiOutDevice::SendMidiReset() {
77 	midiOutReset(midi_out);
78 }
79 
GetName()80 std::string Win32MidiOutDevice::GetName() {
81 	return "Windows MIDI";
82 }
83 
IsInitialized() const84 bool Win32MidiOutDevice::IsInitialized() const {
85 	return midi_out != nullptr;
86 }
87 
88 #endif
89