openMSX
YM2413Burczynski.hh
Go to the documentation of this file.
1#ifndef YM2413BURCZYNSKI_HH
2#define YM2413BURCZYNSKI_HH
3
4#include "YM2413Core.hh"
5#include "FixedPoint.hh"
6#include "serialize_meta.hh"
7#include <array>
8#include <span>
9
10namespace openmsx {
11namespace YM2413Burczynski {
12
13// sin wave entries
14static constexpr int SIN_BITS = 10;
15static constexpr size_t SIN_LEN = 1 << SIN_BITS;
16static constexpr size_t SIN_MASK = SIN_LEN - 1;
17
18class Channel;
19
23
24class Slot
25{
26public:
27 Slot();
28
29 void resetOperators();
30
34 void updateGenerators(Channel& channel);
35
36 [[nodiscard]] inline int calcOutput(Channel& channel, unsigned eg_cnt, bool carrier,
37 unsigned lfo_am, int phase);
38 [[nodiscard]] inline int calc_slot_mod(Channel& channel, unsigned eg_cnt, bool carrier,
39 unsigned lfo_pm, unsigned lfo_am);
40 [[nodiscard]] inline int calc_envelope(Channel& channel, unsigned eg_cnt, bool carrier);
41 [[nodiscard]] inline int calc_phase(Channel& channel, unsigned lfo_pm);
42
43 enum KeyPart { KEY_MAIN = 1, KEY_RHYTHM = 2 };
44 void setKeyOn(KeyPart part);
45 void setKeyOff(KeyPart part);
46 void setKeyOnOff(KeyPart part, bool enabled);
47
50 [[nodiscard]] bool isActive() const;
51
54 void setFrequencyMultiplier(uint8_t value);
55
58 void setKeyScaleRate(bool value);
59
63 void setEnvelopeSustained(bool value);
64
67 void setVibrato(bool value);
68
71 void setAmplitudeModulation(bool value);
72
75 void setTotalLevel(Channel& channel, uint8_t value);
76
79 void setKeyScaleLevel(Channel& channel, uint8_t value);
80
83 void setWaveform(uint8_t value);
84
87 void setFeedbackShift(uint8_t value);
88
91 void setAttackRate(const Channel& channel, uint8_t value);
92
95 void setDecayRate(const Channel& channel, uint8_t value);
96
99 void setReleaseRate(const Channel& channel, uint8_t value);
100
103 void setSustainLevel(uint8_t value);
104
107 void updateFrequency(Channel& channel);
108
109 template<typename Archive>
110 void serialize(Archive& ar, unsigned version);
111
112public: // public for serialization, otherwise could be private
118 };
119
120private:
123 void setEnvelopeState(EnvelopeState state);
124
125 inline void updateTotalLevel(Channel& channel);
126 inline void updateAttackRate(int kcodeScaled);
127 inline void updateDecayRate(int kcodeScaled);
128 inline void updateReleaseRate(int kcodeScaled);
129
130 std::span<const unsigned, SIN_LEN> waveTable; // waveform select
131
132 // Phase Generator
133 FreqIndex phase{0}; // frequency counter
134 FreqIndex freq{0}; // frequency counter step
135
136 // Envelope Generator
137 int TL{0}; // total level: TL << 2
138 int TLL{0}; // adjusted now TL
139 int egOut{0}; // envelope counter
140 int sl{0}; // sustain level: sl_tab[SL]
141 EnvelopeState state;
142
143 std::array<int, 2> op1_out = {0, 0}; // MOD output for feedback
144 bool eg_sustain{false}; // percussive/non-percussive mode
145 uint8_t fb_shift{0}; // feedback shift value
146
147 uint8_t key{0}; // 0 = KEY OFF, >0 = KEY ON
148
149 std::span<const uint8_t, 8> eg_sel_dp;
150 std::span<const uint8_t, 8> eg_sel_ar;
151 std::span<const uint8_t, 8> eg_sel_dr;
152 std::span<const uint8_t, 8> eg_sel_rr;
153 std::span<const uint8_t, 8> eg_sel_rs;
154 unsigned eg_mask_dp{0}; // == (1 << eg_sh_dp) - 1
155 unsigned eg_mask_ar{0}; // == (1 << eg_sh_ar) - 1
156 unsigned eg_mask_dr{0}; // == (1 << eg_sh_dr) - 1
157 unsigned eg_mask_rr{0}; // == (1 << eg_sh_rr) - 1
158 unsigned eg_mask_rs{0}; // == (1 << eg_sh_rs) - 1
159 uint8_t eg_sh_dp{0}; // (dump state)
160 uint8_t eg_sh_ar{0}; // (attack state)
161 uint8_t eg_sh_dr{0}; // (decay state)
162 uint8_t eg_sh_rr{0}; // (release state for non-perc.)
163 uint8_t eg_sh_rs{0}; // (release state for perc.mode)
164
165 uint8_t ar{0}; // attack rate: AR<<2
166 uint8_t dr{0}; // decay rate: DR<<2
167 uint8_t rr{0}; // release rate:RR<<2
168 uint8_t KSR{0}; // key scale rate
169 uint8_t ksl{0}; // key scale level
170 uint8_t mul{0}; // multiple: mul_tab[ML]
171
172 // LFO
173 uint8_t AMmask{0}; // LFO Amplitude Modulation enable mask
174 uint8_t vib{0}; // LFO Phase Modulation enable flag (active high)
175};
176
178{
179public:
182 [[nodiscard]] inline int calcOutput(unsigned eg_cnt, unsigned lfo_pm, unsigned lfo_am, int fm);
183
186 void setFrequency(int block_fnum);
187
190 void setFrequencyLow(uint8_t value);
191
194 void setFrequencyHigh(uint8_t value);
195
200 void updateInstrumentPart(int part, uint8_t value);
201
205 void updateInstrument(std::span<const uint8_t, 8> inst);
206
207 [[nodiscard]] int getBlockFNum() const;
208 [[nodiscard]] FreqIndex getFrequencyIncrement() const;
209 [[nodiscard]] int getKeyScaleLevelBase() const;
210 [[nodiscard]] uint8_t getKeyCode() const;
211 [[nodiscard]] bool isSustained() const;
212 void setSustain(bool sustained);
213
214 template<typename Archive>
215 void serialize(Archive& ar, unsigned version);
216
219
220private:
221 // phase generator state
222 int block_fnum{0}; // block+fnum
223 FreqIndex fc{0}; // Freq. increment base
224 int ksl_base{0}; // KeyScaleLevel Base step
225 bool sus{false}; // sus on/off (release speed in percussive mode)
226};
227
228class YM2413 final : public YM2413Core
229{
230public:
231 YM2413();
232
233 // YM2413Core
234 void reset() override;
235 void writePort(bool port, uint8_t value, int offset) override;
236 void pokeReg(uint8_t reg, uint8_t value) override;
237 [[nodiscard]] uint8_t peekReg(uint8_t reg) const override;
238 void generateChannels(std::span<float*, 9 + 5> bufs, unsigned num) override;
239 [[nodiscard]] float getAmplificationFactor() const override;
240
241 template<typename Archive>
242 void serialize(Archive& ar, unsigned version);
243
244private:
245 void writeReg(uint8_t reg, uint8_t value);
246
249 void resetOperators();
250
251 [[nodiscard]] inline bool isRhythm() const;
252
253 [[nodiscard]] Channel& getChannelForReg(uint8_t reg);
254
259 void updateCustomInstrument(int part, uint8_t value);
260
261 void setRhythmFlags(uint8_t old);
262
263private:
265 std::array<Channel, 9> channels;
266
268 unsigned eg_cnt;
269
271 int noise_rng;
272
274 unsigned idleSamples;
275
278 LFOAMIndex lfo_am_cnt;
279 LFOPMIndex lfo_pm_cnt;
280
287 std::array<std::array<uint8_t, 8>, 19> inst_tab;
288
290 std::array<uint8_t, 0x40> reg;
291 uint8_t registerLatch;
292};
293
294} // namespace YM2413Burczynski
295
299
300} // namespace openmsx
301
302#endif
void updateInstrument(std::span< const uint8_t, 8 > inst)
Sets all synthesis parameters as specified by the instrument.
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)
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.
void updateFrequency(Channel &channel)
Called by Channel when block_fnum changes.
void setEnvelopeSustained(bool value)
Sets the envelope type of the current instrument.
void setKeyOnOff(KeyPart part, bool enabled)
void setAttackRate(const Channel &channel, uint8_t value)
Sets the attack rate [0..15].
int calcOutput(Channel &channel, unsigned eg_cnt, bool carrier, unsigned lfo_am, int phase)
void setTotalLevel(Channel &channel, uint8_t value)
Sets the total level: [0..63].
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.
int calc_envelope(Channel &channel, unsigned eg_cnt, bool carrier)
void serialize(Archive &ar, unsigned version)
void setVibrato(bool value)
Enables (true) or disables (false) vibrato.
int calc_phase(Channel &channel, unsigned lfo_pm)
void setAmplitudeModulation(bool value)
Enables (true) or disables (false) amplitude modulation.
void setDecayRate(const Channel &channel, uint8_t value)
Sets the decay rate [0..15].
void setKeyScaleLevel(Channel &channel, uint8_t value)
Sets the key scale level: 0->0 / 1->1.5 / 2->3.0 / 3->6.0 dB/OCT.
void setFeedbackShift(uint8_t value)
Sets the amount of feedback [0..7].
bool isActive() const
Does this slot currently produce an output signal?
int calc_slot_mod(Channel &channel, unsigned eg_cnt, bool carrier, unsigned lfo_pm, unsigned lfo_am)
void setKeyScaleRate(bool value)
Sets the key scale rate: true->0, false->2.
void updateGenerators(Channel &channel)
Update phase increment counter of operator.
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.
Definition: YM2413Core.hh:28
FixedPoint< 16 > FreqIndex
16.16 fixed point type for frequency calculations.
This file implemented 3 utility functions:
Definition: Autofire.cc:9
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)