Switch to STREAM

This commit is contained in:
OV2 2016-12-03 15:13:51 +01:00
parent fabce282db
commit 2b68d129a7
1 changed files with 59 additions and 40 deletions

View File

@ -199,7 +199,8 @@
#define APU_DEFAULT_INPUT_RATE 32000
std::ifstream dataFile, audioFile;
STREAM dataStream = NULL;
STREAM audioStream = NULL;
uint32 audioLoopPos;
uint32 partial_samples;
@ -210,26 +211,28 @@ bool AudioOpen()
{
MSU1.MSU1_STATUS |= AudioError;
if (audioFile.is_open())
audioFile.close();
if (audioStream)
{
CLOSE_STREAM(audioStream);
audioStream = NULL;
}
char ext[_MAX_EXT];
snprintf(ext, _MAX_EXT, "-%d.pcm", MSU1.MSU1_CURRENT_TRACK);
audioFile.clear();
audioFile.open(S9xGetFilename(ext, ROMFILENAME_DIR), std::ios::in | std::ios::binary);
if (audioFile.good())
audioStream = OPEN_STREAM(S9xGetFilename(ext, ROMFILENAME_DIR), "rb");
if (audioStream)
{
if (audioFile.get() != 'M')
if (GETC_STREAM(audioStream) != 'M')
return false;
if (audioFile.get() != 'S')
if (GETC_STREAM(audioStream) != 'S')
return false;
if (audioFile.get() != 'U')
if (GETC_STREAM(audioStream) != 'U')
return false;
if (audioFile.get() != '1')
if (GETC_STREAM(audioStream) != '1')
return false;
audioFile.read((char *)&audioLoopPos, 4);
READ_STREAM((char *)&audioLoopPos, 4, audioStream);
audioLoopPos = GET_LE32(&audioLoopPos);
audioLoopPos <<= 2;
audioLoopPos += 8;
@ -243,12 +246,14 @@ bool AudioOpen()
bool DataOpen()
{
if (dataFile.is_open())
dataFile.close();
if (dataStream)
{
CLOSE_STREAM(dataStream);
dataStream = NULL;
}
dataFile.clear();
dataFile.open(S9xGetFilename(".msu", ROMFILENAME_DIR), std::ios::in | std::ios::binary);
return dataFile.is_open();
dataStream = OPEN_STREAM(S9xGetFilename(".msu", ROMFILENAME_DIR), "rb");
return dataStream != NULL;
}
void S9xMSU1Init(void)
@ -271,11 +276,17 @@ void S9xMSU1Init(void)
partial_samples = 0;
if (dataFile.is_open())
dataFile.close();
if (dataStream)
{
CLOSE_STREAM(dataStream);
dataStream = NULL;
}
if (audioFile.is_open())
audioFile.close();
if (audioStream)
{
CLOSE_STREAM(audioStream);
audioStream = NULL;
}
DataOpen();
}
@ -286,10 +297,11 @@ void S9xMSU1Generate(int sample_count)
while (((uintptr_t)bufPos < (uintptr_t)bufEnd) && (MSU1.MSU1_STATUS & AudioPlaying) && partial_samples > 320405)
{
if (audioFile.is_open())
if (audioStream)
{
int16 sample;
if (audioFile.read((char *)&sample, 2).good())
int bytes_read = READ_STREAM((char *)&sample, 2, audioStream);
if (bytes_read == 2)
{
sample = (int16)((double)(int16)GET_LE16(&sample) * (double)MSU1.MSU1_VOLUME / 255.0);
@ -298,7 +310,7 @@ void S9xMSU1Generate(int sample_count)
partial_samples -= 320405;
}
else
if (audioFile.eof())
if (bytes_read >= 0)
{
sample = (int16)((double)(int16)GET_LE16(&sample) * (double)MSU1.MSU1_VOLUME / 255.0);
@ -308,15 +320,13 @@ void S9xMSU1Generate(int sample_count)
if (MSU1.MSU1_STATUS & AudioRepeating)
{
audioFile.clear();
MSU1.MSU1_AUDIO_POS = audioLoopPos;
audioFile.seekg(MSU1.MSU1_AUDIO_POS);
REVERT_STREAM(audioStream, MSU1.MSU1_AUDIO_POS, 0);
}
else
{
MSU1.MSU1_STATUS &= ~(AudioPlaying | AudioRepeating);
audioFile.clear();
audioFile.seekg(8);
REVERT_STREAM(audioStream, 8, 0);
return;
}
}
@ -342,12 +352,19 @@ uint8 S9xMSU1ReadPort(int port)
case 0:
return MSU1.MSU1_STATUS;
case 1:
if (MSU1.MSU1_STATUS & DataBusy)
return 0;
if (dataFile.fail() || dataFile.bad() || dataFile.eof())
return 0;
MSU1.MSU1_DATA_POS++;
return dataFile.get();
{
if (MSU1.MSU1_STATUS & DataBusy)
return 0;
if (!dataStream)
return 0;
int data = GETC_STREAM(dataStream);
if (data >= 0)
{
MSU1.MSU1_DATA_POS++;
return data;
}
return 0;
}
case 2:
return 'S';
case 3:
@ -386,8 +403,10 @@ void S9xMSU1WritePort(int port, uint8 byte)
MSU1.MSU1_DATA_SEEK &= 0x00FFFFFF;
MSU1.MSU1_DATA_SEEK |= byte << 24;
MSU1.MSU1_DATA_POS = MSU1.MSU1_DATA_SEEK;
if(dataFile.good())
dataFile.seekg(MSU1.MSU1_DATA_POS);
if (dataStream)
{
REVERT_STREAM(dataStream, MSU1.MSU1_DATA_POS, 0);
}
break;
case 4:
MSU1.MSU1_TRACK_SEEK &= 0xFF00;
@ -414,7 +433,7 @@ void S9xMSU1WritePort(int port, uint8 byte)
MSU1.MSU1_AUDIO_POS = 8;
}
audioFile.seekg(MSU1.MSU1_AUDIO_POS);
REVERT_STREAM(audioStream, MSU1.MSU1_AUDIO_POS, 0);
}
break;
case 6:
@ -450,20 +469,20 @@ void S9xMSU1PostLoadState(void)
{
if (DataOpen())
{
dataFile.seekg(MSU1.MSU1_DATA_POS);
REVERT_STREAM(dataStream, MSU1.MSU1_DATA_POS, 0);
}
if (MSU1.MSU1_STATUS & AudioPlaying)
{
if (AudioOpen())
{
audioFile.seekg(4);
audioFile.read((char *)&audioLoopPos, 4);
REVERT_STREAM(audioStream, 4, 0);
READ_STREAM((char *)&audioLoopPos, 4, audioStream);
audioLoopPos = GET_LE32(&audioLoopPos);
audioLoopPos <<= 2;
audioLoopPos += 8;
audioFile.seekg(MSU1.MSU1_AUDIO_POS);
REVERT_STREAM(audioStream, MSU1.MSU1_AUDIO_POS, 0);
}
else
{