2006-02-24 04:48:15 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2008-04-29 04:33:43 +00:00
|
|
|
#include <math.h>
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2006-09-14 00:02:31 +00:00
|
|
|
#include "opl_mus_player.h"
|
2006-02-24 04:48:15 +00:00
|
|
|
#include "doomtype.h"
|
2012-11-08 05:45:58 +00:00
|
|
|
#include "opl.h"
|
2006-02-24 04:48:15 +00:00
|
|
|
#include "w_wad.h"
|
|
|
|
#include "templates.h"
|
|
|
|
#include "c_cvars.h"
|
|
|
|
#include "i_system.h"
|
|
|
|
#include "stats.h"
|
|
|
|
|
2008-03-28 03:19:18 +00:00
|
|
|
#define IMF_RATE 700.0
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2012-11-06 06:10:04 +00:00
|
|
|
EXTERN_CVAR (Int, opl_numchips)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-28 03:19:18 +00:00
|
|
|
OPLmusicBlock::OPLmusicBlock()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
scoredata = NULL;
|
2008-03-28 03:19:18 +00:00
|
|
|
NextTickIn = 0;
|
2008-04-29 04:33:43 +00:00
|
|
|
LastOffset = 0;
|
2012-11-06 06:10:04 +00:00
|
|
|
NumChips = MIN(*opl_numchips, 2);
|
2008-03-28 03:19:18 +00:00
|
|
|
Looping = false;
|
2012-11-08 05:45:58 +00:00
|
|
|
FullPan = false;
|
2008-03-28 03:19:18 +00:00
|
|
|
io = NULL;
|
|
|
|
io = new OPLio;
|
|
|
|
}
|
|
|
|
|
|
|
|
OPLmusicBlock::~OPLmusicBlock()
|
|
|
|
{
|
|
|
|
delete io;
|
|
|
|
}
|
|
|
|
|
2008-03-29 05:01:38 +00:00
|
|
|
void OPLmusicBlock::ResetChips ()
|
|
|
|
{
|
2008-04-09 04:17:13 +00:00
|
|
|
ChipAccess.Enter();
|
2008-03-29 05:01:38 +00:00
|
|
|
io->OPLdeinit ();
|
2012-11-08 05:45:58 +00:00
|
|
|
NumChips = io->OPLinit(MIN(*opl_numchips, 2), FullPan);
|
2008-04-09 04:17:13 +00:00
|
|
|
ChipAccess.Leave();
|
2008-03-29 05:01:38 +00:00
|
|
|
}
|
|
|
|
|
2008-03-28 03:19:18 +00:00
|
|
|
void OPLmusicBlock::Restart()
|
|
|
|
{
|
|
|
|
OPLstopMusic ();
|
2008-03-29 05:01:38 +00:00
|
|
|
OPLplayMusic (127);
|
2008-03-28 03:19:18 +00:00
|
|
|
MLtime = 0;
|
|
|
|
playingcount = 0;
|
2008-04-29 04:33:43 +00:00
|
|
|
LastOffset = 0;
|
2008-03-28 03:19:18 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 11:25:36 +00:00
|
|
|
OPLmusicFile::OPLmusicFile (FileReader *reader)
|
|
|
|
: ScoreLen (reader->GetLength())
|
2008-03-28 03:19:18 +00:00
|
|
|
{
|
|
|
|
if (io == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2014-06-25 11:25:36 +00:00
|
|
|
scoredata = new BYTE[ScoreLen];
|
2006-04-14 12:58:52 +00:00
|
|
|
|
2014-06-25 11:25:36 +00:00
|
|
|
if (reader->Read(scoredata, ScoreLen) != ScoreLen)
|
|
|
|
{
|
|
|
|
fail: delete[] scoredata;
|
|
|
|
scoredata = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2006-04-14 12:58:52 +00:00
|
|
|
|
2012-11-06 06:10:04 +00:00
|
|
|
if (0 == (NumChips = io->OPLinit(NumChips)))
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-05-30 19:23:53 +00:00
|
|
|
goto fail;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for RDosPlay raw OPL format
|
2008-04-03 02:31:39 +00:00
|
|
|
if (((DWORD *)scoredata)[0] == MAKE_ID('R','A','W','A') &&
|
2006-02-24 04:48:15 +00:00
|
|
|
((DWORD *)scoredata)[1] == MAKE_ID('D','A','T','A'))
|
|
|
|
{
|
|
|
|
RawPlayer = RDosPlay;
|
|
|
|
if (*(WORD *)(scoredata + 8) == 0)
|
|
|
|
{ // A clock speed of 0 is bad
|
|
|
|
*(WORD *)(scoredata + 8) = 0xFFFF;
|
|
|
|
}
|
2008-04-03 02:31:39 +00:00
|
|
|
SamplesPerTick = LittleShort(*(WORD *)(scoredata + 8)) / ADLIB_CLOCK_MUL;
|
|
|
|
}
|
|
|
|
// Check for DosBox OPL dump
|
|
|
|
else if (((DWORD *)scoredata)[0] == MAKE_ID('D','B','R','A') &&
|
2010-05-30 19:23:53 +00:00
|
|
|
((DWORD *)scoredata)[1] == MAKE_ID('W','O','P','L'))
|
2008-04-03 02:31:39 +00:00
|
|
|
{
|
2014-02-24 21:48:41 +00:00
|
|
|
if (LittleShort(((WORD *)scoredata)[5]) == 1)
|
2010-05-30 19:23:53 +00:00
|
|
|
{
|
|
|
|
RawPlayer = DosBox1;
|
|
|
|
SamplesPerTick = OPL_SAMPLE_RATE / 1000;
|
2014-06-25 11:25:36 +00:00
|
|
|
ScoreLen = MIN<int>(ScoreLen - 24, LittleLong(((DWORD *)scoredata)[4])) + 24;
|
2010-05-30 19:23:53 +00:00
|
|
|
}
|
|
|
|
else if (((DWORD *)scoredata)[2] == MAKE_ID(2,0,0,0))
|
|
|
|
{
|
2014-02-23 22:07:10 +00:00
|
|
|
bool okay = true;
|
|
|
|
if (scoredata[21] != 0)
|
2010-05-30 19:23:53 +00:00
|
|
|
{
|
|
|
|
Printf("Unsupported DOSBox Raw OPL format %d\n", scoredata[20]);
|
2014-02-23 22:07:10 +00:00
|
|
|
okay = false;
|
2010-05-30 19:23:53 +00:00
|
|
|
}
|
2014-02-23 22:07:10 +00:00
|
|
|
if (scoredata[22] != 0)
|
2010-05-30 19:23:53 +00:00
|
|
|
{
|
|
|
|
Printf("Unsupported DOSBox Raw OPL compression %d\n", scoredata[21]);
|
2014-02-23 22:07:10 +00:00
|
|
|
okay = false;
|
2010-05-30 19:23:53 +00:00
|
|
|
}
|
2014-02-23 22:07:10 +00:00
|
|
|
if (!okay)
|
|
|
|
goto fail;
|
2010-05-30 19:23:53 +00:00
|
|
|
RawPlayer = DosBox2;
|
|
|
|
SamplesPerTick = OPL_SAMPLE_RATE / 1000;
|
|
|
|
int headersize = 0x1A + scoredata[0x19];
|
2014-06-25 11:25:36 +00:00
|
|
|
ScoreLen = MIN<int>(ScoreLen - headersize, LittleLong(((DWORD *)scoredata)[3]) * 2) + headersize;
|
2010-05-30 19:23:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Printf("Unsupported DOSBox Raw OPL version %d.%d\n", LittleShort(((WORD *)scoredata)[4]), LittleShort(((WORD *)scoredata)[5]));
|
|
|
|
goto fail;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
// Check for modified IMF format (includes a header)
|
|
|
|
else if (((DWORD *)scoredata)[0] == MAKE_ID('A','D','L','I') &&
|
|
|
|
scoredata[4] == 'B' && scoredata[5] == 1)
|
|
|
|
{
|
|
|
|
int songlen;
|
|
|
|
BYTE *max = scoredata + ScoreLen;
|
|
|
|
RawPlayer = IMF;
|
2008-03-28 03:19:18 +00:00
|
|
|
SamplesPerTick = OPL_SAMPLE_RATE / IMF_RATE;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
score = scoredata + 6;
|
|
|
|
// Skip track and game name
|
|
|
|
for (int i = 2; i != 0; --i)
|
|
|
|
{
|
|
|
|
while (score < max && *score++ != '\0') {}
|
|
|
|
}
|
|
|
|
if (score < max) score++; // Skip unknown byte
|
|
|
|
if (score + 8 > max)
|
|
|
|
{ // Not enough room left for song data
|
|
|
|
delete[] scoredata;
|
|
|
|
scoredata = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
songlen = LittleLong(*(DWORD *)score);
|
|
|
|
if (songlen != 0 && (songlen +=4) < ScoreLen - (score - scoredata))
|
|
|
|
{
|
|
|
|
ScoreLen = songlen + int(score - scoredata);
|
|
|
|
}
|
|
|
|
}
|
2010-05-30 19:23:53 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
Restart ();
|
|
|
|
}
|
|
|
|
|
2008-03-28 03:19:18 +00:00
|
|
|
OPLmusicFile::~OPLmusicFile ()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
if (scoredata != NULL)
|
|
|
|
{
|
|
|
|
io->OPLdeinit ();
|
|
|
|
delete[] scoredata;
|
|
|
|
scoredata = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-28 03:19:18 +00:00
|
|
|
bool OPLmusicFile::IsValid () const
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
return scoredata != NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-28 03:19:18 +00:00
|
|
|
void OPLmusicFile::SetLooping (bool loop)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
Looping = loop;
|
|
|
|
}
|
|
|
|
|
2008-03-28 03:19:18 +00:00
|
|
|
void OPLmusicFile::Restart ()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-28 03:19:18 +00:00
|
|
|
OPLmusicBlock::Restart();
|
2008-04-03 02:31:39 +00:00
|
|
|
WhichChip = 0;
|
2010-05-30 19:23:53 +00:00
|
|
|
switch (RawPlayer)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-05-30 19:23:53 +00:00
|
|
|
case RDosPlay:
|
2008-04-03 02:31:39 +00:00
|
|
|
score = scoredata + 10;
|
|
|
|
SamplesPerTick = LittleShort(*(WORD *)(scoredata + 8)) / ADLIB_CLOCK_MUL;
|
2010-05-30 19:23:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DosBox1:
|
2008-04-03 02:31:39 +00:00
|
|
|
score = scoredata + 24;
|
|
|
|
SamplesPerTick = OPL_SAMPLE_RATE / 1000;
|
2010-05-30 19:23:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DosBox2:
|
|
|
|
score = scoredata + 0x1A + scoredata[0x19];
|
|
|
|
SamplesPerTick = OPL_SAMPLE_RATE / 1000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMF:
|
2006-02-24 04:48:15 +00:00
|
|
|
score = scoredata + 6;
|
|
|
|
|
|
|
|
// Skip track and game name
|
|
|
|
for (int i = 2; i != 0; --i)
|
|
|
|
{
|
|
|
|
while (*score++ != '\0') {}
|
|
|
|
}
|
|
|
|
score++; // Skip unknown byte
|
|
|
|
if (*(DWORD *)score != 0)
|
|
|
|
{
|
|
|
|
score += 4; // Skip song length
|
|
|
|
}
|
2010-05-30 19:23:53 +00:00
|
|
|
break;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2008-04-03 02:31:39 +00:00
|
|
|
io->SetClockRate(SamplesPerTick);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OPLmusicBlock::ServiceStream (void *buff, int numbytes)
|
|
|
|
{
|
2012-11-02 03:25:50 +00:00
|
|
|
float *samples1 = (float *)buff;
|
2012-11-08 05:45:58 +00:00
|
|
|
int stereoshift = (int)(FullPan | io->IsOPL3);
|
|
|
|
int numsamples = numbytes / (sizeof(float) << stereoshift);
|
2006-02-24 04:48:15 +00:00
|
|
|
bool prevEnded = false;
|
|
|
|
bool res = true;
|
|
|
|
|
2008-03-29 01:30:37 +00:00
|
|
|
memset(buff, 0, numbytes);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-04-09 04:17:13 +00:00
|
|
|
ChipAccess.Enter();
|
2006-02-24 04:48:15 +00:00
|
|
|
while (numsamples > 0)
|
|
|
|
{
|
2008-03-29 05:01:38 +00:00
|
|
|
double ticky = NextTickIn;
|
|
|
|
int tick_in = int(NextTickIn);
|
|
|
|
int samplesleft = MIN(numsamples, tick_in);
|
2012-11-06 05:21:00 +00:00
|
|
|
size_t i;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
if (samplesleft > 0)
|
|
|
|
{
|
2012-11-06 05:21:00 +00:00
|
|
|
for (i = 0; i < io->NumChips; ++i)
|
|
|
|
{
|
|
|
|
io->chips[i]->Update(samples1, samplesleft);
|
|
|
|
}
|
2012-11-08 05:45:58 +00:00
|
|
|
OffsetSamples(samples1, samplesleft << stereoshift);
|
2008-03-29 05:01:38 +00:00
|
|
|
assert(NextTickIn == ticky);
|
2006-02-24 04:48:15 +00:00
|
|
|
NextTickIn -= samplesleft;
|
|
|
|
assert (NextTickIn >= 0);
|
|
|
|
numsamples -= samplesleft;
|
2012-11-08 05:45:58 +00:00
|
|
|
samples1 += samplesleft << stereoshift;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-03-28 03:19:18 +00:00
|
|
|
if (NextTickIn < 1)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-29 05:01:38 +00:00
|
|
|
int next = PlayTick();
|
|
|
|
assert(next >= 0);
|
2006-02-24 04:48:15 +00:00
|
|
|
if (next == 0)
|
|
|
|
{ // end of song
|
|
|
|
if (!Looping || prevEnded)
|
|
|
|
{
|
|
|
|
if (numsamples > 0)
|
|
|
|
{
|
2012-11-06 05:21:00 +00:00
|
|
|
for (i = 0; i < io->NumChips; ++i)
|
|
|
|
{
|
2014-02-24 04:07:37 +00:00
|
|
|
io->chips[i]->Update(samples1, numsamples);
|
2012-11-06 05:21:00 +00:00
|
|
|
}
|
2012-11-08 05:45:58 +00:00
|
|
|
OffsetSamples(samples1, numsamples << stereoshift);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
res = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Avoid infinite loops from songs that do nothing but end
|
|
|
|
prevEnded = true;
|
|
|
|
Restart ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prevEnded = false;
|
2008-04-03 02:31:39 +00:00
|
|
|
io->WriteDelay(next);
|
2008-03-28 03:19:18 +00:00
|
|
|
NextTickIn += SamplesPerTick * next;
|
2006-02-24 04:48:15 +00:00
|
|
|
assert (NextTickIn >= 0);
|
|
|
|
MLtime += next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-09 04:17:13 +00:00
|
|
|
ChipAccess.Leave();
|
2006-02-24 04:48:15 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-04-29 04:33:43 +00:00
|
|
|
void OPLmusicBlock::OffsetSamples(float *buff, int count)
|
|
|
|
{
|
|
|
|
// Three out of four of the OPL waveforms are non-negative. Depending on
|
|
|
|
// timbre selection, this can cause the output waveform to tend toward
|
|
|
|
// very large positive values. Heretic's music is particularly bad for
|
|
|
|
// this. This function attempts to compensate by offseting the sample
|
|
|
|
// data back to around the [-1.0, 1.0] range.
|
|
|
|
|
|
|
|
double max = -1e10, min = 1e10, offset, step;
|
2008-04-30 00:44:45 +00:00
|
|
|
int i, ramp, largest_at = 0;
|
2008-04-29 04:33:43 +00:00
|
|
|
|
|
|
|
// Find max and min values for this segment of the waveform.
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
if (buff[i] > max)
|
|
|
|
{
|
|
|
|
max = buff[i];
|
2008-04-30 00:44:45 +00:00
|
|
|
largest_at = i;
|
2008-04-29 04:33:43 +00:00
|
|
|
}
|
|
|
|
if (buff[i] < min)
|
|
|
|
{
|
|
|
|
min = buff[i];
|
2008-04-30 00:44:45 +00:00
|
|
|
largest_at = i;
|
2008-04-29 04:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-30 00:44:45 +00:00
|
|
|
// Prefer to keep the offset at 0, even if it means a little clipping.
|
|
|
|
if (LastOffset == 0 && min >= -1.1 && max <= 1.1)
|
2008-04-29 04:33:43 +00:00
|
|
|
{
|
2008-04-30 00:44:45 +00:00
|
|
|
offset = 0;
|
2008-04-29 04:33:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offset = (max + min) / 2;
|
|
|
|
// If the new offset is close to 0, make it 0 to avoid making another
|
|
|
|
// full loop through the sample data.
|
|
|
|
if (fabs(offset) < 1/256.0)
|
|
|
|
{
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Ramp the offset change so there aren't any abrupt clicks in the output.
|
|
|
|
// If the ramp is too short, it can sound scratchy. cblood2.mid is
|
|
|
|
// particularly unforgiving of short ramps.
|
2008-04-30 00:44:45 +00:00
|
|
|
if (count >= 512)
|
|
|
|
{
|
|
|
|
ramp = 512;
|
|
|
|
step = (offset - LastOffset) / 512;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ramp = MIN(count, MAX(196, largest_at));
|
|
|
|
step = (offset - LastOffset) / ramp;
|
|
|
|
}
|
2008-04-29 04:33:43 +00:00
|
|
|
offset = LastOffset;
|
|
|
|
i = 0;
|
|
|
|
if (step != 0)
|
|
|
|
{
|
|
|
|
for (; i < ramp; ++i)
|
|
|
|
{
|
|
|
|
buff[i] = float(buff[i] - offset);
|
|
|
|
offset += step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (offset != 0)
|
|
|
|
{
|
|
|
|
for (; i < count; ++i)
|
|
|
|
{
|
|
|
|
buff[i] = float(buff[i] - offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LastOffset = float(offset);
|
|
|
|
}
|
|
|
|
|
2008-03-28 03:19:18 +00:00
|
|
|
int OPLmusicFile::PlayTick ()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
BYTE reg, data;
|
2010-05-30 19:23:53 +00:00
|
|
|
WORD delay;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-05-30 19:23:53 +00:00
|
|
|
switch (RawPlayer)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-05-30 19:23:53 +00:00
|
|
|
case RDosPlay:
|
2006-02-24 04:48:15 +00:00
|
|
|
while (score < scoredata + ScoreLen)
|
|
|
|
{
|
|
|
|
data = *score++;
|
|
|
|
reg = *score++;
|
|
|
|
switch (reg)
|
|
|
|
{
|
|
|
|
case 0: // Delay
|
|
|
|
if (data != 0)
|
|
|
|
{
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // Speed change or OPL3 switch
|
|
|
|
if (data == 0)
|
|
|
|
{
|
2008-04-03 02:31:39 +00:00
|
|
|
SamplesPerTick = LittleShort(*(WORD *)(score)) / ADLIB_CLOCK_MUL;
|
|
|
|
io->SetClockRate(SamplesPerTick);
|
2006-02-24 04:48:15 +00:00
|
|
|
score += 2;
|
|
|
|
}
|
2008-04-03 02:31:39 +00:00
|
|
|
else if (data == 1)
|
|
|
|
{
|
|
|
|
WhichChip = 0;
|
|
|
|
}
|
|
|
|
else if (data == 2)
|
|
|
|
{
|
|
|
|
WhichChip = 1;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xFF: // End of song
|
|
|
|
if (data == 0xFF)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // It's something to stuff into the OPL chip
|
2012-11-09 04:57:25 +00:00
|
|
|
io->OPLwriteReg(WhichChip, reg, data);
|
2006-02-24 04:48:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-05-30 19:23:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DosBox1:
|
2008-04-03 02:31:39 +00:00
|
|
|
while (score < scoredata + ScoreLen)
|
|
|
|
{
|
|
|
|
reg = *score++;
|
|
|
|
|
|
|
|
if (reg == 4)
|
|
|
|
{
|
|
|
|
reg = *score++;
|
|
|
|
data = *score++;
|
|
|
|
}
|
|
|
|
else if (reg == 0)
|
|
|
|
{ // One-byte delay
|
|
|
|
return *score++ + 1;
|
|
|
|
}
|
|
|
|
else if (reg == 1)
|
|
|
|
{ // Two-byte delay
|
|
|
|
int delay = score[0] + (score[1] << 8) + 1;
|
|
|
|
score += 2;
|
|
|
|
return delay;
|
|
|
|
}
|
|
|
|
else if (reg == 2)
|
|
|
|
{ // Select OPL chip 0
|
|
|
|
WhichChip = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (reg == 3)
|
|
|
|
{ // Select OPL chip 1
|
|
|
|
WhichChip = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = *score++;
|
|
|
|
}
|
2012-11-09 04:57:25 +00:00
|
|
|
io->OPLwriteReg(WhichChip, reg, data);
|
2008-04-03 02:31:39 +00:00
|
|
|
}
|
2010-05-30 19:23:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DosBox2:
|
|
|
|
{
|
|
|
|
BYTE *to_reg = scoredata + 0x1A;
|
|
|
|
BYTE to_reg_size = scoredata[0x19];
|
|
|
|
BYTE short_delay_code = scoredata[0x17];
|
|
|
|
BYTE long_delay_code = scoredata[0x18];
|
|
|
|
|
|
|
|
while (score < scoredata + ScoreLen)
|
|
|
|
{
|
|
|
|
BYTE code = *score++;
|
|
|
|
data = *score++;
|
|
|
|
|
|
|
|
// Which OPL chip to write to is encoded in the high bit of the code value.
|
|
|
|
int which = !!(code & 0x80);
|
|
|
|
code &= 0x7F;
|
|
|
|
|
|
|
|
if (code == short_delay_code)
|
|
|
|
{
|
|
|
|
return data + 1;
|
|
|
|
}
|
|
|
|
else if (code == long_delay_code)
|
|
|
|
{
|
|
|
|
return (data + 1) << 8;
|
|
|
|
}
|
2012-11-09 04:57:25 +00:00
|
|
|
else if (code < to_reg_size)
|
2010-05-30 19:23:53 +00:00
|
|
|
{
|
|
|
|
io->OPLwriteReg(which, to_reg[code], data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-05-30 19:23:53 +00:00
|
|
|
case IMF:
|
|
|
|
delay = 0;
|
2006-02-24 04:48:15 +00:00
|
|
|
while (delay == 0 && score + 4 - scoredata <= ScoreLen)
|
|
|
|
{
|
2008-02-05 23:32:49 +00:00
|
|
|
if (*(DWORD *)score == 0xFFFFFFFF)
|
|
|
|
{ // This is a special value that means to end the song.
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
reg = score[0];
|
|
|
|
data = score[1];
|
|
|
|
delay = LittleShort(((WORD *)score)[1]);
|
|
|
|
score += 4;
|
|
|
|
io->OPLwriteReg (0, reg, data);
|
|
|
|
}
|
|
|
|
return delay;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-25 04:22:19 +00:00
|
|
|
/*
|
2006-09-14 00:02:31 +00:00
|
|
|
ADD_STAT (opl)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
return YM3812GetVoiceString ();
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2009-01-25 04:22:19 +00:00
|
|
|
*/
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-04-03 02:31:39 +00:00
|
|
|
OPLmusicFile::OPLmusicFile(const OPLmusicFile *source, const char *filename)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-04-03 02:31:39 +00:00
|
|
|
ScoreLen = source->ScoreLen;
|
|
|
|
scoredata = new BYTE[ScoreLen];
|
|
|
|
memcpy(scoredata, source->scoredata, ScoreLen);
|
|
|
|
SamplesPerTick = source->SamplesPerTick;
|
|
|
|
RawPlayer = source->RawPlayer;
|
|
|
|
score = source->score;
|
2012-11-06 06:10:04 +00:00
|
|
|
NumChips = source->NumChips;
|
2008-04-03 02:31:39 +00:00
|
|
|
WhichChip = 0;
|
|
|
|
if (io != NULL)
|
|
|
|
{
|
|
|
|
delete io;
|
|
|
|
}
|
|
|
|
io = new DiskWriterIO(filename);
|
2012-11-06 06:10:04 +00:00
|
|
|
NumChips = io->OPLinit(NumChips);
|
2008-04-03 02:31:39 +00:00
|
|
|
Restart();
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2008-04-03 02:31:39 +00:00
|
|
|
void OPLmusicFile::Dump()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-04-03 02:31:39 +00:00
|
|
|
int time;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-04-03 02:31:39 +00:00
|
|
|
time = PlayTick();
|
|
|
|
while (time != 0)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-04-03 02:31:39 +00:00
|
|
|
io->WriteDelay(time);
|
|
|
|
time = PlayTick();
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|