Switch to STREAM

This commit is contained in:
OV2 2016-12-03 15:13:51 +01:00
parent fabce282db
commit 2b68d129a7

View File

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