Files
86Box/src/win/win_midi.c

138 lines
2.4 KiB
C
Raw Normal View History

2017-05-18 14:03:43 -04:00
#include <windows.h>
#include <mmsystem.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
2017-10-17 01:59:09 -04:00
#include "../86box.h"
2017-05-18 14:03:43 -04:00
#include "../config.h"
#include "../sound/midi.h"
#include "../plat.h"
#include "../plat_midi.h"
2017-05-18 14:03:43 -04:00
typedef struct
{
int midi_id;
HANDLE m_event;
HMIDIOUT midi_out_device;
MIDIHDR m_hdr;
} plat_midi_t;
plat_midi_t *pm = NULL;
2017-05-18 14:03:43 -04:00
void
plat_midi_init(void)
{
MMRESULT hr;
pm = (plat_midi_t *) malloc(sizeof(plat_midi_t));
memset(pm, 0, sizeof(plat_midi_t));
pm->midi_id = config_get_int(SYSTEM_MIDI_NAME, "midi", 0);
hr = MMSYSERR_NOERROR;
2017-05-18 14:03:43 -04:00
pm->m_event = CreateEvent(NULL, TRUE, TRUE, NULL);
hr = midiOutOpen(&pm->midi_out_device, pm->midi_id,
(uintptr_t) pm->m_event, 0, CALLBACK_EVENT);
if (hr != MMSYSERR_NOERROR) {
printf("midiOutOpen error - %08X\n", hr);
pm->midi_id = 0;
hr = midiOutOpen(&pm->midi_out_device, pm->midi_id,
(uintptr_t) pm->m_event, 0, CALLBACK_EVENT);
if (hr != MMSYSERR_NOERROR) {
printf("midiOutOpen error - %08X\n", hr);
return;
}
}
midiOutReset(pm->midi_out_device);
2017-05-18 14:03:43 -04:00
}
void
plat_midi_close(void)
2017-05-18 14:03:43 -04:00
{
if (pm) {
if (pm->midi_out_device != NULL) {
midiOutReset(pm->midi_out_device);
midiOutClose(pm->midi_out_device);
CloseHandle(pm->m_event);
}
free(pm);
pm = NULL;
}
2017-05-18 14:03:43 -04:00
}
int
plat_midi_get_num_devs(void)
2017-05-18 14:03:43 -04:00
{
return midiOutGetNumDevs();
2017-05-18 14:03:43 -04:00
}
void
plat_midi_get_dev_name(int num, char *s)
2017-05-18 14:03:43 -04:00
{
MIDIOUTCAPS caps;
2017-05-18 14:03:43 -04:00
midiOutGetDevCaps(num, &caps, sizeof(caps));
strcpy(s, caps.szPname);
2017-05-18 14:03:43 -04:00
}
void
plat_midi_play_msg(uint8_t *msg)
2017-05-18 14:03:43 -04:00
{
if (!pm)
return;
midiOutShortMsg(pm->midi_out_device, *(uint32_t *) msg);
2017-05-18 14:03:43 -04:00
}
void
plat_midi_play_sysex(uint8_t *sysex, unsigned int len)
2017-05-18 14:03:43 -04:00
{
MMRESULT result;
2017-05-18 14:03:43 -04:00
if (!pm)
return;
2017-05-18 14:03:43 -04:00
if (WaitForSingleObject(pm->m_event, 2000) == WAIT_TIMEOUT)
return;
2017-05-18 14:03:43 -04:00
midiOutUnprepareHeader(pm->midi_out_device, &pm->m_hdr, sizeof(pm->m_hdr));
2017-05-18 14:03:43 -04:00
pm->m_hdr.lpData = (char *) sysex;
pm->m_hdr.dwBufferLength = len;
pm->m_hdr.dwBytesRecorded = len;
pm->m_hdr.dwUser = 0;
2017-05-18 14:03:43 -04:00
result = midiOutPrepareHeader(pm->midi_out_device, &pm->m_hdr, sizeof(pm->m_hdr));
if (result != MMSYSERR_NOERROR)
return;
ResetEvent(pm->m_event);
result = midiOutLongMsg(pm->midi_out_device, &pm->m_hdr, sizeof(pm->m_hdr));
if (result != MMSYSERR_NOERROR) {
SetEvent(pm->m_event);
return;
}
2017-05-18 14:03:43 -04:00
}
int
plat_midi_write(uint8_t val)
2017-05-18 14:03:43 -04:00
{
return 0;
2017-05-18 14:03:43 -04:00
}