Skip to content

Commit

Permalink
using a std::queue for audio
Browse files Browse the repository at this point in the history
  • Loading branch information
friol committed Mar 26, 2024
1 parent 905f818 commit ff47873
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 46 deletions.
56 changes: 13 additions & 43 deletions audioSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@

#include "math.h"
#include "audioSystem.h"

extern logger glbTheLogger;

std::vector<float> audioVector;
static bool travasing = false;
float buffaPos = 0;

static std::deque<float> audioQueue;

//

Expand All @@ -18,12 +14,11 @@ DWORD CALLBACK StreamProc(HSTREAM handle, float* buffer, DWORD length, void* use

float* paudioInc = (float*)user;

travasing = true;
unsigned int avsize = (unsigned int)audioVector.size();
const unsigned int avsize = (unsigned int)audioQueue.size();

if (length == 0) return 0;

if (avsize == 0)
if (avsize < 2)
{
for (unsigned int pos = 0;pos < length / sizeof(float);pos ++)
{
Expand All @@ -33,23 +28,21 @@ DWORD CALLBACK StreamProc(HSTREAM handle, float* buffer, DWORD length, void* use
return length;
}

float nsamplesAva = avsize - buffaPos;
//glbTheLogger.logMsg("samples available:"+std::to_string(avsize- buffaPos));
float nsamplesAva = avsize;
//glbTheLogger.logMsg("samples available:"+std::to_string(avsize));
//glbTheLogger.logMsg("audio inc:"+std::to_string((*paudioInc)));

if (nsamplesAva > 15000) (*paudioInc) -= 0.001f;
else (*paudioInc) += 0.001f;

//float inc = 1.0;
//inc = (avsize - buffaPos) / (length / sizeof(float));
//glbTheLogger.logMsg("inc is:" + std::to_string(inc));

for (unsigned int pos = 0;pos < length / sizeof(float);pos += 2)
{
if (buffaPos < avsize)
if (audioQueue.size()>=2)
{
buffer[pos] = audioVector[(unsigned int)buffaPos];
buffer[pos + 1] = audioVector[(unsigned int)buffaPos + 1];
buffaPos += 2;
buffer[pos] = audioQueue[0];
audioQueue.pop_front();
buffer[pos + 1] = audioQueue[0];
audioQueue.pop_front();
}
else
{
Expand All @@ -58,12 +51,8 @@ DWORD CALLBACK StreamProc(HSTREAM handle, float* buffer, DWORD length, void* use
//glbTheLogger.logMsg("not enough samples to fill audio buffer");
}
}

//audioVector.clear();
travasing = false;

return length;
//return avsize*sizeof(float);
}

audioSystem::audioSystem()
Expand Down Expand Up @@ -98,27 +87,8 @@ audioSystem::audioSystem()

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

/*float samplesPerFrame = (sampleRate / 60.0) * 2;
DWORD status = BASS_ChannelGetData(stream, NULL, BASS_DATA_AVAILABLE);
if ((status != -1) && (status < samplesPerFrame))
{
const int dim = audioVector.size();
if (dim>=samplesPerFrame)
{
glbTheLogger.logMsg("Available samples:" + std::to_string(status));
glbTheLogger.logMsg("Feeding:" + std::to_string(dim));
for (unsigned int pos = 0;pos < dim;pos++)
{
tmpbuf[pos] = audioVector[pos];
}
BASS_StreamPutData(stream, (void*)tmpbuf, dim);
audioVector.clear();
}
}*/
audioQueue.push_back(l);
audioQueue.push_back(r);
}

audioSystem::~audioSystem()
Expand Down
2 changes: 1 addition & 1 deletion audioSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#ifndef AUDIOSYSTEM_H
#define AUDIOSYSTEM_H

#include <deque>
#include <vector>
#include <iomanip>
#include <sstream>
#include "bass/bass.h"
#include "apu.h"
#include "logger.h"
//#include "include/AudioFile.h"

class apu;

Expand Down
20 changes: 19 additions & 1 deletion cpu5a22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6369,7 +6369,7 @@ int cpu5a22::stepOne()
unsigned int addr = getDirectPageIndirectX();

doADC(addr);
if (regP.getAccuMemSize())
if (regP.getAccuMemSize()==0)
{
cycAdder += 1;;
}
Expand Down Expand Up @@ -6614,6 +6614,24 @@ int cpu5a22::stepOne()
cycles = 6 + cycAdder;
break;
}
case 0xe1:
{
// SBC (dp,X)
int cycAdder = 0;
unsigned int addr = getDirectPageIndirectX();

doSBC(addr);
if (regP.getAccuMemSize()==0)
{
cycAdder += 1;
}

if (regD & 0xff) cycAdder += 1;

regPC += 2;
cycles = cycAdder + 6;
break;
}
default:
{
// unknown opcode
Expand Down
1 change: 1 addition & 0 deletions debugger5a22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ static const debugInfoRec debugInstrData[] =
{0xDE,3,"DEC param0,X",Absolute16,false,false,true}, // validatedF
{0xdf,4,"CMP param0,X",AbsoluteLong,false,false ,true}, // validatedF
{0xE0,2,"CPX param0",Immediate,true,false,true}, // validatedF
{0xe1,2,"SBC (param0,X)",Immediate8,false,false,true}, // validatedF
{0xe2,2,"SEP param0",Immediate8,false,false,true}, // validatedF
{0xe3,2,"SBC param0,S",Immediate8,false,false,true}, // validatedF
{0xe4,2,"CPX param0",Immediate8,false,false,true}, // validatedF
Expand Down
Loading

0 comments on commit ff47873

Please sign in to comment.