2016-03-01 15:47:10 +00:00
|
|
|
/*
|
|
|
|
** music_win_mididevice.cpp
|
|
|
|
** Provides a WinMM implementation of a MIDI output device.
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2008 Randy Heit
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
2019-07-14 14:07:18 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#include <mmsystem.h>
|
2019-09-28 14:50:00 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <mutex>
|
|
|
|
#include <assert.h>
|
2017-03-10 15:12:52 +00:00
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
// HEADER FILES ------------------------------------------------------------
|
|
|
|
|
2019-09-28 14:50:00 +00:00
|
|
|
#include "mididevice.h"
|
2019-09-28 16:32:25 +00:00
|
|
|
#include "zmusic/m_swap.h"
|
|
|
|
#include "zmusic/mus2midi.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
#ifndef __GNUC__
|
|
|
|
#include <mmdeviceapi.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// MACROS ------------------------------------------------------------------
|
|
|
|
|
|
|
|
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
|
|
|
|
|
|
|
|
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
|
|
|
|
|
|
|
|
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
|
|
|
|
|
|
|
|
static bool IgnoreMIDIVolume(UINT id);
|
|
|
|
|
|
|
|
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
|
|
|
|
|
|
|
|
// PRIVATE DATA DEFINITIONS ------------------------------------------------
|
2017-03-10 15:12:52 +00:00
|
|
|
// WinMM implementation of a MIDI output device -----------------------------
|
|
|
|
|
|
|
|
class WinMIDIDevice : public MIDIDevice
|
|
|
|
{
|
|
|
|
public:
|
2019-09-28 14:50:00 +00:00
|
|
|
WinMIDIDevice(int dev_id, bool precache);
|
2017-03-10 15:12:52 +00:00
|
|
|
~WinMIDIDevice();
|
2019-09-28 08:00:22 +00:00
|
|
|
int Open();
|
2017-03-10 15:12:52 +00:00
|
|
|
void Close();
|
|
|
|
bool IsOpen() const;
|
|
|
|
int GetTechnology() const;
|
|
|
|
int SetTempo(int tempo);
|
|
|
|
int SetTimeDiv(int timediv);
|
|
|
|
int StreamOut(MidiHeader *data);
|
|
|
|
int StreamOutSync(MidiHeader *data);
|
|
|
|
int Resume();
|
|
|
|
void Stop();
|
|
|
|
int PrepareHeader(MidiHeader *data);
|
|
|
|
int UnprepareHeader(MidiHeader *data);
|
|
|
|
bool FakeVolume();
|
|
|
|
bool Pause(bool paused);
|
2017-03-10 18:03:58 +00:00
|
|
|
void InitPlayback() override;
|
|
|
|
bool Update() override;
|
2017-03-10 15:12:52 +00:00
|
|
|
void PrecacheInstruments(const uint16_t *instruments, int count);
|
2017-03-10 18:03:58 +00:00
|
|
|
DWORD PlayerLoop();
|
2018-11-01 09:22:21 +00:00
|
|
|
bool CanHandleSysex() const override
|
|
|
|
{
|
|
|
|
// No Sysex for GS synth.
|
|
|
|
return VolumeWorks;
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:12:52 +00:00
|
|
|
|
2017-03-10 18:03:58 +00:00
|
|
|
//protected:
|
2017-03-10 15:12:52 +00:00
|
|
|
static void CALLBACK CallbackFunc(HMIDIOUT, UINT, DWORD_PTR, DWORD, DWORD);
|
|
|
|
|
|
|
|
HMIDISTRM MidiOut;
|
|
|
|
UINT DeviceID;
|
|
|
|
DWORD SavedVolume;
|
|
|
|
MIDIHDR WinMidiHeaders[2];
|
|
|
|
int HeaderIndex;
|
|
|
|
bool VolumeWorks;
|
2019-09-28 14:50:00 +00:00
|
|
|
bool Precache;
|
2017-03-10 15:12:52 +00:00
|
|
|
|
2017-03-10 18:03:58 +00:00
|
|
|
HANDLE BufferDoneEvent;
|
|
|
|
HANDLE ExitEvent;
|
|
|
|
HANDLE PlayerThread;
|
|
|
|
|
|
|
|
|
2017-03-10 15:12:52 +00:00
|
|
|
};
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// PUBLIC DATA DEFINITIONS -------------------------------------------------
|
|
|
|
|
|
|
|
// CODE --------------------------------------------------------------------
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice Contructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-28 14:50:00 +00:00
|
|
|
WinMIDIDevice::WinMIDIDevice(int dev_id, bool precache)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-28 14:50:00 +00:00
|
|
|
DeviceID = std::max<DWORD>(dev_id, 0);
|
2016-03-01 15:47:10 +00:00
|
|
|
MidiOut = 0;
|
2017-03-10 11:01:29 +00:00
|
|
|
HeaderIndex = 0;
|
2019-09-28 14:50:00 +00:00
|
|
|
Precache = precache;
|
2017-03-10 11:01:29 +00:00
|
|
|
memset(WinMidiHeaders, 0, sizeof(WinMidiHeaders));
|
2017-03-10 18:03:58 +00:00
|
|
|
|
|
|
|
BufferDoneEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
|
|
|
if (BufferDoneEvent == nullptr)
|
|
|
|
{
|
2019-09-28 14:50:00 +00:00
|
|
|
throw std::runtime_error("Could not create buffer done event for MIDI playback");
|
2017-03-10 18:03:58 +00:00
|
|
|
}
|
|
|
|
ExitEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
|
|
|
if (ExitEvent == nullptr)
|
|
|
|
{
|
2019-09-28 14:50:00 +00:00
|
|
|
CloseHandle(BufferDoneEvent);
|
|
|
|
BufferDoneEvent = nullptr;
|
|
|
|
throw std::runtime_error("Could not create exit event for MIDI playback");
|
2017-03-10 18:03:58 +00:00
|
|
|
}
|
|
|
|
PlayerThread = nullptr;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice Destructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
WinMIDIDevice::~WinMIDIDevice()
|
|
|
|
{
|
|
|
|
Close();
|
2017-03-10 18:03:58 +00:00
|
|
|
|
|
|
|
if (ExitEvent != nullptr)
|
|
|
|
{
|
|
|
|
CloseHandle(ExitEvent);
|
|
|
|
}
|
|
|
|
if (BufferDoneEvent != nullptr)
|
|
|
|
{
|
|
|
|
CloseHandle(BufferDoneEvent);
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: Open
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-28 08:00:22 +00:00
|
|
|
int WinMIDIDevice::Open()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
MMRESULT err;
|
|
|
|
|
2017-03-10 18:03:58 +00:00
|
|
|
if (MidiOut == nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
err = midiStreamOpen(&MidiOut, &DeviceID, 1, (DWORD_PTR)CallbackFunc, (DWORD_PTR)this, CALLBACK_FUNCTION);
|
|
|
|
|
|
|
|
if (err == MMSYSERR_NOERROR)
|
|
|
|
{
|
|
|
|
if (IgnoreMIDIVolume(DeviceID))
|
|
|
|
{
|
|
|
|
VolumeWorks = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Set master volume to full, if the device allows it on this interface.
|
|
|
|
VolumeWorks = (MMSYSERR_NOERROR == midiOutGetVolume((HMIDIOUT)MidiOut, &SavedVolume));
|
|
|
|
if (VolumeWorks)
|
|
|
|
{
|
|
|
|
VolumeWorks &= (MMSYSERR_NOERROR == midiOutSetVolume((HMIDIOUT)MidiOut, 0xffffffff));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 13:52:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: Close
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void WinMIDIDevice::Close()
|
|
|
|
{
|
2017-03-10 18:03:58 +00:00
|
|
|
if (MidiOut != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
midiStreamClose(MidiOut);
|
2017-03-10 18:03:58 +00:00
|
|
|
MidiOut = nullptr;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: IsOpen
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool WinMIDIDevice::IsOpen() const
|
|
|
|
{
|
2017-03-10 18:03:58 +00:00
|
|
|
return MidiOut != nullptr;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: GetTechnology
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int WinMIDIDevice::GetTechnology() const
|
|
|
|
{
|
|
|
|
MIDIOUTCAPS caps;
|
|
|
|
|
|
|
|
if (MMSYSERR_NOERROR == midiOutGetDevCaps(DeviceID, &caps, sizeof(caps)))
|
|
|
|
{
|
|
|
|
return caps.wTechnology;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: SetTempo
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int WinMIDIDevice::SetTempo(int tempo)
|
|
|
|
{
|
|
|
|
MIDIPROPTEMPO data = { sizeof(MIDIPROPTEMPO), (DWORD)tempo };
|
|
|
|
return midiStreamProperty(MidiOut, (LPBYTE)&data, MIDIPROP_SET | MIDIPROP_TEMPO);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: SetTimeDiv
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int WinMIDIDevice::SetTimeDiv(int timediv)
|
|
|
|
{
|
|
|
|
MIDIPROPTIMEDIV data = { sizeof(MIDIPROPTIMEDIV), (DWORD)timediv };
|
|
|
|
return midiStreamProperty(MidiOut, (LPBYTE)&data, MIDIPROP_SET | MIDIPROP_TIMEDIV);
|
|
|
|
}
|
|
|
|
|
2017-03-10 18:03:58 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDIStreamer :: PlayerProc Static
|
|
|
|
//
|
|
|
|
// Entry point for the player thread.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
DWORD WINAPI PlayerProc(LPVOID lpParameter)
|
|
|
|
{
|
|
|
|
return ((WinMIDIDevice *)lpParameter)->PlayerLoop();
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: Resume
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int WinMIDIDevice::Resume()
|
|
|
|
{
|
2017-03-10 18:03:58 +00:00
|
|
|
DWORD tid;
|
|
|
|
int ret = midiStreamRestart(MidiOut);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
PlayerThread = CreateThread(nullptr, 0, PlayerProc, this, 0, &tid);
|
|
|
|
if (PlayerThread == nullptr)
|
|
|
|
{
|
|
|
|
Stop();
|
2019-09-28 14:50:00 +00:00
|
|
|
throw std::runtime_error("Creating MIDI thread failed\n");
|
2017-03-10 18:03:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: InitPlayback
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void WinMIDIDevice::InitPlayback()
|
|
|
|
{
|
|
|
|
ResetEvent(ExitEvent);
|
|
|
|
ResetEvent(BufferDoneEvent);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: Stop
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void WinMIDIDevice::Stop()
|
|
|
|
{
|
2017-03-10 18:03:58 +00:00
|
|
|
if (PlayerThread != nullptr)
|
|
|
|
{
|
|
|
|
SetEvent(ExitEvent);
|
|
|
|
WaitForSingleObject(PlayerThread, INFINITE);
|
|
|
|
CloseHandle(PlayerThread);
|
|
|
|
PlayerThread = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
midiStreamStop(MidiOut);
|
|
|
|
midiOutReset((HMIDIOUT)MidiOut);
|
|
|
|
if (VolumeWorks)
|
|
|
|
{
|
|
|
|
midiOutSetVolume((HMIDIOUT)MidiOut, SavedVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 18:03:58 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDIStreamer :: PlayerLoop
|
|
|
|
//
|
|
|
|
// Services MIDI playback events.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
DWORD WinMIDIDevice::PlayerLoop()
|
|
|
|
{
|
|
|
|
HANDLE events[2] = { BufferDoneEvent, ExitEvent };
|
|
|
|
|
|
|
|
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (WaitForMultipleObjects(2, events, FALSE, INFINITE))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
if (Callback != nullptr) Callback(CallbackData);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Should not happen.
|
|
|
|
return MMSYSERR_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: PrecacheInstruments
|
|
|
|
//
|
|
|
|
// Each entry is packed as follows:
|
|
|
|
// Bits 0- 6: Instrument number
|
|
|
|
// Bits 7-13: Bank number
|
|
|
|
// Bit 14: Select drum set if 1, tone bank if 0
|
|
|
|
//
|
|
|
|
// My old GUS PnP needed the instruments to be preloaded, or it would miss
|
|
|
|
// some notes the first time through a song. I doubt any modern
|
|
|
|
// hardware has this problem, but since I'd already written the code for
|
|
|
|
// ZDoom 1.22 and below, I'm resurrecting it now for completeness, since I'm
|
|
|
|
// using preloading for the internal Timidity.
|
|
|
|
//
|
|
|
|
// NOTETOSELF: Why did I never notice the midiOutCache(Drum)Patches calls
|
|
|
|
// before now? Should I switch to them? This code worked on my GUS, but
|
|
|
|
// using the APIs intended for caching might be better.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2017-03-08 17:50:37 +00:00
|
|
|
void WinMIDIDevice::PrecacheInstruments(const uint16_t *instruments, int count)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
// Setting snd_midiprecache to false disables this precaching, since it
|
|
|
|
// does involve sleeping for more than a miniscule amount of time.
|
2019-09-28 14:50:00 +00:00
|
|
|
if (!Precache)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-03-09 18:54:41 +00:00
|
|
|
uint8_t bank[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
2016-03-01 15:47:10 +00:00
|
|
|
int i, chan;
|
|
|
|
|
|
|
|
for (i = 0, chan = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
int instr = instruments[i] & 127;
|
|
|
|
int banknum = (instruments[i] >> 7) & 127;
|
|
|
|
int percussion = instruments[i] >> 14;
|
|
|
|
|
|
|
|
if (percussion)
|
|
|
|
{
|
|
|
|
if (bank[9] != banknum)
|
|
|
|
{
|
|
|
|
midiOutShortMsg((HMIDIOUT)MidiOut, MIDI_CTRLCHANGE | 9 | (0 << 8) | (banknum << 16));
|
|
|
|
bank[9] = banknum;
|
|
|
|
}
|
|
|
|
midiOutShortMsg((HMIDIOUT)MidiOut, MIDI_NOTEON | 9 | ((instruments[i] & 0x7f) << 8) | (1 << 16));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Melodic
|
|
|
|
if (bank[chan] != banknum)
|
|
|
|
{
|
|
|
|
midiOutShortMsg((HMIDIOUT)MidiOut, MIDI_CTRLCHANGE | 9 | (0 << 8) | (banknum << 16));
|
|
|
|
bank[chan] = banknum;
|
|
|
|
}
|
|
|
|
midiOutShortMsg((HMIDIOUT)MidiOut, MIDI_PRGMCHANGE | chan | (instruments[i] << 8));
|
|
|
|
midiOutShortMsg((HMIDIOUT)MidiOut, MIDI_NOTEON | chan | (60 << 8) | (1 << 16));
|
|
|
|
if (++chan == 9)
|
|
|
|
{ // Skip the percussion channel
|
|
|
|
chan = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Once we've got an instrument playing on each melodic channel, sleep to give
|
|
|
|
// the driver time to load the instruments. Also do this for the final batch
|
|
|
|
// of instruments.
|
|
|
|
if (chan == 16 || i == count - 1)
|
|
|
|
{
|
|
|
|
Sleep(250);
|
|
|
|
for (chan = 15; chan-- != 0; )
|
|
|
|
{
|
|
|
|
// Turn all notes off
|
|
|
|
midiOutShortMsg((HMIDIOUT)MidiOut, MIDI_CTRLCHANGE | chan | (123 << 8));
|
|
|
|
}
|
|
|
|
// And now chan is back at 0, ready to start the cycle over.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make sure all channels are set back to bank 0.
|
|
|
|
for (i = 0; i < 16; ++i)
|
|
|
|
{
|
|
|
|
if (bank[i] != 0)
|
|
|
|
{
|
|
|
|
midiOutShortMsg((HMIDIOUT)MidiOut, MIDI_CTRLCHANGE | 9 | (0 << 8) | (0 << 16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: Pause
|
|
|
|
//
|
|
|
|
// Some docs claim pause is unreliable and can cause the stream to stop
|
|
|
|
// functioning entirely. Truth or fiction?
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool WinMIDIDevice::Pause(bool paused)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: StreamOut
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2017-03-10 11:01:29 +00:00
|
|
|
int WinMIDIDevice::StreamOut(MidiHeader *header)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-10 11:01:29 +00:00
|
|
|
auto syshdr = (MIDIHDR*)header->lpNext;
|
|
|
|
assert(syshdr == &WinMidiHeaders[0] || syshdr == &WinMidiHeaders[1]);
|
|
|
|
return midiStreamOut(MidiOut, syshdr, sizeof(MIDIHDR));
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: StreamOutSync
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2017-03-10 11:01:29 +00:00
|
|
|
int WinMIDIDevice::StreamOutSync(MidiHeader *header)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-10 18:03:58 +00:00
|
|
|
return StreamOut(header);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: PrepareHeader
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2017-03-10 11:01:29 +00:00
|
|
|
int WinMIDIDevice::PrepareHeader(MidiHeader *header)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-10 11:01:29 +00:00
|
|
|
// This code depends on the driving implementation only having two buffers that get passed alternatingly.
|
|
|
|
// If there were more buffers this would require more intelligent handling.
|
|
|
|
assert(header->lpNext == nullptr);
|
|
|
|
MIDIHDR *syshdr = &WinMidiHeaders[HeaderIndex ^= 1];
|
|
|
|
memset(syshdr, 0, sizeof(MIDIHDR));
|
|
|
|
syshdr->lpData = (LPSTR)header->lpData;
|
|
|
|
syshdr->dwBufferLength = header->dwBufferLength;
|
|
|
|
syshdr->dwBytesRecorded = header->dwBytesRecorded;
|
|
|
|
// this device does not use the lpNext pointer to link MIDI events so use it to point to the system data structure.
|
|
|
|
header->lpNext = (MidiHeader*)syshdr;
|
|
|
|
return midiOutPrepareHeader((HMIDIOUT)MidiOut, syshdr, sizeof(MIDIHDR));
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: UnprepareHeader
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2017-03-10 11:01:29 +00:00
|
|
|
int WinMIDIDevice::UnprepareHeader(MidiHeader *header)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-10 11:01:29 +00:00
|
|
|
auto syshdr = (MIDIHDR*)header->lpNext;
|
|
|
|
if (syshdr != nullptr)
|
|
|
|
{
|
|
|
|
assert(syshdr == &WinMidiHeaders[0] || syshdr == &WinMidiHeaders[1]);
|
|
|
|
header->lpNext = nullptr;
|
|
|
|
return midiOutUnprepareHeader((HMIDIOUT)MidiOut, syshdr, sizeof(MIDIHDR));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return MMSYSERR_NOERROR;
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: FakeVolume
|
|
|
|
//
|
|
|
|
// Because there are too many MIDI devices out there that don't support
|
|
|
|
// global volume changes, fake the volume for all of them.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool WinMIDIDevice::FakeVolume()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2017-03-10 18:03:58 +00:00
|
|
|
// WinMIDIDevice :: Update
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2017-03-10 18:03:58 +00:00
|
|
|
bool WinMIDIDevice::Update()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-10 18:03:58 +00:00
|
|
|
// If the PlayerThread is signalled, then it's dead.
|
|
|
|
if (PlayerThread != nullptr &&
|
|
|
|
WaitForSingleObject(PlayerThread, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
static const char *const MMErrorCodes[] =
|
|
|
|
{
|
|
|
|
"No error",
|
|
|
|
"Unspecified error",
|
|
|
|
"Device ID out of range",
|
|
|
|
"Driver failed enable",
|
|
|
|
"Device already allocated",
|
|
|
|
"Device handle is invalid",
|
|
|
|
"No device driver present",
|
|
|
|
"Memory allocation error",
|
|
|
|
"Function isn't supported",
|
|
|
|
"Error value out of range",
|
|
|
|
"Invalid flag passed",
|
|
|
|
"Invalid parameter passed",
|
|
|
|
"Handle being used simultaneously on another thread",
|
|
|
|
"Specified alias not found",
|
|
|
|
"Bad registry database",
|
|
|
|
"Registry key not found",
|
|
|
|
"Registry read error",
|
|
|
|
"Registry write error",
|
|
|
|
"Registry delete error",
|
|
|
|
"Registry value not found",
|
|
|
|
"Driver does not call DriverCallback",
|
|
|
|
"More data to be returned",
|
|
|
|
};
|
|
|
|
static const char *const MidiErrorCodes[] =
|
|
|
|
{
|
|
|
|
"MIDI header not prepared",
|
|
|
|
"MIDI still playing something",
|
|
|
|
"MIDI no configured instruments",
|
|
|
|
"MIDI hardware is still busy",
|
|
|
|
"MIDI port no longer connected",
|
|
|
|
"MIDI invalid MIF",
|
|
|
|
"MIDI operation unsupported with open mode",
|
|
|
|
"MIDI through device 'eating' a message",
|
|
|
|
};
|
|
|
|
DWORD code = 0xABADCAFE;
|
|
|
|
GetExitCodeThread(PlayerThread, &code);
|
|
|
|
CloseHandle(PlayerThread);
|
|
|
|
PlayerThread = nullptr;
|
2019-09-28 14:50:00 +00:00
|
|
|
char errmsg[100];
|
|
|
|
const char *m = "MIDI playback failure: ";
|
|
|
|
if (code < 8)
|
2017-03-10 18:03:58 +00:00
|
|
|
{
|
2019-09-28 14:50:00 +00:00
|
|
|
snprintf(errmsg, 100, "%s%s", m, MMErrorCodes[code]);
|
2017-03-10 18:03:58 +00:00
|
|
|
}
|
2019-09-28 14:50:00 +00:00
|
|
|
else if (code >= MIDIERR_BASE && code < MIDIERR_BASE + 8)
|
2017-03-10 18:03:58 +00:00
|
|
|
{
|
2019-09-28 14:50:00 +00:00
|
|
|
snprintf(errmsg, 100, "%s%s", m, MMErrorCodes[code - MIDIERR_BASE]);
|
2017-03-10 18:03:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-28 14:50:00 +00:00
|
|
|
snprintf(errmsg, 100, "%s%08x", m, code);
|
2017-03-10 18:03:58 +00:00
|
|
|
}
|
2019-09-28 14:50:00 +00:00
|
|
|
throw std::runtime_error(errmsg);
|
2017-03-10 18:03:58 +00:00
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// WinMIDIDevice :: CallbackFunc static
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void CALLBACK WinMIDIDevice::CallbackFunc(HMIDIOUT hOut, UINT uMsg, DWORD_PTR dwInstance, DWORD dwParam1, DWORD dwParam2)
|
|
|
|
{
|
|
|
|
WinMIDIDevice *self = (WinMIDIDevice *)dwInstance;
|
2017-03-10 18:03:58 +00:00
|
|
|
if (uMsg == MOM_DONE)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-10 18:03:58 +00:00
|
|
|
SetEvent(self->BufferDoneEvent);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// IgnoreMIDIVolume
|
|
|
|
//
|
|
|
|
// Should we ignore this MIDI device's volume control even if it works?
|
|
|
|
//
|
|
|
|
// Under Windows Vista and up, when using the standard "Microsoft GS
|
|
|
|
// Wavetable Synth", midiOutSetVolume() will affect the application's audio
|
|
|
|
// session volume rather than the volume for just the MIDI stream. At first,
|
|
|
|
// I thought I could get around this by enumerating the streams in the
|
|
|
|
// audio session to find the MIDI device's stream to set its volume
|
|
|
|
// manually, but there doesn't appear to be any way to enumerate the
|
|
|
|
// individual streams in a session. Consequently, we'll just assume the MIDI
|
|
|
|
// device gets created at full volume like we want. (Actual volume changes
|
|
|
|
// are done by sending MIDI channel volume messages to the stream, not
|
|
|
|
// through midiOutSetVolume().)
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
static bool IgnoreMIDIVolume(UINT id)
|
|
|
|
{
|
2019-02-14 21:22:15 +00:00
|
|
|
MIDIOUTCAPSA caps;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2019-02-14 21:22:15 +00:00
|
|
|
if (MMSYSERR_NOERROR == midiOutGetDevCapsA(id, &caps, sizeof(caps)))
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-04-21 08:37:03 +00:00
|
|
|
if (caps.wTechnology == MIDIDEV_MAPPER)
|
|
|
|
{
|
|
|
|
// We cannot determine what this is so we have to assume the worst, as the default
|
|
|
|
// devive's volume control is irreparably broken.
|
2018-01-14 09:47:46 +00:00
|
|
|
return true;
|
2017-04-21 08:37:03 +00:00
|
|
|
}
|
2017-03-10 11:39:23 +00:00
|
|
|
// The Microsoft GS Wavetable Synth advertises itself as MIDIDEV_SWSYNTH with a VOLUME control.
|
2016-03-01 15:47:10 +00:00
|
|
|
// If the one we're using doesn't match that, we don't need to bother checking the name.
|
2017-03-10 11:39:23 +00:00
|
|
|
if (caps.wTechnology == MIDIDEV_SWSYNTH && (caps.dwSupport & MIDICAPS_VOLUME))
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
if (strncmp(caps.szPname, "Microsoft GS", 12) == 0)
|
|
|
|
{
|
2017-04-21 08:37:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-29 18:01:57 +00:00
|
|
|
MIDIDevice *CreateWinMIDIDevice(int mididevice)
|
2017-03-10 15:12:52 +00:00
|
|
|
{
|
2019-09-29 18:01:57 +00:00
|
|
|
return new WinMIDIDevice(mididevice, miscConfig.snd_midiprecache);
|
2017-03-10 15:12:52 +00:00
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
#endif
|
2017-03-10 15:12:52 +00:00
|
|
|
|
|
|
|
|