EightBit/LR35902/inc/AudioFrame.h
Adrian.Conlon 877618f973 Wire audio frame counter (tbc)
Signed-off-by: Adrian.Conlon <adrian.conlon@arup.com>
2017-09-21 21:26:52 +01:00

48 lines
1004 B
C++

#pragma once
#include <Signal.h>
namespace EightBit {
namespace GameBoy {
class AudioFrame final {
private:
enum { StepCycle = 7, Frequency = 512 };
int m_cyclesPerTick;
int m_currentStep;
int m_currentCycles;
public:
AudioFrame(int cyclesPerSecond)
: m_cyclesPerTick(cyclesPerSecond / Frequency),
m_currentStep(0),
m_currentCycles(m_cyclesPerTick) {
}
Signal<int> FrameStep;
Signal<int> LengthStep;
Signal<int> VolumeStep;
Signal<int> SweepStep;
void step() {
m_currentStep = (m_currentStep + 1) % StepCycle;
FrameStep.fire(m_currentStep);
if ((m_currentStep % 2) == 0)
LengthStep.fire(m_currentStep);
if (m_currentStep == 7)
VolumeStep.fire(m_currentStep);
if ((m_currentStep == 2) || (m_currentStep == 6))
SweepStep.fire(m_currentStep);
}
void step(int cycles) {
m_currentCycles -= cycles;
if (m_currentCycles < 0) {
step();
m_currentCycles += m_cyclesPerTick;
}
}
};
}
}