Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HW/SasAudio.h
3186 views
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
19
20
// This is not really hardware, it's a software audio mixer running on the Media Engine.
21
// From the perspective of a PSP app though, it might as well be.
22
23
#pragma once
24
25
#include "Common/CommonTypes.h"
26
#include "Core/HW/BufferQueue.h"
27
#include "Core/HW/SasReverb.h"
28
29
class PointerWrap;
30
31
// General constants.
32
enum {
33
PSP_SAS_VOICES_MAX = 32,
34
PSP_SAS_VOL_MAX = 0x1000,
35
PSP_SAS_MAX_GRAIN = 2048, // Matches the max value of the parameter to sceSasInit
36
PSP_SAS_PITCH_MIN = 0x0000,
37
PSP_SAS_PITCH_BASE = 0x1000,
38
PSP_SAS_PITCH_MASK = 0xFFF,
39
PSP_SAS_PITCH_BASE_SHIFT = 12,
40
PSP_SAS_PITCH_MAX = 0x4000,
41
PSP_SAS_ENVELOPE_HEIGHT_MAX = 0x40000000,
42
PSP_SAS_ENVELOPE_FREQ_MAX = 0x7FFFFFFF,
43
};
44
45
// The type of these are baked into savestates.
46
enum SasADSRCurveMode : int {
47
PSP_SAS_ADSR_CURVE_MODE_LINEAR_INCREASE = 0,
48
PSP_SAS_ADSR_CURVE_MODE_LINEAR_DECREASE = 1,
49
PSP_SAS_ADSR_CURVE_MODE_LINEAR_BENT = 2,
50
PSP_SAS_ADSR_CURVE_MODE_EXPONENT_DECREASE = 3,
51
PSP_SAS_ADSR_CURVE_MODE_EXPONENT_INCREASE = 4,
52
PSP_SAS_ADSR_CURVE_MODE_DIRECT = 5,
53
};
54
55
enum {
56
PSP_SAS_ADSR_ATTACK = 1,
57
PSP_SAS_ADSR_DECAY = 2,
58
PSP_SAS_ADSR_SUSTAIN = 4,
59
PSP_SAS_ADSR_RELEASE = 8,
60
};
61
62
enum SasEffectType {
63
PSP_SAS_EFFECT_TYPE_OFF = -1,
64
PSP_SAS_EFFECT_TYPE_ROOM = 0,
65
PSP_SAS_EFFECT_TYPE_STUDIO_SMALL = 1,
66
PSP_SAS_EFFECT_TYPE_STUDIO_MEDIUM = 2,
67
PSP_SAS_EFFECT_TYPE_STUDIO_LARGE = 3,
68
PSP_SAS_EFFECT_TYPE_HALL = 4,
69
PSP_SAS_EFFECT_TYPE_SPACE = 5,
70
PSP_SAS_EFFECT_TYPE_ECHO = 6,
71
PSP_SAS_EFFECT_TYPE_DELAY = 7,
72
PSP_SAS_EFFECT_TYPE_PIPE = 8,
73
PSP_SAS_EFFECT_TYPE_MAX = 8,
74
};
75
76
enum SasOutputMode {
77
PSP_SAS_OUTPUTMODE_MIXED = 0,
78
PSP_SAS_OUTPUTMODE_RAW = 1,
79
};
80
81
struct WaveformEffect {
82
int type;
83
int delay;
84
int feedback;
85
int leftVol;
86
int rightVol;
87
int isDryOn;
88
int isWetOn;
89
};
90
91
enum VoiceType {
92
VOICETYPE_OFF,
93
VOICETYPE_VAG, // default
94
VOICETYPE_NOISE,
95
VOICETYPE_TRIWAVE, // are these used? there are functions for them (sceSetTriangularWave)
96
VOICETYPE_PULSEWAVE,
97
VOICETYPE_PCM,
98
VOICETYPE_ATRAC3,
99
};
100
101
// VAG is a Sony ADPCM audio compression format, which goes all the way back to the PSX.
102
// It compresses 28 16-bit samples into a block of 16 bytes.
103
class VagDecoder {
104
public:
105
VagDecoder() : data_(0), read_(0), end_(true) {
106
memset(samples, 0, sizeof(samples));
107
}
108
void Start(u32 dataPtr, u32 vagSize, bool loopEnabled);
109
110
void GetSamples(s16 *outSamples, int numSamples);
111
112
void DecodeBlock(const u8 *&readp);
113
bool End() const { return end_; }
114
115
void DoState(PointerWrap &p);
116
117
u32 GetReadPtr() const { return read_; }
118
119
private:
120
s16 samples[28];
121
int curSample = 0;
122
123
u32 data_ = 0;
124
u32 read_ = 0;
125
int curBlock_ = -1;
126
int loopStartBlock_ = -1;
127
int numBlocks_ = 0;
128
129
// rolling state. start at 0, should probably reset to 0 on loops?
130
int s_1 = 0;
131
int s_2 = 0;
132
133
bool loopEnabled_ = false;
134
bool loopAtNextBlock_ = false;
135
bool end_ = false;
136
};
137
138
class SasAtrac3 {
139
public:
140
~SasAtrac3() { delete sampleQueue_; delete[] buf_; }
141
int AtracID() const { return atracID_; } // for the debugger
142
int SetContext(u32 context);
143
void GetNextSamples(s16 *outbuf, int wantedSamples);
144
int Concatenate(u32 bufPtr, u32 addbytes);
145
void DoState(PointerWrap &p);
146
bool End() const {
147
return end_;
148
}
149
150
private:
151
u32 contextAddr_ = 0;
152
int atracID_ = -1;
153
BufferQueue *sampleQueue_ = nullptr;
154
bool end_ = false;
155
s16 *buf_ = nullptr;
156
};
157
158
class ADSREnvelope {
159
public:
160
void SetSimpleEnvelope(u32 ADSREnv1, u32 ADSREnv2);
161
void SetEnvelope(int flag, int a, int d, int s, int r);
162
void SetRate(int flag, int a, int d, int s, int r);
163
void SetSustainLevel(int sl) {
164
sustainLevel = sl;
165
}
166
167
void WalkCurve(int type, int rate);
168
169
void KeyOn();
170
void KeyOff();
171
void End();
172
173
inline void Step();
174
175
int GetHeight() const {
176
return (int)(height_ > (s64)PSP_SAS_ENVELOPE_HEIGHT_MAX ? PSP_SAS_ENVELOPE_HEIGHT_MAX : height_);
177
}
178
bool NeedsKeyOn() const {
179
return state_ == STATE_KEYON;
180
}
181
bool HasEnded() const {
182
return state_ == STATE_OFF;
183
}
184
185
void DoState(PointerWrap &p);
186
187
int attackRate = 0;
188
int decayRate = 0;
189
int sustainRate = 0;
190
int sustainLevel = 0;
191
int releaseRate = 0;
192
193
SasADSRCurveMode attackType = PSP_SAS_ADSR_CURVE_MODE_LINEAR_INCREASE;
194
SasADSRCurveMode decayType = PSP_SAS_ADSR_CURVE_MODE_LINEAR_DECREASE;
195
SasADSRCurveMode sustainType = PSP_SAS_ADSR_CURVE_MODE_LINEAR_DECREASE;
196
SasADSRCurveMode releaseType = PSP_SAS_ADSR_CURVE_MODE_LINEAR_DECREASE;
197
198
private:
199
// Actual PSP values.
200
enum ADSRState {
201
// Okay, this one isn't a real value but it might be.
202
STATE_KEYON_STEP = -42,
203
204
STATE_KEYON = -2,
205
STATE_OFF = -1,
206
STATE_ATTACK = 0,
207
STATE_DECAY = 1,
208
STATE_SUSTAIN = 2,
209
STATE_RELEASE = 3,
210
};
211
void SetState(ADSRState state);
212
213
ADSRState state_ = STATE_OFF;
214
s64 height_ = 0; // s64 to avoid having to care about overflow when calculating. TODO: this should be fine as s32
215
};
216
217
// A SAS voice.
218
// TODO: Look into pre-decoding the VAG samples on SetVoice instead of decoding them on the fly.
219
// It's not very likely that games encode VAG dynamically.
220
struct SasVoice {
221
SasVoice()
222
: playing(false),
223
paused(false),
224
on(false),
225
type(VOICETYPE_OFF),
226
vagAddr(0),
227
vagSize(0),
228
pcmAddr(0),
229
pcmSize(0),
230
pcmIndex(0),
231
pcmLoopPos(0),
232
sampleRate(44100),
233
sampleFrac(0),
234
pitch(PSP_SAS_PITCH_BASE),
235
loop(false),
236
noiseFreq(0),
237
volumeLeft(PSP_SAS_VOL_MAX),
238
volumeRight(PSP_SAS_VOL_MAX),
239
effectLeft(PSP_SAS_VOL_MAX),
240
effectRight(PSP_SAS_VOL_MAX) {
241
memset(resampleHist, 0, sizeof(resampleHist));
242
}
243
244
void Reset();
245
void KeyOn();
246
void KeyOff();
247
248
void DoState(PointerWrap &p);
249
250
void ReadSamples(s16 *output, int numSamples);
251
bool HaveSamplesEnded() const;
252
253
// For debugging.
254
u32 GetReadAddress() const {
255
if (type == VOICETYPE_VAG) {
256
return vag.GetReadPtr();
257
} else {
258
return 0; // TODO.
259
}
260
}
261
262
bool playing;
263
bool paused; // a voice can be playing AND paused. In that case, it won't play.
264
bool on; // key-on, key-off.
265
266
VoiceType type;
267
268
u32 vagAddr;
269
u32 vagSize;
270
u32 pcmAddr;
271
int pcmSize;
272
int pcmIndex;
273
int pcmLoopPos;
274
int sampleRate;
275
276
uint32_t sampleFrac;
277
int pitch;
278
bool loop;
279
280
int noiseFreq;
281
282
int volumeLeft;
283
int volumeRight;
284
285
// volume to "Send" (audio-lingo) to the effects processing engine, like reverb
286
int effectLeft;
287
int effectRight;
288
s16 resampleHist[2];
289
290
ADSREnvelope envelope;
291
292
// TODO: Union these two?
293
VagDecoder vag;
294
SasAtrac3 atrac3;
295
};
296
297
class SasInstance {
298
public:
299
SasInstance();
300
~SasInstance();
301
302
void ClearGrainSize();
303
void SetGrainSize(int newGrainSize);
304
int GetGrainSize() const { return grainSize; }
305
int EstimateMixUs();
306
307
int maxVoices = PSP_SAS_VOICES_MAX;
308
int sampleRate = 44100;
309
int outputMode = PSP_SAS_OUTPUTMODE_MIXED;
310
311
int *mixBuffer = nullptr;
312
int *sendBuffer = nullptr;
313
s16 *sendBufferDownsampled = nullptr;
314
s16 *sendBufferProcessed = nullptr;
315
316
FILE *audioDump = nullptr;
317
318
void Mix(u32 outAddr, u32 inAddr, int leftVol, int rightVol, bool mute);
319
void MixVoice(SasVoice &voice);
320
321
// Applies reverb to send buffer, according to waveformEffect.
322
void ApplyWaveformEffect();
323
void SetWaveformEffectType(int type);
324
void WriteMixedOutput(s16 *outp, const s16 *inp, int leftVol, int rightVol);
325
326
void GetDebugText(char *text, size_t bufsize);
327
328
void DoState(PointerWrap &p);
329
330
SasVoice voices[PSP_SAS_VOICES_MAX];
331
WaveformEffect waveformEffect;
332
333
private:
334
SasReverb reverb_;
335
int grainSize = 0;
336
int16_t mixTemp_[PSP_SAS_MAX_GRAIN * 4 + 2 + 16]; // some extra margin for very high pitches.
337
};
338
339
const char *ADSRCurveModeAsString(SasADSRCurveMode mode);
340
341