-
Notifications
You must be signed in to change notification settings - Fork 0
/
apu.hpp
189 lines (168 loc) · 4.6 KB
/
apu.hpp
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
// Copyright 2019, SUZUKI PLAN (MIT license)
// VGS Audio Processing Unit emulator
#ifndef INCLUDE_APU_HPP
#define INCLUDE_APU_HPP
#include "vgs8.h"
using namespace VGS8;
class APU
{
struct Register {
unsigned char bgmPlaying;
unsigned char bgmCursor;
unsigned char effPausing;
unsigned char effMasterVolume;
unsigned int bgmCurrentTime;
unsigned char effPlaying[256];
unsigned int effCursor[256];
};
private:
VirtualMachine* vm;
void* vgsdec;
struct Register reg;
short soundBuffer1[367];
short soundBuffer2[368];
int soundBufferA;
public:
APU(VirtualMachine* vm)
{
this->vm = vm;
vgsdec = vgsdec_create_context();
soundBufferA = 0;
reset();
}
~APU()
{
vgsdec_release_context(vgsdec);
vgsdec = NULL;
}
void reset()
{
memset(®, 0, sizeof(reg));
}
void playBgm(unsigned char n)
{
reg.bgmCursor = n;
void* data = vm->bank->bgm[n];
if (data) {
vgsdec_load_bgm_from_memory(vgsdec, data, vm->bank->bgmSize[n]);
updateBgmMasterVolume();
reg.bgmPlaying = 1;
} else {
reg.bgmPlaying = 0;
}
}
void pauseBgm()
{
reg.bgmPlaying = 0;
}
void resumeBgm()
{
if (vm->bank->bgm[reg.bgmCursor]) {
reg.bgmPlaying = 1;
}
}
bool isBgmPlaying()
{
return reg.bgmPlaying ? true : false;
}
void updateBgmMasterVolume()
{
vgsdec_set_value(vgsdec, VGSDEC_REG_VOLUME_RATE, 25500 / (255 - vm->cpu->ram[0x5604]));
}
void playEff(unsigned char n)
{
if (vm->bank->eff[n]) {
reg.effPlaying[n] = 1;
reg.effCursor[n] = 0;
}
}
void stopEff(unsigned char n)
{
reg.effPlaying[n] = 0;
}
void pauseEff()
{
reg.effPausing = 1;
}
void resumeEff()
{
reg.effPausing = 0;
}
void updateEffMasterVolume()
{
reg.effMasterVolume = vm->cpu->ram[0x5504];
}
void execute()
{
// 1フレーム毎にバッファを367samplesと368samplesで切り替えることで音ズレの発生を防ぐ
short* soundBuffer;
size_t soundBufferSize;
soundBufferA = 1 - soundBufferA;
if (soundBufferA) {
soundBuffer = soundBuffer1;
soundBufferSize = sizeof(soundBuffer1);
} else {
soundBuffer = soundBuffer2;
soundBufferSize = sizeof(soundBuffer2);
}
memset(soundBuffer, 0, soundBufferSize);
// BGMをバッファに書き込む
if (reg.bgmPlaying) {
vgsdec_execute(vgsdec, soundBuffer, soundBufferSize);
reg.bgmCurrentTime = vgsdec_get_value(vgsdec, VGSDEC_REG_TIME);
}
// 効果音をバッファに合成
if (!reg.effPausing) {
double master = 255.0 / (255 - reg.effMasterVolume);
for (int i = 0; i < 256; i++) {
if (reg.effPlaying[i]) {
for (int j = 0; j < soundBufferSize / 2; j++) {
if (reg.effCursor[i] < vm->bank->effSize[i]) {
int w = vm->bank->eff[i][reg.effCursor[i]];
w += soundBuffer[j];
if (32767 < w) {
w = 32767;
} else if (w < -32768) {
w = -32768;
}
soundBuffer[j] = (short)(w * master);
reg.effCursor[i]++;
} else {
reg.effCursor[i] = 0;
reg.effPlaying[i] = 0;
break;
}
}
}
}
}
}
short* getBuffer(size_t* size)
{
if (soundBufferA) {
*size = sizeof(soundBuffer1);
return soundBuffer1;
} else {
*size = sizeof(soundBuffer2);
return soundBuffer2;
}
}
size_t save(char* buffer)
{
memcpy(buffer, ®, sizeof(reg));
return sizeof(reg);
}
size_t load(char* buffer)
{
memcpy(®, buffer, sizeof(reg));
if (reg.bgmPlaying) {
playBgm(reg.bgmCursor);
if (reg.bgmPlaying) {
vgsdec_set_value(vgsdec, VGSDEC_REG_TIME, reg.bgmCurrentTime);
}
}
updateEffMasterVolume();
return sizeof(reg);
}
};
#endif