1#ifndef YM2413BURCZYNSKI_HH
2#define YM2413BURCZYNSKI_HH
13namespace YM2413Burczynski {
16static constexpr int SIN_BITS = 10;
17static constexpr size_t SIN_LEN = 1 << SIN_BITS;
18static constexpr size_t SIN_MASK = SIN_LEN - 1;
38 [[nodiscard]]
int calcOutput(
const Channel& channel,
unsigned eg_cnt,
bool carrier,
39 unsigned lfo_am,
int phase);
41 unsigned lfo_pm,
unsigned lfo_am);
111 template<
typename Archive>
112 void serialize(Archive& ar,
unsigned version);
127 void updateTotalLevel(
const Channel& channel);
128 void updateAttackRate(
int kcodeScaled);
129 void updateDecayRate(
int kcodeScaled);
130 void updateReleaseRate(
int kcodeScaled);
132 std::span<const unsigned, SIN_LEN> waveTable;
145 std::array<int, 2> op1_out = {0, 0};
146 bool eg_sustain{
false};
151 std::span<const uint8_t, 8> eg_sel_dp;
152 std::span<const uint8_t, 8> eg_sel_ar;
153 std::span<const uint8_t, 8> eg_sel_dr;
154 std::span<const uint8_t, 8> eg_sel_rr;
155 std::span<const uint8_t, 8> eg_sel_rs;
156 unsigned eg_mask_dp{0};
157 unsigned eg_mask_ar{0};
158 unsigned eg_mask_dr{0};
159 unsigned eg_mask_rr{0};
160 unsigned eg_mask_rs{0};
184 [[nodiscard]]
int calcOutput(
unsigned eg_cnt,
unsigned lfo_pm,
unsigned lfo_am,
int fm);
216 template<
typename Archive>
217 void serialize(Archive& ar,
unsigned version);
236 void reset()
override;
237 void writePort(
bool port, uint8_t value,
int offset)
override;
238 void pokeReg(uint8_t reg, uint8_t value)
override;
239 [[nodiscard]] uint8_t
peekReg(uint8_t reg)
const override;
240 void generateChannels(std::span<float*, 9 + 5> bufs,
unsigned num)
override;
243 template<
typename Archive>
244 void serialize(Archive& ar,
unsigned version);
247 void writeReg(uint8_t reg, uint8_t value);
251 void resetOperators();
253 [[nodiscard]]
bool isRhythm()
const;
255 [[nodiscard]]
Channel& getChannelForReg(uint8_t reg);
261 void updateCustomInstrument(
int part, uint8_t value);
263 void setRhythmFlags(uint8_t old);
267 std::array<Channel, 9> channels;
276 unsigned idleSamples;
289 std::array<std::array<uint8_t, 8>, 19> inst_tab;
292 std::array<uint8_t, 0x40> reg;
293 uint8_t registerLatch;
void setSustain(bool sustained)
void updateInstrument(std::span< const uint8_t, 8 > inst)
Sets all synthesis parameters as specified by the instrument.
FreqIndex getFrequencyIncrement() const
void updateInstrumentPart(int part, uint8_t value)
Sets some synthesis parameters as specified by the instrument.
void setFrequencyLow(uint8_t value)
Changes the lower 8 bits of the frequency for this channel.
void serialize(Archive &ar, unsigned version)
uint8_t getKeyCode() const
int getKeyScaleLevelBase() const
void setFrequency(int block_fnum)
Sets the frequency for this channel.
void setFrequencyHigh(uint8_t value)
Changes the higher 4 bits of the frequency for this channel.
int calcOutput(unsigned eg_cnt, unsigned lfo_pm, unsigned lfo_am, int fm)
Calculate the value of the current sample produced by this channel.
void setSustainLevel(uint8_t value)
Sets the sustain level [0..15].
EnvelopeState
Envelope Generator phases Note: These are ordered: phase constants are compared in the code.
int calc_phase(const Channel &channel, unsigned lfo_pm)
int calc_slot_mod(const Channel &channel, unsigned eg_cnt, bool carrier, unsigned lfo_pm, unsigned lfo_am)
int calc_envelope(const Channel &channel, unsigned eg_cnt, bool carrier)
void setEnvelopeSustained(bool value)
Sets the envelope type of the current instrument.
void setKeyOff(KeyPart part)
void setKeyOnOff(KeyPart part, bool enabled)
void setAttackRate(const Channel &channel, uint8_t value)
Sets the attack rate [0..15].
void setFrequencyMultiplier(uint8_t value)
Sets the frequency multiplier [0..15].
void setReleaseRate(const Channel &channel, uint8_t value)
Sets the release rate [0..15].
void setWaveform(uint8_t value)
Sets the waveform: 0 = sinus, 1 = half sinus, half silence.
void updateFrequency(const Channel &channel)
Called by Channel when block_fnum changes.
int calcOutput(const Channel &channel, unsigned eg_cnt, bool carrier, unsigned lfo_am, int phase)
void serialize(Archive &ar, unsigned version)
void setVibrato(bool value)
Enables (true) or disables (false) vibrato.
void setAmplitudeModulation(bool value)
Enables (true) or disables (false) amplitude modulation.
void setKeyOn(KeyPart part)
void setDecayRate(const Channel &channel, uint8_t value)
Sets the decay rate [0..15].
void setFeedbackShift(uint8_t value)
Sets the amount of feedback [0..7].
void setKeyScaleLevel(const Channel &channel, uint8_t value)
Sets the key scale level: 0->0 / 1->1.5 / 2->3.0 / 3->6.0 dB/OCT.
void updateGenerators(const Channel &channel)
Update phase increment counter of operator.
void setTotalLevel(const Channel &channel, uint8_t value)
Sets the total level: [0..63].
bool isActive() const
Does this slot currently produce an output signal?
void setKeyScaleRate(bool value)
Sets the key scale rate: true->0, false->2.
void generateChannels(std::span< float *, 9+5 > bufs, unsigned num) override
Generate the sound output.
void writePort(bool port, uint8_t value, int offset) override
Write to the YM2413 register/data port.
void serialize(Archive &ar, unsigned version)
void reset() override
Reset this YM2413 core.
void pokeReg(uint8_t reg, uint8_t value) override
Write to a YM2413 register (for debug).
float getAmplificationFactor() const override
Returns normalization factor.
uint8_t peekReg(uint8_t reg) const override
Read from a YM2413 register (for debug).
Abstract interface for the YM2413 core.
FixedPoint< 16 > FreqIndex
16.16 fixed point type for frequency calculations.
This file implemented 3 utility functions: