-
Notifications
You must be signed in to change notification settings - Fork 0
/
GBSignalGenerator.cs
243 lines (213 loc) · 7.58 KB
/
GBSignalGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
namespace ZarthGB
{
public class GBSignalGenerator : ISampleProvider
{
public enum ChannelType
{
Sweep,
Square,
Samples,
Noise
}
private int nSample;
private string[] WaveDutyTable =
{
"00000001",
"00000011",
"00001111",
"11111100"
};
private int waveDutyPosition;
private int frequencyTimerStart;
private int frequencyTimer;
private double frequency;
private int currentVolume;
private int periodTimer;
private double gain;
private int envelopePeriod;
private int shadowFrequency;
private int sweepTimer;
private ushort lfsr;
private double clockCorrection;
public WaveFormat WaveFormat { get; }
public int WaveDuty { get; set; }
public int SweepPeriod { get; set; }
public bool SweepAmplify { get; set; }
public int SweepShift { get; set; }
public ChannelType Channel { get; set; }
public bool EnvelopeAmplify { get; set; }
public int[] Samples = new int[32];
public int OutputShift { get; set; }
public int CounterShift { get; set; }
public bool CounterWidthMode { get; set; }
public int CounterDivisor { get; set; }
public double Gain
{
get => gain;
set
{
gain = value;
currentVolume = (int) (15 * gain);
lfsr = (ushort)currentVolume;
}
}
public int EnvelopePeriod
{
get => envelopePeriod;
set => envelopePeriod = value * 2048;
// I cannot explain 2048 -I was expecting 64*4 (64 Hz when sampled in 44.1 kHz, so 256 Hz when sampled in 192 kHz)
}
public double Frequency
{
get => frequency;
set
{
frequency = value;
frequencyTimerStart = (2048 - (int)Frequency) * 4;
frequencyTimerStart = (int) (frequencyTimerStart * clockCorrection);
frequencyTimer = 0;
waveDutyPosition = 0;
}
}
public GBSignalGenerator(int sampleRate, int channels)
{
WaveFormat = WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels);
Gain = 1.0;
clockCorrection = (double)WaveFormat.SampleRate / (double)(4.19*1024*1024); // 4.19 MHz
}
public int Read(float[] buffer, int offset, int count)
{
int bufOffset = offset;
for (int index1 = 0; index1 < count / WaveFormat.Channels; ++index1)
{
double sample;
switch (Channel)
{
case ChannelType.Sweep:
ApplySquareWave();
ApplyEnvelope();
ApplySweep();
sample = (WaveDutyTable[WaveDuty][waveDutyPosition] == '1') ? gain: -gain;
nSample++;
break;
case ChannelType.Square:
ApplySquareWave();
ApplyEnvelope();
sample = (WaveDutyTable[WaveDuty][waveDutyPosition] == '1') ? gain: -gain;
nSample++;
break;
case ChannelType.Samples:
ApplySamples();
sample = (((Samples[waveDutyPosition] & 0xF) >> OutputShift) - 7.5) / 7.5;
nSample++;
break;
case ChannelType.Noise:
ApplyNoise();
ApplyEnvelope();
sample = ((lfsr & 0x01) == 0) ? gain : -gain;
nSample++;
break;
default:
sample = 0.0;
break;
}
for (int index2 = 0; index2 < WaveFormat.Channels; ++index2)
buffer[bufOffset++] = (float) sample;
}
return count;
}
private void ApplyNoise()
{
if (frequencyTimer == 0)
{
frequencyTimer = (CounterDivisor > 0 ? (CounterDivisor << 4) : 8) << CounterShift;
frequencyTimer = (int) (frequencyTimer * clockCorrection);
byte xorResult = (byte)((lfsr & 1) ^ ((lfsr & 2) >> 1));
lfsr = (ushort)((lfsr >> 1) | (xorResult << 14));
if (CounterWidthMode)
{
lfsr = (ushort)(lfsr & ~0x40);
lfsr = (ushort)(lfsr | (xorResult << 6));
}
}
else
frequencyTimer--;
}
private void ApplySamples()
{
if (frequencyTimer == 0)
{
waveDutyPosition = (waveDutyPosition + 1) % Samples.Length;
frequencyTimer = frequencyTimerStart;
}
else
frequencyTimer--;
}
private void ApplySquareWave()
{
if (frequencyTimer == 0)
{
waveDutyPosition = (waveDutyPosition + 1) % WaveDutyTable[WaveDuty].Length;
frequencyTimer = frequencyTimerStart;
}
else
frequencyTimer--;
}
private void ApplyEnvelope()
{
if (envelopePeriod != 0)
{
if (periodTimer > 0)
periodTimer -= 1;
if (periodTimer == 0)
{
periodTimer = envelopePeriod;
if ((currentVolume < 0xF && EnvelopeAmplify) ||
(currentVolume > 0x0 && !EnvelopeAmplify))
{
int adjustment = EnvelopeAmplify ? 1 : -1;
currentVolume += adjustment;
gain = (double) currentVolume / 15.0;
}
}
}
}
private void ApplySweep()
{
if (sweepTimer > 0)
sweepTimer -= 1;
if (sweepTimer == 0)
{
if (SweepPeriod > 0)
sweepTimer = SweepPeriod;
else
sweepTimer = 8;
if (SweepShift > 0 && SweepPeriod > 0)
{
int newFrequency = UpdateFrequency();
if (newFrequency <= 2047 && SweepShift > 0)
{
frequency = newFrequency;
shadowFrequency = newFrequency;
/* for overflow check */
UpdateFrequency();
}
}
}
}
private int UpdateFrequency()
{
int newFrequency = shadowFrequency >> SweepShift;
if (SweepAmplify)
newFrequency = shadowFrequency + newFrequency;
else
newFrequency = shadowFrequency - newFrequency;
// overflow check
if (newFrequency > 2047)
currentVolume = 0; // It should be stop channel completely
return newFrequency;
}
}
}