2010-05-25 10:56:00 +00:00
|
|
|
//-------------------------------------------------------------------------
|
2009-07-27 10:46:42 +00:00
|
|
|
/*
|
2016-02-02 00:21:12 +00:00
|
|
|
Copyright (C) 2016 EDuke32 developers and contributors
|
2009-07-27 10:46:42 +00:00
|
|
|
|
2010-05-25 10:56:00 +00:00
|
|
|
This file is part of EDuke32.
|
|
|
|
|
|
|
|
EDuke32 is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License version 2
|
|
|
|
as published by the Free Software Foundation.
|
2009-07-27 10:46:42 +00:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2014-07-20 08:55:56 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-07-27 10:46:42 +00:00
|
|
|
*/
|
2010-05-25 10:56:00 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2009-07-27 10:46:42 +00:00
|
|
|
#include "mpu401.h"
|
|
|
|
#include "compat.h"
|
2014-10-25 03:29:21 +00:00
|
|
|
#include "pragmas.h"
|
2009-07-27 10:46:42 +00:00
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#include <mmsystem.h>
|
|
|
|
|
|
|
|
static HMIDISTRM hmido = (HMIDISTRM)-1;
|
|
|
|
static MIDIOUTCAPS midicaps;
|
|
|
|
static DWORD mididevice = -1;
|
|
|
|
|
|
|
|
#define PAD(x) ((((x)+3)&(~3)))
|
|
|
|
|
|
|
|
#define BUFFERLEN (32*4*4)
|
|
|
|
#define NUMBUFFERS 6
|
|
|
|
static char eventbuf[NUMBUFFERS][BUFFERLEN];
|
|
|
|
static int32_t eventcnt[NUMBUFFERS];
|
|
|
|
static MIDIHDR bufferheaders[NUMBUFFERS];
|
2016-02-02 00:21:12 +00:00
|
|
|
static int32_t _MPU_CurrentBuffer = 0;
|
2009-07-27 10:46:42 +00:00
|
|
|
int32_t _MPU_BuffersWaiting = 0;
|
|
|
|
|
|
|
|
extern uint32_t _MIDI_GlobalPositionInTicks;
|
2016-02-02 00:21:12 +00:00
|
|
|
static uint32_t _MPU_LastEvent=0;
|
2009-07-27 10:46:42 +00:00
|
|
|
|
|
|
|
#define MIDI_NOTE_OFF 0x80
|
|
|
|
#define MIDI_NOTE_ON 0x90
|
|
|
|
#define MIDI_POLY_AFTER_TCH 0xA0
|
|
|
|
#define MIDI_CONTROL_CHANGE 0xB0
|
|
|
|
#define MIDI_PROGRAM_CHANGE 0xC0
|
|
|
|
#define MIDI_AFTER_TOUCH 0xD0
|
|
|
|
#define MIDI_PITCH_BEND 0xE0
|
|
|
|
#define MIDI_META_EVENT 0xFF
|
|
|
|
#define MIDI_END_OF_TRACK 0x2F
|
|
|
|
#define MIDI_TEMPO_CHANGE 0x51
|
|
|
|
#define MIDI_MONO_MODE_ON 0x7E
|
|
|
|
#define MIDI_ALL_NOTES_OFF 0x7B
|
|
|
|
|
|
|
|
void MPU_FinishBuffer(int32_t buffer)
|
|
|
|
{
|
|
|
|
if (!eventcnt[buffer]) return;
|
|
|
|
ZeroMemory(&bufferheaders[buffer], sizeof(MIDIHDR));
|
|
|
|
bufferheaders[buffer].lpData = eventbuf[buffer];
|
|
|
|
bufferheaders[buffer].dwBufferLength =
|
|
|
|
bufferheaders[buffer].dwBytesRecorded = eventcnt[buffer];
|
|
|
|
midiOutPrepareHeader((HMIDIOUT)hmido, &bufferheaders[buffer], sizeof(MIDIHDR));
|
|
|
|
midiStreamOut(hmido, &bufferheaders[buffer], sizeof(MIDIHDR));
|
|
|
|
// printf("Sending %d bytes (buffer %d)\n",eventcnt[buffer],buffer);
|
|
|
|
_MPU_BuffersWaiting++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MPU_BeginPlayback(void)
|
|
|
|
{
|
|
|
|
_MPU_LastEvent = _MIDI_GlobalPositionInTicks;
|
|
|
|
if (hmido != (HMIDISTRM)-1) midiStreamRestart(hmido);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MPU_Pause(void)
|
|
|
|
{
|
|
|
|
if (hmido != (HMIDISTRM)-1) midiStreamPause(hmido);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MPU_Unpause(void)
|
|
|
|
{
|
|
|
|
if (hmido != (HMIDISTRM)-1) midiStreamRestart(hmido);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Win64 support! (Meaning it works, not that we recommend it for everyday use.)
This includes a complete Windows header and library refresh, including the addition of 64-bit compiled libs:
*libogg 1.3.0
*libvorbis 1.3.3
*zlib 1.2.7
*libpng 1.5.13
*libvpx 9a3de881c0e681ba1a79a166a86308bbc84b4acd
*SDL_mixer 1.2.12 (for RENDERTYPE=SDL)
*DirectX import libraries: dsound and dxguid (now included)
To build in 64-bit, you essentially need MinGW's MSYS (but not MinGW itself) and MinGW-w64 at the top of your PATH. The target is automatically detected using `$(CC) -dumpmachine`. The EDukeWiki will get detailed instrucitons.
All compiler and linker warnings when building in 64-bit mode have been fixed.
Remaining 64-bit to-do:
- The ebacktrace dll does not build under 64-bit. It uses code specific to the format of 32-bit executables and will have to be ported to work with 64-bit executables. A future 64-bit version will be named ebacktrace1-64.dll.
- RENDERTYPE=SDL crashes in SDL_mixer's Mix_Linked_Version().
- DirectInput gives an error and does not function. This only affects joysticks, and the error never happens without any plugged in.
- Port the classic renderer ASM to 64-bit. (Just kidding, this is way out of my league.)
This commit includes a fair bit of Makefile development spanning all platforms, including simplifying the SDLCONFIG code, fixing build on Mac OS X (thanks rhoenie!), globally factoring Apple brew/port inclusion, enforcing that all -L come before all -l, and ensuring that $(shell ) is always :='d.
In addition, I have resurrected the old GCC_MAJOR and GCC_MINOR detection using `$(CC) -dumpversion`, but I have made it failsafe in case the command fails or the version is manually specified. I have applied this new fine-grained detection where applicable, including allowing LTO, and restraining -W's to versions that support them.
git-svn-id: https://svn.eduke32.com/eduke32@3278 1a8010ca-5511-0410-912e-c29ae57300e0
2012-12-13 02:37:20 +00:00
|
|
|
void CALLBACK MPU_MIDICallback(HMIDIOUT handle, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(dwInstance);
|
|
|
|
UNREFERENCED_PARAMETER(dwParam2);
|
|
|
|
|
|
|
|
switch (uMsg)
|
|
|
|
{
|
|
|
|
case MOM_DONE:
|
2010-08-02 08:13:51 +00:00
|
|
|
midiOutUnprepareHeader((HMIDIOUT)handle, (MIDIHDR *)dwParam1, sizeof(MIDIHDR));
|
2016-02-02 00:21:12 +00:00
|
|
|
for (int i=0; i<NUMBUFFERS; i++)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
Win64 support! (Meaning it works, not that we recommend it for everyday use.)
This includes a complete Windows header and library refresh, including the addition of 64-bit compiled libs:
*libogg 1.3.0
*libvorbis 1.3.3
*zlib 1.2.7
*libpng 1.5.13
*libvpx 9a3de881c0e681ba1a79a166a86308bbc84b4acd
*SDL_mixer 1.2.12 (for RENDERTYPE=SDL)
*DirectX import libraries: dsound and dxguid (now included)
To build in 64-bit, you essentially need MinGW's MSYS (but not MinGW itself) and MinGW-w64 at the top of your PATH. The target is automatically detected using `$(CC) -dumpmachine`. The EDukeWiki will get detailed instrucitons.
All compiler and linker warnings when building in 64-bit mode have been fixed.
Remaining 64-bit to-do:
- The ebacktrace dll does not build under 64-bit. It uses code specific to the format of 32-bit executables and will have to be ported to work with 64-bit executables. A future 64-bit version will be named ebacktrace1-64.dll.
- RENDERTYPE=SDL crashes in SDL_mixer's Mix_Linked_Version().
- DirectInput gives an error and does not function. This only affects joysticks, and the error never happens without any plugged in.
- Port the classic renderer ASM to 64-bit. (Just kidding, this is way out of my league.)
This commit includes a fair bit of Makefile development spanning all platforms, including simplifying the SDLCONFIG code, fixing build on Mac OS X (thanks rhoenie!), globally factoring Apple brew/port inclusion, enforcing that all -L come before all -l, and ensuring that $(shell ) is always :='d.
In addition, I have resurrected the old GCC_MAJOR and GCC_MINOR detection using `$(CC) -dumpversion`, but I have made it failsafe in case the command fails or the version is manually specified. I have applied this new fine-grained detection where applicable, including allowing LTO, and restraining -W's to versions that support them.
git-svn-id: https://svn.eduke32.com/eduke32@3278 1a8010ca-5511-0410-912e-c29ae57300e0
2012-12-13 02:37:20 +00:00
|
|
|
if ((MIDIHDR *)dwParam1 == &bufferheaders[i])
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
|
|
|
eventcnt[i] = 0; // marks the buffer as free
|
|
|
|
// printf("Finished buffer %d\n",i);
|
|
|
|
_MPU_BuffersWaiting--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
default: return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t MPU_GetNextBuffer(void)
|
|
|
|
{
|
2016-02-02 00:21:12 +00:00
|
|
|
for (int i = 0; i < NUMBUFFERS; i++)
|
|
|
|
if (eventcnt[i] == 0)
|
|
|
|
return i;
|
|
|
|
|
2009-07-27 10:46:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
static int32_t const masks[3] ={ 0x00ffffffl, 0x0000ffffl, 0x000000ffl };
|
|
|
|
|
2009-07-27 10:46:42 +00:00
|
|
|
void MPU_SendMidi(char *data, int32_t count)
|
|
|
|
{
|
|
|
|
if (count <= 0) return;
|
2016-02-02 00:21:12 +00:00
|
|
|
|
2009-07-27 10:46:42 +00:00
|
|
|
if (count <= 3)
|
|
|
|
{
|
|
|
|
if (eventcnt[_MPU_CurrentBuffer] + 12 > BUFFERLEN)
|
|
|
|
{
|
|
|
|
// buffer over-full
|
2016-02-02 00:21:12 +00:00
|
|
|
int32_t nextbuffer = MPU_GetNextBuffer();
|
2009-07-27 10:46:42 +00:00
|
|
|
if (nextbuffer < 0)
|
|
|
|
{
|
|
|
|
// printf("All buffers full!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MPU_FinishBuffer(_MPU_CurrentBuffer);
|
|
|
|
_MPU_CurrentBuffer = nextbuffer;
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
char *p = eventbuf[_MPU_CurrentBuffer] + eventcnt[_MPU_CurrentBuffer];
|
2010-08-02 08:13:51 +00:00
|
|
|
((int32_t *)p)[0] = _MIDI_GlobalPositionInTicks - _MPU_LastEvent;
|
|
|
|
((int32_t *)p)[1] = 0;
|
|
|
|
((int32_t *)p)[2] = (MEVT_SHORTMSG << 24) | ((*((int32_t *)data)) & masks[count-1]);
|
2009-07-27 10:46:42 +00:00
|
|
|
eventcnt[_MPU_CurrentBuffer] += 12;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-02 00:21:12 +00:00
|
|
|
int32_t padded = PAD(count);
|
2009-07-27 10:46:42 +00:00
|
|
|
if (eventcnt[_MPU_CurrentBuffer] + 12 + padded > BUFFERLEN)
|
|
|
|
{
|
|
|
|
// buffer over-full
|
2016-02-02 00:21:12 +00:00
|
|
|
int32_t nextbuffer = MPU_GetNextBuffer();
|
2009-07-27 10:46:42 +00:00
|
|
|
if (nextbuffer < 0)
|
|
|
|
{
|
|
|
|
// printf("All buffers full!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MPU_FinishBuffer(_MPU_CurrentBuffer);
|
|
|
|
_MPU_CurrentBuffer = nextbuffer;
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
char *p = eventbuf[_MPU_CurrentBuffer] + eventcnt[_MPU_CurrentBuffer];
|
2010-08-02 08:13:51 +00:00
|
|
|
((int32_t *)p)[0] = _MIDI_GlobalPositionInTicks - _MPU_LastEvent;
|
|
|
|
((int32_t *)p)[1] = 0;
|
|
|
|
((int32_t *)p)[2] = (MEVT_LONGMSG<<24) | (count & 0xffffffl);
|
2009-07-27 10:46:42 +00:00
|
|
|
p+=12; eventcnt[_MPU_CurrentBuffer] += 12;
|
|
|
|
for (; count>0; count--, padded--, eventcnt[_MPU_CurrentBuffer]++)
|
|
|
|
*(p++) = *(data++);
|
|
|
|
for (; padded>0; padded--, eventcnt[_MPU_CurrentBuffer]++)
|
|
|
|
*(p++) = 0;
|
|
|
|
}
|
|
|
|
_MPU_LastEvent = _MIDI_GlobalPositionInTicks;
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
int32_t MPU_Reset(void)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
|
|
|
midiStreamStop(hmido);
|
|
|
|
midiStreamClose(hmido);
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
return MPU_Ok;
|
2009-07-27 10:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
int32_t MPU_Init(int32_t addr)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
2016-02-02 00:21:12 +00:00
|
|
|
for (int i=0; i<NUMBUFFERS; i++) eventcnt[i]=0;
|
2009-07-27 10:46:42 +00:00
|
|
|
|
|
|
|
mididevice = addr;
|
|
|
|
|
|
|
|
if (midiOutGetDevCaps(mididevice, &midicaps, sizeof(MIDIOUTCAPS)) != MMSYSERR_NOERROR) return MPU_Error;
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
if (midiStreamOpen(&hmido,(LPUINT)&mididevice,1,(DWORD_PTR)MPU_MIDICallback,0L,CALLBACK_FUNCTION) != MMSYSERR_NOERROR) return MPU_Error;
|
2009-07-27 10:46:42 +00:00
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
return MPU_Ok;
|
2009-07-27 10:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
void MPU_NoteOff(char channel, char key, char velocity)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
2016-02-02 00:21:12 +00:00
|
|
|
char msg[] = { (char)(MIDI_NOTE_OFF | channel), key, velocity };
|
|
|
|
MPU_SendMidi(msg, sizeof(msg));
|
2009-07-27 10:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
void MPU_NoteOn(char channel, char key, char velocity)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
2016-02-02 00:21:12 +00:00
|
|
|
char msg[] = { (char)(MIDI_NOTE_ON | channel), key, velocity };
|
|
|
|
MPU_SendMidi(msg, sizeof(msg));
|
2009-07-27 10:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
void MPU_PolyAftertouch(char channel, char key, char pressure)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
2016-02-02 00:21:12 +00:00
|
|
|
char msg[] = { (char) (MIDI_POLY_AFTER_TCH | channel), key, pressure };
|
|
|
|
MPU_SendMidi(msg, sizeof(msg));
|
2009-07-27 10:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
void MPU_ControlChange(char channel, char number, char value)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
2016-02-02 00:21:12 +00:00
|
|
|
char msg[] = { (char) (MIDI_CONTROL_CHANGE | channel), number, value };
|
|
|
|
MPU_SendMidi(msg, sizeof(msg));
|
2009-07-27 10:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
void MPU_ProgramChange(char channel, char program)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
2016-02-02 00:21:12 +00:00
|
|
|
char msg[] = { (char)(MIDI_PROGRAM_CHANGE | channel), program };
|
|
|
|
MPU_SendMidi(msg, sizeof(msg));
|
2009-07-27 10:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
void MPU_ChannelAftertouch(char channel, char pressure)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
2016-02-02 00:21:12 +00:00
|
|
|
char msg[] = { (char)(MIDI_AFTER_TOUCH | channel), pressure };
|
|
|
|
MPU_SendMidi(msg, sizeof(msg));
|
2009-07-27 10:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:21:12 +00:00
|
|
|
void MPU_PitchBend(char channel, char lsb, char msb)
|
2009-07-27 10:46:42 +00:00
|
|
|
{
|
2016-02-02 00:21:12 +00:00
|
|
|
char msg[] = { (char)(MIDI_PITCH_BEND | channel), lsb, msb };
|
|
|
|
MPU_SendMidi(msg, sizeof(msg));
|
2009-07-27 10:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MPU_SetTempo(int32_t tempo)
|
|
|
|
{
|
|
|
|
MIDIPROPTEMPO prop;
|
|
|
|
prop.cbStruct = sizeof(MIDIPROPTEMPO);
|
2014-10-25 03:29:21 +00:00
|
|
|
prop.dwTempo = tabledivide32_noinline(60000000l, tempo);
|
2009-07-27 10:46:42 +00:00
|
|
|
midiStreamProperty(hmido, (LPBYTE)&prop, MIDIPROP_SET|MIDIPROP_TEMPO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MPU_SetDivision(int32_t division)
|
|
|
|
{
|
|
|
|
MIDIPROPTIMEDIV prop;
|
|
|
|
prop.cbStruct = sizeof(MIDIPROPTIMEDIV);
|
|
|
|
prop.dwTimeDiv = division;
|
|
|
|
midiStreamProperty(hmido, (LPBYTE)&prop, MIDIPROP_SET|MIDIPROP_TIMEDIV);
|
|
|
|
}
|
|
|
|
|