Skip to content

Commit

Permalink
Now you can play all the games in the world
Browse files Browse the repository at this point in the history
  • Loading branch information
friol committed Mar 17, 2024
1 parent abb0e1f commit 79b2e7b
Show file tree
Hide file tree
Showing 10 changed files with 1,504 additions and 306 deletions.
23 changes: 11 additions & 12 deletions apu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ void apu::dspDecodeBrr(int ch)
case 3: s += 2 * old + ((13 * -old) >> 6) - older + ((3 * older) >> 4); break;
}

channels[ch].decodeBuffer[bOff + i] = clamp16(s) * 2;
channels[ch].decodeBuffer[bOff + i] = (signed short int)(clamp16(s) * 2);
older = old;
old = channels[ch].decodeBuffer[bOff + i] >> 1;
}
Expand Down Expand Up @@ -795,7 +795,7 @@ void apu::dspHandleGain(int ch)
}

// store new value
if (dspCheckCounter(rate)) channels[ch].gain = newGain;
if (dspCheckCounter(rate)) channels[ch].gain = (unsigned short int)newGain;
}

static const int rateValues[32] = {
Expand Down Expand Up @@ -920,15 +920,15 @@ void apu::dspCycle(signed short int& sampleOutL, signed short int& sampleOutR, f

// update pitch counter
channels[ch].pitchCounter &= 0x3fff;
channels[ch].pitchCounter += pitch;
channels[ch].pitchCounter += (unsigned short)pitch;
if (channels[ch].pitchCounter > 0x7fff) channels[ch].pitchCounter = 0x7fff;

// set outputs
dspRam[(ch << 4) | 8] = channels[ch].gain >> 4;
dspRam[(ch << 4) | 9] = sample >> 8;
dspRam[(ch << 4) | 8] = (unsigned char)(channels[ch].gain >> 4);
dspRam[(ch << 4) | 9] = (unsigned char)(sample >> 8);

sampleOutL = clamp16(sampleOutL + ((sample * channels[ch].leftVol) >> 7));
sampleOutR = clamp16(sampleOutR + ((sample * channels[ch].rightVol) >> 7));
sampleOutL = (signed short)clamp16(sampleOutL + ((sample * channels[ch].leftVol) >> 7));
sampleOutR = (signed short)clamp16(sampleOutR + ((sample * channels[ch].rightVol) >> 7));

//if (dsp->channel[ch].echoEnable) {
// dsp->echoOutL = clamp16(dsp->echoOutL + ((sample * dsp->channel[ch].volumeL) >> 7));
Expand Down Expand Up @@ -2652,7 +2652,7 @@ int apu::stepOne()
val0 ^= 0xff;
int result = val1 + val0 + 1;
flagC = result > 0xff;
doFlagsNZ(result);
doFlagsNZ((unsigned char)result);

regPC += 3;
cycles = 6;
Expand Down Expand Up @@ -2900,15 +2900,14 @@ int apu::stepOne()
if (flagP) adr1 |= 0x100;

unsigned char val0 = (this->*read8)(adr0);
unsigned char val1 = (this->*read8)(adr1);

unsigned char applyOn = (this->*read8)(adr1);
int result = applyOn + val0 + (flagC?1:0);
flagV = (applyOn & 0x80) == (val0 & 0x80) && (val0 & 0x80) != (result & 0x80);
flagH = ((applyOn & 0xf) + (val0 & 0xf) + (flagC ? 1 : 0)) > 0xf;
flagC = result > 0xff;
(this->*write8)(adr1,result);
doFlagsNZ(result);
(this->*write8)(adr1,(unsigned char)result);
doFlagsNZ((unsigned char)result);

regPC += 3;
cycles = 6;
Expand Down Expand Up @@ -3157,7 +3156,7 @@ int apu::stepOne()
val1 = (unsigned char)result;
doFlagsNZ(val1);

(this->*write8)(adr1, result);
(this->*write8)(adr1, (unsigned char)result);

regPC += 3;
cycles = 6;
Expand Down
94 changes: 3 additions & 91 deletions audioSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,110 +62,22 @@ audioSystem::audioSystem()
BASS_ChannelSetAttribute(stream, BASS_ATTRIB_BUFFER, 0.25);
BASS_ChannelPlay(stream, FALSE);

//loadTestSamples();

outwavbuf = new float[outwavdim * 2];

audioSystemInited = true;
glbTheLogger.logMsg("Audio system inited; sample rate:"+std::to_string(sampleRate));
}

void audioSystem::loadTestSamples()
{
for (unsigned int ins = 0;ins < 0x14;ins++)
{
AudioFile<float> audioFile;
std::string smpName = "../../spcsamples/02 Title_";

std::stringstream strr;
strr << std::hex << std::setw(2) << std::setfill('0') << ins;

smpName += strr.str();
smpName += ".wav";

audioFile.load(smpName.c_str());
int numSamples = audioFile.getNumSamplesPerChannel();

glbTheLogger.logMsg("Loaded sample "+strr.str() + " of len " + std::to_string(numSamples));

insArray[ins].len = numSamples;
insArray[ins].data = new float[numSamples];
for (unsigned int s = 0;s < numSamples;s++)
{
insArray[ins].data[s]= audioFile.samples[0][s];
}
}
glbTheLogger.logMsg("Audio system inited; sample rate:"+std::to_string((int)sampleRate));
}

void audioSystem::feedAudiobuf(float l, float r)
{
audioVector.push_back(l);
audioVector.push_back(r);

audioBuf[bufPos] = l;
/*audioBuf[bufPos] = l;
audioBuf[bufPos+1] = r;
bufPos += 2;
if (bufPos >= (audioBufLen*2)) bufPos = 0;

/*if (outwavpos == -1) return;
if ((outwavpos / 2) < outwavdim)
{
outwavbuf[outwavpos] = l;
outwavbuf[outwavpos + 1] = r;
outwavpos += 2;
}
else
{
AudioFile<float> a;
a.setNumChannels(2);
a.setNumSamplesPerChannel(44100);
a.samples[0].resize(outwavdim);
a.samples[1].resize(outwavdim);
for (unsigned long int k = 0;k < outwavdim;k++)
{
a.samples[0][k] = outwavbuf[k * 2];
a.samples[1][k] = outwavbuf[(k * 2)+1];
}
a.save("d:\\prova\\mar10.wav", AudioFileFormat::Wave);
outwavpos = -1;
}*/
}

void audioSystem::updateStream(apu& theAPU)
{
/*float res = 0.0;
for (unsigned int channel = 0;channel < 8;channel++)
{
if ((theAPU.channels[channel].keyOn)&& (!theAPU.channels[channel].keyOff))
{
int sampleNum = theAPU.channels[channel].sampleSourceEntry;
if (sampleNum < 0x14)
{
if (theAPU.channels[channel].playingPos < insArray[sampleNum].len)
{
res += insArray[sampleNum].data[(unsigned int)theAPU.channels[channel].playingPos];
float finc = ((float)theAPU.channels[channel].samplePitch) / (65536.0/8.0);
theAPU.channels[channel].playingPos += finc;
}
}
}
}
res /= 8.0;
audioBuf[bufPos] = res;
bufPos += 1;
if (bufPos >= 1024) bufPos = 0;*/
if (bufPos >= (audioBufLen*2)) bufPos = 0;*/
}

audioSystem::~audioSystem()
{
BASS_Free();
delete(outwavbuf);
}
17 changes: 1 addition & 16 deletions audioSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@
#include "bass/bass.h"
#include "apu.h"
#include "logger.h"
#include "include/AudioFile.h"
//#include "include/AudioFile.h"

class apu;

struct spcInstrument
{
float* data;
unsigned int len = 0;
};

class audioSystem
{
private:
Expand All @@ -27,23 +21,14 @@ class audioSystem
const unsigned int audioBufLen = 1024;
float audioBuf[1024*2];

float* outwavbuf;
signed long int outwavpos = 0;
const unsigned long int outwavdim = 65536*2;

std::vector<float> audioStream;

spcInstrument insArray[0x14];

void loadTestSamples();

public:

bool audioSystemInited = false;
float sampleRate = 0;

audioSystem();
void updateStream(apu& theAPU);
void feedAudiobuf(float l, float r);
~audioSystem();
};
Expand Down
1 change: 1 addition & 0 deletions example_win32_opengl3.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<AdditionalIncludeDirectories>..\..;..\..\backends;</AdditionalIncludeDirectories>
<BufferSecurityCheck>false</BufferSecurityCheck>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down
Loading

0 comments on commit 79b2e7b

Please sign in to comment.