2008-03-05 03:10:31 +00:00
|
|
|
/*
|
|
|
|
** music_mus_midiout.cpp
|
|
|
|
** Code to let ZDoom play MUS music through the MIDI streaming API.
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 1998-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.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
// HEADER FILES ------------------------------------------------------------
|
|
|
|
|
2009-09-01 02:08:53 +00:00
|
|
|
#if !defined(__FreeBSD__) && !defined(__APPLE__)
|
2009-04-08 04:57:17 +00:00
|
|
|
#include <malloc.h>
|
2009-04-08 04:58:36 +00:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
2006-02-24 04:48:15 +00:00
|
|
|
#include "i_musicinterns.h"
|
|
|
|
#include "templates.h"
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "m_swap.h"
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// MACROS ------------------------------------------------------------------
|
|
|
|
|
|
|
|
// TYPES -------------------------------------------------------------------
|
|
|
|
|
|
|
|
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
|
|
|
|
|
|
|
|
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
|
|
|
|
|
2010-04-06 03:12:32 +00:00
|
|
|
int MUSHeaderSearch(const BYTE *head, int len);
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
|
|
|
|
|
|
|
|
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
|
|
|
|
|
|
|
|
// PRIVATE DATA DEFINITIONS ------------------------------------------------
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
static const BYTE CtrlTranslate[15] =
|
|
|
|
{
|
|
|
|
0, // program change
|
|
|
|
0, // bank select
|
|
|
|
1, // modulation pot
|
|
|
|
7, // volume
|
|
|
|
10, // pan pot
|
|
|
|
11, // expression pot
|
|
|
|
91, // reverb depth
|
|
|
|
93, // chorus depth
|
|
|
|
64, // sustain pedal
|
|
|
|
67, // soft pedal
|
|
|
|
120, // all sounds off
|
|
|
|
123, // all notes off
|
|
|
|
126, // mono
|
|
|
|
127, // poly
|
|
|
|
121, // reset all controllers
|
|
|
|
};
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// PUBLIC DATA DEFINITIONS -------------------------------------------------
|
|
|
|
|
|
|
|
// CODE --------------------------------------------------------------------
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSSong2 Constructor
|
|
|
|
//
|
|
|
|
// Performs some validity checks on the MUS file, buffers it, and creates
|
|
|
|
// the playback thread control events.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-10-02 04:12:21 +00:00
|
|
|
MUSSong2::MUSSong2 (FILE *file, BYTE *musiccache, int len, EMidiDevice type)
|
2008-04-11 04:59:23 +00:00
|
|
|
: MIDIStreamer(type), MusHeader(0), MusBuffer(0)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-04-03 22:34:52 +00:00
|
|
|
#ifdef _WIN32
|
2008-03-05 03:10:31 +00:00
|
|
|
if (ExitEvent == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-04-03 22:34:52 +00:00
|
|
|
#endif
|
2006-04-14 12:58:52 +00:00
|
|
|
|
2010-04-06 03:12:32 +00:00
|
|
|
BYTE front[32];
|
|
|
|
int start;
|
|
|
|
|
|
|
|
if (file == NULL)
|
2006-04-14 12:58:52 +00:00
|
|
|
{
|
2010-04-07 02:02:53 +00:00
|
|
|
memcpy(front, musiccache, sizeof(front));
|
2006-04-14 12:58:52 +00:00
|
|
|
}
|
2010-04-07 02:02:53 +00:00
|
|
|
else if (fread(front, 1, sizeof(front), file) != sizeof(front))
|
2006-04-14 12:58:52 +00:00
|
|
|
{
|
2010-04-06 03:12:32 +00:00
|
|
|
return;
|
2006-04-14 12:58:52 +00:00
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-04-06 03:12:32 +00:00
|
|
|
// To tolerate sloppy wads (diescum.wad, I'm looking at you), we search
|
|
|
|
// the first 32 bytes of the file for a signature. My guess is that DMX
|
|
|
|
// does no validation whatsoever and just assumes it was passed a valid
|
|
|
|
// MUS file, since where the header is offset affects how it plays.
|
|
|
|
start = MUSHeaderSearch(front, sizeof(front));
|
|
|
|
if (start < 0)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-04-06 03:12:32 +00:00
|
|
|
|
|
|
|
// Read the remainder of the song.
|
|
|
|
len = int(len - start);
|
2010-08-11 03:56:31 +00:00
|
|
|
if (len < (int)sizeof(MusHeader))
|
2010-04-06 03:12:32 +00:00
|
|
|
{ // It's too short.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MusHeader = (MUSHeader *)new BYTE[len];
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
memcpy(MusHeader, musiccache + start, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-07 02:02:53 +00:00
|
|
|
memcpy(MusHeader, front + start, sizeof(front) - start);
|
|
|
|
if (fread((BYTE *)MusHeader + sizeof(front) - start, 1, len - (sizeof(front) - start), file) != (size_t)(len - (32 - start)))
|
2010-04-06 03:12:32 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do some validation of the MUS file.
|
2008-03-04 06:36:23 +00:00
|
|
|
if (LittleShort(MusHeader->NumChans) > 15)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-04-07 02:02:53 +00:00
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
MusBuffer = (BYTE *)MusHeader + LittleShort(MusHeader->SongStart);
|
2010-04-06 03:12:32 +00:00
|
|
|
MaxMusP = MIN<int>(LittleShort(MusHeader->SongLen), len - LittleShort(MusHeader->SongStart));
|
2008-03-05 03:10:31 +00:00
|
|
|
Division = 140;
|
|
|
|
InitialTempo = 1000000;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSSong2 Destructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
MUSSong2::~MUSSong2 ()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
if (MusHeader != NULL)
|
2008-03-04 06:36:23 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
delete[] (BYTE *)MusHeader;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSSong2 :: DoInitialSetup
|
|
|
|
//
|
|
|
|
// Sets up initial velocities and channel volumes.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2008-03-04 06:36:23 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
void MUSSong2::DoInitialSetup()
|
|
|
|
{
|
2008-03-04 06:36:23 +00:00
|
|
|
for (int i = 0; i < 16; ++i)
|
|
|
|
{
|
2008-04-19 21:36:53 +00:00
|
|
|
LastVelocity[i] = 100;
|
2008-03-04 06:36:23 +00:00
|
|
|
ChannelVolumes[i] = 127;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSSong2 :: DoRestart
|
|
|
|
//
|
|
|
|
// Rewinds the song.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
void MUSSong2::DoRestart()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
MusP = 0;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSSong2 :: CheckDone
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
bool MUSSong2::CheckDone()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
return MusP >= MaxMusP;
|
2008-03-04 06:36:23 +00:00
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-04-09 03:55:04 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSSong2 :: Precache
|
|
|
|
//
|
|
|
|
// MUS songs contain information in their header for exactly this purpose.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void MUSSong2::Precache()
|
|
|
|
{
|
2008-04-12 05:04:37 +00:00
|
|
|
WORD *work = (WORD *)alloca(MusHeader->NumInstruments * sizeof(WORD));
|
|
|
|
const WORD *used = (WORD *)MusHeader + sizeof(MUSHeader) / sizeof(WORD);
|
2008-04-09 03:55:04 +00:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = j = 0; i < MusHeader->NumInstruments; ++i)
|
|
|
|
{
|
2008-04-12 05:04:37 +00:00
|
|
|
WORD instr = LittleShort(used[i]);
|
|
|
|
if (instr < 128)
|
2008-04-09 03:55:04 +00:00
|
|
|
{
|
2008-04-12 05:04:37 +00:00
|
|
|
work[j++] = instr;
|
2008-04-09 03:55:04 +00:00
|
|
|
}
|
|
|
|
else if (used[i] >= 135 && used[i] <= 181)
|
|
|
|
{ // Percussions are 100-based, not 128-based, eh?
|
2008-04-12 05:04:37 +00:00
|
|
|
work[j++] = instr - 100 + (1 << 14);
|
2008-04-09 03:55:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MIDI->PrecacheInstruments(&work[0], j);
|
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSSong2 :: MakeEvents
|
|
|
|
//
|
|
|
|
// Translates MUS events into MIDI events and puts them into a MIDI stream
|
|
|
|
// buffer. Returns the new position in the buffer.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
DWORD *MUSSong2::MakeEvents(DWORD *events, DWORD *max_event_p, DWORD max_time)
|
2008-03-04 06:36:23 +00:00
|
|
|
{
|
|
|
|
DWORD tot_time = 0;
|
|
|
|
DWORD time = 0;
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
max_time = max_time * Division / Tempo;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
while (events < max_event_p && tot_time <= max_time)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
BYTE mid1, mid2;
|
|
|
|
BYTE channel;
|
|
|
|
BYTE t = 0, status;
|
2008-03-04 06:36:23 +00:00
|
|
|
BYTE event = MusBuffer[MusP++];
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
if ((event & 0x70) != MUS_SCOREEND)
|
|
|
|
{
|
|
|
|
t = MusBuffer[MusP++];
|
|
|
|
}
|
|
|
|
channel = event & 15;
|
|
|
|
|
|
|
|
// Map MUS channels to MIDI channels
|
|
|
|
if (channel == 15)
|
|
|
|
{
|
|
|
|
channel = 9;
|
|
|
|
}
|
|
|
|
else if (channel >= 9)
|
|
|
|
{
|
|
|
|
channel = channel + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = channel;
|
|
|
|
|
|
|
|
switch (event & 0x70)
|
|
|
|
{
|
|
|
|
case MUS_NOTEOFF:
|
2010-09-03 05:08:05 +00:00
|
|
|
status |= MIDI_NOTEON;
|
2006-02-24 04:48:15 +00:00
|
|
|
mid1 = t;
|
2010-09-03 05:08:05 +00:00
|
|
|
mid2 = 0;
|
2006-02-24 04:48:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MUS_NOTEON:
|
|
|
|
status |= MIDI_NOTEON;
|
|
|
|
mid1 = t & 127;
|
|
|
|
if (t & 128)
|
|
|
|
{
|
2008-03-04 06:36:23 +00:00
|
|
|
LastVelocity[channel] = MusBuffer[MusP++];
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
mid2 = LastVelocity[channel];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MUS_PITCHBEND:
|
|
|
|
status |= MIDI_PITCHBEND;
|
|
|
|
mid1 = (t & 1) << 6;
|
|
|
|
mid2 = (t >> 1) & 127;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MUS_SYSEVENT:
|
|
|
|
status |= MIDI_CTRLCHANGE;
|
|
|
|
mid1 = CtrlTranslate[t];
|
|
|
|
mid2 = t == 12 ? LittleShort(MusHeader->NumChans) : 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MUS_CTRLCHANGE:
|
|
|
|
if (t == 0)
|
|
|
|
{ // program change
|
|
|
|
status |= MIDI_PRGMCHANGE;
|
|
|
|
mid1 = MusBuffer[MusP++];
|
|
|
|
mid2 = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status |= MIDI_CTRLCHANGE;
|
|
|
|
mid1 = CtrlTranslate[t];
|
|
|
|
mid2 = MusBuffer[MusP++];
|
|
|
|
if (mid1 == 7)
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
mid2 = VolumeControllerChange(channel, mid2);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MUS_SCOREEND:
|
|
|
|
default:
|
2008-03-04 06:36:23 +00:00
|
|
|
MusP = MaxMusP;
|
|
|
|
goto end;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
events[0] = time; // dwDeltaTime
|
|
|
|
events[1] = 0; // dwStreamID
|
|
|
|
events[2] = status | (mid1 << 8) | (mid2 << 16);
|
|
|
|
events += 3;
|
2008-03-04 06:36:23 +00:00
|
|
|
|
|
|
|
time = 0;
|
2006-02-24 04:48:15 +00:00
|
|
|
if (event & 128)
|
|
|
|
{
|
2008-03-04 06:36:23 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
t = MusBuffer[MusP++];
|
|
|
|
time = (time << 7) | (t & 127);
|
|
|
|
}
|
|
|
|
while (t & 128);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2008-03-04 06:36:23 +00:00
|
|
|
tot_time += time;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2008-03-04 06:36:23 +00:00
|
|
|
end:
|
|
|
|
if (time != 0)
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
events[0] = time; // dwDeltaTime
|
|
|
|
events[1] = 0; // dwStreamID
|
|
|
|
events[2] = MEVT_NOP << 24; // dwEvent
|
|
|
|
events += 3;
|
2008-03-04 06:36:23 +00:00
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
return events;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2008-04-03 02:31:39 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSSong2 :: GetOPLDumper
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
MusInfo *MUSSong2::GetOPLDumper(const char *filename)
|
|
|
|
{
|
2010-10-02 04:12:21 +00:00
|
|
|
return new MUSSong2(this, filename, MDEV_OPL);
|
2008-04-19 21:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSSong2 :: GetWaveDumper
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
MusInfo *MUSSong2::GetWaveDumper(const char *filename, int rate)
|
|
|
|
{
|
2010-10-02 04:12:21 +00:00
|
|
|
return new MUSSong2(this, filename, MDEV_GUS);
|
2008-04-03 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSSong2 OPL Dumping Constructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-10-02 04:12:21 +00:00
|
|
|
MUSSong2::MUSSong2(const MUSSong2 *original, const char *filename, EMidiDevice type)
|
2008-04-19 21:36:53 +00:00
|
|
|
: MIDIStreamer(filename, type)
|
2008-04-03 02:31:39 +00:00
|
|
|
{
|
|
|
|
int songstart = LittleShort(original->MusHeader->SongStart);
|
|
|
|
MaxMusP = original->MaxMusP;
|
|
|
|
MusHeader = (MUSHeader *)new BYTE[songstart + MaxMusP];
|
|
|
|
memcpy(MusHeader, original->MusHeader, songstart + MaxMusP);
|
|
|
|
MusBuffer = (BYTE *)MusHeader + songstart;
|
|
|
|
Division = 140;
|
|
|
|
InitialTempo = 1000000;
|
|
|
|
}
|
2010-04-06 03:12:32 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MUSHeaderSearch
|
|
|
|
//
|
|
|
|
// Searches for the MUS header within the given memory block, returning
|
|
|
|
// the offset it was found at, or -1 if not present.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int MUSHeaderSearch(const BYTE *head, int len)
|
|
|
|
{
|
|
|
|
len -= 4;
|
|
|
|
for (int i = 0; i <= len; ++i)
|
|
|
|
{
|
|
|
|
if (head[i+0] == 'M' && head[i+1] == 'U' && head[i+2] == 'S' && head[i+3] == 0x1A)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|