Skip to content

Commit

Permalink
the actual average BPM code
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Jun 6, 2016
1 parent 915e39e commit 0577dda
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Source/SoundGen.cpp
Expand Up @@ -142,7 +142,8 @@ CSoundGen::CSoundGen() :
m_iClipCounter(0),
m_pSequencePlayPos(NULL),
m_iSequencePlayPos(0),
m_iSequenceTimeout(0)
m_iSequenceTimeout(0),
m_iBPMCachePosition(0) // // //
{
TRACE("SoundGen: Object created\n");

Expand Down Expand Up @@ -1082,6 +1083,16 @@ void CSoundGen::BeginPlayer(play_mode_t Mode, int Track)
std::queue<int>().swap(m_iRegisterStream);
#endif

{ // // // 050B
m_iRowTickCount = 0;

float Tempo = GetTempo();
for (auto &x : m_fBPMCacheValue)
x = Tempo;
for (auto &x : m_iBPMCacheTicks)
x = 1;
}

ResetTempo();
ResetAPU();

Expand Down Expand Up @@ -1466,6 +1477,17 @@ float CSoundGen::GetTempo() const
return !m_iSpeed ? 0 : float(Tempo * 6) / Speed;
}

float CSoundGen::GetAverageBPM() const
{
float BPMtot = 0.f;
float TickTot = 0.f;
for (int i = 0; i < AVERAGE_BPM_SIZE; ++i)
BPMtot += m_fBPMCacheValue[i] * m_iBPMCacheTicks[i];
for (const auto &x : m_iBPMCacheTicks)
TickTot += x;
return static_cast<float>(BPMtot / TickTot);
}

void CSoundGen::RunFrame()
{
// Called from player thread
Expand Down Expand Up @@ -1500,6 +1522,7 @@ void CSoundGen::RunFrame()
}
#endif /* EXPORT_TEST */

++m_iRowTickCount; // // // 050B
m_iStepRows = 0;

// Fetch next row
Expand All @@ -1517,6 +1540,12 @@ void CSoundGen::RunFrame()
m_bUpdateRow = true;
ReadPatternRow();
++m_iRenderRow;

// // // 050B
m_fBPMCacheValue[m_iBPMCachePosition] = GetTempo(); // // // 050B
m_iBPMCacheTicks[m_iBPMCachePosition] = m_iRowTickCount;
m_iRowTickCount = 0;
++m_iBPMCachePosition %= AVERAGE_BPM_SIZE;
}
else {
m_bUpdateRow = false;
Expand Down
7 changes: 7 additions & 0 deletions Source/SoundGen.h
Expand Up @@ -152,6 +152,7 @@ class CSoundGen : public CWinThread, IAudioCallback
void ResetState();
void ResetTempo();
float GetTempo() const;
float GetAverageBPM() const; // // //
bool IsPlaying() const { return m_bPlaying; };

// Stats
Expand Down Expand Up @@ -359,6 +360,7 @@ class CSoundGen : public CWinThread, IAudioCallback
int m_iSkipToRow;
bool m_bDoHalt; // // // Cxx effect
int m_iStepRows; // # of rows skipped last update
int m_iRowTickCount; // // // 050B
play_mode_t m_iPlayMode;

unsigned int m_iNoteLookupTableNTSC[96]; // For 2A03
Expand Down Expand Up @@ -388,6 +390,11 @@ class CSoundGen : public CWinThread, IAudioCallback
int m_iTempoRemainder;
bool m_bUpdateRow;

static const int AVERAGE_BPM_SIZE = 24; // // // 050B
float m_fBPMCacheValue[AVERAGE_BPM_SIZE];
int m_iBPMCacheTicks[AVERAGE_BPM_SIZE];
int m_iBPMCachePosition;

std::queue<int> m_iRegisterStream; // // // vgm export

CWaveFile m_wfWaveFile;
Expand Down

0 comments on commit 0577dda

Please sign in to comment.