2008-03-05 03:10:31 +00:00
|
|
|
/*
|
2012-02-21 20:19:25 +00:00
|
|
|
** music_hmi_midiout.cpp
|
2010-09-02 23:17:58 +00:00
|
|
|
** Code to let ZDoom play HMI MIDI music through the MIDI streaming API.
|
2008-03-05 03:10:31 +00:00
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
2010-09-02 23:17:58 +00:00
|
|
|
** Copyright 2010 Randy Heit
|
2008-03-05 03:10:31 +00:00
|
|
|
** 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 ------------------------------------------------------------
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
#include "i_musicinterns.h"
|
|
|
|
#include "templates.h"
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "m_swap.h"
|
2014-06-25 11:25:36 +00:00
|
|
|
#include "files.h"
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// MACROS ------------------------------------------------------------------
|
|
|
|
|
2010-09-24 02:46:48 +00:00
|
|
|
#define HMP_NEW_DATE "013195"
|
|
|
|
#define HMI_SONG_MAGIC "HMI-MIDISONG061595"
|
2010-09-02 23:17:58 +00:00
|
|
|
#define TRACK_MAGIC "HMI-MIDITRACK"
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// Used by SendCommand to check for unexpected end-of-track conditions.
|
|
|
|
#define CHECK_FINISHED \
|
|
|
|
if (track->TrackP >= track->MaxTrackP) \
|
|
|
|
{ \
|
|
|
|
track->Finished = true; \
|
|
|
|
return events; \
|
|
|
|
}
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
// In song header
|
2012-02-21 20:19:25 +00:00
|
|
|
#define HMI_DIVISION_OFFSET 0xD4
|
2010-09-24 02:46:48 +00:00
|
|
|
#define HMI_TRACK_COUNT_OFFSET 0xE4
|
|
|
|
#define HMI_TRACK_DIR_PTR_OFFSET 0xE8
|
|
|
|
|
|
|
|
#define HMP_DIVISION_OFFSET 0x38
|
|
|
|
#define HMP_TRACK_COUNT_OFFSET 0x30
|
|
|
|
#define HMP_DESIGNATIONS_OFFSET 0x94
|
|
|
|
#define HMP_TRACK_OFFSET_0 0x308 // original HMP
|
|
|
|
#define HMP_TRACK_OFFSET_1 0x388 // newer HMP
|
2010-09-02 23:17:58 +00:00
|
|
|
|
|
|
|
// In track header
|
2010-09-24 02:46:48 +00:00
|
|
|
#define HMITRACK_DATA_PTR_OFFSET 0x57
|
|
|
|
#define HMITRACK_DESIGNATION_OFFSET 0x99
|
|
|
|
|
|
|
|
#define HMPTRACK_LEN_OFFSET 4
|
|
|
|
#define HMPTRACK_DESIGNATION_OFFSET 8
|
|
|
|
#define HMPTRACK_MIDI_DATA_OFFSET 12
|
2010-09-02 23:17:58 +00:00
|
|
|
|
2010-09-24 02:46:48 +00:00
|
|
|
#define NUM_HMP_DESIGNATIONS 5
|
|
|
|
#define NUM_HMI_DESIGNATIONS 8
|
2010-09-02 23:17:58 +00:00
|
|
|
|
|
|
|
// MIDI device types for designation
|
|
|
|
#define HMI_DEV_GM 0xA000 // Generic General MIDI (not a real device)
|
|
|
|
#define HMI_DEV_MPU401 0xA001 // MPU-401, Roland Sound Canvas, Ensoniq SoundScape, Rolad RAP-10
|
|
|
|
#define HMI_DEV_OPL2 0xA002 // SoundBlaster (Pro), ESS AudioDrive
|
|
|
|
#define HMI_DEV_MT32 0xA004 // MT-32
|
|
|
|
#define HMI_DEV_SBAWE32 0xA008 // SoundBlaster AWE32
|
|
|
|
#define HMI_DEV_OPL3 0xA009 // SoundBlaster 16, Microsoft Sound System, Pro Audio Spectrum 16
|
|
|
|
#define HMI_DEV_GUS 0xA00A // Gravis UltraSound, Gravis UltraSound Max/Ace
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// TYPES -------------------------------------------------------------------
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
struct HMISong::TrackInfo
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
const BYTE *TrackBegin;
|
|
|
|
size_t TrackP;
|
|
|
|
size_t MaxTrackP;
|
|
|
|
DWORD Delay;
|
2008-04-26 17:03:53 +00:00
|
|
|
DWORD PlayedTime;
|
2010-09-24 02:46:48 +00:00
|
|
|
WORD Designation[NUM_HMI_DESIGNATIONS];
|
2010-09-02 23:17:58 +00:00
|
|
|
bool Enabled;
|
2006-06-20 20:30:39 +00:00
|
|
|
bool Finished;
|
|
|
|
BYTE RunningStatus;
|
|
|
|
|
2010-09-24 02:46:48 +00:00
|
|
|
DWORD ReadVarLenHMI();
|
|
|
|
DWORD ReadVarLenHMP();
|
2006-06-20 20:30:39 +00:00
|
|
|
};
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
|
|
|
|
|
|
|
|
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
|
|
|
|
|
|
|
|
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
|
|
|
|
|
|
|
|
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
extern char MIDI_EventLengths[7];
|
|
|
|
extern char MIDI_CommonLengths[15];
|
2008-03-05 03:10:31 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
// PRIVATE DATA DEFINITIONS ------------------------------------------------
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// PUBLIC DATA DEFINITIONS -------------------------------------------------
|
|
|
|
|
|
|
|
// CODE --------------------------------------------------------------------
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong Constructor
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// Buffers the file and does some validation of the HMI header.
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2015-12-31 22:03:53 +00:00
|
|
|
HMISong::HMISong (FileReader &reader, EMidiDevice type, const char *args)
|
|
|
|
: MIDIStreamer(type, args), MusHeader(0), Tracks(0)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-30 00:04:09 +00:00
|
|
|
#ifdef _WIN32
|
2008-03-05 03:10:31 +00:00
|
|
|
if (ExitEvent == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-03-30 00:04:09 +00:00
|
|
|
#endif
|
2015-04-25 15:50:57 +00:00
|
|
|
int len = reader.GetLength();
|
2010-09-02 23:17:58 +00:00
|
|
|
if (len < 0x100)
|
|
|
|
{ // Way too small to be HMI.
|
|
|
|
return;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
MusHeader = new BYTE[len];
|
2008-04-03 02:31:39 +00:00
|
|
|
SongLen = len;
|
2010-09-24 02:46:48 +00:00
|
|
|
NumTracks = 0;
|
2015-04-25 15:50:57 +00:00
|
|
|
if (reader.Read(MusHeader, len) != len)
|
2014-06-25 11:25:36 +00:00
|
|
|
return;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2006-06-20 20:30:39 +00:00
|
|
|
// Do some validation of the MIDI file
|
2010-09-24 02:46:48 +00:00
|
|
|
if (memcmp(MusHeader, HMI_SONG_MAGIC, sizeof(HMI_SONG_MAGIC)) == 0)
|
|
|
|
{
|
|
|
|
SetupForHMI(len);
|
|
|
|
}
|
|
|
|
else if (((DWORD *)MusHeader)[0] == MAKE_ID('H','M','I','M') &&
|
|
|
|
((DWORD *)MusHeader)[1] == MAKE_ID('I','D','I','P'))
|
|
|
|
{
|
|
|
|
SetupForHMP(len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// HMISong Destructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
HMISong::~HMISong()
|
|
|
|
{
|
|
|
|
if (Tracks != NULL)
|
|
|
|
{
|
|
|
|
delete[] Tracks;
|
|
|
|
}
|
|
|
|
if (MusHeader != NULL)
|
|
|
|
{
|
|
|
|
delete[] MusHeader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// HMISong :: SetupForHMI
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void HMISong::SetupForHMI(int len)
|
|
|
|
{
|
|
|
|
int i, p;
|
|
|
|
|
|
|
|
ReadVarLen = ReadVarLenHMI;
|
|
|
|
NumTracks = GetShort(MusHeader + HMI_TRACK_COUNT_OFFSET);
|
2006-06-20 20:30:39 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
if (NumTracks <= 0)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
return;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// The division is the number of pulses per quarter note (PPQN).
|
2012-02-21 20:19:25 +00:00
|
|
|
// HMI files have two values here, a full value and a quarter value. Some games,
|
|
|
|
// notably Quarantines, have identical values for some reason, so it's safer to
|
|
|
|
// use the quarter value and multiply it by four than to trust the full value.
|
|
|
|
Division = GetShort(MusHeader + HMI_DIVISION_OFFSET) << 2;
|
2010-09-23 22:37:25 +00:00
|
|
|
InitialTempo = 4000000;
|
2006-06-20 20:30:39 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
Tracks = new TrackInfo[NumTracks + 1];
|
2010-09-24 02:46:48 +00:00
|
|
|
int track_dir = GetInt(MusHeader + HMI_TRACK_DIR_PTR_OFFSET);
|
2006-06-20 20:30:39 +00:00
|
|
|
|
|
|
|
// Gather information about each track
|
2010-09-02 23:17:58 +00:00
|
|
|
for (i = 0, p = 0; i < NumTracks; ++i)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
int start = GetInt(MusHeader + track_dir + i*4);
|
|
|
|
int tracklen, datastart;
|
|
|
|
|
2010-09-24 02:46:48 +00:00
|
|
|
if (start > len - HMITRACK_DESIGNATION_OFFSET - 4)
|
2010-09-02 23:17:58 +00:00
|
|
|
{ // Track is incomplete.
|
|
|
|
continue;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
// BTW, HMI does not actually check the track header.
|
|
|
|
if (memcmp(MusHeader + start, TRACK_MAGIC, 13) != 0)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
continue;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
// The track ends where the next one begins. If this is the
|
|
|
|
// last track, then it ends at the end of the file.
|
|
|
|
if (i == NumTracks - 1)
|
|
|
|
{
|
|
|
|
tracklen = len - start;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tracklen = GetInt(MusHeader + track_dir + i*4 + 4) - start;
|
|
|
|
}
|
|
|
|
// Clamp incomplete tracks to the end of the file.
|
|
|
|
tracklen = MIN(tracklen, len - start);
|
|
|
|
if (tracklen <= 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offset to actual MIDI events.
|
2010-09-24 02:46:48 +00:00
|
|
|
datastart = GetInt(MusHeader + start + HMITRACK_DATA_PTR_OFFSET);
|
2010-09-02 23:17:58 +00:00
|
|
|
tracklen -= datastart;
|
|
|
|
if (tracklen <= 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store track information
|
|
|
|
Tracks[p].TrackBegin = MusHeader + start + datastart;
|
|
|
|
Tracks[p].TrackP = 0;
|
|
|
|
Tracks[p].MaxTrackP = tracklen;
|
|
|
|
|
|
|
|
// Retrieve track designations. We can't check them yet, since we have not yet
|
|
|
|
// connected to the MIDI device.
|
2010-09-24 02:46:48 +00:00
|
|
|
for (int ii = 0; ii < NUM_HMI_DESIGNATIONS; ++ii)
|
2010-09-02 23:17:58 +00:00
|
|
|
{
|
2010-09-24 02:46:48 +00:00
|
|
|
Tracks[p].Designation[ii] = GetShort(MusHeader + start + HMITRACK_DESIGNATION_OFFSET + ii*2);
|
2010-09-02 23:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p++;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In case there were fewer actual chunks in the file than the
|
2010-09-02 23:17:58 +00:00
|
|
|
// header specified, update NumTracks with the current value of p.
|
|
|
|
NumTracks = p;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-24 02:46:48 +00:00
|
|
|
// HMISong :: SetupForHMP
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-09-24 02:46:48 +00:00
|
|
|
void HMISong::SetupForHMP(int len)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-09-24 02:46:48 +00:00
|
|
|
int track_data;
|
|
|
|
int i, p;
|
|
|
|
|
|
|
|
ReadVarLen = ReadVarLenHMP;
|
|
|
|
if (MusHeader[8] == 0)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-09-24 02:46:48 +00:00
|
|
|
track_data = HMP_TRACK_OFFSET_0;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2010-09-24 02:46:48 +00:00
|
|
|
else if (memcmp(MusHeader + 8, HMP_NEW_DATE, sizeof(HMP_NEW_DATE)) == 0)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2010-09-24 02:46:48 +00:00
|
|
|
track_data = HMP_TRACK_OFFSET_1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // unknown HMIMIDIP version
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NumTracks = GetInt(MusHeader + HMP_TRACK_COUNT_OFFSET);
|
|
|
|
|
|
|
|
if (NumTracks <= 0)
|
|
|
|
{
|
|
|
|
return;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
2010-09-24 02:46:48 +00:00
|
|
|
|
|
|
|
// The division is the number of pulses per quarter note (PPQN).
|
|
|
|
Division = GetInt(MusHeader + HMP_DIVISION_OFFSET);
|
|
|
|
InitialTempo = 1000000;
|
|
|
|
|
|
|
|
Tracks = new TrackInfo[NumTracks + 1];
|
|
|
|
|
|
|
|
// Gather information about each track
|
|
|
|
for (i = 0, p = 0; i < NumTracks; ++i)
|
|
|
|
{
|
|
|
|
int start = track_data;
|
|
|
|
int tracklen;
|
|
|
|
|
|
|
|
if (start > len - HMPTRACK_MIDI_DATA_OFFSET)
|
|
|
|
{ // Track is incomplete.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tracklen = GetInt(MusHeader + start + HMPTRACK_LEN_OFFSET);
|
|
|
|
track_data += tracklen;
|
|
|
|
|
|
|
|
// Clamp incomplete tracks to the end of the file.
|
|
|
|
tracklen = MIN(tracklen, len - start);
|
|
|
|
if (tracklen <= 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract track header size.
|
|
|
|
tracklen -= HMPTRACK_MIDI_DATA_OFFSET;
|
|
|
|
if (tracklen <= 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store track information
|
|
|
|
Tracks[p].TrackBegin = MusHeader + start + HMPTRACK_MIDI_DATA_OFFSET;
|
|
|
|
Tracks[p].TrackP = 0;
|
|
|
|
Tracks[p].MaxTrackP = tracklen;
|
|
|
|
|
|
|
|
// Retrieve track designations. We can't check them yet, since we have not yet
|
|
|
|
// connected to the MIDI device.
|
|
|
|
#if 0
|
|
|
|
// This is completely a guess based on knowledge of how designations work with
|
|
|
|
// HMI files. Some songs contain nothing but zeroes for this data, so I'd rather
|
|
|
|
// not go around using it without confirmation.
|
|
|
|
|
|
|
|
Printf("Track %d: %d %08x %d: \034I", i, GetInt(MusHeader + start),
|
|
|
|
GetInt(MusHeader + start + 4), GetInt(MusHeader + start + 8));
|
|
|
|
|
|
|
|
int designations = HMP_DESIGNATIONS_OFFSET +
|
|
|
|
GetInt(MusHeader + start + HMPTRACK_DESIGNATION_OFFSET) * 4 * NUM_HMP_DESIGNATIONS;
|
|
|
|
for (int ii = 0; ii < NUM_HMP_DESIGNATIONS; ++ii)
|
|
|
|
{
|
|
|
|
Printf(" %04x", GetInt(MusHeader + designations + ii*4));
|
|
|
|
}
|
|
|
|
Printf("\n");
|
|
|
|
#endif
|
|
|
|
Tracks[p].Designation[0] = HMI_DEV_GM;
|
|
|
|
Tracks[p].Designation[1] = HMI_DEV_GUS;
|
|
|
|
Tracks[p].Designation[2] = HMI_DEV_OPL2;
|
|
|
|
Tracks[p].Designation[3] = 0;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case there were fewer actual chunks in the file than the
|
|
|
|
// header specified, update NumTracks with the current value of p.
|
|
|
|
NumTracks = p;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: CheckCaps
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// Check track designations and disable tracks that have not been
|
|
|
|
// designated for the device we will be playing on.
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-09-03 05:08:05 +00:00
|
|
|
void HMISong::CheckCaps(int tech)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
// What's the equivalent HMI device for our technology?
|
2008-03-07 00:43:05 +00:00
|
|
|
if (tech == MOD_FMSYNTH)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
tech = HMI_DEV_OPL3;
|
2008-03-07 00:43:05 +00:00
|
|
|
}
|
|
|
|
else if (tech == MOD_MIDIPORT)
|
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
tech = HMI_DEV_MPU401;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Good enough? Or should we just say we're GM.
|
|
|
|
tech = HMI_DEV_SBAWE32;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < NumTracks; ++i)
|
|
|
|
{
|
|
|
|
Tracks[i].Enabled = false;
|
|
|
|
// Track designations are stored in a 0-terminated array.
|
2010-09-27 22:42:35 +00:00
|
|
|
for (unsigned int j = 0; j < countof(Tracks[i].Designation) && Tracks[i].Designation[j] != 0; ++j)
|
2010-09-02 23:17:58 +00:00
|
|
|
{
|
|
|
|
if (Tracks[i].Designation[j] == tech)
|
|
|
|
{
|
|
|
|
Tracks[i].Enabled = true;
|
|
|
|
}
|
|
|
|
// If a track is designated for device 0xA000, it will be played by a MIDI
|
|
|
|
// driver for device types 0xA000, 0xA001, and 0xA008. Why this does not
|
|
|
|
// include the GUS, I do not know.
|
|
|
|
else if (Tracks[i].Designation[j] == HMI_DEV_GM)
|
|
|
|
{
|
|
|
|
Tracks[i].Enabled = (tech == HMI_DEV_MPU401 || tech == HMI_DEV_SBAWE32);
|
|
|
|
}
|
|
|
|
// If a track is designated for device 0xA002, it will be played by a MIDI
|
|
|
|
// driver for device types 0xA002 or 0xA009.
|
|
|
|
else if (Tracks[i].Designation[j] == HMI_DEV_OPL2)
|
|
|
|
{
|
|
|
|
Tracks[i].Enabled = (tech == HMI_DEV_OPL3);
|
|
|
|
}
|
|
|
|
// Any other designation must match the specific MIDI driver device number.
|
|
|
|
// (Which we handled first above.)
|
|
|
|
|
|
|
|
if (Tracks[i].Enabled)
|
|
|
|
{ // This track's been enabled, so we can stop checking other designations.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
}
|
2006-06-20 20:30:39 +00:00
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: DoInitialSetup
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
// Sets the starting channel volumes.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
void HMISong :: DoInitialSetup()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
for (int i = 0; i < 16; ++i)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
ChannelVolumes[i] = 100;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: DoRestart
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
// Rewinds every track.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
void HMISong :: DoRestart()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// Set initial state.
|
2010-09-02 23:17:58 +00:00
|
|
|
FakeTrack = &Tracks[NumTracks];
|
|
|
|
NoteOffs.Clear();
|
|
|
|
for (i = 0; i <= NumTracks; ++i)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
Tracks[i].TrackP = 0;
|
|
|
|
Tracks[i].Finished = false;
|
|
|
|
Tracks[i].RunningStatus = 0;
|
2008-04-26 17:03:53 +00:00
|
|
|
Tracks[i].PlayedTime = 0;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
ProcessInitialMetaEvents ();
|
|
|
|
for (i = 0; i < NumTracks; ++i)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-09-24 02:46:48 +00:00
|
|
|
Tracks[i].Delay = ReadVarLen(&Tracks[i]);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2010-09-02 23:17:58 +00:00
|
|
|
Tracks[i].Delay = 0; // for the FakeTrack
|
|
|
|
Tracks[i].Enabled = true;
|
2008-03-05 03:10:31 +00:00
|
|
|
TrackDue = Tracks;
|
|
|
|
TrackDue = FindNextDue();
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: CheckDone
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
bool HMISong::CheckDone()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
return TrackDue == NULL;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: MakeEvents
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// Copies MIDI events from the file and puts them into a MIDI stream
|
2008-03-05 03:10:31 +00:00
|
|
|
// buffer. Returns the new position in the buffer.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
DWORD *HMISong::MakeEvents(DWORD *events, DWORD *max_event_p, DWORD max_time)
|
2008-03-05 03:10:31 +00:00
|
|
|
{
|
2008-03-30 02:51:34 +00:00
|
|
|
DWORD *start_events;
|
2008-03-05 03:10:31 +00:00
|
|
|
DWORD tot_time = 0;
|
|
|
|
DWORD time = 0;
|
2008-03-30 02:51:34 +00:00
|
|
|
DWORD delay;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-30 02:51:34 +00:00
|
|
|
start_events = events;
|
2008-03-05 03:10:31 +00:00
|
|
|
while (TrackDue && events < max_event_p && tot_time <= max_time)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-04-26 17:03:53 +00:00
|
|
|
// It's possible that this tick may be nothing but meta-events and
|
2008-03-30 02:51:34 +00:00
|
|
|
// not generate any real events. Repeat this until we actually
|
|
|
|
// get some output so we don't send an empty buffer to the MIDI
|
|
|
|
// device.
|
|
|
|
do
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-30 02:51:34 +00:00
|
|
|
delay = TrackDue->Delay;
|
|
|
|
time += delay;
|
|
|
|
// Advance time for all tracks by the amount needed for the one up next.
|
|
|
|
tot_time += delay * Tempo / Division;
|
|
|
|
AdvanceTracks(delay);
|
|
|
|
// Play all events for this tick.
|
|
|
|
do
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2016-01-21 00:37:05 +00:00
|
|
|
bool sysex_noroom = false;
|
|
|
|
DWORD *new_events = SendCommand(events, TrackDue, time, max_event_p - events, sysex_noroom);
|
|
|
|
if (sysex_noroom)
|
|
|
|
{
|
|
|
|
return events;
|
|
|
|
}
|
2008-03-30 02:51:34 +00:00
|
|
|
TrackDue = FindNextDue();
|
|
|
|
if (new_events != events)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2008-03-30 02:51:34 +00:00
|
|
|
time = 0;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
2008-03-30 02:51:34 +00:00
|
|
|
events = new_events;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
2008-03-30 02:51:34 +00:00
|
|
|
while (TrackDue && TrackDue->Delay == 0 && events < max_event_p);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2008-03-30 02:51:34 +00:00
|
|
|
while (start_events == events && TrackDue);
|
|
|
|
time = 0;
|
|
|
|
}
|
|
|
|
return events;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: AdvanceTracks
|
2008-03-30 02:51:34 +00:00
|
|
|
//
|
2010-09-28 03:58:41 +00:00
|
|
|
// Advances time for all tracks by the specified amount.
|
2008-03-30 02:51:34 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
void HMISong::AdvanceTracks(DWORD time)
|
2008-03-30 02:51:34 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
for (int i = 0; i <= NumTracks; ++i)
|
2008-03-30 02:51:34 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
if (Tracks[i].Enabled && !Tracks[i].Finished)
|
2008-03-05 03:10:31 +00:00
|
|
|
{
|
2008-03-30 02:51:34 +00:00
|
|
|
Tracks[i].Delay -= time;
|
2008-04-26 17:03:53 +00:00
|
|
|
Tracks[i].PlayedTime += time;
|
2008-03-05 03:10:31 +00:00
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2010-09-02 23:17:58 +00:00
|
|
|
NoteOffs.AdvanceTime(time);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: SendCommand
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
// Places a single MIDIEVENT in the event buffer.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-06-20 20:30:39 +00:00
|
|
|
|
2016-01-21 00:37:05 +00:00
|
|
|
DWORD *HMISong::SendCommand (DWORD *events, TrackInfo *track, DWORD delay, ptrdiff_t room, bool &sysex_noroom)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
BYTE event, data1 = 0, data2 = 0;
|
2010-09-02 23:17:58 +00:00
|
|
|
|
|
|
|
// If the next event comes from the fake track, pop an entry off the note-off queue.
|
|
|
|
if (track == FakeTrack)
|
|
|
|
{
|
|
|
|
AutoNoteOff off;
|
|
|
|
NoteOffs.Pop(off);
|
|
|
|
events[0] = delay;
|
|
|
|
events[1] = 0;
|
|
|
|
events[2] = MIDI_NOTEON | off.Channel | (off.Key << 8);
|
|
|
|
return events + 3;
|
|
|
|
}
|
2006-06-20 20:30:39 +00:00
|
|
|
|
2016-01-21 00:37:05 +00:00
|
|
|
sysex_noroom = false;
|
|
|
|
size_t start_p = track->TrackP;
|
|
|
|
|
2006-06-20 20:30:39 +00:00
|
|
|
CHECK_FINISHED
|
|
|
|
event = track->TrackBegin[track->TrackP++];
|
|
|
|
CHECK_FINISHED
|
|
|
|
|
2016-01-21 00:37:05 +00:00
|
|
|
// The actual event type will be filled in below. If it's not a NOP,
|
|
|
|
// the events pointer will be advanced once the actual event is written.
|
|
|
|
// Otherwise, we do it at the end of the function.
|
|
|
|
events[0] = delay;
|
|
|
|
events[1] = 0;
|
|
|
|
events[2] = MEVT_NOP << 24;
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
if (event != MIDI_SYSEX && event != MIDI_META && event != MIDI_SYSEXEND && event != 0xFe)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
// Normal short message
|
|
|
|
if ((event & 0xF0) == 0xF0)
|
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
if (MIDI_CommonLengths[event & 15] > 0)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
data1 = track->TrackBegin[track->TrackP++];
|
2010-09-02 23:17:58 +00:00
|
|
|
if (MIDI_CommonLengths[event & 15] > 1)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
data2 = track->TrackBegin[track->TrackP++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((event & 0x80) == 0)
|
|
|
|
{
|
|
|
|
data1 = event;
|
|
|
|
event = track->RunningStatus;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
track->RunningStatus = event;
|
|
|
|
data1 = track->TrackBegin[track->TrackP++];
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_FINISHED
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
if (MIDI_EventLengths[(event&0x70)>>4] == 2)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
data2 = track->TrackBegin[track->TrackP++];
|
|
|
|
}
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
// Monitor channel volume controller changes.
|
|
|
|
if ((event & 0x70) == (MIDI_CTRLCHANGE & 0x70) && data1 == 7)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
data2 = VolumeControllerChange(event & 15, data2);
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
2010-09-02 23:17:58 +00:00
|
|
|
|
|
|
|
if (event != MIDI_META)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2008-04-26 17:12:37 +00:00
|
|
|
events[2] = event | (data1<<8) | (data2<<16);
|
|
|
|
}
|
2010-09-02 23:17:58 +00:00
|
|
|
|
2010-09-24 02:46:48 +00:00
|
|
|
if (ReadVarLen == ReadVarLenHMI && (event & 0x70) == (MIDI_NOTEON & 0x70))
|
2010-09-02 23:17:58 +00:00
|
|
|
{ // HMI note on events include the time until an implied note off event.
|
2010-09-24 02:46:48 +00:00
|
|
|
NoteOffs.AddNoteOff(track->ReadVarLenHMI(), event & 0x0F, data1);
|
2010-09-02 23:17:58 +00:00
|
|
|
}
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-21 00:37:05 +00:00
|
|
|
// SysEx events could potentially not have enough room in the buffer...
|
2008-03-05 03:10:31 +00:00
|
|
|
if (event == MIDI_SYSEX || event == MIDI_SYSEXEND)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2010-09-24 02:46:48 +00:00
|
|
|
len = ReadVarLen(track);
|
2016-01-21 00:37:05 +00:00
|
|
|
if (len >= (MAX_EVENTS-1)*3*4)
|
|
|
|
{ // This message will never fit. Throw it away.
|
|
|
|
track->TrackP += len;
|
|
|
|
}
|
|
|
|
else if (len + 12 >= (size_t)room * 4)
|
|
|
|
{ // Not enough room left in this buffer. Backup and wait for the next one.
|
|
|
|
track->TrackP = start_p;
|
|
|
|
sysex_noroom = true;
|
|
|
|
return events;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BYTE *msg = (BYTE *)&events[3];
|
|
|
|
if (event == MIDI_SYSEX)
|
|
|
|
{ // Need to add the SysEx marker to the message.
|
|
|
|
events[2] = (MEVT_LONGMSG << 24) | (len + 1);
|
|
|
|
*msg++ = MIDI_SYSEX;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
events[2] = (MEVT_LONGMSG << 24) | len;
|
|
|
|
}
|
|
|
|
memcpy(msg, &track->TrackBegin[track->TrackP], len);
|
|
|
|
msg += len;
|
|
|
|
// Must pad with 0
|
|
|
|
while ((size_t)msg & 3)
|
|
|
|
{
|
|
|
|
*msg++ = 0;
|
|
|
|
}
|
|
|
|
track->TrackP += len;
|
|
|
|
}
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
else if (event == MIDI_META)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
// It's a meta-event
|
|
|
|
event = track->TrackBegin[track->TrackP++];
|
|
|
|
CHECK_FINISHED
|
2010-09-24 02:46:48 +00:00
|
|
|
len = ReadVarLen(track);
|
2006-06-20 20:30:39 +00:00
|
|
|
CHECK_FINISHED
|
|
|
|
|
|
|
|
if (track->TrackP + len <= track->MaxTrackP)
|
|
|
|
{
|
|
|
|
switch (event)
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
case MIDI_META_EOT:
|
2006-06-20 20:30:39 +00:00
|
|
|
track->Finished = true;
|
|
|
|
break;
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
case MIDI_META_TEMPO:
|
2006-06-20 20:30:39 +00:00
|
|
|
Tempo =
|
|
|
|
(track->TrackBegin[track->TrackP+0]<<16) |
|
|
|
|
(track->TrackBegin[track->TrackP+1]<<8) |
|
|
|
|
(track->TrackBegin[track->TrackP+2]);
|
2008-03-05 03:10:31 +00:00
|
|
|
events[0] = delay;
|
|
|
|
events[1] = 0;
|
|
|
|
events[2] = (MEVT_TEMPO << 24) | Tempo;
|
2006-06-20 20:30:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
track->TrackP += len;
|
|
|
|
if (track->TrackP == track->MaxTrackP)
|
|
|
|
{
|
|
|
|
track->Finished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
track->Finished = true;
|
|
|
|
}
|
|
|
|
}
|
2010-09-02 23:17:58 +00:00
|
|
|
else if (event == 0xFE)
|
|
|
|
{ // Skip unknown HMI events.
|
|
|
|
event = track->TrackBegin[track->TrackP++];
|
|
|
|
CHECK_FINISHED
|
|
|
|
if (event == 0x13 || event == 0x15)
|
|
|
|
{
|
|
|
|
track->TrackP += 6;
|
|
|
|
}
|
|
|
|
else if (event == 0x12 || event == 0x14)
|
|
|
|
{
|
|
|
|
track->TrackP += 2;
|
|
|
|
}
|
|
|
|
else if (event == 0x10)
|
|
|
|
{
|
|
|
|
track->TrackP += 2;
|
|
|
|
CHECK_FINISHED
|
|
|
|
track->TrackP += track->TrackBegin[track->TrackP] + 5;
|
|
|
|
CHECK_FINISHED
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // No idea.
|
|
|
|
track->Finished = true;
|
|
|
|
}
|
|
|
|
}
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
if (!track->Finished)
|
|
|
|
{
|
2010-09-24 02:46:48 +00:00
|
|
|
track->Delay = ReadVarLen(track);
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
2016-01-21 00:37:05 +00:00
|
|
|
// Advance events pointer unless this is a non-delaying NOP.
|
|
|
|
if (events[0] != 0 || MEVT_EVENTTYPE(events[2]) != MEVT_NOP)
|
|
|
|
{
|
|
|
|
if (MEVT_EVENTTYPE(events[2]) == MEVT_LONGMSG)
|
|
|
|
{
|
|
|
|
events += 3 + ((MEVT_EVENTPARM(events[2]) + 3) >> 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
events += 3;
|
|
|
|
}
|
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
return events;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: ProcessInitialMetaEvents
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
// Handle all the meta events at the start of each track.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-06-20 20:30:39 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
void HMISong::ProcessInitialMetaEvents ()
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
TrackInfo *track;
|
|
|
|
int i;
|
|
|
|
BYTE event;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
for (i = 0; i < NumTracks; ++i)
|
|
|
|
{
|
|
|
|
track = &Tracks[i];
|
|
|
|
while (!track->Finished &&
|
|
|
|
track->TrackP < track->MaxTrackP - 4 &&
|
|
|
|
track->TrackBegin[track->TrackP] == 0 &&
|
|
|
|
track->TrackBegin[track->TrackP+1] == 0xFF)
|
|
|
|
{
|
|
|
|
event = track->TrackBegin[track->TrackP+2];
|
|
|
|
track->TrackP += 3;
|
2010-09-24 02:46:48 +00:00
|
|
|
len = ReadVarLen(track);
|
2006-06-20 20:30:39 +00:00
|
|
|
if (track->TrackP + len <= track->MaxTrackP)
|
|
|
|
{
|
|
|
|
switch (event)
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
case MIDI_META_EOT:
|
2006-06-20 20:30:39 +00:00
|
|
|
track->Finished = true;
|
|
|
|
break;
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
case MIDI_META_TEMPO:
|
|
|
|
SetTempo(
|
2006-06-20 20:30:39 +00:00
|
|
|
(track->TrackBegin[track->TrackP+0]<<16) |
|
|
|
|
(track->TrackBegin[track->TrackP+1]<<8) |
|
2008-03-05 03:10:31 +00:00
|
|
|
(track->TrackBegin[track->TrackP+2])
|
|
|
|
);
|
2006-06-20 20:30:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
track->TrackP += len;
|
|
|
|
}
|
|
|
|
if (track->TrackP >= track->MaxTrackP - 4)
|
|
|
|
{
|
|
|
|
track->Finished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-24 02:46:48 +00:00
|
|
|
// HMISong :: ReadVarLenHMI static
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
DWORD HMISong::ReadVarLenHMI(TrackInfo *track)
|
|
|
|
{
|
|
|
|
return track->ReadVarLenHMI();
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// HMISong :: ReadVarLenHMP static
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
DWORD HMISong::ReadVarLenHMP(TrackInfo *track)
|
|
|
|
{
|
|
|
|
return track->ReadVarLenHMP();
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// HMISong :: TrackInfo :: ReadVarLenHMI
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
// Reads a variable-length SMF number.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-09-24 02:46:48 +00:00
|
|
|
DWORD HMISong::TrackInfo::ReadVarLenHMI()
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
DWORD time = 0, t = 0x80;
|
|
|
|
|
|
|
|
while ((t & 0x80) && TrackP < MaxTrackP)
|
|
|
|
{
|
|
|
|
t = TrackBegin[TrackP++];
|
|
|
|
time = (time << 7) | (t & 127);
|
|
|
|
}
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:46:48 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// HMISong :: TrackInfo :: ReadVarLenHMP
|
|
|
|
//
|
|
|
|
// Reads a variable-length HMP number. This is similar to the standard SMF
|
|
|
|
// variable length number, except it's stored little-endian, and the high
|
|
|
|
// bit set means the number is done.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
DWORD HMISong::TrackInfo::ReadVarLenHMP()
|
|
|
|
{
|
|
|
|
DWORD time = 0;
|
|
|
|
BYTE t = 0;
|
|
|
|
int off = 0;
|
|
|
|
|
|
|
|
while (!(t & 0x80) && TrackP < MaxTrackP)
|
|
|
|
{
|
|
|
|
t = TrackBegin[TrackP++];
|
|
|
|
time |= (t & 127) << off;
|
|
|
|
off += 7;
|
|
|
|
}
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-28 03:58:41 +00:00
|
|
|
// NoteOffQueue :: AddNoteOff
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-09-28 03:58:41 +00:00
|
|
|
void NoteOffQueue::AddNoteOff(DWORD delay, BYTE channel, BYTE key)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
unsigned int i = Reserve(1);
|
|
|
|
while (i > 0 && (*this)[Parent(i)].Delay > delay)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
(*this)[i] = (*this)[Parent(i)];
|
|
|
|
i = Parent(i);
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
2010-09-02 23:17:58 +00:00
|
|
|
(*this)[i].Delay = delay;
|
|
|
|
(*this)[i].Channel = channel;
|
|
|
|
(*this)[i].Key = key;
|
|
|
|
}
|
2006-06-20 20:30:39 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-28 03:58:41 +00:00
|
|
|
// NoteOffQueue :: Pop
|
2010-09-02 23:17:58 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-06-20 20:30:39 +00:00
|
|
|
|
2010-09-28 03:58:41 +00:00
|
|
|
bool NoteOffQueue::Pop(AutoNoteOff &item)
|
2010-09-02 23:17:58 +00:00
|
|
|
{
|
|
|
|
item = (*this)[0];
|
2010-09-04 18:21:51 +00:00
|
|
|
if (TArray<AutoNoteOff>::Pop((*this)[0]))
|
2010-09-02 23:17:58 +00:00
|
|
|
{
|
|
|
|
Heapify();
|
|
|
|
return true;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
2010-09-02 23:17:58 +00:00
|
|
|
return false;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-28 03:58:41 +00:00
|
|
|
// NoteOffQueue :: AdvanceTime
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
//==========================================================================
|
|
|
|
|
2010-09-28 03:58:41 +00:00
|
|
|
void NoteOffQueue::AdvanceTime(DWORD time)
|
2010-09-02 23:17:58 +00:00
|
|
|
{
|
|
|
|
// Because the time is decreasing by the same amount for every entry,
|
|
|
|
// the heap property is maintained.
|
|
|
|
for (unsigned int i = 0; i < Size(); ++i)
|
|
|
|
{
|
|
|
|
assert((*this)[i].Delay >= time);
|
|
|
|
(*this)[i].Delay -= time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-28 03:58:41 +00:00
|
|
|
// NoteOffQueue :: Heapify
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-09-28 03:58:41 +00:00
|
|
|
void NoteOffQueue::Heapify()
|
2008-03-05 03:10:31 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
unsigned int i = 0;
|
|
|
|
for (;;)
|
2008-03-05 03:10:31 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
unsigned int l = Left(i);
|
|
|
|
unsigned int r = Right(i);
|
|
|
|
unsigned int smallest = i;
|
|
|
|
if (l < Size() && (*this)[l].Delay < (*this)[i].Delay)
|
|
|
|
{
|
|
|
|
smallest = l;
|
|
|
|
}
|
|
|
|
if (r < Size() && (*this)[r].Delay < (*this)[smallest].Delay)
|
|
|
|
{
|
|
|
|
smallest = r;
|
|
|
|
}
|
|
|
|
if (smallest == i)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
swapvalues((*this)[i], (*this)[smallest]);
|
|
|
|
i = smallest;
|
2008-03-05 03:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-03 02:31:39 +00:00
|
|
|
|
2008-04-09 03:55:04 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: FindNextDue
|
2008-04-09 03:55:04 +00:00
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// Scans every track for the next event to play. Returns NULL if all events
|
|
|
|
// have been consumed.
|
2008-04-09 03:55:04 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
HMISong::TrackInfo *HMISong::FindNextDue ()
|
2008-04-09 03:55:04 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
TrackInfo *track;
|
|
|
|
DWORD best;
|
|
|
|
int i;
|
|
|
|
|
2010-09-28 03:58:41 +00:00
|
|
|
// Give precedence to whichever track last had events taken from it.
|
2010-09-02 23:17:58 +00:00
|
|
|
if (TrackDue != FakeTrack && !TrackDue->Finished && TrackDue->Delay == 0)
|
2008-04-09 03:55:04 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
return TrackDue;
|
|
|
|
}
|
2010-09-28 03:58:41 +00:00
|
|
|
if (TrackDue == FakeTrack && NoteOffs.Size() != 0 && NoteOffs[0].Delay == 0)
|
|
|
|
{
|
|
|
|
FakeTrack->Delay = 0;
|
|
|
|
return FakeTrack;
|
|
|
|
}
|
2008-04-09 03:55:04 +00:00
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
// Check regular tracks.
|
|
|
|
track = NULL;
|
|
|
|
best = 0xFFFFFFFF;
|
|
|
|
for (i = 0; i < NumTracks; ++i)
|
|
|
|
{
|
|
|
|
if (Tracks[i].Enabled && !Tracks[i].Finished && Tracks[i].Delay < best)
|
2008-04-09 03:55:04 +00:00
|
|
|
{
|
2010-09-02 23:17:58 +00:00
|
|
|
best = Tracks[i].Delay;
|
|
|
|
track = &Tracks[i];
|
2008-04-09 03:55:04 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-02 23:17:58 +00:00
|
|
|
// Check automatic note-offs.
|
|
|
|
if (NoteOffs.Size() != 0 && NoteOffs[0].Delay <= best)
|
|
|
|
{
|
|
|
|
FakeTrack->Delay = NoteOffs[0].Delay;
|
|
|
|
return FakeTrack;
|
|
|
|
}
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
2008-04-09 03:55:04 +00:00
|
|
|
|
2008-04-03 02:31:39 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: GetOPLDumper
|
2008-04-03 02:31:39 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
MusInfo *HMISong::GetOPLDumper(const char *filename)
|
2008-04-03 02:31:39 +00:00
|
|
|
{
|
2010-10-02 04:12:21 +00:00
|
|
|
return new HMISong(this, filename, MDEV_OPL);
|
2008-04-03 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong :: GetWaveDumper
|
2008-04-03 02:31:39 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-09-02 23:17:58 +00:00
|
|
|
MusInfo *HMISong::GetWaveDumper(const char *filename, int rate)
|
2008-04-19 21:36:53 +00:00
|
|
|
{
|
2010-10-02 04:12:21 +00:00
|
|
|
return new HMISong(this, filename, MDEV_GUS);
|
2008-04-19 21:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2010-09-02 23:17:58 +00:00
|
|
|
// HMISong File Dumping Constructor
|
2008-04-19 21:36:53 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2010-10-02 04:12:21 +00:00
|
|
|
HMISong::HMISong(const HMISong *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
|
|
|
{
|
|
|
|
SongLen = original->SongLen;
|
|
|
|
MusHeader = new BYTE[original->SongLen];
|
|
|
|
memcpy(MusHeader, original->MusHeader, original->SongLen);
|
|
|
|
NumTracks = original->NumTracks;
|
|
|
|
Division = original->Division;
|
|
|
|
Tempo = InitialTempo = original->InitialTempo;
|
|
|
|
Tracks = new TrackInfo[NumTracks];
|
|
|
|
for (int i = 0; i < NumTracks; ++i)
|
|
|
|
{
|
|
|
|
TrackInfo *newtrack = &Tracks[i];
|
|
|
|
const TrackInfo *oldtrack = &original->Tracks[i];
|
|
|
|
|
|
|
|
newtrack->TrackBegin = MusHeader + (oldtrack->TrackBegin - original->MusHeader);
|
|
|
|
newtrack->TrackP = 0;
|
|
|
|
newtrack->MaxTrackP = oldtrack->MaxTrackP;
|
|
|
|
}
|
|
|
|
}
|