SID.h
/*
* This file is part of libsidplayfp, a SID player engine.
*
* Copyright 2011-2025 Leandro Nini <drfiemost@users.sourceforge.net>
* Copyright 2007-2010 Antti Lankila
* Copyright 2004 Dag Lem <resid@nimrod.no>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef SIDFP_H
#define SIDFP_H
#include <memory>
#include <cstdint>
#include "siddefs-fp.h"
#include "ExternalFilter.h"
#include "Potentiometer.h"
#include "Voice.h"
#include "sidcxx11.h"
namespace reSIDfp
{
class Filter;
class Filter6581;
class Filter8580;
class Resampler;
class SIDError
{
private:
const char* message;
public:
SIDError(const char* msg) :
message(msg) {}
const char* getMessage() const { return message; }
};
class SID
{
private:
Filter* filter;
Filter6581* const filter6581;
Filter8580* const filter8580;
std::unique_ptr<Resampler> resampler;
ExternalFilter externalFilter;
Potentiometer potX;
Potentiometer potY;
Voice voice[3];
int scaleFactor;
int busValueTtl;
int modelTTL;
unsigned int nextVoiceSync;
ChipModel model;
CombinedWaveforms cws;
unsigned char busValue;
float envDAC[256];
float oscDAC[4096];
private:
void ageBusValue(unsigned int n);
void voiceSync(bool sync);
public:
SID();
~SID();
void setChipModel(ChipModel model);
ChipModel getChipModel() const { return model; }
void setCombinedWaveforms(CombinedWaveforms cws);
void reset();
void input(int value);
unsigned char read(int offset);
void write(int offset, unsigned char value);
void setSamplingParameters(
double clockFrequency,
SamplingMethod method,
double samplingFrequency
);
int clock(unsigned int cycles, short* buf);
void clockSilent(unsigned int cycles);
void setFilter6581Curve(double filterCurve);
void setFilter6581Range ( double adjustment );
void setFilter8580Curve(double filterCurve);
void enableFilter(bool enable);
};
} // namespace reSIDfp
#if RESID_INLINING || defined(SID_CPP)
#include <algorithm>
#include "Filter.h"
#include "ExternalFilter.h"
#include "Voice.h"
#include "resample/Resampler.h"
namespace reSIDfp
{
RESID_INLINE
void SID::ageBusValue(unsigned int n)
{
if (likely(busValueTtl != 0))
{
busValueTtl -= n;
if (unlikely(busValueTtl <= 0))
{
busValue = 0;
busValueTtl = 0;
}
}
}
RESID_INLINE
int SID::clock(unsigned int cycles, short* buf)
{
ageBusValue(cycles);
int s = 0;
while (cycles != 0)
{
unsigned int delta_t = std::min(nextVoiceSync, cycles);
if (likely(delta_t > 0))
{
for (unsigned int i = 0; i < delta_t; i++)
{
// clock waveform generators
voice[0].wave()->clock();
voice[1].wave()->clock();
voice[2].wave()->clock();
// clock envelope generators
voice[0].envelope()->clock();
voice[1].envelope()->clock();
voice[2].envelope()->clock();
const int sidOutput = static_cast<int>(filter->clock(voice[0], voice[1], voice[2]));
const int c64Output = externalFilter.clock(sidOutput + INT16_MIN);
if (unlikely(resampler->input(c64Output)))
{
buf[s++] = resampler->getOutput(scaleFactor);
}
}
cycles -= delta_t;
nextVoiceSync -= delta_t;
}
if (unlikely(nextVoiceSync == 0))
{
voiceSync(true);
}
}
return s;
}
} // namespace reSIDfp
#endif
#endif
reSIDfp::ExternalFilter
Definition ExternalFilter.h:66
reSIDfp::Filter6581
Definition Filter6581.h:321
reSIDfp::Filter
Definition Filter.h:38
reSIDfp::Potentiometer
Definition Potentiometer.h:38
reSIDfp::Resampler
Definition Resampler.h:40
reSIDfp::SID::setChipModel
void setChipModel(ChipModel model)
Definition SID.cpp:217
reSIDfp::SID::input
void input(int value)
Definition SID.cpp:327
reSIDfp::SID::read
unsigned char read(int offset)
Definition SID.cpp:333
reSIDfp::SID::write
void write(int offset, unsigned char value)
Definition SID.cpp:368
reSIDfp::SID::setSamplingParameters
void setSamplingParameters(double clockFrequency, SamplingMethod method, double samplingFrequency)
Definition SID.cpp:487
reSIDfp::SID::setFilter6581Range
void setFilter6581Range(double adjustment)
Definition SID.cpp:167
reSIDfp::SID::getChipModel
ChipModel getChipModel() const
Definition SID.h:157
reSIDfp::SID::setCombinedWaveforms
void setCombinedWaveforms(CombinedWaveforms cws)
Definition SID.cpp:282
reSIDfp::SID::setFilter6581Curve
void setFilter6581Curve(double filterCurve)
Definition SID.cpp:162
reSIDfp::SID::setFilter8580Curve
void setFilter8580Curve(double filterCurve)
Definition SID.cpp:172
reSIDfp::SID::enableFilter
void enableFilter(bool enable)
Definition SID.cpp:177
reSIDfp::SID::reset
void reset()
Definition SID.cpp:306
reSIDfp::SID::clock
int clock(unsigned int cycles, short *buf)
Definition SID.h:323
reSIDfp::SID::clockSilent
void clockSilent(unsigned int cycles)
Definition SID.cpp:506
reSIDfp::Voice
Definition Voice.h:37