2008-03-05 03:10:31 +00:00
|
|
|
/*
|
|
|
|
** music_midi_midiout.cpp
|
|
|
|
** Code to let ZDoom play SMF MIDI 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.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
** This file also supports the Apogee Sound System's EMIDI files. That
|
|
|
|
** basically means you can play the Duke3D songs without any editing and
|
|
|
|
** have them sound right.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// HEADER FILES ------------------------------------------------------------
|
|
|
|
|
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 ------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define MAX_TIME (1000000/20) // Send out 1/20 of a sec of events at a time.
|
|
|
|
|
|
|
|
// Used by SendCommand to check for unexpected end-of-track conditions.
|
|
|
|
#define CHECK_FINISHED \
|
|
|
|
if (track->TrackP >= track->MaxTrackP) \
|
|
|
|
{ \
|
|
|
|
track->Finished = true; \
|
|
|
|
return events; \
|
|
|
|
}
|
|
|
|
|
|
|
|
// TYPES -------------------------------------------------------------------
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2006-06-20 20:30:39 +00:00
|
|
|
struct MIDISong2::TrackInfo
|
|
|
|
{
|
|
|
|
const BYTE *TrackBegin;
|
|
|
|
size_t TrackP;
|
|
|
|
size_t MaxTrackP;
|
|
|
|
DWORD Delay;
|
|
|
|
bool Finished;
|
|
|
|
BYTE RunningStatus;
|
|
|
|
SBYTE LoopCount;
|
|
|
|
bool Designated;
|
|
|
|
bool EProgramChange;
|
|
|
|
bool EVolume;
|
|
|
|
WORD Designation;
|
|
|
|
|
|
|
|
size_t LoopBegin;
|
|
|
|
DWORD LoopDelay;
|
|
|
|
bool LoopFinished;
|
|
|
|
|
|
|
|
DWORD ReadVarLen ();
|
|
|
|
};
|
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 ----------------------------------------------
|
|
|
|
|
|
|
|
// PRIVATE DATA DEFINITIONS ------------------------------------------------
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
static BYTE EventLengths[7] = { 2, 2, 2, 2, 1, 1, 2 };
|
|
|
|
static BYTE CommonLengths[15] = { 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// PUBLIC DATA DEFINITIONS -------------------------------------------------
|
|
|
|
|
|
|
|
// CODE --------------------------------------------------------------------
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 Constructor
|
|
|
|
//
|
|
|
|
// Buffers the file and does some validation of the SMF header.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2008-03-29 05:01:38 +00:00
|
|
|
MIDISong2::MIDISong2 (FILE *file, char *musiccache, int len, bool opl)
|
|
|
|
: MIDIStreamer(opl), MusHeader(0), Tracks(0)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
int p;
|
|
|
|
int i;
|
|
|
|
|
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
|
2006-02-24 04:48:15 +00:00
|
|
|
MusHeader = new BYTE[len];
|
2006-04-14 12:58:52 +00:00
|
|
|
if (file != NULL)
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
if (fread(MusHeader, 1, len, file) != (size_t)len)
|
2006-04-14 12:58:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(MusHeader, musiccache, len);
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2006-06-20 20:30:39 +00:00
|
|
|
// Do some validation of the MIDI file
|
|
|
|
if (MusHeader[4] != 0 || MusHeader[5] != 0 || MusHeader[6] != 0 || MusHeader[7] != 6)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (MusHeader[8] != 0 || MusHeader[9] > 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Format = MusHeader[9];
|
|
|
|
|
|
|
|
if (Format == 0)
|
|
|
|
{
|
|
|
|
NumTracks = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NumTracks = MusHeader[10] * 256 + MusHeader[11];
|
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
// The division is the number of pulses per quarter note (PPQN).
|
|
|
|
Division = MusHeader[12] * 256 + MusHeader[13];
|
|
|
|
if (Division == 0)
|
|
|
|
{ // PPQN is zero? Then the song cannot play because it never pulses.
|
|
|
|
return;
|
|
|
|
}
|
2006-06-20 20:30:39 +00:00
|
|
|
|
|
|
|
Tracks = new TrackInfo[NumTracks];
|
|
|
|
|
|
|
|
// Gather information about each track
|
|
|
|
for (i = 0, p = 14; i < NumTracks && p < len + 8; ++i)
|
|
|
|
{
|
|
|
|
DWORD chunkLen =
|
|
|
|
(MusHeader[p+4]<<24) |
|
|
|
|
(MusHeader[p+5]<<16) |
|
|
|
|
(MusHeader[p+6]<<8) |
|
|
|
|
(MusHeader[p+7]);
|
|
|
|
|
|
|
|
if (chunkLen + p + 8 > (DWORD)len)
|
|
|
|
{ // Track too long, so truncate it
|
|
|
|
chunkLen = len - p - 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MusHeader[p+0] == 'M' &&
|
|
|
|
MusHeader[p+1] == 'T' &&
|
|
|
|
MusHeader[p+2] == 'r' &&
|
|
|
|
MusHeader[p+3] == 'k')
|
|
|
|
{
|
|
|
|
Tracks[i].TrackBegin = MusHeader + p + 8;
|
|
|
|
Tracks[i].TrackP = 0;
|
|
|
|
Tracks[i].MaxTrackP = chunkLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += chunkLen + 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case there were fewer actual chunks in the file than the
|
|
|
|
// header specified, update NumTracks with the current value of i
|
|
|
|
NumTracks = i;
|
|
|
|
|
|
|
|
if (NumTracks == 0)
|
|
|
|
{ // No tracks, so nothing to play
|
|
|
|
return;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 Destructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
MIDISong2::~MIDISong2 ()
|
|
|
|
{
|
|
|
|
if (Tracks != NULL)
|
|
|
|
{
|
|
|
|
delete[] Tracks;
|
|
|
|
}
|
2006-06-20 20:30:39 +00:00
|
|
|
if (MusHeader != NULL)
|
|
|
|
{
|
|
|
|
delete[] MusHeader;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 :: CheckCaps
|
|
|
|
//
|
|
|
|
// Find out if this is an FM synth or not for EMIDI's benefit.
|
2008-03-07 00:43:05 +00:00
|
|
|
// (Do any released EMIDIs use track designations?)
|
2008-03-05 03:10:31 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-07 00:43:05 +00:00
|
|
|
void MIDISong2::CheckCaps()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-07 00:43:05 +00:00
|
|
|
int tech = MIDI->GetTechnology();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
DesignationMask = 0xFF0F;
|
2008-03-07 00:43:05 +00:00
|
|
|
if (tech == MOD_FMSYNTH)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2008-03-07 00:43:05 +00:00
|
|
|
DesignationMask = 0x00F0;
|
|
|
|
}
|
|
|
|
else if (tech == MOD_MIDIPORT)
|
|
|
|
{
|
|
|
|
DesignationMask = 0x0001;
|
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
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 :: DoInitialSetup
|
|
|
|
//
|
|
|
|
// Sets the starting channel volumes.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
void MIDISong2 :: 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
|
|
|
// The ASS uses a default volume of 90, but all the other
|
|
|
|
// sources I can find say it's 100. Ideally, any song that
|
|
|
|
// cares about its volume is going to initialize it to
|
|
|
|
// whatever it wants and override this default.
|
|
|
|
ChannelVolumes[i] = 100;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 :: DoRestart
|
|
|
|
//
|
|
|
|
// Rewinds every track.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
void MIDISong2 :: DoRestart()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// Set initial state.
|
|
|
|
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;
|
|
|
|
Tracks[i].Designated = false;
|
|
|
|
Tracks[i].Designation = 0;
|
|
|
|
Tracks[i].LoopCount = -1;
|
|
|
|
Tracks[i].EProgramChange = false;
|
|
|
|
Tracks[i].EVolume = false;
|
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
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
Tracks[i].Delay = Tracks[i].ReadVarLen();
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
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
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 :: CheckDone
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
bool MIDISong2::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
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 :: MakeEvents
|
|
|
|
//
|
|
|
|
// Copies MIDI events from the SMF 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 *MIDISong2::MakeEvents(DWORD *events, DWORD *max_event_p, DWORD max_time)
|
|
|
|
{
|
|
|
|
DWORD tot_time = 0;
|
|
|
|
DWORD time = 0;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
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-03-05 03:10:31 +00:00
|
|
|
time = TrackDue->Delay;
|
|
|
|
// Advance time for all tracks by the amount needed for the one up next.
|
|
|
|
if (time != 0)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
tot_time += time * Tempo / Division;
|
|
|
|
for (int i = 0; i < NumTracks; ++i)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
if (!Tracks[i].Finished)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
Tracks[i].Delay -= time;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
// Play all events for this tic.
|
|
|
|
do
|
|
|
|
{
|
|
|
|
events = SendCommand(events, TrackDue, time);
|
|
|
|
TrackDue = FindNextDue();
|
|
|
|
time = 0;
|
|
|
|
}
|
|
|
|
while (TrackDue && TrackDue->Delay == 0 && events < max_event_p);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
return events;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 :: SendCommand
|
|
|
|
//
|
|
|
|
// Places a single MIDIEVENT in the event buffer.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-06-20 20:30:39 +00:00
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
DWORD *MIDISong2::SendCommand (DWORD *events, TrackInfo *track, DWORD delay)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
BYTE event, data1 = 0, data2 = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
CHECK_FINISHED
|
|
|
|
event = track->TrackBegin[track->TrackP++];
|
|
|
|
CHECK_FINISHED
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
if (event != MIDI_SYSEX && event != MIDI_META && event != MIDI_SYSEXEND)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
// Normal short message
|
|
|
|
if ((event & 0xF0) == 0xF0)
|
|
|
|
{
|
|
|
|
if (CommonLengths[event & 15] > 0)
|
|
|
|
{
|
|
|
|
data1 = track->TrackBegin[track->TrackP++];
|
|
|
|
if (CommonLengths[event & 15] > 1)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
|
if (EventLengths[(event&0x70)>>4] == 2)
|
|
|
|
{
|
|
|
|
data2 = track->TrackBegin[track->TrackP++];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event & 0x70)
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
case MIDI_PRGMCHANGE & 0x70:
|
2006-06-20 20:30:39 +00:00
|
|
|
if (track->EProgramChange)
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
event = MIDI_META;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
case MIDI_CTRLCHANGE & 0x70:
|
2006-06-20 20:30:39 +00:00
|
|
|
switch (data1)
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
case 7: // Channel volume
|
2006-06-20 20:30:39 +00:00
|
|
|
if (track->EVolume)
|
2008-03-05 03:10:31 +00:00
|
|
|
{ // Tracks that use EMIDI volume ignore normal volume changes.
|
|
|
|
event = MIDI_META;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
data2 = VolumeControllerChange(event & 15, data2);
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
case 39: // Fine channel volume
|
2006-06-20 20:30:39 +00:00
|
|
|
// Skip fine volume adjustment because I am lazy.
|
|
|
|
// (And it doesn't seem to be used much anyway.)
|
2008-03-05 03:10:31 +00:00
|
|
|
event = MIDI_META;
|
2006-06-20 20:30:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 110: // EMIDI Track Designation
|
2008-03-05 03:10:31 +00:00
|
|
|
// Instruments 4, 5, 6, and 7 are all FM synth.
|
2006-06-20 20:30:39 +00:00
|
|
|
// The rest are all wavetable.
|
|
|
|
if (data2 == 127)
|
|
|
|
{
|
|
|
|
track->Designation = ~0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (data2 <= 9)
|
|
|
|
{
|
|
|
|
track->Designation |= 1 << data2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
track->Designated = true;
|
2008-03-07 00:43:05 +00:00
|
|
|
event = MIDI_META;
|
2006-06-20 20:30:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 111: // EMIDI Track Exclusion
|
|
|
|
if (track->Designated)
|
|
|
|
{
|
|
|
|
track->Designation &= ~(1 << data2);
|
|
|
|
}
|
2008-03-07 00:43:05 +00:00
|
|
|
event = MIDI_META;
|
2006-06-20 20:30:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 112: // EMIDI Program Change
|
|
|
|
track->EProgramChange = true;
|
|
|
|
event = 0xC0 | (event & 0x0F);
|
|
|
|
data1 = data2;
|
|
|
|
data2 = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 113: // EMIDI Volume
|
|
|
|
track->EVolume = true;
|
|
|
|
data1 = 7;
|
2008-03-05 03:10:31 +00:00
|
|
|
data2 = VolumeControllerChange(event & 15, data2);
|
2006-06-20 20:30:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 116: // EMIDI Loop Begin
|
|
|
|
track->LoopBegin = track->TrackP;
|
|
|
|
track->LoopDelay = 0;
|
|
|
|
track->LoopCount = data2;
|
|
|
|
track->LoopFinished = track->Finished;
|
|
|
|
event = 0xFF;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 117: // EMIDI Loop End
|
|
|
|
if (track->LoopCount >= 0 && data2 == 127)
|
|
|
|
{
|
|
|
|
if (track->LoopCount == 0 && !m_Looping)
|
|
|
|
{
|
|
|
|
track->Finished = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (track->LoopCount > 0 && --track->LoopCount == 0)
|
|
|
|
{
|
|
|
|
track->LoopCount = -1;
|
|
|
|
}
|
|
|
|
track->TrackP = track->LoopBegin;
|
|
|
|
track->Delay = track->LoopDelay;
|
|
|
|
track->Finished = track->LoopFinished;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event = 0xFF;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 118: // EMIDI Global Loop Begin
|
|
|
|
for (i = 0; i < NumTracks; ++i)
|
|
|
|
{
|
|
|
|
Tracks[i].LoopBegin = Tracks[i].TrackP;
|
|
|
|
Tracks[i].LoopDelay = Tracks[i].Delay;
|
|
|
|
Tracks[i].LoopCount = data2;
|
|
|
|
Tracks[i].LoopFinished = Tracks[i].Finished;
|
|
|
|
}
|
|
|
|
event = 0xFF;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 119: // EMIDI Global Loop End
|
|
|
|
if (data2 == 127)
|
|
|
|
{
|
|
|
|
for (i = 0; i < NumTracks; ++i)
|
|
|
|
{
|
|
|
|
if (Tracks[i].LoopCount >= 0)
|
|
|
|
{
|
|
|
|
if (Tracks[i].LoopCount == 0 && !m_Looping)
|
|
|
|
{
|
|
|
|
Tracks[i].Finished = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Tracks[i].LoopCount > 0 && --Tracks[i].LoopCount == 0)
|
|
|
|
{
|
|
|
|
Tracks[i].LoopCount = -1;
|
|
|
|
}
|
|
|
|
Tracks[i].TrackP = Tracks[i].LoopBegin;
|
|
|
|
Tracks[i].Delay = Tracks[i].LoopDelay;
|
|
|
|
Tracks[i].Finished = Tracks[i].LoopFinished;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
event = MIDI_META;
|
2006-06-20 20:30:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
if (event != MIDI_META && (!track->Designated || (track->Designation & DesignationMask)))
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
events[0] = delay;
|
|
|
|
events[1] = 0;
|
|
|
|
events[2] = event | (data1<<8) | (data2<<16);
|
|
|
|
events += 3;
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
// Skip SysEx events just because I don't want to bother with them.
|
|
|
|
// The old MIDI player ignored them too, so this won't break
|
|
|
|
// anything that played before.
|
|
|
|
if (event == MIDI_SYSEX || event == MIDI_SYSEXEND)
|
2006-06-20 20:30:39 +00:00
|
|
|
{
|
|
|
|
len = track->ReadVarLen ();
|
|
|
|
track->TrackP += len;
|
|
|
|
}
|
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
|
|
|
|
len = track->ReadVarLen ();
|
|
|
|
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;
|
|
|
|
events += 3;
|
2006-06-20 20:30:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
track->TrackP += len;
|
|
|
|
if (track->TrackP == track->MaxTrackP)
|
|
|
|
{
|
|
|
|
track->Finished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
track->Finished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!track->Finished)
|
|
|
|
{
|
2008-03-05 03:10:31 +00:00
|
|
|
track->Delay = track->ReadVarLen();
|
2006-06-20 20:30:39 +00:00
|
|
|
}
|
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
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 :: ProcessInitialMetaEvents
|
|
|
|
//
|
|
|
|
// Handle all the meta events at the start of each track.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-06-20 20:30:39 +00:00
|
|
|
|
|
|
|
void MIDISong2::ProcessInitialMetaEvents ()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
len = track->ReadVarLen ();
|
|
|
|
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
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 :: TrackInfo :: ReadVarLen
|
|
|
|
//
|
|
|
|
// Reads a variable-length SMF number.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-06-20 20:30:39 +00:00
|
|
|
DWORD MIDISong2::TrackInfo::ReadVarLen ()
|
|
|
|
{
|
|
|
|
DWORD time = 0, t = 0x80;
|
|
|
|
|
|
|
|
while ((t & 0x80) && TrackP < MaxTrackP)
|
|
|
|
{
|
|
|
|
t = TrackBegin[TrackP++];
|
|
|
|
time = (time << 7) | (t & 127);
|
|
|
|
}
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2008-03-05 03:10:31 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 :: TrackInfo :: FindNextDue
|
|
|
|
//
|
|
|
|
// Scans every track for the next event to play. Returns NULL if all events
|
|
|
|
// have been consumed.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-06-20 20:30:39 +00:00
|
|
|
MIDISong2::TrackInfo *MIDISong2::FindNextDue ()
|
|
|
|
{
|
|
|
|
TrackInfo *track;
|
2006-02-24 04:48:15 +00:00
|
|
|
DWORD best;
|
2006-06-20 20:30:39 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!TrackDue->Finished && TrackDue->Delay == 0)
|
|
|
|
{
|
|
|
|
return TrackDue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Format)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return Tracks[0].Finished ? NULL : Tracks;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
track = NULL;
|
|
|
|
best = 0xFFFFFFFF;
|
|
|
|
for (i = 0; i < NumTracks; ++i)
|
|
|
|
{
|
|
|
|
if (!Tracks[i].Finished)
|
|
|
|
{
|
|
|
|
if (Tracks[i].Delay < best)
|
|
|
|
{
|
|
|
|
best = Tracks[i].Delay;
|
|
|
|
track = &Tracks[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return track;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
track = TrackDue;
|
|
|
|
if (track->Finished)
|
|
|
|
{
|
|
|
|
track++;
|
|
|
|
}
|
|
|
|
return track < &Tracks[NumTracks] ? track : NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-05 03:10:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// MIDISong2 :: SetTempo
|
|
|
|
//
|
|
|
|
// Sets the tempo from a track's initial meta events.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void MIDISong2::SetTempo(int new_tempo)
|
|
|
|
{
|
2008-03-30 00:04:09 +00:00
|
|
|
if (0 == MIDI->SetTempo(new_tempo))
|
2008-03-05 03:10:31 +00:00
|
|
|
{
|
|
|
|
Tempo = new_tempo;
|
|
|
|
}
|
|
|
|
}
|