-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.cs
521 lines (462 loc) · 21 KB
/
Sound.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NAudio.Wave;
// References:
// - http://marc.rawer.de/Gameboy/Docs/GBCPUman.pdf probably the best GameBoy CPU/memory manual
// - https://nightshade256.github.io/2021/03/27/gb-sound-emulation.html
// - https://github.com/naudio/NAudio/blob/master/Docs/PlaySineWave.md
namespace ZarthGB
{
class Sound
{
public const int PlayStep = 16; // In ms
public const int DesiredLatency = 160; // PS 8 -> DL 156
private Memory memory;
private const int SampleRate = 192000; // Minimum 192kHz to get enough frequency resolution -else sounds distorted
private const int NumChannels = 1;
private Stopwatch stopwatch = new Stopwatch();
private TimeSpan lastTime;
private MixingWaveProvider32 leftChannel, rightChannel;
private MultiplexingWaveProvider mixer;
private WaveOut waveOut = new WaveOut();
private int buffering;
private const int BufferingRounds = 4;
private byte ChannelControl => memory[0xff24];
private int RightVolume => (ChannelControl >> 4) & 7;
private int LeftVolume => ChannelControl & 7;
private bool RightVinFromCartridge => (ChannelControl & 0x80) != 0;
private bool LeftVinFromCartridge => (ChannelControl & 0x08) != 0;
private byte Output => memory[0xff25];
private bool Sound4ToRight => (Output & 0x80) != 0;
private bool Sound3ToRight => (Output & 0x40) != 0;
private bool Sound2ToRight => (Output & 0x20) != 0;
private bool Sound1ToRight => (Output & 0x10) != 0;
private bool Sound4ToLeft => (Output & 0x08) != 0;
private bool Sound3ToLeft => (Output & 0x04) != 0;
private bool Sound2ToLeft => (Output & 0x02) != 0;
private bool Sound1ToLeft => (Output & 0x01) != 0;
private byte OnOff
{
get => memory[0xff26];
set => memory[0xff26] = value;
}
private bool Sound1On => (OnOff & 1) != 0;
private bool Sound2On => (OnOff & (1 << 1)) != 0;
private bool Sound3On => (OnOff & (1 << 2)) != 0;
private bool Sound4On => (OnOff & (1 << 3)) != 0;
#region Sound1
private GBSignalGenerator signal1;
private BufferedWaveProvider waveBuffer1;
private Dictionary<string, byte[]> bufferCache1 = new Dictionary<string, byte[]>();
private byte Sweep1 => memory[0xff10];
private int SweepShift => Sweep1 & 0x7;
private bool SweepAmplify => (Sweep1 & 0x8) == 0;
private int SweepPeriod => (Sweep1 >> 4) & 0x7;
private byte WaveLength1 => memory[0xff11];
private int WaveDuty1 => WaveLength1 >> 6;
private int Length1 => (64 - (WaveLength1 & 0x3F)) * 4;
private int lengthPlayed1 = 0;
private byte Envelope1 => memory[0xff12];
private double Volume1 => (Envelope1 >> 4) / 15.0;
private bool EnvelopeAmplify1 => (Envelope1 & 0x8) != 0;
private int EnvelopePeriod1 => (Envelope1 & 0x7);
private bool TriggerSound1 => (memory[0xff14] >> 7) != 0;
private int Frequency1 => ((memory[0xff14] & 7) << 8) | memory[0xff13];
private bool Loop1 => (memory[0xff14] & 0x40) == 0;
public void StartSound1()
{
if (TriggerSound1)
{
if (SweepPeriod == 0)
{
// No sweep
signal1 = new GBSignalGenerator(SampleRate, NumChannels)
{
Channel = GBSignalGenerator.ChannelType.Square,
Gain = Volume1,
Frequency = Frequency1,
WaveDuty = WaveDuty1,
EnvelopeAmplify = EnvelopeAmplify1,
EnvelopePeriod = EnvelopePeriod1,
};
}
else
{
// Sweep
signal1 = new GBSignalGenerator(SampleRate, NumChannels)
{
Channel = GBSignalGenerator.ChannelType.Sweep,
Gain = Volume1,
Frequency = Frequency1,
WaveDuty = WaveDuty1,
EnvelopeAmplify = EnvelopeAmplify1,
EnvelopePeriod = EnvelopePeriod1,
SweepPeriod = SweepPeriod,
SweepAmplify = SweepAmplify,
SweepShift = SweepShift,
};
Debug.Print($"QUEUE SWEEP Amplify {SweepAmplify}, Period {SweepPeriod}, Shift {SweepShift}");
}
SetSound1On();
}
}
private void SetSound1On()
{
lengthPlayed1 = 0;
OnOff = (byte) (OnOff | 0x01);
}
private void SetSound1Off()
{
OnOff = (byte) (OnOff & 0xFE); // 11111110
lengthPlayed1 = 0;
signal1 = null;
}
#endregion
#region Sound2
private GBSignalGenerator signal2;
private BufferedWaveProvider waveBuffer2;
private Dictionary<string, byte[]> bufferCache2 = new Dictionary<string, byte[]>();
private byte WaveLength2 => memory[0xff16];
private int WaveDuty2 => WaveLength2 >> 6;
private int Length2 => (64 - (WaveLength2 & 0x3F)) * 4;
private int lengthPlayed2 = 0;
private byte Envelope2 => memory[0xff17];
private double Volume2 => (Envelope2 >> 4) / 15.0;
private bool EnvelopeAmplify2 => (Envelope2 & 0x8) != 0;
private int EnvelopePeriod2 => (Envelope2 & 0x7);
private bool TriggerSound2 => (memory[0xff19] >> 7) != 0;
private int Frequency2 => ((memory[0xff19] & 7) << 8) | memory[0xff18];
private bool Loop2 => (memory[0xff19] & 0x40) == 0;
public void StartSound2()
{
if (TriggerSound2)
{
signal2 = new GBSignalGenerator(SampleRate, NumChannels)
{
Channel = GBSignalGenerator.ChannelType.Square,
Gain = Volume2,
Frequency = Frequency2,
WaveDuty = WaveDuty2,
EnvelopeAmplify = EnvelopeAmplify2,
EnvelopePeriod = EnvelopePeriod2,
};
SetSound2On();
}
}
private void SetSound2On()
{
lengthPlayed2 = 0;
OnOff = (byte) (OnOff | 0x02);
}
private void SetSound2Off()
{
OnOff = (byte) (OnOff & 0xFD); // 11111101
lengthPlayed2 = 0;
signal2 = null;
}
#endregion
#region Sound3
private GBSignalGenerator signal3;
private BufferedWaveProvider waveBuffer3;
private Dictionary<string, byte[]> bufferCache3 = new Dictionary<string, byte[]>();
private bool SoundOn3 => (memory[0xff1a] & 0x7) != 0;
private int Length3 => 256 - memory[0xff1b];
private int lengthPlayed3 = 0;
private int OutputLevel3 => (memory[0xff1c] & 0x60) >> 5;
private bool TriggerSound3 => (memory[0xff1e] >> 7) != 0;
private int Frequency3 => (memory[0xff1e] & 7) << 8 | memory[0xff1d];
private bool Loop3 => (memory[0xff1e] & 0x40) == 0;
private int WaveRamStart = 0xff30;
private int[] Samples => new[]
{
memory[WaveRamStart] >> 4, memory[WaveRamStart] & 0xF,
memory[WaveRamStart + 1] >> 4, memory[WaveRamStart + 1] & 0xF,
memory[WaveRamStart + 2] >> 4, memory[WaveRamStart + 2] & 0xF,
memory[WaveRamStart + 3] >> 4, memory[WaveRamStart + 3] & 0xF,
memory[WaveRamStart + 4] >> 4, memory[WaveRamStart + 4] & 0xF,
memory[WaveRamStart + 5] >> 4, memory[WaveRamStart + 5] & 0xF,
memory[WaveRamStart + 6] >> 4, memory[WaveRamStart + 6] & 0xF,
memory[WaveRamStart + 7] >> 4, memory[WaveRamStart + 7] & 0xF,
memory[WaveRamStart + 8] >> 4, memory[WaveRamStart + 8] & 0xF,
memory[WaveRamStart + 9] >> 4, memory[WaveRamStart + 9] & 0xF,
memory[WaveRamStart + 10] >> 4, memory[WaveRamStart + 10] & 0xF,
memory[WaveRamStart + 11] >> 4, memory[WaveRamStart + 11] & 0xF,
memory[WaveRamStart + 12] >> 4, memory[WaveRamStart + 12] & 0xF,
memory[WaveRamStart + 13] >> 4, memory[WaveRamStart + 13] & 0xF,
memory[WaveRamStart + 14] >> 4, memory[WaveRamStart + 14] & 0xF,
memory[WaveRamStart + 15] >> 4, memory[WaveRamStart + 15] & 0xF,
};
public void StartSound3()
{
if (TriggerSound3)
{
signal3 = new GBSignalGenerator(SampleRate, NumChannels)
{
Channel = GBSignalGenerator.ChannelType.Samples,
Frequency = Frequency3,
Samples = Samples,
OutputShift = Pattern2Shift(OutputLevel3)
};
SetSound3On();
}
}
private int Pattern2Shift(int outputLevel)
{
switch (outputLevel)
{
case 0: return 4;
case 1: return 0;
case 2: return 1;
case 3: return 2;
default: throw new Exception("Invalid output level");
}
}
private void SetSound3On()
{
lengthPlayed3 = 0;
OnOff = (byte) (OnOff | 0x04);
}
private void SetSound3Off()
{
OnOff = (byte) (OnOff & 0xFB); // 11111011
lengthPlayed3 = 0;
signal3 = null;
}
#endregion
#region Sound4
private GBSignalGenerator signal4;
private BufferedWaveProvider waveBuffer4;
private Dictionary<string, byte[]> bufferCache4 = new Dictionary<string, byte[]>();
private int Length4 => (64 - (memory[0xff20] & 0x3F)) * 4;
private int lengthPlayed4 = 0;
private byte Envelope4 => memory[0xff21];
private double Volume4 => (Envelope4 >> 4) / 15.0;
private bool EnvelopeAmplify4 => (Envelope4 & 0x8) != 0;
private int EnvelopePeriod4 => (Envelope4 & 0x7);
private byte PolynomialCounter => memory[0xff22];
private int CounterShift => (PolynomialCounter >> 4);
private bool CounterWidthMode => (PolynomialCounter & 8) != 0;
private int CounterDividingRatio => (PolynomialCounter & 7);
private bool TriggerSound4 => (memory[0xff23] >> 7) != 0;
private bool Loop4 => (memory[0xff23] & 0x40) == 0;
public void StartSound4()
{
if (TriggerSound4)
{
signal4 = new GBSignalGenerator(SampleRate, NumChannels)
{
Channel = GBSignalGenerator.ChannelType.Noise,
Gain = Volume4,
CounterShift = CounterShift,
CounterWidthMode = CounterWidthMode,
CounterDivisor = CounterDividingRatio,
EnvelopeAmplify = EnvelopeAmplify4,
EnvelopePeriod = EnvelopePeriod4,
};
SetSound4On();
}
}
private void SetSound4On()
{
lengthPlayed4 = 0;
OnOff = (byte) (OnOff | 0x08);
}
private void SetSound4Off()
{
OnOff = (byte) (OnOff & 0xF7); // 11110111
lengthPlayed4 = 0;
signal4 = null;
}
#endregion
public Sound(Memory memory)
{
this.memory = memory;
Reset();
waveBuffer1 = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(SampleRate,NumChannels));
waveBuffer2 = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(SampleRate,NumChannels));
waveBuffer3 = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(SampleRate,NumChannels));
waveBuffer4 = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(SampleRate,NumChannels));
waveBuffer1.BufferDuration = TimeSpan.FromMilliseconds( PlayStep * 10 );
waveBuffer2.BufferDuration = TimeSpan.FromMilliseconds( PlayStep * 10 );
waveBuffer3.BufferDuration = TimeSpan.FromMilliseconds( PlayStep * 10 );
waveBuffer4.BufferDuration = TimeSpan.FromMilliseconds( PlayStep * 10 );
waveBuffer1.DiscardOnBufferOverflow = true;
waveBuffer2.DiscardOnBufferOverflow = true;
waveBuffer3.DiscardOnBufferOverflow = true;
waveBuffer4.DiscardOnBufferOverflow = true;
waveBuffer1.ReadFully = false;
waveBuffer2.ReadFully = false;
waveBuffer3.ReadFully = false;
waveBuffer4.ReadFully = false;
SetSoundOutput();
buffering = BufferingRounds;
}
public void SetSoundOutput()
{
leftChannel = new MixingWaveProvider32();
rightChannel = new MixingWaveProvider32();
if (Sound1ToLeft) leftChannel.AddInputStream(waveBuffer1);
if (Sound2ToLeft) leftChannel.AddInputStream(waveBuffer2);
if (Sound3ToLeft) leftChannel.AddInputStream(waveBuffer3);
if (Sound4ToLeft) leftChannel.AddInputStream(waveBuffer4);
if (Sound1ToRight) rightChannel.AddInputStream(waveBuffer1);
if (Sound2ToRight) rightChannel.AddInputStream(waveBuffer2);
if (Sound3ToRight) rightChannel.AddInputStream(waveBuffer3);
if (Sound4ToRight) rightChannel.AddInputStream(waveBuffer4);
mixer = new MultiplexingWaveProvider(new [] { leftChannel, rightChannel }, 2);
mixer.ConnectInputToOutput(0, 0);
mixer.ConnectInputToOutput(1, 1);
waveOut = new WaveOut();
waveOut.DesiredLatency = DesiredLatency;
waveOut.Init(mixer);
}
public void Reset()
{
stopwatch.Restart();
}
public void Play()
{
TimeSpan elapsed = stopwatch.Elapsed - lastTime;
lastTime = stopwatch.Elapsed;
int playStep = elapsed.Milliseconds << 1;
Debug.Print($"Sound: {playStep} / {PlayStep}, {waveOut.PlaybackState}");
Debug.Print($"Buffer1: {waveBuffer1.BufferedDuration.Milliseconds}, Buffer2: {waveBuffer2.BufferedDuration.Milliseconds}, Buffer3: {waveBuffer3.BufferedDuration.Milliseconds}, Buffer4: {waveBuffer4.BufferedDuration.Milliseconds}, ");
//Debug.Print($"Sound1On: {Sound1On}, Sound2On: {Sound2On}, Loop1: {Loop1}, Loop2: {Loop2}");
if (signal1 != null)
{
var playLength = (Loop1 || (lengthPlayed1 + playStep) <= Length1) ? playStep : Length1 - lengthPlayed1;
if (playLength > 0)
{
byte[] buffer;
int bytes;
string key = $"{playLength}-{Frequency1}-{WaveDuty1}-{EnvelopeAmplify1}-{EnvelopePeriod1}-{SweepAmplify}-{SweepPeriod}-{SweepShift}-{Volume1}";
if (!Loop1 && bufferCache1.ContainsKey(key))
{
buffer = bufferCache1[key];
bytes = buffer.Length;
}
else
{
buffer = new byte[waveBuffer1.WaveFormat.AverageBytesPerSecond * playLength / 1000];
var sample = signal1.Take(TimeSpan.FromMilliseconds(playLength));
bytes = sample.ToWaveProvider().Read(buffer, 0, buffer.Length);
bufferCache1[key] = buffer;
}
waveBuffer1.AddSamples(buffer, 0, bytes);
lengthPlayed1 += playLength;
if (!Loop1) SetSound1Off();
}
else
SetSound1Off();
}
else
SetSound1Off();
if (signal2 != null)
{
var playLength = (Loop2 || (lengthPlayed2 + playStep) <= Length2) ? playStep : Length2 - lengthPlayed2;
if (playLength > 0)
{
byte[] buffer;
int bytes;
string key = $"{playLength}-{Frequency2}-{WaveDuty2}-{EnvelopeAmplify2}-{EnvelopePeriod2}-{Volume2}";
if (!Loop2 && bufferCache2.ContainsKey(key))
{
buffer = bufferCache2[key];
bytes = buffer.Length;
}
else
{
buffer = new byte[waveBuffer2.WaveFormat.AverageBytesPerSecond * playLength / 1000];
var sample = signal2.Take(TimeSpan.FromMilliseconds(playLength));
bytes = sample.ToWaveProvider().Read(buffer, 0, buffer.Length);
bufferCache2[key] = buffer;
}
waveBuffer2.AddSamples(buffer, 0, bytes);
lengthPlayed2 += playLength;
if (!Loop2) SetSound2Off();
}
else
SetSound2Off();
}
else
SetSound2Off();
if (signal3 != null)
{
var playLength = (Loop3 || (lengthPlayed3 + playStep) <= Length3) ? playStep : Length3 - lengthPlayed3;
if (playLength > 0)
{
byte[] buffer;
int bytes;
string key = $"{playLength}-{Frequency3}-{OutputLevel3}-{Samples.Sum()}";
if (!Loop3 && bufferCache3.ContainsKey(key))
{
buffer = bufferCache3[key];
bytes = buffer.Length;
}
else
{
buffer = new byte[waveBuffer3.WaveFormat.AverageBytesPerSecond * playLength / 1000];
var sample = signal3.Take(TimeSpan.FromMilliseconds(playLength));
bytes = sample.ToWaveProvider().Read(buffer, 0, buffer.Length);
bufferCache3[key] = buffer;
}
waveBuffer3.AddSamples(buffer, 0, bytes);
lengthPlayed3 += playLength;
if (!Loop3) SetSound3Off();
}
else
SetSound3Off();
}
else
SetSound3Off();
if (signal4 != null)
{
var playLength = (Loop4 || (lengthPlayed4 + playStep) <= Length4) ? playStep : Length4 - lengthPlayed4;
if (playLength > 0)
{
byte[] buffer;
int bytes;
string key = $"{playLength}-{EnvelopeAmplify4}-{EnvelopePeriod4}-{Volume4}-{CounterShift}-{CounterWidthMode}-{CounterDividingRatio}";
if (!Loop4 && bufferCache4.ContainsKey(key))
{
buffer = bufferCache4[key];
bytes = buffer.Length;
}
else
{
buffer = new byte[waveBuffer4.WaveFormat.AverageBytesPerSecond * playLength / 1000];
var sample = signal4.Take(TimeSpan.FromMilliseconds(playLength));
bytes = sample.ToWaveProvider().Read(buffer, 0, buffer.Length);
bufferCache4[key] = buffer;
}
waveBuffer4.AddSamples(buffer, 0, bytes);
lengthPlayed4 += playLength;
if (!Loop4) SetSound4Off();
}
else
SetSound4Off();
}
else
SetSound4Off();
//Debug.Print($"Buffer1: {waveBuffer1.BufferedDuration.Milliseconds}, Buffer2: {waveBuffer2.BufferedDuration.Milliseconds}, Buffer3: {waveBuffer3.BufferedDuration.Milliseconds}, Buffer4: {waveBuffer4.BufferedDuration.Milliseconds}, ");
if (waveOut.PlaybackState != PlaybackState.Playing)
{
if (Sound1On || Sound2On || Sound3On || Sound4On)
{
if (buffering > 0)
buffering--; // Give time to the buffers to build up
else
waveOut.Play();
}
else
buffering = BufferingRounds;
}
if (!Sound1On) SetSound1Off();
if (!Sound2On) SetSound2Off();
if (!Sound3On || !SoundOn3) SetSound3Off();
if (!Sound4On) SetSound4Off();
}
}
}