-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCookieboySound.cpp
193 lines (160 loc) · 4.53 KB
/
CookieboySound.cpp
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
#include "CookieboySound.h"
#include <SDL.h>
#include <algorithm>
Cookieboy::Sound::Sound(const bool &_CGB, int sampleRate, int sampleBufferLength):
Sound1GlobalToggle(1),
Sound2GlobalToggle(1),
Sound3GlobalToggle(1),
Sound4GlobalToggle(1),
CGB(_CGB),
SampleRate(sampleRate),
SampleBufferLength(sampleBufferLength)
{
SampleBuffer = new short[SampleBufferLength];
SamplePeriod = 4194304 / SampleRate;
memset(SampleBuffer, 0, sizeof(short) * SampleBufferLength);
MasterVolume = 1;
Sound1 = new SoundUnit1(CGB, *this);
Sound2 = new SoundUnit2(CGB, *this);
Sound3 = new SoundUnit3(CGB, *this);
Sound4 = new SoundUnit4(CGB, *this);
Reset();
}
Cookieboy::Sound::~Sound()
{
delete Sound1;
delete Sound2;
delete Sound3;
delete Sound4;
delete[] SampleBuffer;
}
void Cookieboy::Sound::Step(DWORD clockDelta)
{
Sound1->TimerStep(clockDelta);
Sound2->TimerStep(clockDelta);
Sound3->TimerStep(clockDelta);
Sound4->TimerStep(clockDelta);
FrameSequencerClock += clockDelta;
if (FrameSequencerClock >= 4194304 / 512)
{
FrameSequencerClock -= 4194304 / 512;
FrameSequencerStep++;
FrameSequencerStep &= 0x7;
if (AllSoundEnabled)
{
Sound1->FrameSequencerStep(FrameSequencerStep);
Sound2->FrameSequencerStep(FrameSequencerStep);
Sound3->FrameSequencerStep(FrameSequencerStep);
Sound4->FrameSequencerStep(FrameSequencerStep);
}
}
SampleCounter += clockDelta;
if (SampleCounter >= SamplePeriod)
{
SampleCounter %= SamplePeriod;
//Mixing is done by adding voltages from every channel
SampleBuffer[SampleBufferPos] = Sound1->GetWaveLeftOutput() * Sound1GlobalToggle +
Sound2->GetWaveLeftOutput() * Sound2GlobalToggle +
Sound3->GetWaveLeftOutput() * Sound3GlobalToggle +
Sound4->GetWaveLeftOutput() * Sound4GlobalToggle;
SampleBuffer[SampleBufferPos + 1] = Sound1->GetWaveRightOutput() * Sound1GlobalToggle +
Sound2->GetWaveRightOutput() * Sound2GlobalToggle +
Sound3->GetWaveRightOutput() * Sound3GlobalToggle +
Sound4->GetWaveRightOutput() * Sound4GlobalToggle;
//Amplifying sound
//Max amplitude for 16-bit audio is 32767. Max channel volume is 15. Max master volume is 7 + 1
//So gain is 32767 / (15 * 8 * 4) ~ 64
SampleBuffer[SampleBufferPos] *= 64;
SampleBuffer[SampleBufferPos + 1] *= 64;
//DMG doesn't have this one. This is global volume so we don't need to use OS volume settings to change emulator volume
SampleBuffer[SampleBufferPos] = short(SampleBuffer[SampleBufferPos] * MasterVolume);
SampleBuffer[SampleBufferPos + 1] = short(SampleBuffer[SampleBufferPos + 1] * MasterVolume);
SampleBufferPos += 2;
//"Resampling" DMG samples to actual sound samples
if (SampleBufferPos >= SampleBufferLength)
{
SampleBufferPos = 0;
NewFrameReady = true;
}
}
}
void Cookieboy::Sound::SetVolume(double vol)
{
MasterVolume = vol;
}
void Cookieboy::Sound::Reset()
{
Sound1->Reset();
Sound2->Reset();
Sound3->Reset();
Sound4->Reset();
NR50Changed(0, true);
NR51Changed(0, true);
AllSoundEnabled = 0;
FrameSequencerClock = 0;
FrameSequencerStep = 1;
SampleCounter = 0;
SampleBufferPos = 0;
NewFrameReady = false;
}
void Cookieboy::Sound::EmulateBIOS()
{
Sound1->EmulateBIOS();
Sound2->EmulateBIOS();
Sound3->EmulateBIOS();
Sound4->EmulateBIOS();
NR50Changed(0x77, true);
NR51Changed(0xF3, true);
NR52Changed(0xF1);
AllSoundEnabled = 1;
}
void Cookieboy::Sound::NR50Changed(BYTE value, bool override)
{
if (!AllSoundEnabled && !override)
{
return;
}
NR50 = value;
}
void Cookieboy::Sound::NR51Changed(BYTE value, bool override)
{
if (!AllSoundEnabled && !override)
{
return;
}
NR51 = value;
}
void Cookieboy::Sound::NR52Changed(BYTE value)
{
//If sound being turned in
if (value & 0x80)
{
if (!AllSoundEnabled)
{
//On power on frame sequencer starts at 1
FrameSequencerStep = 1;
FrameSequencerClock = 0;
Sound1->NR52Changed(value);
Sound2->NR52Changed(value);
Sound3->NR52Changed(value);
Sound4->NR52Changed(value);
//Very important to let know sound units that frame sequencer was reset.
//Test "08-len ctr during power" requires this to pass
Sound1->FrameSequencerStep(FrameSequencerStep);
Sound2->FrameSequencerStep(FrameSequencerStep);
Sound3->FrameSequencerStep(FrameSequencerStep);
Sound4->FrameSequencerStep(FrameSequencerStep);
}
}
else
{
NR50Changed(0, true);
NR51Changed(0, true);
Sound1->NR52Changed(value);
Sound2->NR52Changed(value);
Sound3->NR52Changed(value);
Sound4->NR52Changed(value);
}
//Only all sound on/off can be changed
AllSoundEnabled = value >> 7;
}